1 /*
2  * Copyright (C) 2015 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 <pthread.h>
18 
19 #include "Thread.h"
20 
Thread()21 Thread::Thread() {
22   pthread_cond_init(&cond_, nullptr);
23 }
24 
~Thread()25 Thread::~Thread() {
26   pthread_cond_destroy(&cond_);
27 }
28 
WaitForReady()29 void Thread::WaitForReady() {
30   pthread_mutex_lock(&mutex_);
31   while (pending_) {
32     pthread_cond_wait(&cond_, &mutex_);
33   }
34   pthread_mutex_unlock(&mutex_);
35 }
36 
WaitForPending()37 void Thread::WaitForPending() {
38   pthread_mutex_lock(&mutex_);
39   while (!pending_) {
40     pthread_cond_wait(&cond_, &mutex_);
41   }
42   pthread_mutex_unlock(&mutex_);
43 }
44 
SetPending()45 void Thread::SetPending() {
46   pthread_mutex_lock(&mutex_);
47   pending_ = true;
48   pthread_mutex_unlock(&mutex_);
49   pthread_cond_signal(&cond_);
50 }
51 
ClearPending()52 void Thread::ClearPending() {
53   pthread_mutex_lock(&mutex_);
54   pending_ = false;
55   pthread_mutex_unlock(&mutex_);
56   pthread_cond_signal(&cond_);
57 }
58