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; }
59 class CMutex
: public PreventCopy
61 friend class CCondition
;
75 inline bool TryLock(void)
77 if (MutexTryLock(m_mutex
))
85 inline bool Lock(void)
92 inline void Unlock(void)
96 if (m_iLockCount
>= 2)
103 MutexUnlock(m_mutex
);
107 inline bool Clear(void)
112 unsigned int iLockCount
= m_iLockCount
;
113 for (unsigned int iPtr
= 0; iPtr
< iLockCount
; iPtr
++)
122 volatile unsigned int m_iLockCount
;
125 class CLockObject
: public PreventCopy
128 inline CLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
130 m_bClearOnExit(bClearOnExit
)
135 inline ~CLockObject(void)
143 inline bool TryLock(void)
145 return m_mutex
.TryLock();
148 inline void Unlock(void)
153 inline bool Clear(void)
155 return m_mutex
.Clear();
158 inline bool Lock(void)
160 return m_mutex
.Lock();
168 class CTryLockObject
: public PreventCopy
171 inline CTryLockObject(CMutex
&mutex
, bool bClearOnExit
= false) :
173 m_bClearOnExit(bClearOnExit
),
174 m_bIsLocked(m_mutex
.TryLock())
178 inline ~CTryLockObject(void)
182 else if (m_bIsLocked
)
186 inline bool TryLock(void)
188 bool bReturn
= m_mutex
.TryLock();
189 m_bIsLocked
|= bReturn
;
193 inline void Unlock(void)
202 inline bool Clear(void)
205 return m_mutex
.Clear();
208 inline bool Lock(void)
210 bool bReturn
= m_mutex
.Lock();
211 m_bIsLocked
|= bReturn
;
215 inline bool IsLocked(void) const
223 volatile bool m_bIsLocked
;
226 class CCondition
: public PreventCopy
229 inline CCondition(void) :
231 m_iWaitingThreads(0) {}
232 inline ~CCondition(void)
240 m_condition
.Broadcast();
246 m_condition
.Signal();
249 bool Wait(CMutex
&mutex
, uint32_t iTimeout
= 0)
252 CLockObject
lock(m_mutex
);
258 CTimeout
timeout(iTimeout
);
259 while (!m_bPredicate
&& timeout
.TimeLeft() > 0)
260 m_condition
.Wait(mutex
.m_mutex
, timeout
.TimeLeft());
264 while (!m_bPredicate
)
265 m_condition
.Wait(mutex
.m_mutex
, 0);
268 return ResetAndReturn();
271 static void Sleep(uint32_t iTimeout
)
280 void Set(bool bBroadcast
= false)
282 CLockObject
lock(m_mutex
);
284 m_bBroadcast
= bBroadcast
;
287 bool ResetAndReturn(void)
289 CLockObject
lock(m_mutex
);
290 bool bReturn(m_bPredicate
);
291 if (bReturn
&& (--m_iWaitingThreads
== 0 || !m_bBroadcast
))
292 m_bPredicate
= false;
297 CConditionImpl m_condition
;
298 volatile bool m_bPredicate
;
299 volatile bool m_bBroadcast
;
300 unsigned int m_iWaitingThreads
;