1 /*
2 * Copyright 2019, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Augustin Cavalier <waddlesplash>
7 */
8 #ifndef __NVME_ATOMIC_H__
9 #define __NVME_ATOMIC_H__
10
11 #include <OS.h>
12
13
14 /* 32-bit atomics */
15 typedef int32 nvme_atomic_t;
16
17 #define NVME_ATOMIC_INIT(val) (val)
18
19 static inline void
nvme_atomic_init(nvme_atomic_t * v)20 nvme_atomic_init(nvme_atomic_t* v)
21 {
22 *v = 0;
23 }
24
25 static inline void
nvme_atomic_clear(nvme_atomic_t * v)26 nvme_atomic_clear(nvme_atomic_t* v)
27 {
28 *v = 0;
29 }
30
31
32 static inline int32
nvme_atomic_read(const nvme_atomic_t * v)33 nvme_atomic_read(const nvme_atomic_t* v)
34 {
35 return atomic_get(v);
36 }
37
38
39 static inline void
nvme_atomic_set(nvme_atomic_t * v,int32 new_value)40 nvme_atomic_set(nvme_atomic_t* v, int32 new_value)
41 {
42 atomic_set(v, new_value);
43 }
44
45
46 static inline void
nvme_atomic_add(nvme_atomic_t * v,int32 inc)47 nvme_atomic_add(nvme_atomic_t* v, int32 inc)
48 {
49 atomic_add(v, inc);
50 }
51
52
53 static inline void
nvme_atomic_sub(nvme_atomic_t * v,int32 dec)54 nvme_atomic_sub(nvme_atomic_t* v, int32 dec)
55 {
56 atomic_add(v, -dec);
57 }
58
59
60 static inline void
nvme_atomic_inc(nvme_atomic_t * v)61 nvme_atomic_inc(nvme_atomic_t* v)
62 {
63 nvme_atomic_add(v, 1);
64 }
65
66
67 static inline void
nvme_atomic_dec(nvme_atomic_t * v)68 nvme_atomic_dec(nvme_atomic_t* v)
69 {
70 nvme_atomic_sub(v, 1);
71 }
72
73
74 static inline int32
nvme_atomic_add_return(nvme_atomic_t * v,int32 inc)75 nvme_atomic_add_return(nvme_atomic_t* v, int32 inc)
76 {
77 return atomic_add(v, inc);
78 }
79
80
81 static inline int32
nvme_atomic_sub_return(nvme_atomic_t * v,int32 dec)82 nvme_atomic_sub_return(nvme_atomic_t* v, int32 dec)
83 {
84 return atomic_add(v, -dec);
85 }
86
87
88 static inline int
nvme_atomic_inc_and_test(nvme_atomic_t * v)89 nvme_atomic_inc_and_test(nvme_atomic_t *v)
90 {
91 return nvme_atomic_add_return(v, 1) == 0;
92 }
93
94
95 static inline int
nvme_atomic_dec_and_test(nvme_atomic64_t * v)96 nvme_atomic_dec_and_test(nvme_atomic64_t *v)
97 {
98 return nvme_atomic_sub_return(v, 1) == 0;
99 }
100
101
102 static inline int
nvme_atomic_test_and_set(nvme_atomic_t * v)103 nvme_atomic_test_and_set(nvme_atomic_t* v)
104 {
105 return atomic_test_and_set(v, 1, 0);
106 }
107
108
109 /* 64-bit atomics */
110 typedef int64 nvme_atomic64_t;
111
112 #define NVME_ATOMIC64_INIT(val) (val)
113
114 static inline void
nvme_atomic64_init(nvme_atomic64_t * v)115 nvme_atomic64_init(nvme_atomic64_t *v)
116 {
117 atomic_set64(v, 0);
118 }
119
120 static inline void
nvme_atomic64_clear(nvme_atomic64_t * v)121 nvme_atomic64_clear(nvme_atomic64_t *v)
122 {
123 atomic_set64(v, 0);
124 }
125
126
127 static inline int64
nvme_atomic64_read(nvme_atomic64_t * v)128 nvme_atomic64_read(nvme_atomic64_t *v)
129 {
130 return atomic_get64(v);
131 }
132
133
134 static inline void
nvme_atomic64_set(nvme_atomic64_t * v,int64_t new_value)135 nvme_atomic64_set(nvme_atomic64_t *v, int64_t new_value)
136 {
137 atomic_set64(v, new_value);
138 }
139
140
141 static inline void
nvme_atomic64_add(nvme_atomic64_t * v,int64_t inc)142 nvme_atomic64_add(nvme_atomic64_t *v, int64_t inc)
143 {
144 atomic_add64(v, inc);
145 }
146
147
148 static inline void
nvme_atomic64_sub(nvme_atomic64_t * v,int64_t dec)149 nvme_atomic64_sub(nvme_atomic64_t *v, int64_t dec)
150 {
151 nvme_atomic64_add(v, -dec);
152 }
153
154
155 static inline void
nvme_atomic64_inc(nvme_atomic64_t * v)156 nvme_atomic64_inc(nvme_atomic64_t *v)
157 {
158 nvme_atomic64_add(v, 1);
159 }
160
161
162 static inline void
nvme_atomic64_dec(nvme_atomic64_t * v)163 nvme_atomic64_dec(nvme_atomic64_t *v)
164 {
165 nvme_atomic64_add(v, -1);
166 }
167
168
169 static inline int64
nvme_atomic64_add_return(nvme_atomic64_t * v,int64_t inc)170 nvme_atomic64_add_return(nvme_atomic64_t *v, int64_t inc)
171 {
172 return atomic_add64(v, inc);
173 }
174
175
176 static inline int64_t
nvme_atomic64_sub_return(nvme_atomic64_t * v,int64_t dec)177 nvme_atomic64_sub_return(nvme_atomic64_t *v, int64_t dec)
178 {
179 return nvme_atomic64_add_return(v, -dec);
180 }
181
182
183 static inline int
nvme_atomic64_inc_and_test(nvme_atomic64_t * v)184 nvme_atomic64_inc_and_test(nvme_atomic64_t *v)
185 {
186 return nvme_atomic64_add_return(v, 1) == 0;
187 }
188
189
190 static inline int
nvme_atomic64_dec_and_test(nvme_atomic64_t * v)191 nvme_atomic64_dec_and_test(nvme_atomic64_t *v)
192 {
193 return nvme_atomic64_sub_return(v, 1) == 0;
194 }
195
196
197 static inline int
nvme_atomic64_test_and_set(nvme_atomic64_t * v)198 nvme_atomic64_test_and_set(nvme_atomic64_t *v)
199 {
200 return atomic_test_and_set64(v, 1, 0);
201 }
202
203
204 #endif
205