blob: bf25384a57582c86c5ca06e7e7678e86e0db7ba7 [file] [log] [blame]
Jason Sams12b14ae2010-03-18 11:39:44 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsSignal.h"
Stephen Hines43cfc0c2013-08-15 10:02:11 -070018#include <errno.h>
Jason Sams12b14ae2010-03-18 11:39:44 -070019
20using namespace android;
21using namespace android::renderscript;
22
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024Signal::Signal() {
Jason Sams12b14ae2010-03-18 11:39:44 -070025 mSet = true;
26}
27
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080028Signal::~Signal() {
Jason Sams12b14ae2010-03-18 11:39:44 -070029 pthread_mutex_destroy(&mMutex);
30 pthread_cond_destroy(&mCondition);
31}
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033bool Signal::init() {
Chris Wailes44bef6f2014-08-12 13:51:10 -070034 int status = pthread_mutex_init(&mMutex, nullptr);
Jason Sams12b14ae2010-03-18 11:39:44 -070035 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000036 ALOGE("LocklessFifo mutex init failure");
Jason Sams12b14ae2010-03-18 11:39:44 -070037 return false;
38 }
39
Chris Wailes44bef6f2014-08-12 13:51:10 -070040 status = pthread_cond_init(&mCondition, nullptr);
Jason Sams12b14ae2010-03-18 11:39:44 -070041 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000042 ALOGE("LocklessFifo condition init failure");
Jason Sams12b14ae2010-03-18 11:39:44 -070043 pthread_mutex_destroy(&mMutex);
44 return false;
45 }
46
47 return true;
48}
49
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080050void Signal::set() {
Jason Sams12b14ae2010-03-18 11:39:44 -070051 int status;
52
53 status = pthread_mutex_lock(&mMutex);
54 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000055 ALOGE("LocklessCommandFifo: error %i locking for set condition.", status);
Jason Sams12b14ae2010-03-18 11:39:44 -070056 return;
57 }
58
59 mSet = true;
60
61 status = pthread_cond_signal(&mCondition);
62 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000063 ALOGE("LocklessCommandFifo: error %i on set condition.", status);
Jason Sams12b14ae2010-03-18 11:39:44 -070064 }
65
66 status = pthread_mutex_unlock(&mMutex);
67 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000068 ALOGE("LocklessCommandFifo: error %i unlocking for set condition.", status);
Jason Sams12b14ae2010-03-18 11:39:44 -070069 }
70}
71
Jason Samse0aab4a2011-08-12 15:05:15 -070072bool Signal::wait(uint64_t timeout) {
Jason Sams12b14ae2010-03-18 11:39:44 -070073 int status;
Jason Samse0aab4a2011-08-12 15:05:15 -070074 bool ret = false;
Jason Sams12b14ae2010-03-18 11:39:44 -070075
76 status = pthread_mutex_lock(&mMutex);
77 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +000078 ALOGE("LocklessCommandFifo: error %i locking for condition.", status);
Jason Samse0aab4a2011-08-12 15:05:15 -070079 return false;
Jason Sams12b14ae2010-03-18 11:39:44 -070080 }
81
82 if (!mSet) {
Jason Samse0aab4a2011-08-12 15:05:15 -070083 if (!timeout) {
84 status = pthread_cond_wait(&mCondition, &mMutex);
85 } else {
86#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
87 status = pthread_cond_timeout_np(&mCondition, &mMutex, timeout / 1000000);
88#else
89 // This is safe it will just make things less reponsive
90 status = pthread_cond_wait(&mCondition, &mMutex);
91#endif
Jason Sams12b14ae2010-03-18 11:39:44 -070092 }
93 }
Jason Samse0aab4a2011-08-12 15:05:15 -070094
95 if (!status) {
96 mSet = false;
97 ret = true;
98 } else {
Tim Murray0b575de2013-03-15 15:56:43 -070099#ifndef RS_SERVER
Jason Samse0aab4a2011-08-12 15:05:15 -0700100 if (status != ETIMEDOUT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000101 ALOGE("LocklessCommandFifo: error %i waiting for condition.", status);
Jason Samse0aab4a2011-08-12 15:05:15 -0700102 }
Tim Murray0b575de2013-03-15 15:56:43 -0700103#endif
Jason Samse0aab4a2011-08-12 15:05:15 -0700104 }
Jason Sams12b14ae2010-03-18 11:39:44 -0700105
106 status = pthread_mutex_unlock(&mMutex);
107 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000108 ALOGE("LocklessCommandFifo: error %i unlocking for condition.", status);
Jason Sams12b14ae2010-03-18 11:39:44 -0700109 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700110
111 return ret;
Jason Sams12b14ae2010-03-18 11:39:44 -0700112}
113