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"
18 #include <errno.h>
19 
20 namespace android {
21 namespace renderscript {
22 
Signal()23 Signal::Signal() {
24     mSet = true;
25 }
26 
~Signal()27 Signal::~Signal() {
28     pthread_mutex_destroy(&mMutex);
29     pthread_cond_destroy(&mCondition);
30 }
31 
init()32 bool Signal::init() {
33     int status = pthread_mutex_init(&mMutex, nullptr);
34     if (status) {
35         ALOGE("Signal::init: mutex init failure: %s", strerror(status));
36         return false;
37     }
38 
39     status = pthread_cond_init(&mCondition, nullptr);
40     if (status) {
41         ALOGE("Signal::init: condition init failure: %s", strerror(status));
42         pthread_mutex_destroy(&mMutex);
43         return false;
44     }
45 
46     return true;
47 }
48 
set()49 void Signal::set() {
50     int status = pthread_mutex_lock(&mMutex);
51     if (status) {
52         ALOGE("Signal::set: error locking for set condition: %s", strerror(status));
53         return;
54     }
55 
56     mSet = true;
57 
58     status = pthread_cond_signal(&mCondition);
59     if (status) {
60         ALOGE("Signal::set: error on set condition: %s", strerror(status));
61     }
62 
63     status = pthread_mutex_unlock(&mMutex);
64     if (status) {
65         ALOGE("Signal::set: error unlocking for set condition: %s", strerror(status));
66     }
67 }
68 
wait()69 void Signal::wait() {
70     int status = pthread_mutex_lock(&mMutex);
71     if (status) {
72         ALOGE("Signal::wait: error locking for condition: %s", strerror(status));
73         return;
74     }
75 
76     if (!mSet) {
77         status = pthread_cond_wait(&mCondition, &mMutex);
78     }
79 
80     if (!status) {
81         mSet = false;
82     } else {
83         ALOGE("Signal::wait: error waiting for condition: %s", strerror(status));
84     }
85 
86     status = pthread_mutex_unlock(&mMutex);
87     if (status) {
88         ALOGE("Signal::wait: error unlocking for condition: %s", strerror(status));
89     }
90 }
91 
92 } // namespace renderscript
93 } // namespace android
94