]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Add once_flag, ONCE_FLAG_INIT and call_once to stdlib.h for C23
authorJoseph Myers <josmyers@redhat.com>
Wed, 1 Oct 2025 15:15:15 +0000 (15:15 +0000)
committerJoseph Myers <josmyers@redhat.com>
Wed, 1 Oct 2025 15:15:15 +0000 (15:15 +0000)
C23 adds once_flag, ONCE_FLAG_INIT and call_once to stdlib.h (in C11
they were only in threads.h, in C23 they are in both headers; this
change came from N2840).  Implement this change, with a
bits/types/once_flag.h header for the common type and initializer
definitions.

Note that there's an omnibus bug (bug 33001) that covers more than
just these missing definitions.

This doesn't seem a significant enough feature to be worth mentioning
in NEWS.

ISO C is not concerned with whether functions are in libc or
libpthread, but POSIX links this to what header they are declared in,
so functions declared in stdlib.h are supposed to be in libc.
However, the current edition of POSIX is based on C17; hopefully Hurd
glibc will have completed the merge of libpthread into libc (in
particular, moving call_once) well before a future edition of POSIX
based on C23 (or a later version of ISO C) is released.

Tested for x86_64 and x86.

bits/types/once_flag.h [new file with mode: 0644]
manual/threads.texi
stdlib/Makefile
stdlib/stdlib.h
sysdeps/pthread/threads.h

diff --git a/bits/types/once_flag.h b/bits/types/once_flag.h
new file mode 100644 (file)
index 0000000..9180adc
--- /dev/null
@@ -0,0 +1,27 @@
+/* Define once_flag and ONCE_FLAG_INIT.
+   Copyright (C) 2018-2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef __once_flag_defined
+#define __once_flag_defined 1
+
+#include <bits/thread-shared-types.h>
+
+typedef __once_flag once_flag;
+#define ONCE_FLAG_INIT __ONCE_FLAG_INIT
+
+#endif
index eeefa9b8003697999776dc34ba438b5d9e8bdd48..1e9be32d6691b5de093f40762450cd1786b3708c 100644 (file)
@@ -219,6 +219,9 @@ invoked from several threads.  The completion of the function
 @code{call_once} with the same @code{flag} variable.
 @end deftypefun
 
+These definitions are from C11, where they appear in @file{threads.h}.
+In C23, they appear in @file{stdlib.h} as well as in @file{threads.h}.
+
 
 @node ISO C Mutexes
 @subsection Mutexes
index 25f777e1a5d69bced9ae9fc74aaccc1d4f61923d..d2c594885a25204abdd28769fccee165e069cb60 100644 (file)
@@ -37,6 +37,7 @@ headers := \
   bits/time64.h \
   bits/timesize.h \
   bits/types/error_t.h \
+  bits/types/once_flag.h \
   bits/wordsize.h \
   errno.h \
   fmtmsg.h \
index cd4503c761887324ddda8f0fd630e99c2bff196d..1d6a83a22c63d7a13f0b0f32fbd08d1564e06f35 100644 (file)
@@ -1158,6 +1158,14 @@ extern int getloadavg (double __loadavg[], int __nelem)
 extern int ttyslot (void) __THROW;
 #endif
 
+#if __GLIBC_USE (ISOC23)
+# include <bits/types/once_flag.h>
+
+/* Call function __FUNC exactly once, even if invoked from several threads.
+   All calls must be made with the same __FLAGS object.  */
+extern void call_once (once_flag *__flag, void (*__func)(void));
+#endif
+
 #include <bits/stdlib-float.h>
 
 /* Define some macros helping to catch buffer overflows.  */
index 44c9bc7b1f90b744c1e9ecd3b0e5f23fbf246841..48fcda5992e338a8e973cdde861aa07ecd0436ee 100644 (file)
@@ -25,6 +25,7 @@
 __BEGIN_DECLS
 
 #include <bits/thread-shared-types.h>
+#include <bits/types/once_flag.h>
 #include <bits/types/struct_timespec.h>
 
 #if (!defined __STDC_VERSION__                         \
@@ -58,9 +59,6 @@ enum
   mtx_timed     = 2
 };
 
-typedef __once_flag once_flag;
-#define ONCE_FLAG_INIT __ONCE_FLAG_INIT
-
 typedef union
 {
   char __size[__SIZEOF_PTHREAD_MUTEX_T];