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