3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "lib/platform/os.h"
36 #if defined(__WINDOWS__)
37 #include "lib/platform/windows/os-threads.h"
39 #include "lib/platform/posix/os-threads.h"
42 #include "lib/platform/util/timeutils.h"
49 inline PreventCopy(void) {}
50 inline ~PreventCopy(void) {}
53 inline PreventCopy(const PreventCopy
&c
) { *this = c
; }
54 inline PreventCopy
&operator=(const PreventCopy
& UNUSED(c
)){ return *this; }
57 template <typename _Predicate
>
60 class CMutex
: public PreventCopy
62 template <typename _Predicate
>
63 friend class CCondition
;
77 inline bool TryLock(void)
79 if (MutexTryLock(m_mutex
))
87 inline bool Lock(void)
89 if (MutexLock(m_mutex
))
97 inline void Unlock(void)
101 if (m_iLockCount
>= 2)
104 MutexUnlock(m_mutex
);
108 MutexUnlock(m_mutex
);
112 inline bool Clear(void)
117 unsigned int iLockCount
= m_iLockCount
;
118 for (unsigned int iPtr
= 0; iPtr
< iLockCount
; iPtr
++)
127 volatile unsigned int m_iLockCount
;
130 class CLockObject
: public PreventCopy
133 inline CLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
135 m_bClearOnExit(bClearOnExit
)
140 inline ~CLockObject(void)
148 inline bool TryLock(void)
150 return m_mutex
.TryLock();
153 inline void Unlock(void)
158 inline bool Clear(void)
160 return m_mutex
.Clear();
163 inline bool Lock(void)
165 return m_mutex
.Lock();
173 class CTryLockObject
: public PreventCopy
176 inline CTryLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
178 m_bClearOnExit(bClearOnExit
),
179 m_bIsLocked(m_mutex
.TryLock())
183 inline ~CTryLockObject(void)
187 else if (m_bIsLocked
)
191 inline bool TryLock(void)
193 bool bReturn
= m_mutex
.TryLock();
194 m_bIsLocked
|= bReturn
;
198 inline void Unlock(void)
207 inline bool Clear(void)
210 return m_mutex
.Clear();
213 inline bool Lock(void)
215 bool bReturn
= m_mutex
.Lock();
216 m_bIsLocked
|= bReturn
;
220 inline bool IsLocked(void) const
228 volatile bool m_bIsLocked
;
231 template <typename _Predicate
>
232 class CCondition
: public PreventCopy
235 inline CCondition(void) {}
236 inline ~CCondition(void)
238 m_condition
.Broadcast();
241 inline void Broadcast(void)
243 m_condition
.Broadcast();
246 inline void Signal(void)
248 m_condition
.Signal();
251 inline bool Wait(CMutex
&mutex
, _Predicate
&predicate
)
254 m_condition
.Wait(mutex
.m_mutex
);
258 inline bool Wait(CMutex
&mutex
, _Predicate
&predicate
, uint32_t iTimeout
)
261 return Wait(mutex
, predicate
);
268 CTimeout
timeout(iTimeout
);
271 while (!bReturn
&& !bBreak
)
273 iMsLeft
= timeout
.TimeLeft();
274 if ((bReturn
= predicate
) == false && (bBreak
= iMsLeft
== 0) == false)
275 m_condition
.Wait(mutex
.m_mutex
, iMsLeft
);
281 CConditionImpl m_condition
;
287 CEvent(bool bAutoReset
= true) :
290 m_iWaitingThreads(0),
291 m_bAutoReset(bAutoReset
) {}
292 virtual ~CEvent(void) {}
297 m_condition
.Broadcast();
303 m_condition
.Signal();
308 CLockObject
lock(m_mutex
);
311 bool bReturn
= m_condition
.Wait(m_mutex
, m_bSignaled
);
312 return ResetAndReturn() && bReturn
;
315 bool Wait(uint32_t iTimeout
)
320 CLockObject
lock(m_mutex
);
322 bool bReturn
= m_condition
.Wait(m_mutex
, m_bSignaled
, iTimeout
);
323 return ResetAndReturn() && bReturn
;
326 static void Sleep(uint32_t iTimeout
)
329 event
.Wait(iTimeout
);
333 void Set(bool bBroadcast
= false)
335 CLockObject
lock(m_mutex
);
337 m_bBroadcast
= bBroadcast
;
340 bool ResetAndReturn(void)
342 CLockObject
lock(m_mutex
);
343 bool bReturn(m_bSignaled
);
344 if (bReturn
&& (--m_iWaitingThreads
== 0 || !m_bBroadcast
) && m_bAutoReset
)
349 volatile bool m_bSignaled
;
350 CCondition
<volatile bool> m_condition
;
352 volatile bool m_bBroadcast
;
353 unsigned int m_iWaitingThreads
;