diff mbox series

[v8,5/8] nptl: Add C11 threads tss_* functions

Message ID 1517591084-11347-6-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show
Series Add support for ISO C threads.h | expand

Commit Message

Adhemerval Zanella Feb. 2, 2018, 5:04 p.m. UTC
This patch adds the tss_* definitions from C11 threads (ISO/IEC 9899:2011),
more specifically tss_create, tss_delete, tss_get, tss_set, and required
types.

Mostly of the definitions are composed based on POSIX conterparts, including
tss_t (pthread_key_t).

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 (thread_local): New macro.
	(TSS_DTOR_ITERATIONS): Likewise.
	(tss_t): New type.
	(tss_dtor_t): Likewise.
	(tss_create): New function.
	(tss_get): Likewise.
	(tss_set): Likewise.
	(tss_delete): Likewise.
	* nptl/Makefile (libpthread-routines): Add tss_create, tss_delete,
	tss_get, and tss_set objects.
	* nptl/Versions (libpthread) [GLIBC_2.27]: Likewise.
	* nptl/tss_create.c: New file.
	* nptl/tss_delete.c: Likewise.
	* nptl/tss_get.c: Likewise.
	* nptl/tss_set.c: Likewise.
	* sysdeps/nptl/threads.h (thread_local): New define.
	(TSS_DTOR_ITERATIONS): Likewise.
	(tss_t): New typedef.
	(tss_dtor_t): Likewise.
	(tss_create): New prototype.
	(tss_get): Likewise.
	(tss_set): Likewise.
	(tss_delete): Likewise.
---
 ChangeLog                   | 24 ++++++++++++++++++++++++
 conform/data/threads.h-data |  9 +++++++++
 nptl/Makefile               |  3 ++-
 nptl/Versions               |  3 ++-
 nptl/tss_create.c           | 28 ++++++++++++++++++++++++++++
 nptl/tss_delete.c           | 25 +++++++++++++++++++++++++
 nptl/tss_get.c              | 25 +++++++++++++++++++++++++
 nptl/tss_set.c              | 26 ++++++++++++++++++++++++++
 sysdeps/nptl/threads.h      | 24 ++++++++++++++++++++++++
 9 files changed, 165 insertions(+), 2 deletions(-)
 create mode 100644 nptl/tss_create.c
 create mode 100644 nptl/tss_delete.c
 create mode 100644 nptl/tss_get.c
 create mode 100644 nptl/tss_set.c

-- 
2.7.4

Comments

Florian Weimer July 12, 2018, 4:48 p.m. UTC | #1
On 02/02/2018 06:04 PM, Adhemerval Zanella wrote:
> diff --git a/sysdeps/nptl/threads.h b/sysdeps/nptl/threads.h

> index eb89b0a..6a807ee 100644

> --- a/sysdeps/nptl/threads.h

> +++ b/sysdeps/nptl/threads.h

> @@ -28,6 +28,10 @@ __BEGIN_DECLS

>   #include <bits/types/struct_timespec.h>

>   

>   #define ONCE_FLAG_INIT 0

> +#define thread_local _Thread_local


This needs a __cplusplus conditional.  _Thread_local does not exist in 
C++.  The macro has to be defined to thread_local starting with C++11.

Thanks,
Florian
Adhemerval Zanella July 12, 2018, 8:32 p.m. UTC | #2
On 12/07/2018 13:48, Florian Weimer wrote:
> On 02/02/2018 06:04 PM, Adhemerval Zanella wrote:

>> diff --git a/sysdeps/nptl/threads.h b/sysdeps/nptl/threads.h

>> index eb89b0a..6a807ee 100644

>> --- a/sysdeps/nptl/threads.h

>> +++ b/sysdeps/nptl/threads.h

>> @@ -28,6 +28,10 @@ __BEGIN_DECLS

>>   #include <bits/types/struct_timespec.h>

>>     #define ONCE_FLAG_INIT 0

>> +#define thread_local _Thread_local

> 

> This needs a __cplusplus conditional.  _Thread_local does not exist in C++.  The macro has to be defined to thread_local starting with C++11.


I think we can just not define it for C++, not really checking which
version is being used.
Florian Weimer July 12, 2018, 8:37 p.m. UTC | #3
On 07/12/2018 10:32 PM, Adhemerval Zanella wrote:
>> This needs a __cplusplus conditional.  _Thread_local does not exist in C++.  The macro has to be defined to thread_local starting with C++11.


> I think we can just not define it for C++, not really checking which

> version is being used.


Sounds fine to me.

Thanks,
Florian
diff mbox series

Patch

diff --git a/conform/data/threads.h-data b/conform/data/threads.h-data
index d7c562e..406e497 100644
--- a/conform/data/threads.h-data
+++ b/conform/data/threads.h-data
@@ -1,6 +1,8 @@ 
 #if defined ISO11
 
 macro ONCE_FLAG_INIT
+macro thread_local
+macro-int-constant TSS_DTOR_ITERATIONS
 
 constant thrd_success
 constant thrd_busy
@@ -17,6 +19,8 @@  type thrd_start_t
 type mtx_t
 type once_flag
 type cnd_t
+type tss_t
+type tss_dtor_t
 
 function int thrd_create (thrd_t*, thrd_start_t, void*)
 function int thrd_equal (thrd_t, thrd_t)
