| 1 | /* @title: One-time atomic tokens */ |
| 2 | #pragma once |
| 3 | #include <atomic.h> |
| 4 | #include <compiler/core.h> |
| 5 | #include <stdbool.h> |
| 6 | |
| 7 | #define ONCE_TOKEN_DEFINE(type, name) \ |
| 8 | struct { \ |
| 9 | atomic(type) state; \ |
| 10 | } name |
| 11 | |
| 12 | #define ONCE_TOKEN_INIT(val) {.state = ATOMIC_VAR_INIT(val)} |
| 13 | |
| 14 | /* ===== typecheck ===== */ |
| 15 | #define once_token_typecheck_internal(tok) ((void) sizeof((tok)->state)) |
| 16 | |
| 17 | #define once_token_typecheck_internal_type(tok, type) \ |
| 18 | ct_typecheck(type, atomic_load_rlx(&(tok)->state)) |
| 19 | |
| 20 | #define once_token_typecheck_internal_bool(tok) \ |
| 21 | once_token_typecheck_internal_type(tok, bool) |
| 22 | |
| 23 | #define once_token_typecheck_internal_same(tok1, tok2) \ |
| 24 | ct_typecheck_same(atomic_load_rlx(&(tok1)->state), \ |
| 25 | atomic_load_rlx(&(tok2)->state)) |
| 26 | |
| 27 | /* ===== init ===== */ |
| 28 | #define once_token_init_1(tok) \ |
| 29 | ({ \ |
| 30 | __auto_type __ot_tok = (tok); \ |
| 31 | once_token_typecheck_internal_bool(__ot_tok); \ |
| 32 | static_assert(cc_generic((__ot_tok->state), bool : 1, default : 0), \ |
| 33 | "once_token_init_1 requires a boolean token"); \ |
| 34 | atomic_init(&__ot_tok->state, false); \ |
| 35 | }) |
| 36 | |
| 37 | #define once_token_init_2(tok, val) \ |
| 38 | ({ \ |
| 39 | __auto_type __ot_tok = (tok); \ |
| 40 | once_token_typecheck_internal(__ot_tok); \ |
| 41 | typeof(atomic_load_rlx(&__ot_tok->state)) __ot_val = (val); \ |
| 42 | atomic_init(&__ot_tok->state, __ot_val); \ |
| 43 | }) |
| 44 | |
| 45 | #define once_token_init(...) PP_CALL(once_token_init, __VA_ARGS__) |
| 46 | |
| 47 | /* ===== claim ===== */ |
| 48 | /* The 2 and 4 argument versions return what was actually there, which |
| 49 | * can be used to inspect whether something unexpected happened */ |
| 50 | #define once_token_claim_4(tok, from, to, out) \ |
| 51 | ({ \ |
| 52 | __auto_type __ot_tok = (tok); \ |
| 53 | once_token_typecheck_internal(__ot_tok); \ |
| 54 | ct_typecheck_same(from, to); \ |
| 55 | typeof(atomic_load_rlx(&__ot_tok->state)) __ot_exp = (from); \ |
| 56 | typeof(__ot_exp) __ot_to = (to); \ |
| 57 | __auto_type __ot_out = (out); \ |
| 58 | static_assert(ct_is_type(typeof(__ot_exp) *, out) || \ |
| 59 | (ct_is_const(out) && (out) == NULL), \ |
| 60 | "once_token_claim out must point to the token state " \ |
| 61 | "type or be NULL"); \ |
| 62 | bool ret = atomic_cas_strong(&__ot_tok->state, &__ot_exp, __ot_to, \ |
| 63 | mo_acq_rel, mo_acquire); \ |
| 64 | if (__ot_out) \ |
| 65 | *((typeof(__ot_exp) *) __ot_out) = __ot_exp; \ |
| 66 | ret; \ |
| 67 | }) |
| 68 | |
| 69 | #define once_token_claim_3(tok, from, to) \ |
| 70 | once_token_claim_4(tok, from, to, NULL) |
| 71 | |
| 72 | #define once_token_claim_2(tok, out) \ |
| 73 | ({ \ |
| 74 | once_token_typecheck_internal_bool(tok); \ |
| 75 | static_assert(cc_generic(((tok)->state), bool : 1, default : 0), \ |
| 76 | "once_token_claim_1 argument requires a boolean token"); \ |
| 77 | once_token_claim_4(tok, false, true, out); \ |
| 78 | }) |
| 79 | |
| 80 | #define once_token_claim_1(tok) once_token_claim_2(tok, NULL) |
| 81 | |
| 82 | #define once_token_claim(...) PP_CALL(once_token_claim, __VA_ARGS__) |
| 83 | |
| 84 | /* ===== claimed ===== */ |
| 85 | #define once_token_claimed_1(tok) \ |
| 86 | ({ \ |
| 87 | __auto_type __ot_tok = (tok); \ |
| 88 | once_token_typecheck_internal_bool(__ot_tok); \ |
| 89 | static_assert(cc_generic((__ot_tok->state), bool : 1, default : 0), \ |
| 90 | "once_token_claimed_1 requires a boolean token"); \ |
| 91 | \ |
| 92 | atomic_load_acq(&__ot_tok->state); \ |
| 93 | }) |
| 94 | |
| 95 | #define once_token_claimed_2(tok, val) \ |
| 96 | ({ \ |
| 97 | __auto_type __ot_tok = (tok); \ |
| 98 | once_token_typecheck_internal(__ot_tok); \ |
| 99 | typeof(atomic_load_rlx(&__ot_tok->state)) __ot_val = (val); \ |
| 100 | \ |
| 101 | atomic_load_acq(&__ot_tok->state) == __ot_val; \ |
| 102 | }) |
| 103 | |
| 104 | #define once_token_claimed(...) PP_CALL(once_token_claimed, __VA_ARGS__) |
| 105 | |
| 106 | /* ===== reset ===== */ |
| 107 | #define once_token_reset_1(tok) \ |
| 108 | ({ \ |
| 109 | __auto_type __ot_tok = (tok); \ |
| 110 | once_token_typecheck_internal_bool(__ot_tok); \ |
| 111 | static_assert(cc_generic((__ot_tok->state), bool : 1, default : 0), \ |
| 112 | "once_token_reset_1 requires a boolean token"); \ |
| 113 | atomic_store_release(&__ot_tok->state, false); \ |
| 114 | }) |
| 115 | |
| 116 | #define once_token_reset_2(tok, val) \ |
| 117 | ({ \ |
| 118 | __auto_type __ot_tok = (tok); \ |
| 119 | once_token_typecheck_internal(__ot_tok); \ |
| 120 | typeof(atomic_load_rlx(&__ot_tok->state)) __ot_val = (val); \ |
| 121 | atomic_store_release(&__ot_tok->state, __ot_val); \ |
| 122 | }) |
| 123 | |
| 124 | #define once_token_reset(...) PP_CALL(once_token_reset, __VA_ARGS__) |
| 125 | |
| 126 | #define once_token_read(tok) \ |
| 127 | ({ \ |
| 128 | __auto_type __ot_tok = (tok); \ |
| 129 | once_token_typecheck_internal(__ot_tok); \ |
| 130 | atomic_load_acq(&__ot_tok->state); \ |
| 131 | }) |
| 132 | |