SyncEvent.h revision e9df6ba5a8fcccf306a80b1670b423be8fe7746a
1f837290e142d49c9e1332841ec2c49ee2f09584avegorov@chromium.org/******************************************************************************
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org *
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org *  Copyright (C) 2012 Broadcom Corporation
4f837290e142d49c9e1332841ec2c49ee2f09584avegorov@chromium.org *
5f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org *  Licensed under the Apache License, Version 2.0 (the "License");
6f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org *  you may not use this file except in compliance with the License.
7f837290e142d49c9e1332841ec2c49ee2f09584avegorov@chromium.org *  You may obtain a copy of the License at:
8f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org *
9f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org *  http://www.apache.org/licenses/LICENSE-2.0
10f837290e142d49c9e1332841ec2c49ee2f09584avegorov@chromium.org *
11f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org *  Unless required by applicable law or agreed to in writing, software
12f837290e142d49c9e1332841ec2c49ee2f09584avegorov@chromium.org *  distributed under the License is distributed on an "AS IS" BASIS,
13ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org *  See the License for the specific language governing permissions and
15ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org *  limitations under the License.
16ea88ce93dcb41a9200ec8747ae7642a5db1f4ce7sgjesse@chromium.org *
17f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org ******************************************************************************/
18f05f2913e034b9332e55c02c9395e701725c02c1kmillikin@chromium.org
19/******************************************************************************
20 *
21 *  Synchronize two or more threads using a condition variable and a mutex.
22 *
23 ******************************************************************************/
24#pragma once
25#include "CondVar.h"
26#include "Mutex.h"
27
28
29class SyncEvent
30{
31public:
32    /*******************************************************************************
33    **
34    ** Function:        ~SyncEvent
35    **
36    ** Description:     Cleanup all resources.
37    **
38    ** Returns:         None.
39    **
40    *******************************************************************************/
41    ~SyncEvent ()
42    {
43    }
44
45
46    /*******************************************************************************
47    **
48    ** Function:        start
49    **
50    ** Description:     Start a synchronization operation.
51    **
52    ** Returns:         None.
53    **
54    *******************************************************************************/
55    void start ()
56    {
57        mMutex.lock ();
58    }
59
60
61    /*******************************************************************************
62    **
63    ** Function:        wait
64    **
65    ** Description:     Block the thread and wait for the event to occur.
66    **
67    ** Returns:         None.
68    **
69    *******************************************************************************/
70    void wait ()
71    {
72        mCondVar.wait (mMutex);
73    }
74
75
76    /*******************************************************************************
77    **
78    ** Function:        wait
79    **
80    ** Description:     Block the thread and wait for the event to occur.
81    **                  millisec: Timeout in milliseconds.
82    **
83    ** Returns:         True if wait is successful; false if timeout occurs.
84    **
85    *******************************************************************************/
86    bool wait (long millisec)
87    {
88        bool retVal = mCondVar.wait (mMutex, millisec);
89        return retVal;
90    }
91
92
93    /*******************************************************************************
94    **
95    ** Function:        notifyOne
96    **
97    ** Description:     Notify a blocked thread that the event has occured. Unblocks it.
98    **
99    ** Returns:         None.
100    **
101    *******************************************************************************/
102    void notifyOne ()
103    {
104        mCondVar.notifyOne ();
105    }
106
107
108    /*******************************************************************************
109    **
110    ** Function:        end
111    **
112    ** Description:     End a synchronization operation.
113    **
114    ** Returns:         None.
115    **
116    *******************************************************************************/
117    void end ()
118    {
119        mMutex.unlock ();
120    }
121
122private:
123    CondVar mCondVar;
124    Mutex mMutex;
125};
126
127
128/*****************************************************************************/
129/*****************************************************************************/
130
131
132/*****************************************************************************
133**
134**  Name:           SyncEventGuard
135**
136**  Description:    Automatically start and end a synchronization event.
137**
138*****************************************************************************/
139class SyncEventGuard
140{
141public:
142    /*******************************************************************************
143    **
144    ** Function:        SyncEventGuard
145    **
146    ** Description:     Start a synchronization operation.
147    **
148    ** Returns:         None.
149    **
150    *******************************************************************************/
151    SyncEventGuard (SyncEvent& event)
152    :   mEvent (event)
153    {
154        event.start (); //automatically start operation
155    };
156
157
158    /*******************************************************************************
159    **
160    ** Function:        ~SyncEventGuard
161    **
162    ** Description:     End a synchronization operation.
163    **
164    ** Returns:         None.
165    **
166    *******************************************************************************/
167    ~SyncEventGuard ()
168    {
169        mEvent.end (); //automatically end operation
170    };
171
172private:
173    SyncEvent& mEvent;
174};
175
176