]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Add C11 threads call_once functions
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 27 Jun 2017 13:49:51 +0000 (10:49 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 24 Jul 2018 17:07:01 +0000 (14:07 -0300)
This patch adds the call_* definitions from C11 threads (ISO/IEC 9899:2011),
more specifically call_once and required types.

Mostly of the definitions are composed based on POSIX conterparts,including
once_flag (pthread_once_t).  The idea is to make possible to share POSIX
internal implementations for mostly of the code (and making adjustment only
when required).

Checked with a build for all major ABI (aarch64-linux-gnu, alpha-linux-gnu,
arm-linux-gnueabi, i386-linux-gnu, ia64-linux-gnu, m68k-linux-gnu,
microblaze-linux-gnu [1], mips{64}-linux-gnu, nios2-linux-gnu,
powerpc{64le}-linux-gnu, s390{x}-linux-gnu, sparc{64}-linux-gnu,
and x86_64-linux-gnu).

Also ran a full check on aarch64-linux-gnu, x86_64-linux-gnu, i686-linux-gnu,
arm-linux-gnueabhf, and powerpc64le-linux-gnu.

[BZ #14092]
* conform/data/threads.h-data (ONCE_FLAG_INIT): New macro.
(once_flag): New type.
(call_once): New function.
* nptl/Makefile (libpthread-routines): Add call_once object.
* nptl/Versions (libphread) [GLIBC_2.28]: Add call_once symbol.
* nptl/call_once.c: New file.
* sysdeps/nptl/threads.h (ONCE_FLAG_INIT): New define.
(once_flag): New type.
(call_once): New prototype.

ChangeLog
conform/data/threads.h-data
nptl/Makefile
nptl/Versions
nptl/call_once.c [new file with mode: 0644]
nptl/threads.h

index 1f04a0c64929f96f29d0bf41e3f653f1b4352ee1..3d5d1e067a759d99a41a0a0ed354d4720fe33ac8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2018-07-24  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
 
+       [BZ #14092]
+       * conform/data/threads.h-data (ONCE_FLAG_INIT): New macro.
+       (once_flag): New type.
+       (call_once): New function.
+       * nptl/Makefile (libpthread-routines): Add call_once object.
+       * nptl/Versions (libphread) [GLIBC_2.28]: Add call_once symbol.
+       * nptl/call_once.c: New file.
+       * sysdeps/nptl/threads.h (ONCE_FLAG_INIT): New define.
+       (once_flag): New type.
+       (call_once): New prototype.
+
        [BZ #14092]
        * conform/data/threads.h-data (mtx_plain): New constant.
        (mtx_recursive): Likewise.
index bb5ca7552445fda1a8f7b3fa031a96b954e44755..70b2fe03ca4697cb1b702020e47bd7403de38472 100644 (file)
@@ -1,5 +1,7 @@
 #if defined ISO11
 
+macro ONCE_FLAG_INIT
+
 constant thrd_success
 constant thrd_busy
 constant thrd_error
@@ -13,6 +15,7 @@ constant mtx_timed
 type thrd_t
 type thrd_start_t
 type mtx_t
+type once_flag
 
 function int thrd_create (thrd_t*, thrd_start_t, void*)
 function int thrd_equal (thrd_t, thrd_t)
@@ -30,6 +33,8 @@ function int mtx_trylock (mtx_t*)
 function int mtx_unlock (mtx_t*)
 function void mtx_destroy (mtx_t*)
 
+function void call_once (once_flag*, void (*)(void))
+
 #include "time.h-data"
 
 #endif
index 4b889ab04e3a5264e214f1b4a5995665c895ca89..d55d24b09a8b4a73b830d84d1854e376909736b9 100644 (file)
@@ -143,7 +143,7 @@ libpthread-routines = nptl-init nptlfreeres vars events version pt-interp \
                      pthread_setattr_default_np pthread_getattr_default_np \
                      thrd_create thrd_detach thrd_exit thrd_join \
                      mtx_destroy mtx_init mtx_lock mtx_timedlock \
-                     mtx_trylock mtx_unlock
+                     mtx_trylock mtx_unlock call_once
 #                    pthread_setuid pthread_seteuid pthread_setreuid \
 #                    pthread_setresuid \
 #                    pthread_setgid pthread_setegid pthread_setregid \
index ca1be37354ddc5ea72c9f1e685b25a292de64a50..0fdba18c7455feba0c6379def8dac56512fdb833 100644 (file)
@@ -273,6 +273,7 @@ libpthread {
   GLIBC_2.28 {
     thrd_create; thrd_detach; thrd_exit; thrd_join;
     mtx_init; mtx_lock; mtx_timedlock; mtx_trylock; mtx_unlock; mtx_destroy;
+    call_once;
   }
 
   GLIBC_PRIVATE {
diff --git a/nptl/call_once.c b/nptl/call_once.c
new file mode 100644 (file)
index 0000000..5bc88cb
--- /dev/null
@@ -0,0 +1,31 @@
+/* C11 threads call once implementation.
+   Copyright (C) 2018 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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdalign.h>
+
+#include "thrd_priv.h"
+
+void
+call_once (once_flag *flag, void (*func)(void))
+{
+  _Static_assert (sizeof (once_flag) == sizeof (pthread_once_t),
+                 "sizeof (once_flag) != sizeof (pthread_once_t)");
+  _Static_assert (alignof (once_flag) == alignof (pthread_once_t),
+                 "alignof (once_flag) != alignof (pthread_once_t)");
+  __pthread_once (&flag->__data, func);
+}
index 13d0075ea959c63092369e9c0dc9c36f22bbf0c1..32f7cf8f75bc1172e6dfb656798d0731a9c0503a 100644 (file)
@@ -48,6 +48,12 @@ enum
   mtx_timed     = 2
 };
 
+typedef struct
+{
+  int __data __ONCE_ALIGNMENT;
+} once_flag;
+#define ONCE_FLAG_INIT { 0 }
+
 typedef union
 {
   char __size[__SIZEOF_PTHREAD_MUTEX_T];
@@ -129,6 +135,11 @@ extern int mtx_unlock (mtx_t *__mutex);
 /* Destroy the mutex object pointed by __MUTEX.  */
 extern void mtx_destroy (mtx_t *__mutex);
 
+
+/* 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));
+
 __END_DECLS
 
 #endif /* _THREADS_H */