summaryrefslogtreecommitdiff
path: root/include/win/fakestdatomic.h.in
blob: 48cde8321b14525ebd7bb8df34eca7896335ff77 (plain)
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
// This is a non-atomic fake implementation of stdatomic.h for when
// compiler C11 stdatomic support is missing and only single threaded
// operation is required

#ifndef FAKE_STD_ATOMICS_H
#define FAKE_STD_ATOMICS_H

#include <inttypes.h>

#define _Atomic volatile
#define ATOMIC_FLAG_INIT 0

typedef uint8_t atomic_bool;
typedef uint8_t atomic_flag;

#define atomic_compare_and_exchange_strong(x, y, z) return ((*(x) == *(y)) ? ((*(x) = z), true) : ((*(y) = *(x)),false))

#define atomic_fetch_add(x, y) *(x) += (y), (*(x) - (y))  

static inline void atomic_flag_clear(volatile atomic_flag* flag)
{
  *flag = ATOMIC_FLAG_INIT;
}

static inline atomic_bool atomic_flag_test_and_set( volatile atomic_flag* flag )
{
  atomic_bool result = *flag;
  *flag = 1;
  return result;
}

#define atomic_load(x) (*(x))
#define atomic_store(x, y) do { *(x) = (y); } while (0)


#endif // FAKE_STD_ATOMICS_H