This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 3/8] nptl: Add C11 threads call_once functions

This patch adds the mtx_* 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,
tile{pro,gx}-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.

	* 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.26]: 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                   | 10 ++++++++++
 conform/data/threads.h-data |  5 +++++
 nptl/Makefile               |  2 +-
 nptl/Versions               |  2 +-
 nptl/call_once.c            | 27 +++++++++++++++++++++++++++
 sysdeps/nptl/threads.h      |  7 +++++++
 6 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 nptl/call_once.c

diff --git a/conform/data/threads.h-data b/conform/data/threads.h-data
index bb5ca75..70b2fe0 100644
--- a/conform/data/threads.h-data
+++ b/conform/data/threads.h-data
@@ -1,5 +1,7 @@
 #if defined ISO11
 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"
diff --git a/nptl/Makefile b/nptl/Makefile
index 157ebe1..4d65c57 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -142,7 +142,7 @@ libpthread-routines = nptl-init vars events version pt-interp \
 		      thrd_create thrd_current thrd_detach thrd_equal \
 		      thrd_exit thrd_join thrd_sleep thrd_yield \
 		      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 \
diff --git a/nptl/Versions b/nptl/Versions
index 2fd362f..07f3aeb 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -269,7 +269,7 @@ libpthread {
   GLIBC_2.26 {
     thrd_create; thrd_current; thrd_detach; thrd_equal; thrd_exit; thrd_join;
     thrd_sleep; thrd_yield; mtx_init; mtx_lock; mtx_timedlock; mtx_trylock;
-    mtx_unlock; mtx_destroy;
+    mtx_unlock; mtx_destroy; call_once;
diff --git a/nptl/call_once.c b/nptl/call_once.c
new file mode 100644
index 0000000..460e917
--- /dev/null
+++ b/nptl/call_once.c
@@ -0,0 +1,27 @@
+/* C11 threads call once implementation.
+   Copyright (C) 2017 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
+   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
+   <>.  */
+#include "thrd_priv.h"
+/* Call function func exactly once, even if invoked from several threads.
+   All calls must be made with the same flag object.  */
+call_once (once_flag *flag, void (*func)(void))
+  __pthread_once (flag, func);
diff --git a/sysdeps/nptl/threads.h b/sysdeps/nptl/threads.h
index f6d4f1a..9784983 100644
--- a/sysdeps/nptl/threads.h
+++ b/sysdeps/nptl/threads.h
@@ -27,8 +27,11 @@ __BEGIN_DECLS
 #include <bits/thread-shared-types.h>
 #include <bits/types/struct_timespec.h>
+#define ONCE_FLAG_INIT 0
 typedef unsigned long int thrd_t;
 typedef int (*thrd_start_t) (void*);
+typedef int __ONCE_ALIGNMENT once_flag;
 /* Exit and error codes.  */
@@ -130,6 +133,10 @@ 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 __flag object.  */
+extern void call_once (once_flag *__flag, void (*__func)(void));
 #endif /* _THREADS_H */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]