29 #ifdef OD_POSIX_THREADS
31 #elif defined(ODA_WINDOWS)
32 #ifndef WIN32_LEAN_AND_MEAN
33 #define WIN32_LEAN_AND_MEAN
51 #ifdef OD_POSIX_THREADS
52 pthread_mutex_t _mutex;
56 pthread_mutexattr_t attr;
57 pthread_mutexattr_init(&attr);
58 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
59 pthread_mutex_init(&_mutex, &attr);
60 pthread_mutexattr_destroy(&attr);
64 pthread_mutex_destroy((pthread_mutex_t*)&_mutex);
71 pthread_mutex_lock(&_mutex);
78 pthread_mutex_unlock(&_mutex);
80 #elif (defined(ODA_WINDOWS)) && !defined(_WIN32_WCE) && !defined(_WINRT)
81 CRITICAL_SECTION _mutex;
85 InitializeCriticalSection(&_mutex);
89 DeleteCriticalSection(&_mutex);
96 EnterCriticalSection(&_mutex);
103 LeaveCriticalSection(&_mutex);
149 #ifndef TD_SINGLE_THREAD
150 #define TD_AUTOLOCK(Mutex) OdMutexAutoLock autoLock(Mutex);
152 #define TD_AUTOLOCK(Mutex)
156 #ifndef TD_SINGLE_THREAD
157 #if defined(_MSC_VER) && _M_IX86 >= 400 && !defined(_WIN32_WCE)
159 #pragma warning(push)
160 #pragma warning(disable:4035)
161 #pragma warning(disable:4793)
162 inline int OdInterlockedExchange(
volatile int* dest,
int val)
171 inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
180 inline int OdInterlockedIncrement(
volatile int* dest)
190 inline int OdInterlockedDecrement(
volatile int* dest)
201 #elif (defined(_WIN32) || defined(_WIN64)) && !defined(_WIN32_WCE) && !defined(_WINRT) && !defined(ODA_WINDOWS_GCC)
203 #define OdInterlockedExchange(dest, val) InterlockedExchange((LONG*)(dest), val)
204 #define OdInterlockedExchangeAdd(dest, incr) InterlockedExchangeAdd((LONG*)(dest), incr)
205 #define OdInterlockedIncrement(dest) InterlockedIncrement((LONG*)(dest))
206 #define OdInterlockedDecrement(dest) InterlockedDecrement((LONG*)(dest))
207 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
209 inline int OdInterlockedExchange(
volatile int* dest,
int val)
212 __asm__ __volatile__ (
213 "lock; xchgl %0, (%1)"
215 :
"r" (dest),
"0" (val)
219 inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
222 __asm__ __volatile__ (
223 "lock; xaddl %0, (%1)"
225 :
"r" (dest),
"0" (incr)
229 inline int OdInterlockedIncrement(
volatile int* dest)
231 return OdInterlockedExchangeAdd(dest, 1) + 1;
233 inline int OdInterlockedDecrement(
volatile int* dest)
235 return OdInterlockedExchangeAdd(dest, -1) - 1;
237 #elif defined(__GNUC__) && defined(__POWERPC__)
239 inline int OdInterlockedExchange(
volatile int* dest,
int val)
244 __asm__ __volatile__ (
245 "0: lwarx %0, 0, %1\n"
246 " stwcx. %2, 0, %1\n"
250 :
"r"(dest),
"r"(val)
251 :
"cr0",
"memory",
"r0");
254 inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
260 __asm__ __volatile__ (
261 "0: lwarx %0, %3, %1\n"
263 " stwcx. %0, %3, %1\n"
267 :
"r"(dest),
"r"(incr),
"r"(zero)
268 :
"cr0",
"memory",
"r0");
271 inline int OdInterlockedIncrement(
volatile int* dest)
273 return OdInterlockedExchangeAdd(dest, 1) + 1;
275 inline int OdInterlockedDecrement(
volatile int* dest)
277 return OdInterlockedExchangeAdd(dest, -1) - 1;
279 #elif defined(__APPLE__)
281 #include <libkern/OSAtomic.h>
282 inline int OdInterlockedExchange(
volatile int* dest,
int val)
284 int oldValue = *dest;
285 while (!OSAtomicCompareAndSwapIntBarrier(oldValue, val, dest))
289 inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
291 return OSAtomicAdd32Barrier(incr, dest) - incr;
293 inline int OdInterlockedIncrement(
volatile int* dest)
295 return OSAtomicIncrement32Barrier(dest);
297 inline int OdInterlockedDecrement(
volatile int* dest)
299 return OSAtomicDecrement32Barrier(dest);
325 #elif defined(ANDROID)
326 inline int OdInterlockedExchange(
volatile int* dest,
int val)
328 return __sync_lock_test_and_set(dest, val);
330 inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
333 return __sync_fetch_and_add(dest, incr);
335 inline int OdInterlockedIncrement(
volatile int* dest)
337 return __sync_fetch_and_add(dest, 1) + 1;
339 inline int OdInterlockedDecrement(
volatile int* dest)
341 return __sync_fetch_and_sub(dest, 1) - 1;
387 #define TD_SINGLE_THREAD
388 #endif //architecture
389 #endif //TD_SINGLE_THREAD
397 #ifndef TD_SINGLE_THREAD
399 #pragma managed(push, off)
405 typedef int RefCounterType;
406 volatile RefCounterType _ref_count;
407 OdRefCounter& operator = (RefCounterType n) { _ref_count = 0; OdInterlockedExchange(&_ref_count, n);
return *
this; }
408 operator RefCounterType ()
const {
return OdInterlockedExchangeAdd(
const_cast<RefCounterType*
>(&_ref_count), 0); }
409 RefCounterType operator ++ () {
return OdInterlockedIncrement(&_ref_count); }
410 RefCounterType operator -- () {
return OdInterlockedDecrement(&_ref_count); }
421 typedef int VolatileType;
422 volatile VolatileType _val;
423 OdVolatile& operator = (VolatileType n) { _val = 0; OdInterlockedExchange(&_val, n);
return *
this; }
424 operator VolatileType ()
const {
return OdInterlockedExchangeAdd(
const_cast<VolatileType*
>(&_val), 0); }
425 VolatileType operator|=(VolatileType n) {
return OdInterlockedExchange(&_val, _val|n); }
426 VolatileType operator&=(VolatileType n) {
return OdInterlockedExchange(&_val, _val&n); }