| 1 | #pragma once |
| 2 | /* |
| 3 | * This file is part of the libCEC(R) library. |
| 4 | * |
| 5 | * libCEC(R) is Copyright (C) 2011-2012 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 | #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 | |
| 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 | { |
| 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 | } |
| 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: |
| 119 | mutex_t m_mutex; |
| 120 | volatile unsigned int m_iLockCount; |
| 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: |
| 169 | inline CCondition(void) |
| 170 | { |
| 171 | ConditionCreate(m_condition); |
| 172 | } |
| 173 | |
| 174 | inline ~CCondition(void) |
| 175 | { |
| 176 | Broadcast(); |
| 177 | ConditionDelete(m_condition); |
| 178 | } |
| 179 | |
| 180 | inline void Broadcast(void) |
| 181 | { |
| 182 | ConditionBroadcast(m_condition); |
| 183 | } |
| 184 | |
| 185 | inline void Signal(void) |
| 186 | { |
| 187 | ConditionSignal(m_condition); |
| 188 | } |
| 189 | |
| 190 | inline bool Wait(CMutex &mutex, uint32_t iTimeout = 0) |
| 191 | { |
| 192 | return ConditionWait(m_condition, mutex.m_mutex, iTimeout); |
| 193 | } |
| 194 | |
| 195 | static void Sleep(uint32_t iTimeout) |
| 196 | { |
| 197 | CCondition w; |
| 198 | CMutex m; |
| 199 | CLockObject lock(m); |
| 200 | w.Wait(m, iTimeout); |
| 201 | } |
| 202 | |
| 203 | condition_t m_condition; |
| 204 | }; |
| 205 | } |