1 /****************************************************************************** 2 * 3 * Copyright (C) 2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * Encapsulate a condition variable for thread synchronization. 22 * 23 ******************************************************************************/ 24 25 #pragma once 26 #include <pthread.h> 27 #include "Mutex.h" 28 29 class CondVar { 30 public: 31 /******************************************************************************* 32 ** 33 ** Function: CondVar 34 ** 35 ** Description: Initialize member variables. 36 ** 37 ** Returns: None. 38 ** 39 *******************************************************************************/ 40 CondVar(); 41 42 /******************************************************************************* 43 ** 44 ** Function: ~CondVar 45 ** 46 ** Description: Cleanup all resources. 47 ** 48 ** Returns: None. 49 ** 50 *******************************************************************************/ 51 ~CondVar(); 52 53 /******************************************************************************* 54 ** 55 ** Function: wait 56 ** 57 ** Description: Block the caller and wait for a condition. 58 ** 59 ** Returns: None. 60 ** 61 *******************************************************************************/ 62 void wait(Mutex& mutex); 63 64 /******************************************************************************* 65 ** 66 ** Function: wait 67 ** 68 ** Description: Block the caller and wait for a condition. 69 ** millisec: Timeout in milliseconds. 70 ** 71 ** Returns: True if wait is successful; false if timeout occurs. 72 ** 73 *******************************************************************************/ 74 bool wait(Mutex& mutex, long millisec); 75 76 /******************************************************************************* 77 ** 78 ** Function: notifyOne 79 ** 80 ** Description: Unblock the waiting thread. 81 ** 82 ** Returns: None. 83 ** 84 *******************************************************************************/ 85 void notifyOne(); 86 87 private: 88 pthread_cond_t mCondition; 89 }; 90