• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  * Copyright 2019-2020 NXP.
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  *  Synchronize two or more threads using a condition variable and a mutex.
20  */
21 #pragma once
22 #include <list>
23 
24 #include "CondVar.h"
25 #include "Mutex.h"
26 using namespace std;
27 
28 class SyncEvent;
29 
30 extern std::list<SyncEvent *> syncEventList;
31 
32 class SyncEvent {
33 public:
34   /*******************************************************************************
35   **
36   ** Function:        ~SyncEvent
37   **
38   ** Description:     Cleanup all resources.
39   **
40   ** Returns:         None.
41   **
42   *******************************************************************************/
43   ~SyncEvent();
44 
45   /*******************************************************************************
46   **
47   ** Function:        start
48   **
49   ** Description:     Start a synchronization operation.
50   **
51   ** Returns:         None.
52   **
53   *******************************************************************************/
54   void start();
55 
56   /*******************************************************************************
57   **
58   ** Function:        wait
59   **
60   ** Description:     Block the thread and wait for the event to occur.
61   **
62   ** Returns:         None.
63   **
64   *******************************************************************************/
65   void wait();
66 
67   /*******************************************************************************
68   **
69   ** Function:        wait
70   **
71   ** Description:     Block the thread and wait for the event to occur.
72   **                  millisec: Timeout in milliseconds.
73   **
74   ** Returns:         True if wait is successful; false if timeout occurs.
75   **
76   *******************************************************************************/
77   bool wait(long millisec);
78 
79   /*******************************************************************************
80   **
81   ** Function:        notifyOne
82   **
83   ** Description:     Notify a blocked thread that the event has occurred.
84   *Unblocks it.
85   **                  Deregisters cached event.
86   ** Returns:         None.
87   **
88   *******************************************************************************/
89   void notifyOne();
90 
91   /*******************************************************************************
92   **
93   ** Function:        notify
94   **
95   ** Description:     Notify a blocked thread that the event has occurred.
96   *Unblocks it.
97   **                  This function won't deregister cached event
98   ** Returns:         None.
99   **
100   *******************************************************************************/
101   void notify();
102 
103   /*******************************************************************************
104   **
105   ** Function:        end
106   **
107   ** Description:     End a synchronization operation.
108   **
109   ** Returns:         None.
110   **
111   *******************************************************************************/
112   void end();
113 
114   /********Implement equality operator for SyncEvent
115    * Class***********************/
116   bool operator==(const SyncEvent &event) { return (this == &event); }
117 
118   /*******************************************************************************
119   **
120   ** Function:        addEvent
121   **
122   ** Description:     cache event locally
123   **
124   ** Returns:         None.
125   **
126   *******************************************************************************/
127   void addEvent();
128 
129   /*******************************************************************************
130   **
131   ** Function:        notifyAll
132   **
133   ** Description:     Notify all blocked thread that the event has occurred.
134   *Unblocks it.
135   **                  clears the event cache
136   ** Returns:         None.
137   **
138   *******************************************************************************/
139   void notifyAll();
140 
141   /*******************************************************************************
142   **
143   ** Function:        removeEvent
144   **
145   ** Description:     remove event from cache event.
146   **
147   ** Returns:         None.
148   **
149   *******************************************************************************/
150   void removeEvent();
151 
152 private:
153   CondVar mCondVar;
154   Mutex mMutex;
155   bool mWait = false;
156 };
157 
158 /*****************************************************************************/
159 /*****************************************************************************/
160 
161 /*****************************************************************************
162 **
163 **  Name:           SyncEventGuard
164 **
165 **  Description:    Automatically start and end a synchronization event.
166 **
167 *****************************************************************************/
168 class SyncEventGuard {
169 public:
170   /*******************************************************************************
171   **
172   ** Function:        SyncEventGuard
173   **
174   ** Description:     Start a synchronization operation.
175   **
176   ** Returns:         None.
177   **
178   *******************************************************************************/
SyncEventGuard(SyncEvent & event)179   SyncEventGuard(SyncEvent &event) : mEvent(event) {
180     event.start(); // automatically start operation
181   };
182 
183   /*******************************************************************************
184   **
185   ** Function:        ~SyncEventGuard
186   **
187   ** Description:     End a synchronization operation.
188   **
189   ** Returns:         None.
190   **
191   *******************************************************************************/
~SyncEventGuard()192   ~SyncEventGuard() {
193     mEvent.end(); // automatically end operation
194   };
195 
196 private:
197   SyncEvent &mEvent;
198 };
199