-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtds_atomic_counter.c
53 lines (42 loc) · 985 Bytes
/
tds_atomic_counter.c
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
#include "tds_atomic_counter.h"
#include "tds_utils.h"
char tds_has_atomic(void) {
#if HAS_TORCH
return 1;
#else
return 0;
#endif
}
#if HAS_TORCH
#include "THAtomic.h"
struct tds_atomic_counter_ {
long count;
int refcount;
};
tds_atomic_counter* tds_atomic_new(void) {
tds_atomic_counter *atomic = tds_malloc(sizeof(tds_atomic_counter));
if (!atomic)
return NULL;
atomic->count = 0;
atomic->refcount = 1;
return atomic;
}
long tds_atomic_inc(tds_atomic_counter *atomic) {
return THAtomicAddLong(&atomic->count, 1);
}
long tds_atomic_get(tds_atomic_counter *atomic) {
return THAtomicGetLong(&atomic->count);
}
void tds_atomic_set(tds_atomic_counter *atomic, long value) {
THAtomicSetLong(&atomic->count, value);
}
void tds_atomic_retain(tds_atomic_counter *atomic) {
THAtomicIncrementRef(&atomic->refcount);
}
void tds_atomic_free(tds_atomic_counter *atomic) {
if(THAtomicDecrementRef(&atomic->refcount))
{
tds_free(atomic);
}
}
#endif