25 #ifndef OPIC_COMMON_OP_ATOMIC_H 26 #define OPIC_COMMON_OP_ATOMIC_H 1 30 #include <stdatomic.h> 31 #include "op_macros.h" 35 typedef _Atomic int8_t a_int8_t;
36 typedef _Atomic int16_t a_int16_t;
37 typedef _Atomic int32_t a_int32_t;
38 typedef _Atomic int64_t a_int64_t;
39 typedef _Atomic uint8_t a_uint8_t;
40 typedef _Atomic uint16_t a_uint16_t;
41 typedef _Atomic uint32_t a_uint32_t;
42 typedef _Atomic uint64_t a_uint64_t;
44 #define atomic_check_in(PUNCH_CARD) \ 45 _Generic((PUNCH_CARD), \ 46 a_int8_t*: atomic_check_in_8, \ 47 a_int16_t*: atomic_check_in_16, \ 48 a_int32_t*: atomic_check_in_32, \ 49 a_int64_t*: atomic_check_in_64) \ 52 #define atomic_check_out(PUNCH_CARD) \ 53 _Generic((PUNCH_CARD), \ 54 a_int8_t*: atomic_check_out_8, \ 55 a_int16_t*: atomic_check_out_16, \ 56 a_int32_t*: atomic_check_out_32, \ 57 a_int64_t*: atomic_check_out_64) \ 60 #define atomic_book_critical(PUNCH_CARD) \ 61 _Generic((PUNCH_CARD), \ 62 a_int8_t*: atomic_book_critical_8, \ 63 a_int16_t*: atomic_book_critical_16, \ 64 a_int32_t*: atomic_book_critical_32, \ 65 a_int64_t*: atomic_book_critical_64) \ 68 #define atomic_check_in_book(PUNCH_CARD) \ 69 _Generic((PUNCH_CARD), \ 70 a_int8_t*: atomic_check_in_book_8, \ 71 a_int16_t*: atomic_check_in_book_16, \ 72 a_int32_t*: atomic_check_in_book_32, \ 73 a_int64_t*: atomic_check_in_book_64) \ 76 #define atomic_is_booked(PUNCH_CARD) \ 77 _Generic((PUNCH_CARD), \ 78 a_int8_t*: atomic_is_booked_8, \ 79 a_int16_t*: atomic_is_booked_16, \ 80 a_int32_t*: atomic_is_booked_32, \ 81 a_int64_t*: atomic_is_booked_64) \ 84 #define atomic_enter_critical(PUNCH_CARD) \ 85 _Generic((PUNCH_CARD), \ 86 a_int8_t*: atomic_enter_critical_8, \ 87 a_int16_t*: atomic_enter_critical_16, \ 88 a_int32_t*: atomic_enter_critical_32, \ 89 a_int64_t*: atomic_enter_critical_64) \ 92 #define atomic_exit_critical(PUNCH_CARD) \ 93 _Generic((PUNCH_CARD), \ 94 a_int8_t*: atomic_exit_critical_8, \ 95 a_int16_t*: atomic_exit_critical_16, \ 96 a_int32_t*: atomic_exit_critical_32, \ 97 a_int64_t*: atomic_exit_critical_64) \ 100 #define atomic_exit_check_out(PUNCH_CARD) \ 101 _Generic((PUNCH_CARD), \ 102 a_int8_t*: atomic_exit_check_out_8, \ 103 a_int16_t*: atomic_exit_check_out_16, \ 104 a_int32_t*: atomic_exit_check_out_32, \ 105 a_int64_t*: atomic_exit_check_out_64) \ 109 atomic_check_in_8(a_int8_t* punch_card)
111 int8_t val = atomic_load_explicit(punch_card,
112 memory_order_relaxed);
118 while (!atomic_compare_exchange_weak_explicit
119 (punch_card, &val, val + 1,
120 memory_order_acquire,
121 memory_order_relaxed));
126 atomic_check_in_16(a_int16_t* punch_card)
128 int16_t val = atomic_load_explicit(punch_card,
129 memory_order_relaxed);
135 while (!atomic_compare_exchange_weak_explicit
136 (punch_card, &val, val + 1,
137 memory_order_acquire,
138 memory_order_relaxed));
143 atomic_check_in_32(a_int32_t* punch_card)
145 int32_t val = atomic_load_explicit(punch_card,
146 memory_order_relaxed);
152 while (!atomic_compare_exchange_weak_explicit
153 (punch_card, &val, val + 1,
154 memory_order_acquire,
155 memory_order_relaxed));
160 atomic_check_in_64(a_int64_t* punch_card)
162 int64_t val = atomic_load_explicit(punch_card,
163 memory_order_relaxed);
169 while (!atomic_compare_exchange_weak_explicit
170 (punch_card, &val, val + 1,
171 memory_order_acquire,
172 memory_order_relaxed));
177 atomic_check_out_8(a_int8_t* punch_card)
179 atomic_fetch_sub_explicit(punch_card,
181 memory_order_release);
185 atomic_check_out_16(a_int16_t* punch_card)
187 atomic_fetch_sub_explicit(punch_card,
189 memory_order_release);
193 atomic_check_out_32(a_int32_t* punch_card)
195 atomic_fetch_sub_explicit(punch_card,
197 memory_order_release);
201 atomic_check_out_64(a_int64_t* punch_card)
203 atomic_fetch_sub_explicit(punch_card,
205 memory_order_release);
209 atomic_book_critical_8(a_int8_t* punch_card)
211 return atomic_fetch_or_explicit
212 (punch_card, 1<<7, memory_order_acq_rel) > 0;
216 atomic_book_critical_16(a_int16_t* punch_card)
218 return atomic_fetch_or_explicit
219 (punch_card, 1<<15, memory_order_acq_rel) > 0;
223 atomic_book_critical_32(a_int32_t* punch_card)
225 return atomic_fetch_or_explicit
226 (punch_card, 1<<31, memory_order_acq_rel) > 0;
230 atomic_book_critical_64(a_int64_t* punch_card)
232 return atomic_fetch_or_explicit
233 (punch_card, 1L<<63, memory_order_acq_rel) > 0;
237 atomic_check_in_book_8(a_int8_t* punch_card)
239 int8_t val = atomic_load_explicit(punch_card,
240 memory_order_relaxed);
246 while (!atomic_compare_exchange_weak_explicit
247 (punch_card, &val, (val + 1) | (1<<7),
248 memory_order_acquire,
249 memory_order_relaxed));
254 atomic_check_in_book_16(a_int16_t* punch_card)
256 int16_t val = atomic_load_explicit(punch_card,
257 memory_order_relaxed);
263 while (!atomic_compare_exchange_weak_explicit
264 (punch_card, &val, (val + 1) | (1<<15),
265 memory_order_acquire,
266 memory_order_relaxed));
271 atomic_check_in_book_32(a_int32_t* punch_card)
273 int32_t val = atomic_load_explicit(punch_card,
274 memory_order_relaxed);
280 while (!atomic_compare_exchange_weak_explicit
281 (punch_card, &val, (val + 1) | (1<<31),
282 memory_order_acquire,
283 memory_order_relaxed));
288 atomic_check_in_book_64(a_int64_t* punch_card)
290 int64_t val = atomic_load_explicit(punch_card,
291 memory_order_relaxed);
297 while (!atomic_compare_exchange_weak_explicit
298 (punch_card, &val, (val + 1) | (1UL<<63),
299 memory_order_acquire,
300 memory_order_relaxed));
305 atomic_is_booked_8(a_int8_t* punch_card)
307 return atomic_load_explicit(punch_card, memory_order_acquire) < 0;
311 atomic_is_booked_16(a_int16_t* punch_card)
313 return atomic_load_explicit(punch_card, memory_order_acquire) < 0;
317 atomic_is_booked_32(a_int32_t* punch_card)
319 return atomic_load_explicit(punch_card, memory_order_acquire) < 0;
323 atomic_is_booked_64(a_int64_t* punch_card)
325 return atomic_load_explicit(punch_card, memory_order_acquire) < 0;
329 atomic_enter_critical_8(a_int8_t* punch_card)
331 while (atomic_load_explicit(punch_card, memory_order_relaxed)
334 atomic_fetch_sub_explicit(punch_card, 1, memory_order_acq_rel);
338 atomic_enter_critical_16(a_int16_t* punch_card)
340 while (atomic_load_explicit(punch_card, memory_order_relaxed)
343 atomic_fetch_sub_explicit(punch_card, 1, memory_order_acq_rel);
347 atomic_enter_critical_32(a_int32_t* punch_card)
349 while (atomic_load_explicit(punch_card, memory_order_relaxed)
352 atomic_fetch_sub_explicit(punch_card, 1, memory_order_acq_rel);
356 atomic_enter_critical_64(a_int64_t* punch_card)
358 while (atomic_load_explicit(punch_card, memory_order_relaxed)
361 atomic_fetch_sub_explicit(punch_card, 1, memory_order_acq_rel);
365 atomic_exit_critical_8(a_int8_t* punch_card)
367 atomic_store_explicit(punch_card, 1, memory_order_release);
371 atomic_exit_critical_16(a_int16_t* punch_card)
373 atomic_store_explicit(punch_card, 1, memory_order_release);
377 atomic_exit_critical_32(a_int32_t* punch_card)
379 atomic_store_explicit(punch_card, 1, memory_order_release);
383 atomic_exit_critical_64(a_int64_t* punch_card)
385 atomic_store_explicit(punch_card, 1, memory_order_release);
389 atomic_exit_check_out_8(a_int8_t* punch_card)
391 atomic_store_explicit(punch_card, 0, memory_order_release);
395 atomic_exit_check_out_16(a_int16_t* punch_card)
397 atomic_store_explicit(punch_card, 0, memory_order_release);
401 atomic_exit_check_out_32(a_int32_t* punch_card)
403 atomic_store_explicit(punch_card, 0, memory_order_release);
407 atomic_exit_check_out_64(a_int64_t* punch_card)
409 atomic_store_explicit(punch_card, 0, memory_order_release);