33#ifdef OD_POSIX_THREADS
67#ifdef OD_POSIX_THREADS
68 pthread_mutex_t _mutex;
72 pthread_mutexattr_t attr;
73 pthread_mutexattr_init(&attr);
74 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
75 pthread_mutex_init(&_mutex, &attr);
76 pthread_mutexattr_destroy(&attr);
80 pthread_mutex_destroy((pthread_mutex_t*)&_mutex);
87 pthread_mutex_lock(&_mutex);
94 pthread_mutex_unlock(&_mutex);
96#elif (defined(ODA_WINDOWS)) && !defined(_WIN32_WCE) && !defined(_WINRT)
99 std::recursive_mutex _mutex;
123 CRITICAL_SECTION _mutex;
127 InitializeCriticalSection(&_mutex);
131 DeleteCriticalSection(&_mutex);
138 EnterCriticalSection(&_mutex);
145 LeaveCriticalSection(&_mutex);
191#ifndef TD_SINGLE_THREAD
192#define TD_AUTOLOCK(Mutex) OdMutexAutoLock autoLock(Mutex);
194#define TD_AUTOLOCK(Mutex)
198#ifndef TD_SINGLE_THREAD
199#if defined(_MSC_VER) && defined(_M_IX86) && (_M_IX86 >= 400) && !defined(_WIN32_WCE)
201inline int OdInterlockedExchange(
volatile int* dest,
int val)
210inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
220inline int OdInterlockedCompareExchange(
volatile int* dest,
int x,
int compare)
227 lock cmpxchg[edx], ecx
231#elif (defined(_WIN32) || defined(_WIN64)) && !defined(ODA_WINDOWS_GCC)
233#define OdInterlockedExchange(dest, val) InterlockedExchange((LONG*)(dest), val)
234#define OdInterlockedExchangeAdd(dest, incr) InterlockedExchangeAdd((LONG*)(dest), incr)
235#define OdInterlockedCompareExchange(dest, x, compare) InterlockedCompareExchange((LONG*)(dest), x, compare)
236#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(ANDROID) && !defined(_APPLE_)
238inline int OdInterlockedExchange(
volatile int* dest,
int val)
241 __asm__ __volatile__ (
242 "lock; xchgl %0, (%1)"
244 :
"r" (dest),
"0" (val)
248inline int OdInterlockedCompareExchange(
int volatile *dest,
int xchg,
int compare)
251 __asm__ __volatile__(
"lock; cmpxchgl %2,(%1)"
252 :
"=a" (ret) :
"r" (dest),
"r" (xchg),
"0" (compare) :
"memory");
255#elif defined(__GNUC__) && defined(__POWERPC__)
257inline int OdInterlockedExchange(
volatile int* dest,
int val)
262 __asm__ __volatile__ (
263 "0: lwarx %0, 0, %1\n"
264 " stwcx. %2, 0, %1\n"
268 :
"r"(dest),
"r"(val)
269 :
"cr0",
"memory",
"r0");
272inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
278 __asm__ __volatile__ (
279 "0: lwarx %0, %3, %1\n"
281 " stwcx. %0, %3, %1\n"
285 :
"r"(dest),
"r"(incr),
"r"(zero)
286 :
"cr0",
"memory",
"r0");
290inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
294 __asm__ __volatile__(
302 :
"=&r"(ret),
"=&r"(scratch)
303 :
"r"(dest),
"r"(xchg),
"r"(compare)
304 :
"cr0",
"memory",
"r0");
308#elif defined(__APPLE__)
310#include <libkern/OSAtomic.h>
311inline int OdInterlockedExchange(
volatile int* dest,
int val)
313 int oldValue = *dest;
314 while (!OSAtomicCompareAndSwapIntBarrier(oldValue, val, dest))
318inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
320 return OSAtomicAdd32Barrier(incr, dest) - incr;
322inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
325 return OSAtomicCompareAndSwapIntBarrier(compare, xchg, dest) ? compare : xchg;
327#elif defined(ANDROID) && !defined(ANDROID_GOOGLE)
362#elif defined(ANDROID)
363inline int OdInterlockedExchange(
volatile int* dest,
int val)
365 return __sync_lock_test_and_set(dest, val);
367inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
370 return __sync_fetch_and_add(dest, incr);
372inline int OdInterlockedIncrement(
volatile int* dest)
374 return __sync_fetch_and_add(dest, 1) + 1;
376inline int OdInterlockedDecrement(
volatile int* dest)
378 return __sync_fetch_and_sub(dest, 1) - 1;
405#elif defined(ANDROID) || ( defined(__linux__) && defined(__aarch64__) && (__cplusplus >= 201103L) )
407inline int OdInterlockedExchange(
volatile int* dest,
int val)
410 __sync_val_compare_and_swap(dest, oldVal, val);
413inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
416 __sync_val_compare_and_swap(dest, oldVal, incr + oldVal);
419inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
421 bool bRes = __sync_bool_compare_and_swap(dest, compare, xchg);
422 return bRes ? xchg : compare;
426#define TD_SINGLE_THREAD
437#ifndef TD_SINGLE_THREAD
439#pragma managed(push, off)
446 typedef int RefCounterType;
447 volatile RefCounterType _ref_count;
448 OdRefCounter& operator = (RefCounterType n) { _ref_count = 0; OdInterlockedExchange(&_ref_count, n);
return *
this; }
449 operator RefCounterType ()
const {
return OdInterlockedExchangeAdd(
const_cast<RefCounterType*
>(&_ref_count), 0); }
450 RefCounterType operator ++ () {
return OdInterlockedIncrement(&_ref_count); }
451 RefCounterType operator -- () {
return OdInterlockedDecrement(&_ref_count); }
463 typedef int VolatileType;
464 volatile VolatileType _val;
465 OdVolatile& operator = (VolatileType n) { _val = 0; OdInterlockedExchange(&_val, n);
return *
this; }
466 operator VolatileType ()
const {
return OdInterlockedExchangeAdd(
const_cast<VolatileType*
>(&_val), 0); }
467 VolatileType operator|=(VolatileType n) {
return OdInterlockedExchange(&_val, _val|n); }
468 VolatileType operator&=(VolatileType n) {
return OdInterlockedExchange(&_val, _val&n); }
OdMutexAutoLock(OdMutex &mutex)