29 #ifndef SAFE_REFCOUNT_H 30 #define SAFE_REFCOUNT_H 35 #include "platform_config.h" 75 return static_cast<int const volatile &
>( count );
78 void init(
int p_value=1) {
94 #if defined( PLATFORM_REFCOUNT ) 96 #include "platform_refcount.h" 99 #elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) ) 101 #define REFCOUNT_T volatile int 102 #define REFCOUNT_GET_T int const volatile& 104 static inline int atomic_conditional_increment(
volatile int * pw ) {
115 "test %%eax, %%eax\n\t" 120 "cmpxchgl %2, %0\n\t" 123 "=m"( *pw ),
"=&a"( rv ),
"=&r"( tmp ):
131 static inline int atomic_decrement(
volatile int *pw) {
142 "=m" (*pw),
"=qm" (rv):
146 return static_cast<int>(rv);
151 #elif ( defined( __GNUC__ ) ) && ( defined( __powerpc__ ) || defined( __ppc__ ) ) 153 #define REFCOUNT_T int 154 #define REFCOUNT_GET_T int const volatile& 156 inline int atomic_conditional_increment(
int * pw )
166 "lwarx %1, 0, %2\n\t" 171 "stwcx. %1, 0, %2\n\t" 174 "=m"( *pw ),
"=&b"( rv ):
175 "r"( pw ),
"m"( *pw ):
183 inline int atomic_decrement(
int * pw )
193 "lwarx %1, 0, %2\n\t" 194 "addi %1, %1, -1\n\t" 195 "stwcx. %1, 0, %2\n\t" 199 "=m"( *pw ),
"=&b"( rv ):
200 "r"( pw ),
"m"( *pw ):
209 #elif defined( __GNUC__ ) && ( defined( __arm__ ) ) 211 #define REFCOUNT_T int 212 #define REFCOUNT_GET_T int const volatile& 214 inline int atomic_conditional_increment(
volatile int* v)
219 __asm__ __volatile__(
220 "1: ldrex %0, [%2] \n" 225 " strex %1, %0, [%2] \n" 229 :
"=&r" (t),
"=&r" (tmp)
237 inline int atomic_decrement(
volatile int* v)
242 __asm__ __volatile__(
243 "1: ldrex %0, [%2] \n" 244 " add %0, %0, #-1 \n" 245 " strex %1, %0, [%2] \n" 249 :
"=&r" (t),
"=&r" (tmp)
260 #elif ( defined( __MWERKS__ ) ) && defined( __POWERPC__ ) 262 inline long atomic_conditional_increment(
register long * pw )
286 inline long atomic_decrement(
register long * pw )
309 #elif defined( _MSC_VER ) 313 #define REFCOUNT_T long 314 #define REFCOUNT_GET_T long const volatile& 316 long atomic_conditional_increment(
register long * pw );
317 long atomic_decrement(
register long * pw );
320 #elif defined( __GNUC__ ) && defined( ARMV6_ENABLED) 330 #error This platform cannot use safe refcount, compile with NO_THREADS or implement it. 346 return atomic_conditional_increment( &count ) != 0;
351 return atomic_conditional_increment( &count );
356 if( atomic_decrement ( &count ) == 0 ) {
365 return static_cast<REFCOUNT_GET_T
>( count );
368 void init(
int p_value=1) {
377 #endif // no thread safe Definition: safe_refcount.h:336