Zycore 1.4.0.0
Loading...
Searching...
No Matches
Atomic.h
Go to the documentation of this file.
1/***************************************************************************************************
2
3 Zyan Core Library (Zyan-C)
4
5 Original Author : Florian Bernd
6
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24
25***************************************************************************************************/
26
32#ifndef ZYCORE_ATOMIC_H
33#define ZYCORE_ATOMIC_H
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39#include <Zycore/Defines.h>
40#include <Zycore/Types.h>
41
42/* ============================================================================================== */
43/* Enums and Types */
44/* ============================================================================================== */
45
46/*
47 * Wraps a 32-bit value to provide atomic access.
48 */
49typedef struct ZyanAtomic32_
50{
51 ZyanU32 volatile value;
53
54/*
55 * Wraps a 64-bit value to provide atomic access.
56 */
57typedef struct ZyanAtomic64_
58{
59 ZyanU64 volatile value;
61
62/*
63 * Wraps a pointer-sized value to provide atomic access.
64 */
65typedef struct ZyanAtomicPointer_
66{
67 ZyanVoidPointer volatile value;
69
70/* ============================================================================================== */
71/* Macros */
72/* ============================================================================================== */
73
74/* ---------------------------------------------------------------------------------------------- */
75/* Pointer sized */
76/* ---------------------------------------------------------------------------------------------- */
77
81#define ZYAN_ATOMIC_COMPARE_EXCHANGE(destination, comparand, value) \
82 ZyanAtomicCompareExchange((ZyanAtomicPointer*)&(destination), (comparand), (value))
83
87#define ZYAN_ATOMIC_INCREMENT(destination) \
88 ZyanAtomicIncrement((ZyanAtomicPointer*)&(destination));
89
93#define ZYAN_ATOMIC_DECREMENT(destination) \
94 ZyanAtomicDecrement((ZyanAtomicPointer*)&(destination));
95
96/* ---------------------------------------------------------------------------------------------- */
97/* 32-bit */
98/* ---------------------------------------------------------------------------------------------- */
99
103#define ZYAN_ATOMIC_COMPARE_EXCHANGE32(destination, comparand, value) \
104 ZyanAtomicCompareExchange32((ZyanAtomic32*)&(destination), (comparand), (value))
105
109#define ZYAN_ATOMIC_INCREMENT32(destination) \
110 ZyanAtomicIncrement32((ZyanAtomic32*)&(destination));
111
115#define ZYAN_ATOMIC_DECREMENT32(destination) \
116 ZyanAtomicDecrement32((ZyanAtomic32*)&(destination));
117
118/* ---------------------------------------------------------------------------------------------- */
119/* 64-bit */
120/* ---------------------------------------------------------------------------------------------- */
121
125#define ZYAN_ATOMIC_COMPARE_EXCHANGE64(destination, comparand, value) \
126 ZyanAtomicCompareExchange64((ZyanAtomic64*)&(destination), (comparand), (value))
127
131#define ZYAN_ATOMIC_INCREMENT64(destination) \
132 ZyanAtomicIncrement64((ZyanAtomic64*)&(destination));
133
137#define ZYAN_ATOMIC_DECREMENT64(destination) \
138 ZyanAtomicDecrement64((ZyanAtomic64*)&(destination));
139
140/* ---------------------------------------------------------------------------------------------- */
141
142/* ============================================================================================== */
143/* Functions */
144/* ============================================================================================== */
145
146/* ---------------------------------------------------------------------------------------------- */
147/* Pointer sized */
148/* ---------------------------------------------------------------------------------------------- */
149
159static ZyanUPointer ZyanAtomicCompareExchange(ZyanAtomicPointer* destination,
160 ZyanUPointer comparand, ZyanUPointer value);
161
169static ZyanUPointer ZyanAtomicIncrement(ZyanAtomicPointer* destination);
170
178static ZyanUPointer ZyanAtomicDecrement(ZyanAtomicPointer* destination);
179
180/* ---------------------------------------------------------------------------------------------- */
181/* 32-bit */
182/* ---------------------------------------------------------------------------------------------- */
183
187static ZyanU32 ZyanAtomicCompareExchange32(ZyanAtomic32* destination,
188 ZyanU32 comparand, ZyanU32 value);
189
193static ZyanU32 ZyanAtomicIncrement32(ZyanAtomic32* destination);
194
198static ZyanU32 ZyanAtomicDecrement32(ZyanAtomic32* destination);
199
200/* ---------------------------------------------------------------------------------------------- */
201/* 64-bit */
202/* ---------------------------------------------------------------------------------------------- */
203
207static ZyanU64 ZyanAtomicCompareExchange64(ZyanAtomic64* destination,
208 ZyanU64 comparand, ZyanU64 value);
209
213static ZyanU64 ZyanAtomicIncrement64(ZyanAtomic64* destination);
214
218static ZyanU64 ZyanAtomicDecrement64(ZyanAtomic64* destination);
219
220/* ---------------------------------------------------------------------------------------------- */
221
222/* ============================================================================================== */
223
224#if defined(ZYAN_CLANG) || defined(ZYAN_GCC) || defined(ZYAN_ICC)
225# include <Zycore/Internal/AtomicGNU.h>
226#elif defined(ZYAN_MSVC)
227# include <Zycore/Internal/AtomicMSVC.h>
228#else
229# error "Unsupported compiler detected"
230#endif
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* ZYCORE_ATOMIC_H */
void * ZyanVoidPointer
Definition Types.h:166
Definition Atomic.h:50
Definition Atomic.h:58
Definition Atomic.h:66