cec: and now proper predicates
[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);
264 uint64_t iMsLeft(0);
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:
960f33c6
LOK
283 CEvent(void) :
284 m_bSignaled(false),
285 m_bBroadcast(false),
286 m_iWaitingThreads(0) {}
287 virtual ~CEvent(void) {}
288
289 void Broadcast(void)
f00ff009 290 {
960f33c6 291 Set(true);
f2d82f4b 292 m_condition.Broadcast();
f00ff009
LOK
293 }
294
960f33c6 295 void Signal(void)
f00ff009 296 {
960f33c6
LOK
297 Set(false);
298 m_condition.Signal();
f00ff009
LOK
299 }
300
960f33c6 301 bool Wait(void)
f00ff009 302 {
960f33c6
LOK
303 CLockObject lock(m_mutex);
304 ++m_iWaitingThreads;
305
306 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled);
307 return ResetAndReturn() && bReturn;
f00ff009
LOK
308 }
309
960f33c6 310 bool Wait(uint32_t iTimeout)
f00ff009 311 {
960f33c6
LOK
312 if (iTimeout == 0)
313 return Wait();
314
315 CLockObject lock(m_mutex);
316 ++m_iWaitingThreads;
317 bool bReturn = m_condition.Wait(m_mutex, m_bSignaled, iTimeout);
318 return ResetAndReturn() && bReturn;
f00ff009
LOK
319 }
320
321 static void Sleep(uint32_t iTimeout)
322 {
960f33c6
LOK
323 CEvent event;
324 event.Wait(iTimeout);
f00ff009
LOK
325 }
326
3a590d6a 327 private:
960f33c6
LOK
328 void Set(bool bBroadcast = false)
329 {
330 CLockObject lock(m_mutex);
331 m_bSignaled = true;
332 m_bBroadcast = bBroadcast;
333 }
334
335 bool ResetAndReturn(void)
336 {
337 CLockObject lock(m_mutex);
338 bool bReturn(m_bSignaled);
339 if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast))
340 m_bSignaled = false;
341 return bReturn;
342 }
343
344 volatile bool m_bSignaled;
345 CCondition<volatile bool&> m_condition;
346 CMutex m_mutex;
347 volatile bool m_bBroadcast;
348 unsigned int m_iWaitingThreads;
f00ff009
LOK
349 };
350}