forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libatomic_gcc.h
106 lines (90 loc) · 2.64 KB
/
libatomic_gcc.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/******************************************************************************
* Copyright (C) 2014-2015
* file: libatomic_gcc.h
* author: gozfree <[email protected]>
* created: 2016-04-24 17:57
* updated: 2016-04-24 17:57
******************************************************************************/
#ifndef LIBATOMIC_GCC_H
#define LIBATOMIC_GCC_H
#include <stdint.h>
#include "libatomic.h"
#ifdef __cplusplus
extern "C" {
#endif
#define GCC_VERSION (__GNUC__*100 + __GNUC_MINOR__*10 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION > 463
#define HAVE_ATOMIC_COMPARE_EXCHANGE 1
#else
#define HAVE_ATOMIC_COMPARE_EXCHANGE 0
#endif
#define HAVE_SYNC_VAL_COMPARE_AND_SWAP 1
#define atomic_int_get atomic_int_get_gcc
static inline int atomic_int_get_gcc(volatile int *ptr)
{
#if HAVE_ATOMIC_COMPARE_EXCHANGE
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST);
#else
__sync_synchronize();
return *ptr;
#endif
}
#define atomic_int_set atomic_int_set_gcc
static inline void atomic_int_set_gcc(volatile int *ptr, int val)
{
#if HAVE_ATOMIC_COMPARE_EXCHANGE
__atomic_store_n(ptr, val, __ATOMIC_SEQ_CST);
#else
*ptr = val;
__sync_synchronize();
#endif
}
#define atomic_int_add_and_fetch atomic_int_add_and_fetch_gcc
static inline int atomic_int_add_and_fetch_gcc(volatile int *ptr, int inc)
{
#if HAVE_ATOMIC_COMPARE_EXCHANGE
return __atomic_add_fetch(ptr, inc, __ATOMIC_SEQ_CST);
#else
return __sync_add_and_fetch(ptr, inc);
#endif
}
#define atomic_int_sub_and_fetch atomic_int_sub_and_fetch_gcc
static inline int atomic_int_sub_and_fetch_gcc(volatile int *ptr, int inc)
{
#if HAVE_ATOMIC_COMPARE_EXCHANGE
return __atomic_sub_fetch(ptr, inc, __ATOMIC_SEQ_CST);
#else
return __sync_sub_and_fetch(ptr, inc);
#endif
}
#define atomic_int_inc atomic_int_inc_gcc
static inline int atomic_int_inc_gcc(volatile int *ptr)
{
return atomic_int_add_and_fetch(ptr, 1);
}
#define atomic_int_dec atomic_int_dec_gcc
static inline int atomic_int_dec_gcc(volatile int *ptr)
{
return atomic_int_add_and_fetch(ptr, -1);
}
#define atomic_ptr_cas atomic_ptr_cas_gcc
static inline void *atomic_ptr_cas_gcc(void * volatile *ptr,
void *oldval, void *newval)
{
#if HAVE_SYNC_VAL_COMPARE_AND_SWAP
#ifdef __ARMCC_VERSION
// armcc will throw an error if ptr is not an integer type
volatile uintptr_t *tmp = (volatile uintptr_t*)ptr;
return (void*)__sync_val_compare_and_swap(tmp, oldval, newval);
#else
return __sync_val_compare_and_swap(ptr, oldval, newval);
#endif
#else
__atomic_compare_exchange_n(ptr, &oldval, newval, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
return oldval;
#endif
}
#ifdef __cplusplus
}
#endif
#endif