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 | |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 20 | namespace android { |
| 21 | namespace renderscript { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 22 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 23 | Signal::Signal() { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 24 | mSet = true; |
| 25 | } |
| 26 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 27 | Signal::~Signal() { |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 28 | pthread_mutex_destroy(&mMutex); |
| 29 | pthread_cond_destroy(&mCondition); |
| 30 | } |
| 31 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | bool Signal::init() { |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 33 | int status = pthread_mutex_init(&mMutex, nullptr); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 34 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 35 | ALOGE("Signal::init: mutex init failure: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 39 | status = pthread_cond_init(&mCondition, nullptr); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 40 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 41 | ALOGE("Signal::init: condition init failure: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 42 | pthread_mutex_destroy(&mMutex); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 49 | void Signal::set() { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 50 | int status = pthread_mutex_lock(&mMutex); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 51 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 52 | ALOGE("Signal::set: error locking for set condition: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | mSet = true; |
| 57 | |
| 58 | status = pthread_cond_signal(&mCondition); |
| 59 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 60 | ALOGE("Signal::set: error on set condition: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | status = pthread_mutex_unlock(&mMutex); |
| 64 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 65 | ALOGE("Signal::set: error unlocking for set condition: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 69 | void Signal::wait() { |
| 70 | int status = pthread_mutex_lock(&mMutex); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 71 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 72 | ALOGE("Signal::wait: error locking for condition: %s", strerror(status)); |
| 73 | return; |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (!mSet) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 77 | status = pthread_cond_wait(&mCondition, &mMutex); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 78 | } |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 79 | |
| 80 | if (!status) { |
| 81 | mSet = false; |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 82 | } else { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 83 | ALOGE("Signal::wait: error waiting for condition: %s", strerror(status)); |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 84 | } |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 85 | |
| 86 | status = pthread_mutex_unlock(&mMutex); |
| 87 | if (status) { |
Elliott Hughes | 4698e2b | 2016-04-25 12:49:59 -0700 | [diff] [blame] | 88 | ALOGE("Signal::wait: error unlocking for condition: %s", strerror(status)); |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 89 | } |
Jason Sams | 12b14ae | 2010-03-18 11:39:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 92 | } // namespace renderscript |
| 93 | } // namespace android |