@@ -43,6 +47,11 @@  function int cnd_wait (cnd_t*, mtx_t*)
 function int cnd_timedwait (cnd_t*, mtx_t*, const struct timespec*)
 function void cnd_destroy (cnd_t*)
 
+function int tss_create (tss_t*, tss_dtor_t)
+function {void*} tss_get (tss_t)
+function int tss_set (tss_t, void*)
+function void tss_delete (tss_t)
+
 #include "time.h-data"
 
 #endif
diff --git a/nptl/Makefile b/nptl/Makefile
index e13c07a..4ee1db0 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -144,7 +144,8 @@  libpthread-routines = nptl-init vars events version pt-interp \
 		      thrd_exit thrd_join thrd_sleep thrd_yield \
 		      mtx_destroy mtx_init mtx_lock mtx_timedlock \
 		      mtx_trylock mtx_unlock call_once cnd_broadcast \
-		      cnd_destroy cnd_init cnd_signal cnd_timedwait cnd_wait
+		      cnd_destroy cnd_init cnd_signal cnd_timedwait cnd_wait \
+		      tss_create tss_delete tss_get tss_set
 #		      pthread_setuid pthread_seteuid pthread_setreuid \
 #		      pthread_setresuid \
 #		      pthread_setgid pthread_setegid pthread_setregid \
diff --git a/nptl/Versions b/nptl/Versions
index 0672286..3df32db 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -270,7 +270,8 @@  libpthread {
     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; call_once; cnd_broadcast; cnd_destroy; cnd_init;
-    cnd_signal; cnd_timedwait; cnd_wait; mtx_destroy;
+    cnd_signal; cnd_timedwait; cnd_wait; mtx_destroy; tss_create; tss_delete;
+    tss_get; tss_set;
   }
 
   GLIBC_PRIVATE {
diff --git a/nptl/tss_create.c b/nptl/tss_create.c
new file mode 100644
index 0000000..da1c91a
--- /dev/null
+++ b/nptl/tss_create.c
@@ -0,0 +1,28 @@ 
+/* C11 threads thread-specific creation 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 "thrd_priv.h"
+
+int
+tss_create (tss_t *tss_id, tss_dtor_t destructor)
+{
+  _Static_assert (sizeof (tss_t) == sizeof (pthread_key_t), "tss_t size");
+
+  int err_code = __pthread_key_create (tss_id, destructor);
+  return thrd_err_map (err_code);
+}
diff --git a/nptl/tss_delete.c b/nptl/tss_delete.c
new file mode 100644
index 0000000..0fc2975
--- /dev/null
+++ b/nptl/tss_delete.c
@@ -0,0 +1,25 @@ 
+/* C11 threads thread-specific delete 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 "thrd_priv.h"
+
+void
+tss_delete (tss_t tss_id)
+{
+  __pthread_key_delete (tss_id);
+}
diff --git a/nptl/tss_get.c b/nptl/tss_get.c
new file mode 100644
index 0000000..5e09766
--- /dev/null
+++ b/nptl/tss_get.c
@@ -0,0 +1,25 @@ 
+/* C11 threads thread-specific get 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 "thrd_priv.h"
+
+void *
+tss_get (tss_t tss_id)
+{
+  return __pthread_getspecific (tss_id);
+}
diff --git a/nptl/tss_set.c b/nptl/tss_set.c
new file mode 100644
index 0000000..03973d9
--- /dev/null
+++ b/nptl/tss_set.c
@@ -0,0 +1,26 @@ 
+/* C11 threads thread-specific set 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 "thrd_priv.h"
+
+int
+tss_set (tss_t tss_id, void *val)
+{
+  int err_code = __pthread_setspecific (tss_id, val);
+  return thrd_err_map (err_code);
+}
diff --git a/sysdeps/nptl/threads.h b/sysdeps/nptl/threads.h
index eb89b0a..6a807ee 100644
--- a/sysdeps/nptl/threads.h
+++ b/sysdeps/nptl/threads.h
@@ -28,6 +28,10 @@  __BEGIN_DECLS
 #include <bits/types/struct_timespec.h>
 
 #define ONCE_FLAG_INIT 0
+#define thread_local _Thread_local
+#define TSS_DTOR_ITERATIONS 4
+typedef unsigned int tss_t;
+typedef void (*tss_dtor_t) (void*);
 
 typedef unsigned long int thrd_t;
 typedef int (*thrd_start_t) (void*);
@@ -171,6 +175,26 @@  extern int cnd_timedwait (cnd_t *__restrict __cond,
    resources.  */
 extern void cnd_destroy (cnd_t *__COND);
 
+
+/* Thread specific storage functions.  */
+
+/* Create new thread-specific storage key and stores it in the object pointed
+   by __TSS_ID.  If __DESTRUCTOR is not NULL, the function will be called when
+   the thread terminates.  */
+extern int tss_create (tss_t *__tss_id, tss_dtor_t __destructor);
+
+/* Return the value held in thread-specific storage for the current thread
+   identified by __TSS_ID.  */
+extern void *tss_get (tss_t __tss_id);
+
+/* Sets the value of the thread-specific storage identified by __TSS_ID for
+   the current thread to __VAL.  */
+extern int tss_set (tss_t __tss_id, void *__val);
+
+/* Destroys the thread-specific storage identified by __TSS_ID.  The
+   destructor is not called until thrd_exit is called.  */
+extern void tss_delete (tss_t __tss_id);
+
 __END_DECLS
 
 #endif /* _THREADS_H */