Changeset 5443
- Timestamp:
- 08/13/06 09:17:42
- Files:
-
- OpenThreads/trunk/win32_src/Win32Mutex.cpp (modified) (8 diffs)
- OpenThreads/trunk/win32_src/Win32MutexPrivateData.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
OpenThreads/trunk/win32_src/Win32Mutex.cpp
r5313 r5443 28 28 using namespace OpenThreads; 29 29 30 Win32MutexPrivateData::Win32MutexPrivateData() 31 { 32 #ifdef USE_CRITICAL_SECTION 33 InitializeCriticalSection( &_cs ); 34 #else 35 mutex = 0; 36 #endif 37 } 30 38 Win32MutexPrivateData::~Win32MutexPrivateData() 31 39 { 32 } 33 34 40 #ifdef USE_CRITICAL_SECTION 41 DeleteCriticalSection( &_cs ); 42 #endif 43 } 44 45 46 #ifndef USE_CRITICAL_SECTION 35 47 36 48 template <int instance> … … 55 67 if (__log_nsec <= 20) { 56 68 SwitchToThread(); //Sleep(0); // adegli replaced it Sleep by SwitchToThread 57 } else {69 } else { 58 70 Sleep(1 << (__log_nsec - 20)); 59 }71 } 60 72 } 61 73 62 74 63 75 #if defined(_MSC_VER) && _MSC_VER <= 1300 64 template WIN32MutexSpin <0>;76 template WIN32MutexSpin <0>; 65 77 #endif 78 79 #endif // USE_CRITICAL_SECTION 66 80 67 81 //---------------------------------------------------------------------------- … … 73 87 Mutex::Mutex() { 74 88 Win32MutexPrivateData *pd = new Win32MutexPrivateData(); 75 pd->mutex = 0;76 89 _prvData = static_cast<void *>(pd); 77 90 } … … 97 110 static_cast<Win32MutexPrivateData*>(_prvData); 98 111 112 #ifdef USE_CRITICAL_SECTION 113 114 // Block until we can take this lock. 115 EnterCriticalSection( &(pd->_cs) ); 116 117 return 0; 118 119 #else 120 99 121 volatile unsigned long* lock = &pd->mutex; 100 // InterlockedExchange returns old value101 // if old_value == 0 mutex wasn't locked , now it is102 if( !InterlockedExchange((long*)lock, 1L)) {122 // InterlockedExchange returns old value 123 // if old_value == 0 mutex wasn't locked , now it is 124 if( !InterlockedExchange((long*)lock, 1L)) { 103 125 return 0; 104 126 } … … 136 158 _S_nsec_sleep(__log_nsec); 137 159 } 138 return -1; 160 return -1; 161 162 #endif // USE_CRITICAL_SECTION 139 163 } 140 164 … … 148 172 Win32MutexPrivateData *pd = 149 173 static_cast<Win32MutexPrivateData*>(_prvData); 174 175 #ifdef USE_CRITICAL_SECTION 176 177 // Release this lock. CRITICAL_SECTION is nested, thus 178 // unlock() calls must be paired with lock() calls. 179 LeaveCriticalSection( &(pd->_cs) ); 180 181 return 0; 182 183 #else 150 184 151 185 volatile unsigned long* lock = &pd->mutex; … … 153 187 // This is not sufficient on many multiprocessors, since 154 188 // writes to protected variables and the lock may be reordered. 155 return 0; 189 return 0; 190 191 #endif // USE_CRITICAL_SECTION 156 192 } 157 193 … … 166 202 static_cast<Win32MutexPrivateData*>(_prvData); 167 203 204 #ifdef USE_CRITICAL_SECTION 205 206 // Take the lock if we can; regardless don't block. 207 // 'result' is FALSE if we took the lock or already held 208 // it amd TRUE if another thread already owns the lock. 209 BOOL result = TryEnterCriticalSection( &(pd->_cs) ); 210 211 return( (result==TRUE) ? 0 : 1 ); 212 213 #else 214 168 215 volatile unsigned long* lock = &pd->mutex; 169 216 170 if( !InterlockedExchange((long*)lock, 1L)) {217 if( !InterlockedExchange((long*)lock, 1L)) { 171 218 return 1; // TRUE 172 219 } 173 220 174 return 0; // FALSE 175 176 } 221 return 0; // FALSE 222 223 #endif // USE_CRITICAL_SECTION 224 } 225 OpenThreads/trunk/win32_src/Win32MutexPrivateData.h
r5313 r5443 38 38 private: 39 39 40 Win32MutexPrivateData() {};40 Win32MutexPrivateData(); 41 41 42 42 ~Win32MutexPrivateData(); 43 43 #define USE_CRITICAL_SECTION 44 #ifdef USE_CRITICAL_SECTION 45 CRITICAL_SECTION _cs; 46 #else 44 47 volatile unsigned long mutex; 48 #endif 45 49 46 50 };
