Commit | Line | Data |
---|---|---|
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 | ||
f00ff009 LOK |
42 | namespace PLATFORM |
43 | { | |
44 | class PreventCopy | |
45 | { | |
46 | public: | |
47 | inline PreventCopy(void) {} | |
48 | inline ~PreventCopy(void) {} | |
49 | ||
50 | private: | |
51 | inline PreventCopy(const PreventCopy &c) { *this = c; } | |
52 | inline PreventCopy &operator=(const PreventCopy &c){ *this = c; return *this; } | |
53 | }; | |
54 | ||
55 | class CCondition; | |
56 | ||
57 | class CMutex : public PreventCopy | |
58 | { | |
59 | friend class CCondition; | |
60 | public: | |
61 | inline CMutex(void) : | |
62 | m_iLockCount(0) | |
63 | { | |
64 | MutexCreate(m_mutex); | |
65 | } | |
66 | ||
67 | inline ~CMutex(void) | |
68 | { | |
69 | Clear(); | |
70 | MutexDelete(m_mutex); | |
71 | } | |
72 | ||
73 | inline bool TryLock(void) | |
74 | { | |
75 | if (MutexTryLock(m_mutex)) | |
76 | { | |
77 | ++m_iLockCount; | |
78 | return true; | |
79 | } | |
80 | return false; | |
81 | } | |
82 | ||
83 | inline bool Lock(void) | |
84 | { | |
85 | MutexLock(m_mutex); | |
86 | ++m_iLockCount; | |
87 | return true; | |
88 | } | |
89 | ||
90 | inline void Unlock(void) | |
91 | { | |
ee2304ce LOK |
92 | if (Lock()) |
93 | { | |
94 | if (m_iLockCount >= 2) | |
95 | { | |
96 | --m_iLockCount; | |
97 | MutexUnlock(m_mutex); | |
98 | } | |
99 | ||
100 | --m_iLockCount; | |
101 | MutexUnlock(m_mutex); | |
102 | } | |
f00ff009 LOK |
103 | } |
104 | ||
105 | inline bool Clear(void) | |
106 | { | |
107 | bool bReturn(false); | |
108 | if (TryLock()) | |
109 | { | |
110 | unsigned int iLockCount = m_iLockCount; | |
111 | for (unsigned int iPtr = 0; iPtr < iLockCount; iPtr++) | |
112 | Unlock(); | |
113 | bReturn = true; | |
114 | } | |
115 | return bReturn; | |
116 | } | |
117 | ||
118 | private: | |
ee2304ce LOK |
119 | mutex_t m_mutex; |
120 | volatile unsigned int m_iLockCount; | |
f00ff009 LOK |
121 | }; |
122 | ||
123 | class CLockObject : public PreventCopy | |
124 | { | |
125 | public: | |
126 | inline CLockObject(CMutex &mutex, bool bClearOnExit = false) : | |
127 | m_mutex(mutex), | |
128 | m_bClearOnExit(bClearOnExit) | |
129 | { | |
130 | m_mutex.Lock(); | |
131 | } | |
132 | ||
133 | inline ~CLockObject(void) | |
134 | { | |
135 | if (m_bClearOnExit) | |
136 | Clear(); | |
137 | else | |
138 | Unlock(); | |
139 | } | |
140 | ||
141 | inline bool TryLock(void) | |
142 | { | |
143 | return m_mutex.TryLock(); | |
144 | } | |
145 | ||
146 | inline void Unlock(void) | |
147 | { | |
148 | m_mutex.Unlock(); | |
149 | } | |
150 | ||
151 | inline bool Clear(void) | |
152 | { | |
153 | return m_mutex.Clear(); | |
154 | } | |
155 | ||
156 | inline bool Lock(void) | |
157 | { | |
158 | return m_mutex.Lock(); | |
159 | } | |
160 | ||
161 | private: | |
162 | CMutex &m_mutex; | |
163 | bool m_bClearOnExit; | |
164 | }; | |
165 | ||
166 | class CCondition : public PreventCopy | |
167 | { | |
168 | public: | |
3a590d6a | 169 | inline CCondition(void) {} |
f00ff009 LOK |
170 | inline ~CCondition(void) |
171 | { | |
3a590d6a | 172 | m_condition.Broadcast(); |
f00ff009 LOK |
173 | } |
174 | ||
175 | inline void Broadcast(void) | |
176 | { | |
3a590d6a | 177 | m_condition.Broadcast(); |
f00ff009 LOK |
178 | } |
179 | ||
180 | inline void Signal(void) | |
181 | { | |
3a590d6a | 182 | m_condition.Signal(); |
f00ff009 LOK |
183 | } |
184 | ||
185 | inline bool Wait(CMutex &mutex, uint32_t iTimeout = 0) | |
186 | { | |
3a590d6a | 187 | return m_condition.Wait(mutex.m_mutex, iTimeout); |
f00ff009 LOK |
188 | } |
189 | ||
190 | static void Sleep(uint32_t iTimeout) | |
191 | { | |
192 | CCondition w; | |
193 | CMutex m; | |
194 | CLockObject lock(m); | |
195 | w.Wait(m, iTimeout); | |
196 | } | |
197 | ||
3a590d6a LOK |
198 | private: |
199 | CConditionImpl m_condition; | |
f00ff009 LOK |
200 | }; |
201 | } |