Skip to content

Commit ea42da3

Browse files
author
Dan Streetman
committed
macro: add ONCE macro that evaluates to 1 one time
Every location that this macro is used, it will be true the first time it's checked, then false each time after that. This can be useful for things such as one-time logging.
1 parent f267c31 commit ea42da3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/fundamental/macro-fundamental.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050
#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
5151
#define UNIQ __COUNTER__
5252

53+
/* Note that this works differently from pthread_once(): this macro does
54+
* not synchronize code execution, i.e. code that is run conditionalized
55+
* on this macro will run concurrently to all other code conditionalized
56+
* the same way, there's no ordering or completion enforced. */
57+
#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
58+
#define __ONCE(o) \
59+
({ \
60+
static bool (o) = false; \
61+
__sync_bool_compare_and_swap(&(o), false, true); \
62+
})
63+
5364
#undef MAX
5465
#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
5566
#define __MAX(aq, a, bq, b) \

0 commit comments

Comments
 (0)