]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: add ONCE macro that evaluates to 1 one time
authorDan Streetman <ddstreet@canonical.com>
Wed, 19 May 2021 14:01:59 +0000 (10:01 -0400)
committerDan Streetman <ddstreet@canonical.com>
Thu, 20 May 2021 19:39:15 +0000 (15:39 -0400)
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.

src/fundamental/macro-fundamental.h

index 6ff8372f3cdd856e123b21935cb934f317d20d4e..967518600d404db52ad232d87789302899f2ff21 100644 (file)
 #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
 #define UNIQ __COUNTER__
 
+/* Note that this works differently from pthread_once(): this macro does
+ * not synchronize code execution, i.e. code that is run conditionalized
+ * on this macro will run concurrently to all other code conditionalized
+ * the same way, there's no ordering or completion enforced. */
+#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
+#define __ONCE(o)                                                       \
+        ({                                                              \
+                static bool (o) = false;                                \
+                __sync_bool_compare_and_swap(&(o), false, true);        \
+        })
+
 #undef MAX
 #define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
 #define __MAX(aq, a, bq, b)                             \