cec: don't reset the "adapter message sent" event
[deb_libcec.git] / src / lib / platform / threads / mutex.h
CommitLineData
f00ff009
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
b492c10e 5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
f00ff009
LOK
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
ba65909d
LOK
34#include "../os.h"
35
36#if defined(__WINDOWS__)
37#include "../windows/os-threads.h"
38#else
39#include "../posix/os-threads.h"
40#endif
41
960f33c6
LOK
42#include "../util/timeutils.h"
43
f00ff009
LOK
44namespace PLATFORM
45{
46 class PreventCopy
47 {
48 public:
49 inline PreventCopy(void) {}
50 inline ~PreventCopy(void) {}
51
52 private:
53 inline PreventCopy(const PreventCopy &c) { *this = c; }
54 inline PreventCopy &operator=(const PreventCopy &c){ *this = c; return *this; }
55 };
56
960f33c6
LOK
57 template <typename _Predicate>
58 class CCondition;
f00ff009
LOK
59
60 class CMutex : public PreventCopy
61 {
960f33c6
LOK
62 template <typename _Predicate>
63 friend class CCondition;
f00ff009
LOK
64 public:
65 inline CMutex(void) :
66 m_iLockCount(0)
67 {
68 MutexCreate(m_mutex);
69 }
70
71 inline ~CMutex(void)
72 {
73 Clear();
74 MutexDelete(m_mutex);
75 }
76
77 inline bool TryLock(void)
78 {
79 if (MutexTryLock(m_mutex))
80 {
81 ++m_iLockCount;
82 return true;
83 }
84 return false;
85 }
86
87 inline bool Lock(void)
88 {
89 MutexLock(m_mutex);
90 ++m_iLockCount;
91 return true;
92 }
93
94 inline void Unlock(void)
95 {
ee2304ce
LOK
96 if (Lock())
97 {
98 if (m_iLockCount >= 2)
99 {
100 --m_iLockCount;
101 MutexUnlock(m_mutex);
102 }
103
104 --m_iLockCount;
105 MutexUnlock(m_mutex);
106 }
f00ff009
LOK
107 }
108
109 inline bool Clear(void)
110 {
111 bool bReturn(false);
112 if (TryLock())
113 {
114 unsigned int iLockCount = m_iLockCount;
115 for (unsigned int iPtr = 0; iPtr < iLockCount; iPtr++)
116 Unlock();
117 bReturn = true;
118 }
119 return bReturn;
120 }
121
122 private:
ee2304ce
LOK
123 mutex_t m_mutex;
124 volatile unsigned int m_iLockCount;
f00ff009
LOK
125 };
126
127 class CLockObject : public PreventCopy
128 {
129 public:
130 inline CLockObject(CMutex &mutex, bool bClearOnExit = false) :
131 m_mutex(mutex),
132 m_bClearOnExit(bClearOnExit)
133 {
134 m_mutex.Lock();
135 }
136
137 inline ~CLockObject(void)
138 {
139 if (m_bClearOnExit)
140 Clear();
141 else
142 Unlock();
143 }
144
145 inline bool TryLock(void)
146 {
147 return m_mutex.TryLock();
148 }
149
150 inline void Unlock(void)
151 {
152 m_mutex.Unlock();
153 }
154
155 inline bool Clear(void)
156 {
157 return m_mutex.Clear();
158 }
159
160 inline bool Lock(void)
161 {
162 return m_mutex.Lock();
163 }
164
165 private:
166 CMutex &m_mutex;
167 bool m_bClearOnExit;
168 };
169
c183fa4b
LOK
170 class CTryLockObject : public PreventCopy
171 {
172 public:
173 inline CTryLockObject(CMutex &mutex, bool bClearOnExit = false) :
174 m_mutex(mutex),
175 m_bClearOnExit(bClearOnExit),
176 m_bIsLocked(m_mutex.TryLock())
177 {
178 }
179
180 inline ~CTryLockObject(void)
181 {
182 if (m_bClearOnExit)
183 Clear();
184 else if (m_bIsLocked)
185 Unlock();
186 }
187
188 inline bool TryLock(void)
189 {
190 bool bReturn = m_mutex.TryLock();
191 m_bIsLocked |= bReturn;
192 return bReturn;
193 }
194
195 inline void Unlock(void)
196 {
197 if (m_bIsLocked)
198 {
199 m_bIsLocked = false;
200 m_mutex.Unlock();
201 }
202 }
203
204 inline bool Clear(void)
205 {
206 m_bIsLocked = false;
207 return m_mutex.Clear();
208 }
209
210 inline bool Lock(void)
211 {
212 bool bReturn = m_mutex.Lock();
213 m_bIsLocked |= bReturn;
214 return bReturn;
215 }
216
217 inline bool IsLocked(void) const
218 {
219 return m_bIsLocked;
220 }
221
222 private:
223 CMutex & m_mutex;
224 bool m_bClearOnExit;
225 volatile bool m_bIsLocked;
226 };
227
960f33c6
LOK
228 template <typename _Predicate>
229 class CCondition : public PreventCopy
230 {
231 public:
232 inline CCondition(void) {}
233 inline ~CCondition(void)
234 {
235 m_condition.Broadcast();
236 }
237
238 inline void Broadcast(void)
239 {
240 m_condition.Broadcast();
241 }
242
243 inline void Signal(void)
244 {
245 m_condition.Signal();
246 }
247
248 inline bool Wait(CMutex &mutex, _Predicate &predicate)
249 {
250 while(!predicate)
251 m_condition.Wait(mutex.m_mutex);
252 return true;
253 }
254
255 inline bool Wait(CMutex &mutex, _Predicate &predicate, uint32_t iTimeout)
256 {
257 if (iTimeout == 0)
258 return Wait(mutex, predicate);
259
260 bool bReturn(true);
261 if (!predicate)
262 {
263 CTimeout timeout(iTimeout);
78850b01 264 uint32_t iMsLeft(0);
960f33c6
LOK
265 bReturn = false;
266 while (!bReturn)
267 {
268 iMsLeft = timeout.TimeLeft();
269 if ((bReturn = iMsLeft == 0 || predicate) == false)
270 m_condition.Wait(mutex.m_mutex, iMsLeft);
271 }
272 }
273 return bReturn;
274 }
275
276 private:
277 CConditionImpl m_condition;
278 };
279
280 class CEvent
f00ff009
LOK
281 {
282 public:
de49d80b 283 CEvent(bool bAutoReset = true) :
960f33c6
LOK
284 m_bSignaled(false),
285 m_bBroadcast(false),
de49d80b
LOK
286 m_iWaitingThreads(0),
287 m_bAutoReset(bAutoReset) {}
960f33c6
LOK
288 virtual ~CEvent(void) {}
289
290 void Broadcast(void)
f00ff009 291 {
960f33c6 292 Set(true);
f2d82f4b 293 m_condition.Broadcast();
f00ff009
LOK
294 }
295
960f33c6 296 void Signal(void)
f00ff009 297 {
960f33c6
LOK
298 Set(false);
299 m_condition.Signal();
f00ff009
LOK
300 }
301
960f33c6 302 bool Wait(void)
f00ff009 303 {
960f33c6
LOK
304 CLockObject lock(m_mutex);
305 ++m_iWaitingThreads;
306
307 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled);
308 return ResetAndReturn() && bReturn;
f00ff009
LOK
309 }
310
960f33c6 311 bool Wait(uint32_t iTimeout)
f00ff009 312 {
960f33c6
LOK
313 if (iTimeout == 0)
314 return Wait();
315
316 CLockObject lock(m_mutex);
317 ++m_iWaitingThreads;
318 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled, iTimeout);
319 return ResetAndReturn() && bReturn;
f00ff009
LOK
320 }
321
322 static void Sleep(uint32_t iTimeout)
323 {
960f33c6
LOK
324 CEvent event;
325 event.Wait(iTimeout);
f00ff009
LOK
326 }
327
3a590d6a 328 private:
960f33c6
LOK
329 void Set(bool bBroadcast = false)
330 {
331 CLockObject lock(m_mutex);
332 m_bSignaled = true;
333 m_bBroadcast = bBroadcast;
334 }
335
336 bool ResetAndReturn(void)
337 {
338 CLockObject lock(m_mutex);
339 bool bReturn(m_bSignaled);
de49d80b 340 if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast) && m_bAutoReset)
960f33c6
LOK
341 m_bSignaled = false;
342 return bReturn;
343 }
344
345 volatile bool m_bSignaled;
346 CCondition<volatile bool&> m_condition;
347 CMutex m_mutex;
348 volatile bool m_bBroadcast;
349 unsigned int m_iWaitingThreads;
de49d80b 350 bool m_bAutoReset;
f00ff009
LOK
351 };
352}