00001 #ifndef __ATOMIC_WIN32_H__ 00002 #define __ATOMIC_WIN32_H__ 00003 00004 #include <Windows.h> 00005 00006 #include "../compat/inttypes.h" 00007 00008 struct atomic_t { 00009 atomic_t(LONG l = 0) : value(l) {} 00010 00011 volatile LONG value; 00012 }; 00013 00021 static inline void 00022 atomic_add(volatile atomic_t *v, int32_t i) 00023 { 00024 InterlockedExchangeAdd(&v->value, i); 00025 } 00026 00034 // static inline void 00035 // atomic_sub(volatile atomic_t* v, int32_t i) 00036 // { 00037 // __asm__ __volatile__( 00038 // LOCK "subl %1,%0" 00039 // :"=m" (v->value) 00040 // :"ir" (i), "m" (v->value)); 00041 // } 00042 00048 static inline void 00049 atomic_incr(volatile atomic_t* v) 00050 { 00051 InterlockedIncrement(&v->value); 00052 } 00053 00060 static inline void 00061 atomic_decr(volatile atomic_t* v) 00062 { 00063 InterlockedDecrement(&v->value); 00064 } 00065 00075 static inline bool 00076 atomic_decr_test(volatile atomic_t* v) 00077 { 00078 return 0 == InterlockedDecrement(&v->value); 00079 } 00080 00091 static inline int32_t 00092 atomic_cmpxchg32(volatile atomic_t* v, int32_t o, int32_t n) 00093 { 00094 return InterlockedCompareExchange(&v->value, 00095 static_cast<LONG>(n), 00096 static_cast<LONG>(o)); 00097 } 00098 00099 // /** 00100 // * Atomic increment function that returns the new value. Note that the 00101 // * implementation loops until it can successfully do the operation and 00102 // * store the value, so there is an extremely low chance that this will 00103 // * never return. 00104 // * 00105 // * @param v pointer to current value 00106 // */ 00107 // static inline int32_t 00108 // atomic_incr_ret(volatile atomic_t* v) 00109 // { 00110 // register volatile int32_t o, n; 00111 00112 // #if defined(NDEBUG) && NDEBUG == 1 00113 // while (1) 00114 // #else 00115 // register int j; 00116 // for (j = 0; j < 1000000; ++j) 00117 // #endif 00118 // { 00119 // o = v->value; 00120 // n = o + 1; 00121 // if (atomic_cmpxchg32(v, o, n) == o) 00122 // return n; 00123 // } 00124 00125 // NOTREACHED; 00126 // return 0; 00127 // } 00128 00129 // /** 00130 // * Atomic addition function that returns the new value. Note that the 00131 // * implementation loops until it can successfully do the operation and 00132 // * store the value, so there is an extremely low chance that this will 00133 // * never return. 00134 // * 00135 // * @param v pointer to current value 00136 // * @param i integer to add 00137 // */ 00138 // static inline int32_t 00139 // atomic_add_ret(volatile atomic_t* v, int32_t i) 00140 // { 00141 // register int32_t o, n; 00142 00143 // #if defined(NDEBUG) && NDEBUG == 1 00144 // while (1) 00145 // #else 00146 // register int j; 00147 // for (j = 0; j < 1000000; ++j) 00148 // #endif 00149 // { 00150 // o = v->value; 00151 // n = o + i; 00152 // if (atomic_cmpxchg32(v, o, n) == o) 00153 // return n; 00154 // } 00155 00156 // NOTREACHED; 00157 // return 0; 00158 // } 00159 00160 00161 #endif /* __ATOMIC_WIN32_H__ */