LibCecSharp: better handling of callbacks
[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; }
dc6366e8 54 inline PreventCopy &operator=(const PreventCopy &c){ return *this; }
f00ff009
LOK
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
44d840e6
LOK
260 if (predicate)
261 return true;
262
263 bool bReturn(false);
264 bool bBreak(false);
265 CTimeout timeout(iTimeout);
266 uint32_t iMsLeft(0);
267
268 while (!bReturn && !bBreak)
960f33c6 269 {
44d840e6
LOK
270 iMsLeft = timeout.TimeLeft();
271 if ((bReturn = predicate) == false && (bBreak = iMsLeft == 0) == false)
272 m_condition.Wait(mutex.m_mutex, iMsLeft);
960f33c6
LOK
273 }
274 return bReturn;
275 }
276
277 private:
278 CConditionImpl m_condition;
279 };
280
281 class CEvent
f00ff009
LOK
282 {
283 public:
de49d80b 284 CEvent(bool bAutoReset = true) :
960f33c6
LOK
285 m_bSignaled(false),
286 m_bBroadcast(false),
de49d80b
LOK
287 m_iWaitingThreads(0),
288 m_bAutoReset(bAutoReset) {}
960f33c6
LOK
289 virtual ~CEvent(void) {}
290
291 void Broadcast(void)
f00ff009 292 {
960f33c6 293 Set(true);
f2d82f4b 294 m_condition.Broadcast();
f00ff009
LOK
295 }
296
960f33c6 297 void Signal(void)
f00ff009 298 {
960f33c6
LOK
299 Set(false);
300 m_condition.Signal();
f00ff009
LOK
301 }
302
960f33c6 303 bool Wait(void)
f00ff009 304 {
960f33c6
LOK
305 CLockObject lock(m_mutex);
306 ++m_iWaitingThreads;
307
308 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled);
309 return ResetAndReturn() && bReturn;
f00ff009
LOK
310 }
311
960f33c6 312 bool Wait(uint32_t iTimeout)
f00ff009 313 {
960f33c6
LOK
314 if (iTimeout == 0)
315 return Wait();
316
317 CLockObject lock(m_mutex);
318 ++m_iWaitingThreads;
319 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled, iTimeout);
320 return ResetAndReturn() && bReturn;
f00ff009
LOK
321 }
322
323 static void Sleep(uint32_t iTimeout)
324 {
960f33c6
LOK
325 CEvent event;
326 event.Wait(iTimeout);
f00ff009
LOK
327 }
328
3a590d6a 329 private:
960f33c6
LOK
330 void Set(bool bBroadcast = false)
331 {
332 CLockObject lock(m_mutex);
333 m_bSignaled = true;
334 m_bBroadcast = bBroadcast;
335 }
336
337 bool ResetAndReturn(void)
338 {
339 CLockObject lock(m_mutex);
340 bool bReturn(m_bSignaled);
de49d80b 341 if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast) && m_bAutoReset)
960f33c6
LOK
342 m_bSignaled = false;
343 return bReturn;
344 }
345
8f6e48cd
LOK
346 volatile bool m_bSignaled;
347 CCondition<volatile bool> m_condition;
348 CMutex m_mutex;
349 volatile bool m_bBroadcast;
350 unsigned int m_iWaitingThreads;
351 bool m_bAutoReset;
f00ff009
LOK
352 };
353}