From de37da4184c55c6811dd02707fdd3b1773a7ce66 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 4 Jul 2025 08:13:48 +0100 Subject: [PATCH] common: Add ONCE macro to run code only once This macro can be used to run code once, especially for debug messages and similar. eg: /* Print this once in the log. */ ONCE (nbdkit_debug ("falling back to less efficient method")); (cherry picked from commit ad8630deab4639e636212f11a5a47d2c34ef2949) --- .gitignore | 1 + common/include/Makefile.am | 6 ++ common/include/once.h | 67 ++++++++++++++++++++ common/include/test-once.c | 126 +++++++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 common/include/once.h create mode 100644 common/include/test-once.c diff --git a/.gitignore b/.gitignore index 3629ef39..827fd53c 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,7 @@ plugins/*/*.3 /common/include/test-iszero /common/include/test-minmax /common/include/test-nextnonzero +/common/include/test-once /common/include/test-random /common/include/test-tvdiff /common/protocol/generate-protostrings.sh diff --git a/common/include/Makefile.am b/common/include/Makefile.am index ca488e68..3a3757e2 100644 --- a/common/include/Makefile.am +++ b/common/include/Makefile.am @@ -49,6 +49,7 @@ EXTRA_DIST = \ iszero.h \ minmax.h \ nextnonzero.h \ + once.h \ random.h \ rounding.h \ static-assert.h \ @@ -71,6 +72,7 @@ TESTS = \ test-iszero \ test-minmax \ test-nextnonzero \ + test-once \ test-random \ test-tvdiff \ $(NULL) @@ -120,6 +122,10 @@ test_nextnonzero_SOURCES = test-nextnonzero.c nextnonzero.h test_nextnonzero_CPPFLAGS = -I$(srcdir) test_nextnonzero_CFLAGS = $(WARNINGS_CFLAGS) +test_once_SOURCES = test-once.c once.h +test_once_CPPFLAGS = -I$(srcdir) +test_once_CFLAGS = $(WARNINGS_CFLAGS) + test_random_SOURCES = test-random.c random.h test_random_CPPFLAGS = -I$(srcdir) test_random_CFLAGS = $(WARNINGS_CFLAGS) diff --git a/common/include/once.h b/common/include/once.h new file mode 100644 index 00000000..bb93e767 --- /dev/null +++ b/common/include/once.h @@ -0,0 +1,67 @@ +/* nbdkit + * Copyright Red Hat + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of Red Hat nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef NBDKIT_ONCE_H +#define NBDKIT_ONCE_H + +#ifdef HAVE_STDATOMIC_H +#include +#else +/* This is best effort on platforms that don't support atomic. + * 32 bit ints are generally fine in reality. + */ +#define _Atomic /**/ +#endif + +#include "unique-name.h" + +/* Run the statement once (per nbdkit run). */ +#define ONCE(stmt) ONCE_1(NBDKIT_UNIQUE_NAME(_once), (stmt)) + +/* The actual implementation: + * + * The comparison with 0 avoids var wrapping around. Mostly var will + * only be 0 or 1, or in rare cases other small integers. + * + * The atomic increment & comparison with 1 is what only allows a + * single thread to run the statement. + * + * To avoid optimisations: Use 'volatile' so reads and writes are not + * removed, and use 'unsigned' to avoid any with signed overflow. + */ +#define ONCE_1(var, stmt) \ + do { \ + static volatile _Atomic unsigned var = 0; \ + if (var == 0 && ++var == 1) { stmt; } \ + } while (0) + +#endif /* NBDKIT_ONCE_H */ diff --git a/common/include/test-once.c b/common/include/test-once.c new file mode 100644 index 00000000..d7dd5c42 --- /dev/null +++ b/common/include/test-once.c @@ -0,0 +1,126 @@ +/* nbdkit + * Copyright Red Hat + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of Red Hat nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#ifndef HAVE_STDATOMIC_H + +/* Skip the test if no */ + +int +main (void) +{ + printf ("SKIP: no on this platform\n"); + exit (77); +} + +#else /* HAVE_STDATOMIC_H */ + +#include +#include +#include + +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ +#include + +#include + +#include "once.h" + +#define NR_THREADS 8 + +static volatile _Atomic unsigned count1 = 0, count2 = 0, + count3 = 0, count4 = 0; +static pthread_barrier_t barrier; + +static void * __attribute__((noreturn)) +start_thread (void *idxp) +{ + //int idx = * (int*) idxp; + + pthread_barrier_wait (&barrier); + + for (;;) { + ONCE (count1++); + ONCE (count2++); + ONCE (count3++); + ONCE (count4++); + } +} + +int +main (void) +{ + int i, err; + pthread_t th[NR_THREADS]; + int idx[NR_THREADS]; + + err = pthread_barrier_init (&barrier, NULL, NR_THREADS); + if (err != 0) { + errno = err; + perror ("pthread_barrier_init"); + exit (EXIT_FAILURE); + } + + for (i = 0; i < NR_THREADS; ++i) { + idx[i] = i; + err = pthread_create (&th[i], NULL, start_thread, &idx[i]); + if (err != 0) { + errno = err; + perror ("pthread_create"); + exit (EXIT_FAILURE); + } + } + + do { + sleep (1); + } while (count1 + count2 + count3 + count4 < 4); + + for (i = 0; i < NR_THREADS; ++i) { + pthread_cancel (th[i]); + } + + pthread_barrier_destroy (&barrier); + + if (count1 != 1 || count2 != 1 || count3 != 1 || count4 != 1) { + fprintf (stderr, "FAIL: counts incremented to %u %u %u %u " + "(expected 1 1 1 1)\n", count1, count2, count3, count4); + exit (EXIT_FAILURE); + } + + exit (EXIT_SUCCESS); +} + +#endif /* HAVE_STDATOMIC_H */ -- 2.47.1