unix_atomic.h (1573B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#ifndef _UNIX_ATOMIC_H_
#define _UNIX_ATOMIC_H_
#include "intrinsics.h"
#define read_barrier() asm volatile ( "" ::: "memory" )
#define write_barrier() asm volatile ( "" ::: "memory" )
#define ATOMIC_DEFS( T ) \
inline T atomic_fetch_add_##T( atomic_##T * atom, T x ) { \
return __sync_fetch_and_add( &atom->v, x ); \
} \
inline T atomic_fetch_sub_##T( atomic_##T * atom, T x ) { \
return __sync_fetch_and_sub( &atom->v, x ); \
} \
inline T atomic_swap_##T( atomic_##T * atom, T x ) { \
return __sync_lock_test_and_set( &atom->v, x ); \
} \
inline T atomic_cas_##T( atomic_##T * atom, T oldval, T newval ) { \
return __sync_bool_compare_and_swap( &atom->v, oldval, newval ); \
} \
inline T atomic_get_##T( atomic_##T * atom ) { \
return atom->v; \
} \
inline void atomic_set_##T( atomic_##T * atom, T x ) { \
atom->v = x; \
}
ATOMIC_DEFS( s8 );
ATOMIC_DEFS( s16 );
ATOMIC_DEFS( s32 );
ATOMIC_DEFS( s64 );
ATOMIC_DEFS( u8 );
ATOMIC_DEFS( u16 );
ATOMIC_DEFS( u32 );
ATOMIC_DEFS( u64 );
#undef ATOMIC_DEFS
template< typename T >
inline bool atomic_swap_pointer( atomic_ptr< T > * atom, T * x ) {
return __sync_lock_test_and_set( &atom->v, x );
}
template< typename T >
inline bool atomic_cas_pointer( atomic_ptr< T > * atom, T * oldval, T * newval ) {
return __sync_lock_compare_and_swap( &atom->v, oldval, newval );
}
template< typename T >
inline T atomic_get_ptr( atomic_ptr< T > * atom ) {
return atom->v;
}
template< typename T >
inline void atomic_set_ptr( atomic_ptr< T > * atom, T * x ) {
atom->v = x;
}
#endif // _UNIX_ATOMIC_H_
|