blob: 3f6a13c9b65407a4a04b4b470840226702145ee4 [file] [log] [blame]
Jason Samsc1d726c2010-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"
18
19using namespace android;
20using namespace android::renderscript;
21
22
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080023Signal::Signal() {
Jason Samsc1d726c2010-03-18 11:39:44 -070024 mSet = true;
25}
26
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080027Signal::~Signal() {
Jason Samsc1d726c2010-03-18 11:39:44 -070028 pthread_mutex_destroy(&mMutex);
29 pthread_cond_destroy(&mCondition);
30}
31
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080032bool Signal::init() {
Jason Samsc1d726c2010-03-18 11:39:44 -070033 int status = pthread_mutex_init(&mMutex, NULL);
34 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000035 ALOGE("LocklessFifo mutex init failure");
Jason Samsc1d726c2010-03-18 11:39:44 -070036 return false;
37 }
38
39 status = pthread_cond_init(&mCondition, NULL);
40 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000041 ALOGE("LocklessFifo condition init failure");
Jason Samsc1d726c2010-03-18 11:39:44 -070042 pthread_mutex_destroy(&mMutex);
43 return false;
44 }
45
46 return true;
47}
48
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080049void Signal::set() {
Jason Samsc1d726c2010-03-18 11:39:44 -070050 int status;
51
52 status = pthread_mutex_lock(&mMutex);
53 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000054 ALOGE("LocklessCommandFifo: error %i locking for set condition.", status);
Jason Samsc1d726c2010-03-18 11:39:44 -070055 return;
56 }
57
58 mSet = true;
59
60 status = pthread_cond_signal(&mCondition);
61 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000062 ALOGE("LocklessCommandFifo: error %i on set condition.", status);
Jason Samsc1d726c2010-03-18 11:39:44 -070063 }
64
65 status = pthread_mutex_unlock(&mMutex);
66 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000067 ALOGE("LocklessCommandFifo: error %i unlocking for set condition.", status);
Jason Samsc1d726c2010-03-18 11:39:44 -070068 }
69}
70
Jason Samsbfc78912011-08-12 15:05:15 -070071bool Signal::wait(uint64_t timeout) {
Jason Samsc1d726c2010-03-18 11:39:44 -070072 int status;
Jason Samsbfc78912011-08-12 15:05:15 -070073 bool ret = false;
Jason Samsc1d726c2010-03-18 11:39:44 -070074
75 status = pthread_mutex_lock(&mMutex);
76 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +000077 ALOGE("LocklessCommandFifo: error %i locking for condition.", status);
Jason Samsbfc78912011-08-12 15:05:15 -070078 return false;
Jason Samsc1d726c2010-03-18 11:39:44 -070079 }
80
81 if (!mSet) {
Jason Samsbfc78912011-08-12 15:05:15 -070082 if (!timeout) {
83 status = pthread_cond_wait(&mCondition, &mMutex);
84 } else {
85#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)
86 status = pthread_cond_timeout_np(&mCondition, &mMutex, timeout / 1000000);
87#else
88 // This is safe it will just make things less reponsive
89 status = pthread_cond_wait(&mCondition, &mMutex);
90#endif
Jason Samsc1d726c2010-03-18 11:39:44 -070091 }
92 }
Jason Samsbfc78912011-08-12 15:05:15 -070093
94 if (!status) {
95 mSet = false;
96 ret = true;
97 } else {
98 if (status != ETIMEDOUT) {
Steve Blockc6aacce2012-01-06 19:20:56 +000099 ALOGE("LocklessCommandFifo: error %i waiting for condition.", status);
Jason Samsbfc78912011-08-12 15:05:15 -0700100 }
101 }
Jason Samsc1d726c2010-03-18 11:39:44 -0700102
103 status = pthread_mutex_unlock(&mMutex);
104 if (status) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000105 ALOGE("LocklessCommandFifo: error %i unlocking for condition.", status);
Jason Samsc1d726c2010-03-18 11:39:44 -0700106 }
Jason Samsbfc78912011-08-12 15:05:15 -0700107
108 return ret;
Jason Samsc1d726c2010-03-18 11:39:44 -0700109}
110