1 /* 2 3 Copyright (C) 2006,2007 Grame 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France 20 research@grame.fr 21 22 */ 23 24 #ifndef __TMutex__ 25 #define __TMutex__ 26 27 #if defined(WIN32) && !defined(__MINGW32__) 28 #include <windows.h> 29 #else 30 #include <pthread.h> 31 #endif 32 33 #include<assert.h> 34 35 class TMutex 36 { 37 38 private: 39 40 #if defined(WIN32) && !defined(__MINGW32__) 41 HANDLE fMutex; 42 #else 43 pthread_mutex_t fMutex; 44 #endif 45 46 public: 47 48 #if defined(WIN32) && !defined(__MINGW32__) 49 TMutex()50 TMutex() 51 { 52 fMutex = CreateMutex(0, FALSE, 0); 53 } ~TMutex()54 virtual ~TMutex() 55 { 56 CloseHandle(fMutex); 57 } 58 Lock()59 bool Lock() 60 { 61 return (WaitForSingleObject(fMutex, INFINITE) == WAIT_OBJECT_0); 62 } 63 TryLock()64 bool TryLock() 65 { 66 return (WaitForSingleObject(fMutex, 0) == WAIT_TIMEOUT); 67 } 68 Unlock()69 void Unlock() 70 { 71 ReleaseMutex(fMutex); 72 } 73 74 #else 75 76 TMutex() 77 { 78 // Use recursive mutex 79 pthread_mutexattr_t mutex_attr; 80 assert(pthread_mutexattr_init(&mutex_attr) == 0); 81 assert(pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE) == 0); 82 assert(pthread_mutex_init(&fMutex, &mutex_attr) == 0); 83 } 84 virtual ~TMutex() 85 { 86 pthread_mutex_destroy(&fMutex); 87 } 88 89 bool Lock() 90 { 91 return (pthread_mutex_lock(&fMutex) == 0); 92 } 93 94 bool TryLock() 95 { 96 return (pthread_mutex_trylock(&fMutex) == 0); 97 } 98 99 void Unlock() 100 { 101 pthread_mutex_unlock(&fMutex); 102 } 103 104 #endif 105 }; 106 107 class TLockAble 108 { 109 110 private: 111 112 TMutex fMutex; 113 114 public: 115 TLockAble()116 TLockAble() {} ~TLockAble()117 virtual ~TLockAble() {} 118 Lock()119 bool Lock() 120 { 121 return fMutex.Lock(); 122 } 123 Unlock()124 void Unlock() 125 { 126 fMutex.Unlock(); 127 } 128 TryLock()129 bool TryLock() 130 { 131 return fMutex.TryLock(); 132 } 133 134 }; 135 136 class TLock 137 { 138 private: 139 140 TLockAble* fObj; 141 142 public: 143 TLock(TLockAble * obj)144 TLock(TLockAble* obj):fObj(obj) 145 { 146 fObj->Lock(); 147 } 148 TLock(const TLockAble * obj)149 TLock(const TLockAble* obj):fObj((TLockAble*)obj) 150 { 151 fObj->Lock(); 152 } 153 ~TLock()154 virtual ~TLock() 155 { 156 fObj->Unlock(); 157 } 158 }; 159 160 #endif 161