atomic_int_apple.h
Go to the documentation of this file.
00001 #ifndef _ATOMIC_INT_APPLE_H
00002 
00003 #define _ATOMIC_INT_APPLE_H
00004 
00005 #include <libkern/OSAtomic.h>
00006 
00007 
00008 //http://developer.apple.com/library/mac/#documentation/Darwin/Reference/KernelIOKitFramework/OSAtomic_h/index.html
00009 
00010 namespace mt{
00011 
00012 class atomicInt
00013 {
00014     
00015 private:
00016     volatile int _q_value;
00017     
00018 public:
00019   atomicInt()
00020   {
00021     _q_value = 0;
00022   }
00023 
00024   atomicInt( int value )
00025   {
00026   _q_value = value;
00027   }
00028 
00029 // atomic API
00030 
00041   inline int fetchAndAddAcquire( int valueToAdd )
00042   {
00043   //T *originalValue = currentValue;
00044   //currentValue += valueToAdd;
00045   //return originalValue;
00046 
00047     int originalValue;
00048     do {
00049       originalValue = _q_value;
00050     } while (!OSAtomicCompareAndSwap32Barrier(originalValue, originalValue+valueToAdd, &_q_value));
00051     return originalValue;
00052   }
00053 
00057   inline bool ref()
00058   {
00059     return OSAtomicIncrement32Barrier(&_q_value) != 0;
00060   }
00061 
00062 /*
00063 Atomically decrements the value of this QAtomicInt.
00064 Returns true if the new value is non-zero, false otherwise.*/
00065   inline bool deref()
00066   {
00067     return OSAtomicDecrement32Barrier(&_q_value) != 0;
00068   }
00069 
00070 /*
00071 If the current _q_value of this QAtomicInt is the expectedValue,
00072 the test-and-set functions assign the newValue to this QAtomicInt
00073 and return true. If the values are not the same, this function
00074 does nothing and returns false.
00075 */
00076   inline bool testAndSetOrdered(int expectedValue, int newValue)
00077   {
00078     //if (currentValue == expectedValue) {
00079     //   currentValue = newValue;
00080     //   return true;
00081     // }
00082     //return false;
00083     return OSAtomicCompareAndSwap32Barrier(expectedValue, newValue, &_q_value);
00084   }
00085 
00086 // Non-atomic API
00087 
00088   inline bool operator==(int value) const
00089   {
00090     return _q_value == value;
00091   }
00092 
00093   inline bool operator!=(int value) const
00094   {
00095     return _q_value != value;
00096   }
00097 
00098   inline bool operator!() const
00099   {
00100     return _q_value == 0;
00101   }
00102 
00103   inline operator int() const
00104   {
00105     return _q_value;
00106   }
00107 
00108   inline atomicInt &operator=(int value)
00109   {
00110     _q_value = value;
00111     return *this;
00112   }
00113 
00114   inline bool operator>(int value) const
00115   {
00116     return _q_value > value;
00117   }
00118 
00119   inline bool operator<(int value) const
00120   {
00121     return _q_value < value;
00122   }
00123 
00124 
00125 };
00126 
00127 }//namespace
00128 
00129 #endif
00130 


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:29:00