18 #include "odversion.h" 21 # define __WINATOMICS__ 25 # define __GCCATOMICS__ 38 Atomic(
const Atomic&);
41 operator T()
const {
return get(); }
47 inline T operator+=(T);
48 inline T operator-=(T);
49 inline T operator++();
50 inline T operator--();
51 inline T operator++(
int);
52 inline T operator--(
int);
54 inline T exchange(T newval);
59 inline bool setIfValueIs(T curval,T newval,T* actualvalptr);
61 inline bool setIfValueIs(T curval,T newval,T* actualvalptr = 0);
75 #elif (defined __WINATOMICS__) 76 volatile T values_[8];
85 const void* getStorage()
const;
104 int* actualvalptr = 0 )
108 const int oldval =InterlockedCompareExchange( (
volatile long*) &val, newval,
110 if ( oldval!=curval )
112 if ( actualvalptr ) *actualvalptr = oldval;
119 const int old = __sync_val_compare_and_swap( &val, curval, newval );
122 if ( actualvalptr ) *actualvalptr = old;
132 #define mAtomicPointerType long long 134 #define mAtomicPointerType T* 147 inline AtomicPointer(T* newptr = 0);
149 inline bool setIfEqual(
const T* curptr, T* newptr);
160 inline T* setToNull();
163 T* exchange(T* newptr);
166 inline operator T*()
const;
167 inline T* operator->();
168 inline const T* operator->()
const;
170 inline T* operator+=(
int);
171 inline T* operator-=(
int);
172 inline T* operator++();
173 inline T* operator--();
174 inline T* operator++(
int);
175 inline T* operator--(
int);
203 SpinLock(
bool recursive =
false);
208 SpinLock(
const SpinLock&);
226 int count()
const {
return count_; }
233 #ifdef __STDATOMICS__ 234 template <
class T>
inline 239 template <
class T>
inline 246 template <
class T>
inline 252 template <
class T>
inline 255 return (
const void*) &val_;
259 template <
class T>
inline 266 template <
class T>
inline 273 template <
class T>
inline 280 template <
class T>
inline 287 template <
class T>
inline 294 template <
class T>
inline 301 template <
class T>
inline 304 T presumedval = curval;
305 if ( !val_.compare_exchange_strong( presumedval, newval ) && actualvalptr )
307 *actualvalptr = presumedval;
315 template <
class T>
inline 318 return val_.exchange( newval );
324 #ifdef __GCCATOMICS__ 325 template <
class T>
inline 330 template <
class T>
inline 336 template <
class T>
inline 341 template <
class T>
inline 348 template <
class T>
inline 356 template <
class T>
inline 359 return __sync_add_and_fetch(&
val_, b);
363 template <
class T>
inline 366 return __sync_sub_and_fetch(&
val_, b);
370 template <
class T>
inline 373 return __sync_add_and_fetch(&
val_, 1);
377 template <
class T>
inline 380 return __sync_sub_and_fetch(&
val_, 1);
384 template <
class T>
inline 387 return __sync_fetch_and_add(&
val_, 1);
391 template <
class T>
inline 394 return __sync_fetch_and_sub(&
val_, 1);
397 template <
class T>
inline 400 const T old = __sync_val_compare_and_swap( &
val_, curval, newval );
403 if ( actualvalptr ) *actualvalptr = old;
418 template <
class T>
inline 421 return __sync_lock_test_and_set( &
val_, newval );
425 template <
class T>
inline 428 return (
const void*) &
val_;
432 #endif //__GCCATOMICS__ 436 #ifdef __WINATOMICS__ 439 template <
class T>
inline 447 template <
class T>
inline 455 template <
class T>
inline 458 return (
const void*) valptr_;
462 template <
class T>
inline 469 template <
class T>
inline 476 template <
class T>
inline 487 template <
class T>
inline 495 template <
class T>
inline 499 return (*valptr_) += b;
503 template <
class T>
inline 507 return (*valptr_) -= b;
511 template <
class T>
inline 519 template <
class T>
inline 527 template <
class T>
inline 535 template <
class T>
inline 544 template <
class T>
inline 548 const bool res = (*valptr_)==curval;
551 else if ( actualvalptr ) *actualvalptr = (*valptr_);
563 valptr_ = &values_[0];
564 while ( ((
long long) valptr_) % 64 )
573 long long* actualvalptr )
575 const long long prevval =
576 InterlockedCompareExchange64(valptr_,newval,curval);
577 if ( prevval==curval )
579 if ( actualvalptr ) *actualvalptr = prevval;
587 return InterlockedAdd64( valptr_, b );
594 return InterlockedAdd64( valptr_, -b );
601 return InterlockedIncrement64( valptr_ );
608 return InterlockedDecrement64( valptr_ );
615 return InterlockedIncrement64( valptr_ )-1;
622 return InterlockedExchange64( valptr_, newval );
629 return InterlockedDecrement64( valptr_ )+1;
639 valptr_ = &values_[0];
640 while ( ((
int) valptr_) % 32 )
651 InterlockedCompareExchange((
volatile long*) valptr_,newval,curval);
652 if ( prevval==curval )
654 if ( actualvalptr ) *actualvalptr = prevval;
663 return InterlockedExchangeAdd( (
volatile long*) valptr_, b ) + b;
670 return InterlockedExchangeAdd( (
volatile long*) valptr_, -b ) -b;
677 return InterlockedIncrement( (
volatile long*) valptr_ );
684 return InterlockedDecrement( (
volatile long*) valptr_ );
691 return InterlockedIncrement( (
volatile long*) valptr_ )-1;
698 return InterlockedExchange( (
volatile long*) valptr_, newval );
705 return InterlockedDecrement( (
volatile long*) valptr_ )+1;
713 valptr_ = &values_[0];
714 while ( ((
long long) valptr_) % 32 )
725 InterlockedCompareExchange( valptr_, newval, curval );
726 if ( prevval==curval )
728 if ( actualvalptr ) *actualvalptr = prevval;
736 return InterlockedExchangeAdd( valptr_, (
long) b ) + b;
743 return InterlockedExchangeAdd( valptr_, -b ) - b;
750 return InterlockedIncrement( valptr_ );
757 return InterlockedDecrement( valptr_ );
764 return InterlockedIncrement( valptr_ )-1;
771 return InterlockedExchange( valptr_, newval );
778 return InterlockedDecrement( valptr_ )+1;
782 #endif //__WINATOMICS__ 785 template <
class T>
inline 791 template <
class T>
inline 799 template <
class T>
inline 806 template <
class T>
inline 817 template <
class T>
inline 825 template <
class T>
inline 832 template <
class T>
inline 836 template <
class T>
inline 840 template <
class T>
inline 844 template <
class T>
inline 846 {
return ptr_ += b*
sizeof(T); }
849 template <
class T>
inline 851 {
return ptr_ -= b*
sizeof(T); }
855 #define mImplAtomicPointerOperator( func, op, ret ) \ 856 template <class T> inline \ 857 T* AtomicPointer<T>::func \ 859 mAtomicPointerType old = (mAtomicPointerType) ptr_.get(); \ 861 while ( !ptr_.setIfValueIs( old, (mAtomicPointerType) (op), &old ) ) \ #define mExpClass(module)
Definition: commondefs.h:160
Atomic(T val=0)
Definition: atomic.h:326
T operator=(T v)
Definition: atomic.h:349
bool setIfValueIs(T curval, T newval, T *actualvalptr)
Definition: atomic.h:398
SpinLock & operator=(const SpinLock &b)
Definition: atomic.h:211
~Atomic()
Definition: atomic.h:337
bool setIfEqual(const T *curptr, T *newptr)
Definition: atomic.h:792
AtomicPointer(const AtomicPointer< T > &)
Definition: atomic.h:178
T * setToNull()
Definition: atomic.h:807
T * exchange(T *newptr)
Definition: atomic.h:800
#define mAtomicPointerType
Definition: atomic.h:134
AtomicPointer< const void > lockingthread_
Definition: atomic.h:219
const void * getStorage() const
Only for debugging.
Definition: atomic.h:426
T operator++()
Definition: atomic.h:371
T operator=(const Atomic< T > &v)
Definition: atomic.h:45
AtomicPointer(T *newptr=0)
Definition: atomic.h:786
T operator--()
Definition: atomic.h:378
interface to threads that should be portable.
Definition: atomic.h:28
T operator+=(T)
Definition: atomic.h:357
int count() const
Definition: atomic.h:226
T operator-=(T)
Definition: atomic.h:364
T * operator+=(int)
Definition: atomic.h:845
Is an alternative to Mutex. It is a lock which causes a thread trying to acquire it to simply wait in...
Definition: atomic.h:200
T * operator-=(int)
Definition: atomic.h:850
bool recursive_
Definition: atomic.h:223
bool atomicSetIfValueIs(volatile int &val, int curval, int newval, int *actualvalptr)
Definition: atomic.h:99
T get() const
Definition: atomic.h:342
T exchange(T newval)
Definition: atomic.h:419
Atomic instantiated with a pointer. The class really only handles the casting from a void* to a T*...
Definition: atomic.h:144
Is a lock that allows a thread to have exlusive rights to something.
Definition: thread.h:43
int count_
Definition: atomic.h:222
#define mImplAtomicPointerOperator(func, op, ret)
Definition: atomic.h:855
Atomic< T * > ptr_
Definition: atomic.h:181
#define mClass(module)
Definition: commondefs.h:164
const void * getStorage() const
Only for debugging.
Definition: atomic.h:184
T * operator->()
Definition: atomic.h:837
volatile T val_
Definition: atomic.h:82
AtomicPointer< T > & operator=(T *ptr)
Definition: atomic.h:818