45 #if defined(__clang__)
46 # define PTEX_COMPILER_CLANG
47 #elif defined(__GNUC__)
48 # define PTEX_COMPILER_GCC
50 # define PTEX_COMPILER_ICC
51 #elif defined(_MSC_VER)
52 # define PTEX_COMPILER_MSVC
56 #if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS) || defined(_MSC_VER)
57 #define PTEX_PLATFORM_WINDOWS
58 #define _CRT_NONSTDC_NO_DEPRECATE 1
59 #define _CRT_SECURE_NO_DEPRECATE 1
73 #if !defined(__FreeBSD__)
81 #include <sys/types.h>
83 #define PTEX_PLATFORM_MACOS
85 #define PTEX_PLATFORM_UNIX
95 #ifdef PTEX_PLATFORM_WINDOWS
97 #define fseeko _fseeki64
98 #define ftello _ftelli64
112 #ifdef PTEX_PLATFORM_WINDOWS
116 Mutex() {
_mutex = CreateMutex(NULL, FALSE, NULL); }
118 void lock() { WaitForSingleObject(
_mutex, INFINITE); }
119 bool trylock() {
return WAIT_TIMEOUT != WaitForSingleObject(
_mutex,0);}
179 #ifdef PTEX_PLATFORM_WINDOWS
180 #define ATOMIC_ALIGNED __declspec(align(8))
181 #define ATOMIC_ADD32(x,y) (InterlockedExchangeAdd((volatile long*)(x),(long)(y)) + (y))
182 #define ATOMIC_ADD64(x,y) (InterlockedExchangeAdd64((volatile long long*)(x),(long long)(y)) + (y))
183 #define ATOMIC_SUB32(x,y) (InterlockedExchangeAdd((volatile long*)(x),-((long)(y))) - (y))
184 #define ATOMIC_SUB64(x,y) (InterlockedExchangeAdd64((volatile long long*)(x),-((long long)(y))) - (y))
185 #define MEM_FENCE() MemoryBarrier()
186 #define BOOL_CMPXCH32(x,y,z) (InterlockedCompareExchange((volatile long*)(x),(long)(z),(long)(y)) == (y))
187 #define BOOL_CMPXCH64(x,y,z) (InterlockedCompareExchange64((volatile long long*)(x),(long long)(z),(long long)(y)) == (y))
189 #define PTEX_INLINE __forceinline
191 #define PTEX_INLINE inline
194 #define ATOMIC_ALIGNED __attribute__((aligned(8)))
195 #define ATOMIC_ADD32(x,y) __sync_add_and_fetch(x,y)
196 #define ATOMIC_ADD64(x,y) __sync_add_and_fetch(x,y)
197 #define ATOMIC_SUB32(x,y) __sync_sub_and_fetch(x,y)
198 #define ATOMIC_SUB64(x,y) __sync_sub_and_fetch(x,y)
199 #define MEM_FENCE() __sync_synchronize()
200 #define BOOL_CMPXCH32(x,y,z) __sync_bool_compare_and_swap((x),(y),(z))
201 #define BOOL_CMPXCH64(x,y,z) __sync_bool_compare_and_swap((x),(y),(z))
204 #define PTEX_INLINE inline __attribute__((always_inline))
206 #define PTEX_INLINE inline
210 template <
typename T>
221 assert(0==
"Can only use 32 or 64 bit atomics");
226 template <
typename T>
232 template <
typename T>
243 assert(0==
"Can only use 32 or 64 bit atomics");
248 template <
typename T>
258 template <
typename T,
size_t n>
261 template <
typename T>
270 template <
typename T>
279 template <
typename T>
285 template <
typename T>
298 #ifndef CACHE_LINE_SIZE
299 #define CACHE_LINE_SIZE 64
302 #define CACHE_LINE_PAD(var,type) char var##_pad[CACHE_LINE_SIZE - sizeof(type)]
303 #define CACHE_LINE_PAD_INIT(var) memset(&var##_pad[0], 0, sizeof(var##_pad))
307 #endif // PtexPlatform_h
PTEX_INLINE bool operator()(T volatile *target, T oldvalue, T newvalue)
pthread_spinlock_t _spinlock
Portable fixed-width integer types.
#define PTEX_NAMESPACE_END
PTEX_INLINE bool operator()(T volatile *target, T oldvalue, T newvalue)