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  *  Synchronize two or more threads using a condition variable and a mutex.
19  */
20 #pragma once
21 #include "CondVar.h"
22 #include "Mutex.h"
23 
24 
25 class SyncEvent
26 {
27 public:
28     /*******************************************************************************
29     **
30     ** Function:        ~SyncEvent
31     **
32     ** Description:     Cleanup all resources.
33     **
34     ** Returns:         None.
35     **
36     *******************************************************************************/
~SyncEvent()37     ~SyncEvent ()
38     {
39     }
40 
41 
42     /*******************************************************************************
43     **
44     ** Function:        start
45     **
46     ** Description:     Start a synchronization operation.
47     **
48     ** Returns:         None.
49     **
50     *******************************************************************************/
start()51     void start ()
52     {
53         mMutex.lock ();
54     }
55 
56 
57     /*******************************************************************************
58     **
59     ** Function:        wait
60     **
61     ** Description:     Block the thread and wait for the event to occur.
62     **
63     ** Returns:         None.
64     **
65     *******************************************************************************/
wait()66     void wait ()
67     {
68         mCondVar.wait (mMutex);
69     }
70 
71 
72     /*******************************************************************************
73     **
74     ** Function:        wait
75     **
76     ** Description:     Block the thread and wait for the event to occur.
77     **                  millisec: Timeout in milliseconds.
78     **
79     ** Returns:         True if wait is successful; false if timeout occurs.
80     **
81     *******************************************************************************/
wait(long millisec)82     bool wait (long millisec)
83     {
84         bool retVal = mCondVar.wait (mMutex, millisec);
85         return retVal;
86     }
87 
88 
89     /*******************************************************************************
90     **
91     ** Function:        notifyOne
92     **
93     ** Description:     Notify a blocked thread that the event has occured. Unblocks it.
94     **
95     ** Returns:         None.
96     **
97     *******************************************************************************/
notifyOne()98     void notifyOne ()
99     {
100         mCondVar.notifyOne ();
101     }
102 
103 
104     /*******************************************************************************
105     **
106     ** Function:        end
107     **
108     ** Description:     End a synchronization operation.
109     **
110     ** Returns:         None.
111     **
112     *******************************************************************************/
end()113     void end ()
114     {
115         mMutex.unlock ();
116     }
117 
118 private:
119     CondVar mCondVar;
120     Mutex mMutex;
121 };
122 
123 
124 /*****************************************************************************/
125 /*****************************************************************************/
126 
127 
128 /*****************************************************************************
129 **
130 **  Name:           SyncEventGuard
131 **
132 **  Description:    Automatically start and end a synchronization event.
133 **
134 *****************************************************************************/
135 class SyncEventGuard
136 {
137 public:
138     /*******************************************************************************
139     **
140     ** Function:        SyncEventGuard
141     **
142     ** Description:     Start a synchronization operation.
143     **
144     ** Returns:         None.
145     **
146     *******************************************************************************/
SyncEventGuard(SyncEvent & event)147     SyncEventGuard (SyncEvent& event)
148     :   mEvent (event)
149     {
150         event.start (); //automatically start operation
151     };
152 
153 
154     /*******************************************************************************
155     **
156     ** Function:        ~SyncEventGuard
157     **
158     ** Description:     End a synchronization operation.
159     **
160     ** Returns:         None.
161     **
162     *******************************************************************************/
~SyncEventGuard()163     ~SyncEventGuard ()
164     {
165         mEvent.end (); //automatically end operation
166     };
167 
168 private:
169     SyncEvent& mEvent;
170 };
171 
172