Warning: That file was not part of the compilation database. It may have many parsing errors.
| 1 | /* @title: One-time atomic counters */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <atomic.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | struct once_counter { |
| 8 | atomic_uint64_t count; |
| 9 | }; |
| 10 | |
| 11 | #define ONCE_COUNTER_INIT {.count = ATOMIC_VAR_INIT(0)} |
| 12 | |
| 13 | static inline void once_counter_init(struct once_counter *c) { |
| 14 | atomic_init(&c->count, 0); |
| 15 | } |
| 16 | |
| 17 | static inline bool once_counter_claim(struct once_counter *c) { |
| 18 | return atomic_fetch_add_acq_rel(&c->count, 1) == 0; |
| 19 | } |
| 20 | |
| 21 | static inline bool once_counter_claim_ticket(struct once_counter *c, |
| 22 | uint64_t *out_ticket) { |
| 23 | uint64_t ticket = atomic_fetch_add_acq_rel(&c->count, 1); |
| 24 | |
| 25 | if (out_ticket) |
| 26 | *out_ticket = ticket; |
| 27 | |
| 28 | return ticket == 0; |
| 29 | } |
| 30 | |
| 31 | static inline uint64_t once_counter_count(const struct once_counter *c) { |
| 32 | return atomic_load_acq(&c->count); |
| 33 | } |
| 34 |
Warning: That file was not part of the compilation database. It may have many parsing errors.