diff --git a/atomic.lua b/atomic.lua index e7f5ef8..aaeca4a 100644 --- a/atomic.lua +++ b/atomic.lua @@ -20,6 +20,9 @@ function mt:set(value) end function atomic:__new(...) + if C.tds_has_atomic()==0 then + error('atomic counter not available (Torch not found)') + end local self = C.tds_atomic_new() if self == NULL then error('unable to allocate atomic') diff --git a/cdefs.lua b/cdefs.lua index d2b674a..84e1f5c 100644 --- a/cdefs.lua +++ b/cdefs.lua @@ -54,6 +54,7 @@ void tds_vec_retain(tds_vec *vec); void tds_vec_free(tds_vec* vec); /* atomic counter */ +char tds_has_atomic(void); typedef struct tds_atomic_counter_ tds_atomic_counter; tds_atomic_counter* tds_atomic_new(void); long tds_atomic_inc(tds_atomic_counter *atomic); diff --git a/tds_atomic_counter.c b/tds_atomic_counter.c index 9220e7b..147276f 100644 --- a/tds_atomic_counter.c +++ b/tds_atomic_counter.c @@ -1,12 +1,17 @@ #include "tds_atomic_counter.h" #include "tds_utils.h" +char tds_has_atomic(void) { #if HAS_TORCH -#include "THAtomic.h" + return 1; #else -#include -static pthread_mutex_t ptm = PTHREAD_MUTEX_INITIALIZER; + return 0; #endif +} + +#if HAS_TORCH + +#include "THAtomic.h" struct tds_atomic_counter_ { long count; @@ -23,62 +28,26 @@ tds_atomic_counter* tds_atomic_new(void) { } long tds_atomic_inc(tds_atomic_counter *atomic) { -#if HAS_TORCH return THAtomicAddLong(&atomic->count, 1); -#else - pthread_mutex_lock(&ptm); - long lastValue = atomic->count; - atomic->count++; - pthread_mutex_unlock(&ptm); - return lastValue; -#endif } long tds_atomic_get(tds_atomic_counter *atomic) { -#if HAS_TORCH return THAtomicGetLong(&atomic->count); -#else - pthread_mutex_lock(&ptm); - long value = atomic->count; - pthread_mutex_unlock(&ptm); - return value; -#endif } void tds_atomic_set(tds_atomic_counter *atomic, long value) { -#if HAS_TORCH THAtomicSetLong(&atomic->count, value); -#else - pthread_mutex_lock(&ptm); - atomic->count = value; - pthread_mutex_unlock(&ptm); -#endif } void tds_atomic_retain(tds_atomic_counter *atomic) { -#if HAS_TORCH THAtomicIncrementRef(&atomic->refcount); -#else - pthread_mutex_lock(&ptm); - atomic->refcount++; - pthread_mutex_unlock(&ptm); -#endif } void tds_atomic_free(tds_atomic_counter *atomic) { -#if HAS_TORCH if(THAtomicDecrementRef(&atomic->refcount)) { tds_free(atomic); } -#else - pthread_mutex_lock(&ptm); - atomic->refcount--; - if(atomic->refcount == 0) - { - tds_free(atomic); - } - pthread_mutex_unlock(&ptm); -#endif } +#endif \ No newline at end of file diff --git a/tds_atomic_counter.h b/tds_atomic_counter.h index 64b9113..21026d8 100644 --- a/tds_atomic_counter.h +++ b/tds_atomic_counter.h @@ -1,12 +1,16 @@ #ifndef TDS_ATOMIC_H #define TDS_ATOMIC_H -typedef struct tds_atomic_counter_ tds_atomic_counter; +char tds_has_atomic(void); +#if HAS_TORCH +typedef struct tds_atomic_counter_ tds_atomic_counter; tds_atomic_counter* tds_atomic_new(void); long tds_atomic_inc(tds_atomic_counter *atomic); long tds_atomic_get(tds_atomic_counter *atomic); void tds_atomic_set(tds_atomic_counter *atomic, long value); void tds_atomic_retain(tds_atomic_counter *atomic); void tds_atomic_free(tds_atomic_counter *atomic); +#endif + #endif \ No newline at end of file diff --git a/test/test_atomic.lua b/test/test_atomic.lua index 5173187..58c7695 100644 --- a/test/test_atomic.lua +++ b/test/test_atomic.lua @@ -4,9 +4,15 @@ local a = tds.AtomicCounter() print(a) print(a:get()) +assert(a:get()==0) print(a:inc()) +assert(a:get()==1) print(a:inc()) +assert(a:get()==2) print(a) a:set(100) print(a:get()) -print(a) \ No newline at end of file +assert(a:get()==100) +print(a) + +print('OK')