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 
30 class CondVar
31 {
32 public:
33     /*******************************************************************************
34     **
35     ** Function:        CondVar
36     **
37     ** Description:     Initialize member variables.
38     **
39     ** Returns:         None.
40     **
41     *******************************************************************************/
42     CondVar ();
43 
44 
45     /*******************************************************************************
46     **
47     ** Function:        ~CondVar
48     **
49     ** Description:     Cleanup all resources.
50     **
51     ** Returns:         None.
52     **
53     *******************************************************************************/
54     ~CondVar ();
55 
56 
57     /*******************************************************************************
58     **
59     ** Function:        wait
60     **
61     ** Description:     Block the caller and wait for a condition.
62     **
63     ** Returns:         None.
64     **
65     *******************************************************************************/
66     void wait (Mutex& mutex);
67 
68 
69     /*******************************************************************************
70     **
71     ** Function:        wait
72     **
73     ** Description:     Block the caller and wait for a condition.
74     **                  millisec: Timeout in milliseconds.
75     **
76     ** Returns:         True if wait is successful; false if timeout occurs.
77     **
78     *******************************************************************************/
79     bool wait (Mutex& mutex, long millisec);
80 
81 
82     /*******************************************************************************
83     **
84     ** Function:        notifyOne
85     **
86     ** Description:     Unblock the waiting thread.
87     **
88     ** Returns:         None.
89     **
90     *******************************************************************************/
91     void notifyOne ();
92 
93 private:
94     pthread_cond_t mCondition;
95 };
96