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