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