Skip to content

Commit

Permalink
only implement if TORCH present
Browse files Browse the repository at this point in the history
  • Loading branch information
lake4790k committed May 14, 2016
1 parent e5a33ad commit e4f9481
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 42 deletions.
3 changes: 3 additions & 0 deletions atomic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions cdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 9 additions & 40 deletions tds_atomic_counter.c
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>
static pthread_mutex_t ptm = PTHREAD_MUTEX_INITIALIZER;
return 0;
#endif
}

#if HAS_TORCH

#include "THAtomic.h"

struct tds_atomic_counter_ {
long count;
Expand All @@ -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
6 changes: 5 additions & 1 deletion tds_atomic_counter.h
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion test/test_atomic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
assert(a:get()==100)
print(a)

print('OK')

0 comments on commit e4f9481

Please sign in to comment.