Rizin
unix-like reverse engineering framework and cli tools
atomic.h
Go to the documentation of this file.
1 #ifndef TREE_SITTER_ATOMIC_H_
2 #define TREE_SITTER_ATOMIC_H_
3 
4 #include <stdint.h>
5 
6 #ifdef __TINYC__
7 
8 static inline size_t atomic_load(const volatile size_t *p) {
9  return *p;
10 }
11 
12 static inline uint32_t atomic_inc(volatile uint32_t *p) {
13  *p += 1;
14  return *p;
15 }
16 
17 static inline uint32_t atomic_dec(volatile uint32_t *p) {
18  *p-= 1;
19  return *p;
20 }
21 
22 #elif defined(_WIN32)
23 
24 #include <windows.h>
25 
26 static inline size_t atomic_load(const volatile size_t *p) {
27  return *p;
28 }
29 
30 static inline uint32_t atomic_inc(volatile uint32_t *p) {
31  return InterlockedIncrement((long volatile *)p);
32 }
33 
34 static inline uint32_t atomic_dec(volatile uint32_t *p) {
35  return InterlockedDecrement((long volatile *)p);
36 }
37 
38 #else
39 
40 static inline size_t atomic_load(const volatile size_t *p) {
41 #ifdef __ATOMIC_RELAXED
42  return __atomic_load_n(p, __ATOMIC_RELAXED);
43 #else
44  return __sync_fetch_and_add((volatile size_t *)p, 0);
45 #endif
46 }
47 
48 static inline uint32_t atomic_inc(volatile uint32_t *p) {
49  return __sync_add_and_fetch(p, 1u);
50 }
51 
52 static inline uint32_t atomic_dec(volatile uint32_t *p) {
53  return __sync_sub_and_fetch(p, 1u);
54 }
55 
56 #endif
57 
58 #endif // TREE_SITTER_ATOMIC_H_
InterlockedDecrement
Definition: kernel.h:55
InterlockedIncrement
Definition: kernel.h:56
void * p
Definition: libc.cpp:67
unsigned int uint32_t
Definition: sftypes.h:29
static uint32_t atomic_dec(volatile uint32_t *p)
Definition: atomic.h:52
static size_t atomic_load(const volatile size_t *p)
Definition: atomic.h:40
static uint32_t atomic_inc(volatile uint32_t *p)
Definition: atomic.h:48