/usr/include/cec-platform/threads/mutex.h is in libcec-platform-dev 1.0.10+dfsg1-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | #pragma once
/*
* This file is part of the libCEC(R) library.
*
* libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
*
* This program is dual-licensed; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Alternatively, you can license this library under a commercial license,
* please contact Pulse-Eight Licensing for more information.
*
* For more information contact:
* Pulse-Eight Licensing <license@pulse-eight.com>
* http://www.pulse-eight.com/
* http://www.pulse-eight.net/
*/
#include "../os.h"
#if defined(__WINDOWS__)
#include "../windows/os-threads.h"
#else
#include "../posix/os-threads.h"
#endif
#include "../util/timeutils.h"
namespace PLATFORM
{
class PreventCopy
{
public:
inline PreventCopy(void) {}
inline ~PreventCopy(void) {}
private:
inline PreventCopy(const PreventCopy &c) { *this = c; }
inline PreventCopy &operator=(const PreventCopy &c){ (void)c; return *this; }
};
template <typename _Predicate>
class CCondition;
class CMutex : public PreventCopy
{
template <typename _Predicate>
friend class CCondition;
public:
inline CMutex(void) :
m_iLockCount(0)
{
MutexCreate(m_mutex);
}
inline ~CMutex(void)
{
Clear();
MutexDelete(m_mutex);
}
inline bool TryLock(void)
{
if (MutexTryLock(m_mutex))
{
++m_iLockCount;
return true;
}
return false;
}
inline bool Lock(void)
{
MutexLock(m_mutex);
++m_iLockCount;
return true;
}
inline void Unlock(void)
{
if (Lock())
{
if (m_iLockCount >= 2)
{
--m_iLockCount;
MutexUnlock(m_mutex);
}
--m_iLockCount;
MutexUnlock(m_mutex);
}
}
inline bool Clear(void)
{
bool bReturn(false);
if (TryLock())
{
unsigned int iLockCount = m_iLockCount;
for (unsigned int iPtr = 0; iPtr < iLockCount; iPtr++)
Unlock();
bReturn = true;
}
return bReturn;
}
private:
mutex_t m_mutex;
volatile unsigned int m_iLockCount;
};
class CLockObject : public PreventCopy
{
public:
inline CLockObject(CMutex &mutex, bool bClearOnExit = false) :
m_mutex(mutex),
m_bClearOnExit(bClearOnExit)
{
m_mutex.Lock();
}
inline ~CLockObject(void)
{
if (m_bClearOnExit)
Clear();
else
Unlock();
}
inline bool TryLock(void)
{
return m_mutex.TryLock();
}
inline void Unlock(void)
{
m_mutex.Unlock();
}
inline bool Clear(void)
{
return m_mutex.Clear();
}
inline bool Lock(void)
{
return m_mutex.Lock();
}
private:
CMutex &m_mutex;
bool m_bClearOnExit;
};
class CTryLockObject : public PreventCopy
{
public:
inline CTryLockObject(CMutex &mutex, bool bClearOnExit = false) :
m_mutex(mutex),
m_bClearOnExit(bClearOnExit),
m_bIsLocked(m_mutex.TryLock())
{
}
inline ~CTryLockObject(void)
{
if (m_bClearOnExit)
Clear();
else if (m_bIsLocked)
Unlock();
}
inline bool TryLock(void)
{
bool bReturn = m_mutex.TryLock();
m_bIsLocked |= bReturn;
return bReturn;
}
inline void Unlock(void)
{
if (m_bIsLocked)
{
m_bIsLocked = false;
m_mutex.Unlock();
}
}
inline bool Clear(void)
{
m_bIsLocked = false;
return m_mutex.Clear();
}
inline bool Lock(void)
{
bool bReturn = m_mutex.Lock();
m_bIsLocked |= bReturn;
return bReturn;
}
inline bool IsLocked(void) const
{
return m_bIsLocked;
}
private:
CMutex & m_mutex;
bool m_bClearOnExit;
volatile bool m_bIsLocked;
};
typedef bool (*PredicateCallback) (void *param);
template <typename _Predicate>
class CCondition : public PreventCopy
{
private:
static bool _PredicateCallbackDefault ( void *param )
{
_Predicate *p = (_Predicate*)param;
return (*p);
}
public:
inline CCondition(void) {}
inline ~CCondition(void)
{
m_condition.Broadcast();
}
inline void Broadcast(void)
{
m_condition.Broadcast();
}
inline void Signal(void)
{
m_condition.Signal();
}
inline bool Wait(CMutex &mutex, uint32_t iTimeout)
{
return m_condition.Wait(mutex.m_mutex, iTimeout);
}
inline bool Wait(CMutex &mutex, PredicateCallback callback, void *param, uint32_t iTimeout)
{
bool bReturn(false);
CTimeout timeout(iTimeout);
while (!bReturn)
{
if ((bReturn = callback(param)) == true)
break;
uint32_t iMsLeft = timeout.TimeLeft();
if ((iTimeout != 0) && (iMsLeft == 0))
break;
m_condition.Wait(mutex.m_mutex, iMsLeft);
}
return bReturn;
}
inline bool Wait(CMutex &mutex, _Predicate &predicate, uint32_t iTimeout = 0)
{
return Wait(mutex, _PredicateCallbackDefault, (void*)&predicate, iTimeout);
}
private:
CConditionImpl m_condition;
};
class CEvent
{
public:
CEvent(bool bAutoReset = true) :
m_bSignaled(false),
m_bBroadcast(false),
m_iWaitingThreads(0),
m_bAutoReset(bAutoReset) {}
virtual ~CEvent(void) {}
void Broadcast(void)
{
Set(true);
m_condition.Broadcast();
}
void Signal(void)
{
Set(false);
m_condition.Signal();
}
bool Wait(void)
{
CLockObject lock(m_mutex);
++m_iWaitingThreads;
bool bReturn = m_condition.Wait(m_mutex, m_bSignaled);
return ResetAndReturn() && bReturn;
}
bool Wait(uint32_t iTimeout)
{
if (iTimeout == 0)
return Wait();
CLockObject lock(m_mutex);
++m_iWaitingThreads;
bool bReturn = m_condition.Wait(m_mutex, m_bSignaled, iTimeout);
return ResetAndReturn() && bReturn;
}
static void Sleep(uint32_t iTimeout)
{
CEvent event;
event.Wait(iTimeout);
}
void Reset(void)
{
CLockObject lock(m_mutex);
m_bSignaled = false;
}
private:
void Set(bool bBroadcast = false)
{
CLockObject lock(m_mutex);
m_bSignaled = true;
m_bBroadcast = bBroadcast;
}
bool ResetAndReturn(void)
{
CLockObject lock(m_mutex);
bool bReturn(m_bSignaled);
if (bReturn && (--m_iWaitingThreads == 0 || !m_bBroadcast) && m_bAutoReset)
m_bSignaled = false;
return bReturn;
}
volatile bool m_bSignaled;
CCondition<volatile bool> m_condition;
CMutex m_mutex;
volatile bool m_bBroadcast;
unsigned int m_iWaitingThreads;
bool m_bAutoReset;
};
}
|