This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [RFC] Fixing pthread_* namespace issues for thrd_* symbols
- From: Juan Manuel Torres Palma <j dot m dot torrespalma at gmail dot com>
- To: Szabolcs Nagy <nsz at port70 dot net>
- Cc: libc-alpha <libc-alpha at sourceware dot org>
- Date: Thu, 18 Jun 2015 09:56:58 +0200
- Subject: Re: [RFC] Fixing pthread_* namespace issues for thrd_* symbols
- Authentication-results: sourceware.org; auth=none
- References: <CAD82F-pVYZvT+HHK4XchTeSEFeOUQoUBwtFA=-b52zs8i+_bJw at mail dot gmail dot com> <20150430162657 dot GD863 at port70 dot net> <CAD82F-raesW2rAFEaMO_CYtyaXVmEAfeQa2Fi3vOXLwc-TyTEg at mail dot gmail dot com> <20150501131002 dot GA29166 at port70 dot net>
Sorry for late reply.
My solution so far is this one, only for x86, will work on other
architectures as long as this strategy is acceptable. What I have
mainly done is copy pthread_mutex_t and pthread_cond_t renaming them,
so I won't be breaking any ABI and namespaces will be clean. Let me
know if it's acceptable.
Cheers.
--------------------------------------------------------------------------------------------------------------------------------------------
>From 73f2aee3c1fc299c73607d23a49d75fefc72ad75 Mon Sep 17 00:00:00 2001
From: Juan Manuel Torres Palma <jmtorrespalma@gmail.com>
Date: Thu, 18 Jun 2015 09:48:19 +0200
Subject: [PATCH] New threads.h types
---
nptl/Makefile | 3 +-
sysdeps/x86/bits/threadstypes.h | 116 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 118 insertions(+), 1 deletion(-)
create mode 100644 sysdeps/x86/bits/threadstypes.h
diff --git a/nptl/Makefile b/nptl/Makefile
index 530d14b..1527134 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -22,7 +22,8 @@ subdir := nptl
include ../Makeconfig
-headers := pthread.h semaphore.h bits/semaphore.h
+headers := pthread.h semaphore.h bits/semaphore.h \
+ bits/threadstypes.h
extra-libs := libpthread
extra-libs-others := $(extra-libs)
diff --git a/sysdeps/x86/bits/threadstypes.h b/sysdeps/x86/bits/threadstypes.h
new file mode 100644
index 0000000..6a7a68a
--- /dev/null
+++ b/sysdeps/x86/bits/threadstypes.h
@@ -0,0 +1,116 @@
+/* Copyright (C) 2002-2015 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/>. */
+
+#ifndef _BITS_THREADSTYPES_H
+#define _BITS_THREADSTYPES_H 1
+
+#include <bits/wordsize.h>
+
+
+#ifdef __x86_64__
+# if __WORDSIZE == 64
+# define __SIZEOF_MTX_T 40
+# define __SIZEOF_CND_T 48
+# else
+# define __SIZEOF_MTX_T 32
+# define __SIZEOF_CND_T 48
+# endif
+#else
+# define __SIZEOF_MTX_T 24
+# define __SIZEOF_CND_T 48
+#endif
+
+
+#ifdef __x86_64__
+typedef struct __pthread_internal_list
+{
+ struct __pthread_internal_list *__prev;
+ struct __pthread_internal_list *__next;
+} __pthread_list_t;
+#else
+typedef struct __pthread_internal_slist
+{
+ struct __pthread_internal_slist *__next;
+} __pthread_slist_t;
+#endif
+
+
+/* Data structure for C11 mtx_t. It's a copy of pthread_mutex_t to
+ make both types compatible*/
+
+typedef union
+{
+ struct __pthread_mutex_s
+ {
+ int __lock;
+ unsigned int __count;
+ int __owner;
+#ifdef __x86_64__
+ unsigned int __nusers;
+#endif
+ /* KIND must stay at this position in the structure to maintain
+ binary compatibility. */
+ int __kind;
+#ifdef __x86_64__
+ short __spins;
+ short __elision;
+ __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV 1
+/* Mutex __spins initializer used by PTHREAD_MUTEX_INITIALIZER. */
+# define __PTHREAD_SPINS 0, 0
+#else
+ unsigned int __nusers;
+ __extension__ union
+ {
+ struct
+ {
+ short __espins;
+ short __elision;
+# define __spins __elision_data.__espins
+# define __elision __elision_data.__elision
+# define __PTHREAD_SPINS { 0, 0 }
+ } __elision_data;
+ __pthread_slist_t __list;
+ };
+#endif
+ } __data;
+ char __size[__SIZEOF_MTX_T];
+ long int __align;
+} pthread_mutex_t;
+
+
+/* Data structure for C11 cnd_t. A copy of pthread_cond_t
+ to make them compatible. */
+
+typedef union
+{
+ struct
+ {
+ int __lock;
+ unsigned int __futex;
+ __extension__ unsigned long long int __total_seq;
+ __extension__ unsigned long long int __wakeup_seq;
+ __extension__ unsigned long long int __woken_seq;
+ void *__mutex;
+ unsigned int __nwaiters;
+ unsigned int __broadcast_seq;
+ } __data;
+ char __size[__SIZEOF_CND_T];
+ __extension__ long long int __align;
+} cnd_t;
+
+#endif /* bits/threadstypes.h */
--
2.1.0
2015-05-01 15:10 GMT+02:00 Szabolcs Nagy <nsz@port70.net>:
> * Juan Manuel Torres Palma <j.m.torrespalma@gmail.com> [2015-05-01 13:49:44 +0200]:
>> > how do you plan to solve the namespace issue for the pthread types?
>>
>> Most pthread types (some of them will be required for C11 types) are
>> architecture dependent, so my plan was to create a new header file
>> (keeping all the architecture dependent defines and types here)
>> including definitions of types like __pthread_t.
>>
>> Then the pthread.h will include it and do:
>> typedef __pthread_mutex_t pthread__mutex_t;
>>
>
> that will break the c++ abi of pthreads
>
> (we had the same problem back when c11 threads were implemented
> in musl libc and ended up duplicating the typedefs i think..
> you may want to look at the discussions on the musl list around
> the alternatives
>
> i think the type duplication within the implementation is ok,
> but not pedantically correct on the abstract c language level:
> wrappers redeclare the __pthread* functions with incompatible
> types to be able to pass c11 type pointers
>
> for future reference: all types in libc api should have type tag
> in the implementation reserved namespace (ie __ prefixed) to
> allow fixes like c11 threads later despite the hideous c++ abi)
>
>> For other types like pthread_t that basically are unsigned long, will
>> just copy the type:
>> typedef unsigned long thrd_t;
>>
>> Cheers.
>>
>> --
>> Juan Manuel Torres Palma.
>> Computer Science Student at Universidad de Granada.
--
Juan Manuel Torres Palma.
Computer Science Student at Universidad de Granada.