33#ifdef OD_POSIX_THREADS
66#ifdef OD_POSIX_THREADS
67 pthread_mutex_t _mutex;
71 pthread_mutexattr_t attr;
72 pthread_mutexattr_init(&attr);
73 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
74 pthread_mutex_init(&_mutex, &attr);
75 pthread_mutexattr_destroy(&attr);
79 pthread_mutex_destroy((pthread_mutex_t*)&_mutex);
86 pthread_mutex_lock(&_mutex);
93 pthread_mutex_unlock(&_mutex);
95#elif (defined(ODA_WINDOWS)) && !defined(_WIN32_WCE) && !defined(_WINRT)
96 CRITICAL_SECTION _mutex;
100 InitializeCriticalSection(&_mutex);
104 DeleteCriticalSection(&_mutex);
111 EnterCriticalSection(&_mutex);
118 LeaveCriticalSection(&_mutex);
164#ifndef TD_SINGLE_THREAD
165#define TD_AUTOLOCK(Mutex) OdMutexAutoLock autoLock(Mutex);
167#define TD_AUTOLOCK(Mutex)
171#ifndef TD_SINGLE_THREAD
172#if defined(_MSC_VER) && (_M_IX86) && (_M_IX86 >= 400) && !defined(_WIN32_WCE)
175#pragma warning(disable:4035)
176#pragma warning(disable:4793)
177inline int OdInterlockedExchange(
volatile int* dest,
int val)
186inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
196inline int OdInterlockedCompareExchange(
volatile int* dest,
int x,
int compare)
203 lock cmpxchg[edx], ecx
208#elif (defined(_WIN32) || defined(_WIN64)) && !defined(ODA_WINDOWS_GCC)
210#define OdInterlockedExchange(dest, val) InterlockedExchange((LONG*)(dest), val)
211#define OdInterlockedExchangeAdd(dest, incr) InterlockedExchangeAdd((LONG*)(dest), incr)
212#define OdInterlockedCompareExchange(dest, x, compare) InterlockedCompareExchange((LONG*)(dest), x, compare)
213#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(ANDROID) && !defined(_APPLE_)
215inline int OdInterlockedExchange(
volatile int* dest,
int val)
218 __asm__ __volatile__ (
219 "lock; xchgl %0, (%1)"
221 :
"r" (dest),
"0" (val)
225inline int OdInterlockedCompareExchange(
int volatile *dest,
int xchg,
int compare)
228 __asm__ __volatile__(
"lock; cmpxchgl %2,(%1)"
229 :
"=a" (ret) :
"r" (dest),
"r" (xchg),
"0" (compare) :
"memory");
232#elif defined(__GNUC__) && defined(__POWERPC__)
234inline int OdInterlockedExchange(
volatile int* dest,
int val)
239 __asm__ __volatile__ (
240 "0: lwarx %0, 0, %1\n"
241 " stwcx. %2, 0, %1\n"
245 :
"r"(dest),
"r"(val)
246 :
"cr0",
"memory",
"r0");
249inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
255 __asm__ __volatile__ (
256 "0: lwarx %0, %3, %1\n"
258 " stwcx. %0, %3, %1\n"
262 :
"r"(dest),
"r"(incr),
"r"(zero)
263 :
"cr0",
"memory",
"r0");
267inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
271 __asm__ __volatile__(
279 :
"=&r"(ret),
"=&r"(scratch)
280 :
"r"(dest),
"r"(xchg),
"r"(compare)
281 :
"cr0",
"memory",
"r0");
285#elif defined(__APPLE__)
287#include <libkern/OSAtomic.h>
288inline int OdInterlockedExchange(
volatile int* dest,
int val)
290 int oldValue = *dest;
291 while (!OSAtomicCompareAndSwapIntBarrier(oldValue, val, dest))
295inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
297 return OSAtomicAdd32Barrier(incr, dest) - incr;
299inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
302 return OSAtomicCompareAndSwapIntBarrier(compare, xchg, dest) ? compare : xchg;
304#elif defined(ANDROID) && !defined(ANDROID_GOOGLE)
339#elif defined(ANDROID)
340inline int OdInterlockedExchange(
volatile int* dest,
int val)
342 return __sync_lock_test_and_set(dest, val);
344inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
347 return __sync_fetch_and_add(dest, incr);
349inline int OdInterlockedIncrement(
volatile int* dest)
351 return __sync_fetch_and_add(dest, 1) + 1;
353inline int OdInterlockedDecrement(
volatile int* dest)
355 return __sync_fetch_and_sub(dest, 1) - 1;
382#elif defined(ANDROID)
384inline int OdInterlockedExchange(
volatile int* dest,
int val)
387 __sync_val_compare_and_swap(dest, oldVal, val);
390inline int OdInterlockedExchangeAdd(
volatile int* dest,
int incr)
393 __sync_val_compare_and_swap(dest, oldVal, incr + oldVal);
396inline int OdInterlockedCompareExchange(
volatile int *dest,
int xchg,
int compare)
398 bool bRes = __sync_bool_compare_and_swap(dest, compare, xchg);
399 return bRes ? xchg : compare;
406 typedef int RefCounterType;
407 volatile RefCounterType _ref_count;
408 OdRefCounter& operator = (RefCounterType n) { _ref_count = 0; OdInterlockedExchange(&_ref_count, n);
return *
this; }
409 operator RefCounterType ()
const {
return OdInterlockedExchangeAdd(
const_cast<RefCounterType*
>(&_ref_count), 0); }
410 RefCounterType operator ++ () {
return OdInterlockedIncrement(&_ref_count); }
411 RefCounterType operator -- () {
return OdInterlockedDecrement(&_ref_count); }
423 typedef int VolatileType;
424 volatile VolatileType _val;
425 OdVolatile& operator = (VolatileType n) { _val = 0; OdInterlockedExchange(&_val, n);
return *
this; }
426 operator VolatileType ()
const {
return OdInterlockedExchangeAdd(
const_cast<VolatileType*
>(&_val), 0); }
427 VolatileType operator|=(VolatileType n) {
return OdInterlockedExchange(&_val, _val | n); }
428 VolatileType operator&=(VolatileType n) {
return OdInterlockedExchange(&_val, _val&n); }
435#define TD_SINGLE_THREAD
445#if !(defined(ANDROID))
446#ifndef TD_SINGLE_THREAD
448#pragma managed(push, off)
455 typedef int RefCounterType;
456 volatile RefCounterType _ref_count;
457 OdRefCounter& operator = (RefCounterType n) { _ref_count = 0; OdInterlockedExchange(&_ref_count, n);
return *
this; }
458 operator RefCounterType ()
const {
return OdInterlockedExchangeAdd(
const_cast<RefCounterType*
>(&_ref_count), 0); }
459 RefCounterType operator ++ () {
return OdInterlockedIncrement(&_ref_count); }
460 RefCounterType operator -- () {
return OdInterlockedDecrement(&_ref_count); }
472 typedef int VolatileType;
473 volatile VolatileType _val;
474 OdVolatile& operator = (VolatileType n) { _val = 0; OdInterlockedExchange(&_val, n);
return *
this; }
475 operator VolatileType ()
const {
return OdInterlockedExchangeAdd(
const_cast<VolatileType*
>(&_val), 0); }
476 VolatileType operator|=(VolatileType n) {
return OdInterlockedExchange(&_val, _val|n); }
477 VolatileType operator&=(VolatileType n) {
return OdInterlockedExchange(&_val, _val&n); }
OdMutexAutoLock(OdMutex &mutex)