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/
36 #if defined(__WINDOWS__)
37 #include "../windows/os-threads.h"
39 #include "../posix/os-threads.h"
42 #include "../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
&c
){ *this = 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)
94 inline void Unlock(void)
98 if (m_iLockCount
>= 2)
101 MutexUnlock(m_mutex
);
105 MutexUnlock(m_mutex
);
109 inline bool Clear(void)
114 unsigned int iLockCount
= m_iLockCount
;
115 for (unsigned int iPtr
= 0; iPtr
< iLockCount
; iPtr
++)
124 volatile unsigned int m_iLockCount
;
127 class CLockObject
: public PreventCopy
130 inline CLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
132 m_bClearOnExit(bClearOnExit
)
137 inline ~CLockObject(void)
145 inline bool TryLock(void)
147 return m_mutex
.TryLock();
150 inline void Unlock(void)
155 inline bool Clear(void)
157 return m_mutex
.Clear();
160 inline bool Lock(void)
162 return m_mutex
.Lock();
170 class CTryLockObject
: public PreventCopy
173 inline CTryLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
175 m_bClearOnExit(bClearOnExit
),
176 m_bIsLocked(m_mutex
.TryLock())
180 inline ~CTryLockObject(void)
184 else if (m_bIsLocked
)
188 inline bool TryLock(void)
190 bool bReturn
= m_mutex
.TryLock();
191 m_bIsLocked
|= bReturn
;
195 inline void Unlock(void)
204 inline bool Clear(void)
207 return m_mutex
.Clear();
210 inline bool Lock(void)
212 bool bReturn
= m_mutex
.Lock();
213 m_bIsLocked
|= bReturn
;
217 inline bool IsLocked(void) const
225 volatile bool m_bIsLocked
;
228 template <typename _Predicate
>
229 class CCondition
: public PreventCopy
232 inline CCondition(void) {}
233 inline ~CCondition(void)
235 m_condition
.Broadcast();
238 inline void Broadcast(void)
240 m_condition
.Broadcast();
243 inline void Signal(void)
245 m_condition
.Signal();
248 inline bool Wait(CMutex
&mutex
, _Predicate
&predicate
)
251 m_condition
.Wait(mutex
.m_mutex
);
255 inline bool Wait(CMutex
&mutex
, _Predicate
&predicate
, uint32_t iTimeout
)
258 return Wait(mutex
, predicate
);
265 CTimeout
timeout(iTimeout
);
268 while (!bReturn
&& !bBreak
)
270 iMsLeft
= timeout
.TimeLeft();
271 if ((bReturn
= predicate
) == false && (bBreak
= iMsLeft
== 0) == false)
272 m_condition
.Wait(mutex
.m_mutex
, iMsLeft
);
278 CConditionImpl m_condition
;
284 CEvent(bool bAutoReset
= true) :
287 m_iWaitingThreads(0),
288 m_bAutoReset(bAutoReset
) {}
289 virtual ~CEvent(void) {}
294 m_condition
.Broadcast();
300 m_condition
.Signal();
305 CLockObject
lock(m_mutex
);
308 bool bReturn
= m_condition
.Wait(m_mutex
, m_bSignaled
);
309 return ResetAndReturn() && bReturn
;
312 bool Wait(uint32_t iTimeout
)
317 CLockObject
lock(m_mutex
);
319 bool bReturn
= m_condition
.Wait(m_mutex
, m_bSignaled
, iTimeout
);
320 return ResetAndReturn() && bReturn
;
323 static void Sleep(uint32_t iTimeout
)
326 event
.Wait(iTimeout
);
330 void Set(bool bBroadcast
= false)
332 CLockObject
lock(m_mutex
);
334 m_bBroadcast
= bBroadcast
;
337 bool ResetAndReturn(void)
339 CLockObject
lock(m_mutex
);
340 bool bReturn(m_bSignaled
);
341 if (bReturn
&& (--m_iWaitingThreads
== 0 || !m_bBroadcast
) && m_bAutoReset
)
346 volatile bool m_bSignaled
;
347 CCondition
<volatile bool> m_condition
;
349 volatile bool m_bBroadcast
;
350 unsigned int m_iWaitingThreads
;