summaryrefslogtreecommitdiffstats
path: root/src/contrib/atomic.h
blob: b8c34dd18e6dc581136d36a86ef0e22f05257a79 (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
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
/*  Copyright (C) 2024 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program 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/>.
 */

/*!
 * \brief C11 atomic operations with fallbacks.
 */

#pragma once

#ifdef HAVE_C11_ATOMIC           /* C11 */
 #include <stdatomic.h>

 #define ATOMIC_INIT(dst, val) atomic_store_explicit(&(dst), (val), memory_order_relaxed)
 #define ATOMIC_DEINIT(dst)
 #define ATOMIC_SET(dst, val)  atomic_store_explicit(&(dst), (val), memory_order_relaxed)
 #define ATOMIC_GET(src)       atomic_load_explicit(&(src), memory_order_relaxed)
 #define ATOMIC_ADD(dst, val)  (void)atomic_fetch_add_explicit(&(dst), (val), memory_order_relaxed)
 #define ATOMIC_SUB(dst, val)  (void)atomic_fetch_sub_explicit(&(dst), (val), memory_order_relaxed)
 #define ATOMIC_XCHG(dst, val) atomic_exchange_explicit(&(dst), (val), memory_order_relaxed)

 typedef atomic_uint_fast16_t knot_atomic_uint16_t;
 typedef atomic_uint_fast64_t knot_atomic_uint64_t;
 typedef atomic_size_t knot_atomic_size_t;
 typedef _Atomic (void *) knot_atomic_ptr_t;
 typedef atomic_bool knot_atomic_bool;
#elif defined(HAVE_GCC_ATOMIC)   /* GCC __atomic */
 #include <stdint.h>
 #include <stdbool.h>
 #include <stddef.h>

 #define ATOMIC_INIT(dst, val) __atomic_store_n(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_DEINIT(dst)
 #define ATOMIC_SET(dst, val)  __atomic_store_n(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_GET(src)       __atomic_load_n(&(src), __ATOMIC_RELAXED)
 #define ATOMIC_ADD(dst, val)  __atomic_add_fetch(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_SUB(dst, val)  __atomic_sub_fetch(&(dst), (val), __ATOMIC_RELAXED)
 #define ATOMIC_XCHG(dst, val) __atomic_exchange_n(&(dst), (val), __ATOMIC_RELAXED)

 typedef uint16_t knot_atomic_uint16_t;
 typedef uint64_t knot_atomic_uint64_t;
 typedef size_t knot_atomic_size_t;
 typedef void* knot_atomic_ptr_t;
 typedef bool knot_atomic_bool;
#else                            /* Fallback using spinlocks. Much slower. */
 #include <stdint.h>
 #include <stdbool.h>
 #include <stddef.h>

 #include "contrib/spinlock.h"

 #define ATOMIC_SET(dst, val) ({ \
	knot_spin_lock((knot_spin_t *)&(dst).lock); \
	(dst).value.vol = (val); \
	knot_spin_unlock((knot_spin_t *)&(dst).lock); \
 })

 #define ATOMIC_INIT(dst, val) ({ \
	knot_spin_init((knot_spin_t *)&(dst).lock); \
	ATOMIC_SET(dst, val); \
 })

 #define ATOMIC_DEINIT(dst) ({ \
	knot_spin_destroy((knot_spin_t *)&(dst).lock); \
 })

 #define ATOMIC_GET(src) ({ \
	knot_spin_lock((knot_spin_t *)&(src).lock); \
	typeof((src).value.non_vol) _z = (typeof((src).value.non_vol))(src).value.vol; \
	knot_spin_unlock((knot_spin_t *)&(src).lock); \
	_z; \
 })

 #define ATOMIC_ADD(dst, val) ({ \
	knot_spin_lock((knot_spin_t *)&(dst).lock); \
	(dst).value.vol += (val); \
	knot_spin_unlock((knot_spin_t *)&(dst).lock); \
 })

 #define ATOMIC_SUB(dst, val) ({ \
	knot_spin_lock((knot_spin_t *)&(dst).lock); \
	(dst).value.vol -= (val); \
	knot_spin_unlock((knot_spin_t *)&(dst).lock); \
 })

 #define ATOMIC_XCHG(dst, val) ({ \
	knot_spin_lock((knot_spin_t *)&(dst).lock); \
	typeof((dst).value.non_vol) _z = (typeof((dst).value.non_vol))(dst).value.vol; \
	(dst).value.vol = (val); \
	knot_spin_unlock((knot_spin_t *)&(dst).lock); \
	_z; \
 })

 #define ATOMIC_T(x) struct { \
	knot_spin_t lock; \
	union { \
		volatile x vol; \
		x non_vol; \
	} value; \
 }

 typedef ATOMIC_T(uint16_t) knot_atomic_uint16_t;
 typedef ATOMIC_T(uint64_t) knot_atomic_uint64_t;
 typedef ATOMIC_T(size_t) knot_atomic_size_t;
 typedef ATOMIC_T(void*) knot_atomic_ptr_t;
 typedef ATOMIC_T(bool) knot_atomic_bool;
#endif