-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.h
More file actions
174 lines (120 loc) · 4.1 KB
/
compat.h
File metadata and controls
174 lines (120 loc) · 4.1 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Compatibility header for supported platforms.
This file is part of libsref.
libsref is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#ifdef SREF_USE_C11
#include <stdatomic.h>
#include <threads.h>
#define xatomic_load_rlx(ptr) \
atomic_load_explicit ((ptr), memory_order_relaxed)
#define xatomic_load_acq(ptr) \
atomic_load_explicit ((ptr), memory_order_acquire)
#define xatomic_store_rel(ptr, val) \
atomic_store_explicit ((ptr), (val), memory_order_release)
#define xatomic_mfence_acq() atomic_signal_fence (memory_order_acquire)
#define xatomic_mfence_full() atomic_signal_fence (memory_order_seq_cst)
#define xthread_sleep(mlsec) \
thrd_sleep (&(struct timespec) { .tv_sec = 0, \
.tv_nsec = (mlsec) * 1000000 }, NULL)
typedef mtx_t xmutex_t;
static inline int
xmutex_init (xmutex_t *mtx)
{
return (mtx_init (mtx, mtx_plain) == thrd_success ? 0 : -1);
}
#define xmutex_lock mtx_lock
#define xmutex_unlock mtx_unlock
#define xmutex_destroy mtx_destroy
typedef tss_t xkey_t;
#define xkey_set tss_set
static inline int
xkey_create (xkey_t *key, void (*fini) (void *))
{
return (tss_create (key, fini) == thrd_success ? 0 : -1);
}
#define xkey_delete tss_delete
#define xthread_local thread_local
#elif defined (SREF_USE_PTHREADS) && \
(defined (__GNUC__) || defined (__clang__))
#include <pthread.h>
#include <unistd.h>
#define xatomic_load_rlx(ptr) __atomic_load_n ((ptr), __ATOMIC_RELAXED)
#define xatomic_load_acq(ptr) __atomic_load_n ((ptr), __ATOMIC_ACQUIRE)
#define xatomic_store_rel(ptr, val) \
__atomic_store_n ((ptr), (val), __ATOMIC_RELEASE)
#define xatomic_mfence_acq() __atomic_thread_fence (__ATOMIC_ACQUIRE)
#define xatomic_mfence_full() __atomic_thread_fence (__ATOMIC_SEQ_CST)
#define xthread_sleep(mlsec) usleep ((mlsec) * 1000)
typedef pthread_mutex_t xmutex_t;
#define xmutex_init(mutex) pthread_mutex_init ((mutex), NULL)
#define xmutex_lock pthread_mutex_lock
#define xmutex_unlock pthread_mutex_unlock
#define xmutex_destroy pthread_mutex_destroy
typedef pthread_key_t xkey_t;
#define xkey_create pthread_key_create
#define xkey_set pthread_setspecific
#define xkey_delete pthread_key_delete
#define xthread_local __thread
#elif defined (_MSC_VER)
#include <windows.h>
#include <intrin.h>
#include <synchapi.h>
static inline uintptr_t
xatomic_load_rlx (uintptr_t *ptr)
{
return (*(volatile uintptr_t *)ptr);
}
static inline uintptr_t
xatomic_load_acq (uintptr_t *ptr)
{
_ReadWriteBarrier ();
MemoryBarrier ();
return (*ptr);
}
static inline void
xatomic_store_rel (uintptr_t *ptr, uintptr_t val)
{
*ptr = val;
_WriteBarrier ();
MemoryBarrier ();
}
#define xatomic_mfence_acq() \
do \
{ \
_ReadWriteBarrier (); \
MemoryBarrier (); \
} \
while (0)
#define xatomic_mfence_full xatomic_mfence_acq
#define xthread_sleep Sleep
typedef SRWLOCK xmutex_t;
#define xmutex_init(mtx) (InitializeSRWLock (mtx), 0)
#define xmutex_lock AcquireSRWLockExclusive
#define xmutex_unlock ReleaseSRWLockExclusive
#define xmutex_destroy(mtx) ((void)(mtx))
typedef int xkey_t;
extern int __tlregdtor (void (*fn) (void));
static inline void
xkey_set (xkey_t *key, void (*fn) (void))
{
(void)key;
while (1)
if (__tlregdtor (fn) == 0)
break;
}
#define xkey_create(key, cb) 0
#define xkey_delete(key) ((void)(key))
#define XKEY_ARG(arg)
#define XKEY_LOCAL(x, y) x
#define xthread_local __declspec(thread)
#else
# error "unsupported platform"
#endif