Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Hines | 43cfc0c | 2013-08-15 10:02:11 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 19 | |
| 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
| 23 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Signal::Signal() { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 25 | mSet = true; |
| 26 | } |
| 27 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 28 | Signal::~Signal() { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 29 | pthread_mutex_destroy(&mMutex); |
| 30 | pthread_cond_destroy(&mCondition); |
| 31 | } |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | bool Signal::init() { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 34 | int status = pthread_mutex_init(&mMutex, nullptr); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 35 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 36 | ALOGE("LocklessFifo mutex init failure"); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 40 | status = pthread_cond_init(&mCondition, nullptr); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 41 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 42 | ALOGE("LocklessFifo condition init failure"); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 43 | pthread_mutex_destroy(&mMutex); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 50 | void Signal::set() { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 51 | int status; |
| 52 | |
| 53 | status = pthread_mutex_lock(&mMutex); |
| 54 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 55 | ALOGE("LocklessCommandFifo: error %i locking for set condition.", status); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
| 59 | mSet = true; |
| 60 | |
| 61 | status = pthread_cond_signal(&mCondition); |
| 62 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 63 | ALOGE("LocklessCommandFifo: error %i on set condition.", status); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | status = pthread_mutex_unlock(&mMutex); |
| 67 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 68 | ALOGE("LocklessCommandFifo: error %i unlocking for set condition.", status); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 72 | bool Signal::wait(uint64_t timeout) { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 73 | int status; |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 74 | bool ret = false; |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 75 | |
| 76 | status = pthread_mutex_lock(&mMutex); |
| 77 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 78 | ALOGE("LocklessCommandFifo: error %i locking for condition.", status); |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 79 | return false; |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!mSet) { |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 83 | 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 Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 94 | |
| 95 | if (!status) { |
| 96 | mSet = false; |
| 97 | ret = true; |
| 98 | } else { |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 99 | #ifndef RS_SERVER |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 100 | if (status != ETIMEDOUT) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 101 | ALOGE("LocklessCommandFifo: error %i waiting for condition.", status); |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 102 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 103 | #endif |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 104 | } |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 105 | |
| 106 | status = pthread_mutex_unlock(&mMutex); |
| 107 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 108 | ALOGE("LocklessCommandFifo: error %i unlocking for condition.", status); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 109 | } |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 110 | |
| 111 | return ret; |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 112 | } |
| 113 | |