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