1 /* 2 * Copyright (C) 2012 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 /* 18 * Encapsulate a condition variable for thread synchronization. 19 */ 20 21 #pragma once 22 #include <pthread.h> 23 24 #include <mutex> 25 26 class CondVar { 27 public: 28 /******************************************************************************* 29 ** 30 ** Function: CondVar 31 ** 32 ** Description: Initialize member variables. 33 ** 34 ** Returns: None. 35 ** 36 *******************************************************************************/ 37 CondVar(); 38 39 /******************************************************************************* 40 ** 41 ** Function: ~CondVar 42 ** 43 ** Description: Cleanup all resources. 44 ** 45 ** Returns: None. 46 ** 47 *******************************************************************************/ 48 ~CondVar(); 49 50 /******************************************************************************* 51 ** 52 ** Function: wait 53 ** 54 ** Description: Block the caller and wait for a condition. 55 ** 56 ** Returns: None. 57 ** 58 *******************************************************************************/ 59 void wait(std::mutex& mutex); 60 61 /******************************************************************************* 62 ** 63 ** Function: wait 64 ** 65 ** Description: Block the caller and wait for a condition. 66 ** millisec: Timeout in milliseconds. 67 ** 68 ** Returns: True if wait is successful; false if timeout occurs. 69 ** 70 *******************************************************************************/ 71 bool wait(std::mutex& mutex, long millisec); 72 73 /******************************************************************************* 74 ** 75 ** Function: notifyOne 76 ** 77 ** Description: Unblock the waiting thread. 78 ** 79 ** Returns: None. 80 ** 81 *******************************************************************************/ 82 void notifyOne(); 83 84 private: 85 pthread_cond_t mCondition; 86 }; 87