diff options
author | Willy Tarreau <w@1wt.eu> | 2021-08-26 15:46:51 +0200 |
---|---|---|
committer | Willy Tarreau <w@1wt.eu> | 2021-08-26 16:35:00 +0200 |
commit | 906f7daed16a3b1c4058b77fed44b3c4265e645f (patch) | |
tree | bb8274c756618c338d44f9776577e76984888848 | |
parent | dd56520cdfe1ccb848ae119e8b2dbd6ce62b8a78 (diff) | |
download | haproxy-906f7daed16a3b1c4058b77fed44b3c4265e645f.tar.gz |
MINOR: compiler: implement an ONLY_ONCE() macro
There are regularly places, especially in config analysis, where we
need to report certain things (warnings or errors) only once, but
where implementing a counter is sufficiently deterrent so that it's
not done.
Let's add a simple ONLY_ONCE() macro that implements a static variable
(char) which is atomically turned on, and returns true if it's set for
the first time. This uses fairly compact code, a single byte of BSS
and is thread-safe. There are probably a number of places in the config
parser where this could be used. It may also be used to implement a
WARN_ON() similar to BUG_ON() but which would only warn once.
-rw-r--r-- | include/haproxy/compiler.h | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h index 72d158b37..ca3b844c1 100644 --- a/include/haproxy/compiler.h +++ b/include/haproxy/compiler.h @@ -128,6 +128,14 @@ */ #define DISGUISE(v) ({ typeof(v) __v = (v); ALREADY_CHECKED(__v); __v; }) +/* Implements a static event counter where it's used. This is typically made to + * report some warnings only once, either during boot or at runtime. It only + * returns true on the very first call, and zero later. It's thread-safe and + * uses a single byte of memory per call place. It relies on the atomic xchg + * defined in atomic.h which is also part of the common API. + */ +#define ONLY_ONCE() ({ static char __cnt; !_HA_ATOMIC_XCHG(&__cnt, 1); }) + /* * Gcc >= 3 provides the ability for the program to give hints to the * compiler about what branch of an if is most likely to be taken. This |