blob: 66d3f5464c309401683eaeaeaab8c8b3df412c9a [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
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080020namespace android {
21namespace renderscript {
Jason Sams12b14ae2010-03-18 11:39:44 -070022
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080023Signal::Signal() {
Jason Sams12b14ae2010-03-18 11:39:44 -070024 mSet = true;
25}
26
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080027Signal::~Signal() {
Jason Sams12b14ae2010-03-18 11:39:44 -070028 pthread_mutex_destroy(&mMutex);
29 pthread_cond_destroy(&mCondition);
30}
31
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080032bool Signal::init() {
Chris Wailes44bef6f2014-08-12 13:51:10 -070033 int status = pthread_mutex_init(&mMutex, nullptr);
Jason Sams12b14ae2010-03-18 11:39:44 -070034 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070035 ALOGE("Signal::init: mutex init failure: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070036 return false;
37 }
38
Chris Wailes44bef6f2014-08-12 13:51:10 -070039 status = pthread_cond_init(&mCondition, nullptr);
Jason Sams12b14ae2010-03-18 11:39:44 -070040 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070041 ALOGE("Signal::init: condition init failure: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070042 pthread_mutex_destroy(&mMutex);
43 return false;
44 }
45
46 return true;
47}
48
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049void Signal::set() {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070050 int status = pthread_mutex_lock(&mMutex);
Jason Sams12b14ae2010-03-18 11:39:44 -070051 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070052 ALOGE("Signal::set: error locking for set condition: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070053 return;
54 }
55
56 mSet = true;
57
58 status = pthread_cond_signal(&mCondition);
59 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070060 ALOGE("Signal::set: error on set condition: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070061 }
62
63 status = pthread_mutex_unlock(&mMutex);
64 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070065 ALOGE("Signal::set: error unlocking for set condition: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070066 }
67}
68
Elliott Hughes4698e2b2016-04-25 12:49:59 -070069void Signal::wait() {
70 int status = pthread_mutex_lock(&mMutex);
Jason Sams12b14ae2010-03-18 11:39:44 -070071 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070072 ALOGE("Signal::wait: error locking for condition: %s", strerror(status));
73 return;
Jason Sams12b14ae2010-03-18 11:39:44 -070074 }
75
76 if (!mSet) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070077 status = pthread_cond_wait(&mCondition, &mMutex);
Jason Sams12b14ae2010-03-18 11:39:44 -070078 }
Jason Samse0aab4a2011-08-12 15:05:15 -070079
80 if (!status) {
81 mSet = false;
Jason Samse0aab4a2011-08-12 15:05:15 -070082 } else {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070083 ALOGE("Signal::wait: error waiting for condition: %s", strerror(status));
Jason Samse0aab4a2011-08-12 15:05:15 -070084 }
Jason Sams12b14ae2010-03-18 11:39:44 -070085
86 status = pthread_mutex_unlock(&mMutex);
87 if (status) {
Elliott Hughes4698e2b2016-04-25 12:49:59 -070088 ALOGE("Signal::wait: error unlocking for condition: %s", strerror(status));
Jason Sams12b14ae2010-03-18 11:39:44 -070089 }
Jason Sams12b14ae2010-03-18 11:39:44 -070090}
91
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080092} // namespace renderscript
93} // namespace android