]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
nptl: Add struct_mutex.h
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 8 Nov 2017 11:23:34 +0000 (09:23 -0200)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 26 Nov 2019 13:53:36 +0000 (13:53 +0000)
The current way of defining the common mutex definition for POSIX and
C11 on pthreadtypes-arch.h (added by commit 06be6368da16104be5) is
not really the best options for newer ports.  It requires define some
misleading flags that should be always defined as 0
(__PTHREAD_COMPAT_PADDING_MID and __PTHREAD_COMPAT_PADDING_END), it
exposes options used solely for linuxthreads compat mode
(__PTHREAD_MUTEX_USE_UNION and __PTHREAD_MUTEX_NUSERS_AFTER_KIND), and
requires newer ports to explicit define them (adding more boilerplate
code).

This patch adds a new default __pthread_mutex_s definition meant to
be used by newer ports.  Its layout mimics the current usage on both
32 and 64 bits ports and it allows most ports to use the generic
definition.  Only ports that use some arch-specific definition (such
as hardware lock-elision or linuxthreads compat) requires specific
headers.

For 32 bit, the generic definitions mimic the other 32-bit ports
of using an union to define the fields uses on adaptive and robust
mutexes (thus not allowing both usage at same time) and by using a
single linked-list for robust mutexes.  Both decisions seemed to
follow what recent ports have done and make the resulting
pthread_mutex_t/mtx_t object smaller.

Also the static intialization macro for pthread_mutex_t is set to use
a macro __PTHREAD_MUTEX_INITIALIZER where the architecture can redefine
in its struct_mutex.h if it requires additional fields to be
initialized.

Checked with a build on affected abis.

Change-Id: I30a22c3e3497805fd6e52994c5925897cffcfe13

26 files changed:
nptl/Makefile
sysdeps/aarch64/nptl/bits/pthreadtypes-arch.h
sysdeps/alpha/nptl/bits/pthreadtypes-arch.h
sysdeps/arm/nptl/bits/pthreadtypes-arch.h
sysdeps/csky/nptl/bits/pthreadtypes-arch.h
sysdeps/hppa/nptl/bits/pthreadtypes-arch.h
sysdeps/hppa/nptl/bits/struct_mutex.h [new file with mode: 0644]
sysdeps/ia64/nptl/bits/pthreadtypes-arch.h
sysdeps/m68k/nptl/bits/pthreadtypes-arch.h
sysdeps/microblaze/nptl/bits/pthreadtypes-arch.h
sysdeps/mips/nptl/bits/pthreadtypes-arch.h
sysdeps/mips/nptl/bits/struct_mutex.h [new file with mode: 0644]
sysdeps/nios2/nptl/bits/pthreadtypes-arch.h
sysdeps/nptl/bits/struct_mutex.h [new file with mode: 0644]
sysdeps/nptl/bits/thread-shared-types.h
sysdeps/nptl/pthread.h
sysdeps/powerpc/nptl/bits/pthreadtypes-arch.h
sysdeps/powerpc/nptl/bits/struct_mutex.h [new file with mode: 0644]
sysdeps/riscv/nptl/bits/pthreadtypes-arch.h
sysdeps/s390/nptl/bits/pthreadtypes-arch.h
sysdeps/s390/nptl/bits/struct_mutex.h [new file with mode: 0644]
sysdeps/sh/nptl/bits/pthreadtypes-arch.h
sysdeps/sparc/nptl/bits/pthreadtypes-arch.h
sysdeps/unix/sysv/linux/hppa/pthread.h
sysdeps/x86/nptl/bits/pthreadtypes-arch.h
sysdeps/x86/nptl/bits/struct_mutex.h [new file with mode: 0644]

index f9aadfd644a91d0544faca72747baabc41ee28e3..8178aba65190826771f66d41bf379edc8c2e5891 100644 (file)
@@ -22,7 +22,8 @@ subdir        := nptl
 
 include ../Makeconfig
 
-headers := pthread.h semaphore.h bits/semaphore.h threads.h
+headers := pthread.h semaphore.h bits/semaphore.h threads.h \
+          bits/struct_mutex.h
 
 extra-libs := libpthread
 extra-libs-others := $(extra-libs)
index 61c02e7bbcb9c2a36c39b394104262f547418773..78abd6960bd3e6f398074c1dc4748cda2a75ad0a 100644 (file)
 #define __SIZEOF_PTHREAD_COND_T         48
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T  8
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION   0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  0
-#define __PTHREAD_MUTEX_USE_UNION          0
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index f5b7d1940eb9218f1e521cd8d38c144eab87b877..b832590d827900dcf144cc62bfa2cdd691674359 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T     32
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  0
-#define __PTHREAD_MUTEX_USE_UNION          0
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index a2c550f62a3ed86ead83c1557d11718de2c7d363..5ae7d0d72136c566c63808ce3a4bce0ebaed8468 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T 20
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Data structure for mutex handling. */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index c1656ae2f6dd8fc2a063c76b50a24f9d90535a50..685515724f2ac412aed7b0fe587145fa201667cf 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T             20
 #define __SIZEOF_PTHREAD_BARRIERATTR_T         4
 
-/* Data structure for mutex handling.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION           0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND      1
-#define __PTHREAD_MUTEX_USE_UNION              1
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index 7c97b89d603308d3021c731dc5f56b9525c763b1..c11add1af87201eb0bda30f69c97b264c5e62a71 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCK_T 64
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 
-/* The old 4-word 16-byte aligned lock. This is initalized
-   to all ones by the Linuxthreads PTHREAD_MUTEX_INITIALIZER.
-   Unused in NPTL.  */
-#define __PTHREAD_COMPAT_PADDING_MID  int __compat_padding[4];
-/* Two more words are left before the NPTL
-   pthread_mutex_t is larger than Linuxthreads.  */
-#define __PTHREAD_COMPAT_PADDING_END  int __reserved[2];
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT __attribute__ ((__aligned__(16)))
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/hppa/nptl/bits/struct_mutex.h b/sysdeps/hppa/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..7f81256
--- /dev/null
@@ -0,0 +1,53 @@
+/* HPPA internal mutex struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+struct __pthread_mutex_s
+{
+  int __lock __LOCK_ALIGNMENT;
+  unsigned int __count;
+  int __owner;
+  /* KIND must stay at this position in the structure to maintain
+     binary compatibility with static initializers.  */
+  int __kind;
+  /* The old 4-word 16-byte aligned lock. This is initalized
+     to all ones by the Linuxthreads PTHREAD_MUTEX_INITIALIZER.
+     Unused in NPTL.  */
+  int __glibc_compat_padding[4];
+  /* In the old structure there are 4 words left due to alignment.
+     In NPTL two words are used.  */
+  unsigned int __nusers;
+  __extension__ union
+  {
+    int __spins;
+    __pthread_slist_t __list;
+  };
+  /* Two more words are left before the NPTL
+     pthread_mutex_t is larger than Linuxthreads.  */
+  int __glibc_reserved1;
+  int __glibc_reserved2;
+};
+
+#define __PTHREAD_MUTEX_HAVE_PREV       0
+
+#define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, __kind, { 0, 0, 0, 0 }, 0, { 0 }, 0, 0
+
+#endif
index 8eb1bd5ebefc5f71c1dbf42daceb47e1585fd62d..7a1fd3ed2811a360a51275cf90897ec9cee15934 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T 32
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  0
-#define __PTHREAD_MUTEX_USE_UNION          0
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index cefce5659341fd70e03c59f9fde22af4e4e98da4..0647c48b5aac5f6edb27e7dde26f76f47c592aa0 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T 20
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Data structure for mutex handling. */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT __attribute__ ((__aligned__ (4)))
 #define __ONCE_ALIGNMENT __attribute__ ((__aligned__ (4)))
 
index 00b50df2273bf8187f677d68628fcf3e5057873b..21b090aaf6803ca21905fd9fc63a7392672a8889 100644 (file)
 # define __SIZEOF_PTHREAD_BARRIER_T      20
 # define __SIZEOF_PTHREAD_BARRIERATTR_T   4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index 629cd72048a74a52f85f49994a79f7849dc8de5b..5bcc90a7fe941d380519d625230a2eb7e5dfe32d 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Data structure for mutex handling. */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  (_MIPS_SIM != _ABI64)
-#define __PTHREAD_MUTEX_USE_UNION          (_MIPS_SIM != _ABI64)
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/mips/nptl/bits/struct_mutex.h b/sysdeps/mips/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..3307265
--- /dev/null
@@ -0,0 +1,56 @@
+/* MIPS internal mutex struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+struct __pthread_mutex_s
+{
+  int __lock;
+  unsigned int __count;
+  int __owner;
+#if _MIPS_SIM == _ABI64
+  unsigned int __nusers;
+#endif
+  /* KIND must stay at this position in the structure to maintain
+     binary compatibility with static initializers.  */
+  int __kind;
+#if _MIPS_SIM == _ABI64
+  int __spins;
+  __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV      1
+#else
+  unsigned int __nusers;
+  __extension__ union
+  {
+    int __spins;
+    __pthread_slist_t __list;
+  };
+# define __PTHREAD_MUTEX_HAVE_PREV      0
+#endif
+};
+
+#if _MIPS_SIM == _ABI64
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, 0, { 0, 0 }
+#else
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, __kind, 0, { 0 }
+#endif
+
+#endif
index bba904bcc42ca8fd6624673be9c311c690fbff54..7e71f723d0111ef4c8d1828e0eba5eac198fd796 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T 20
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Data structure for mutex handling. */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/nptl/bits/struct_mutex.h b/sysdeps/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..138320b
--- /dev/null
@@ -0,0 +1,84 @@
+/* Default mutex implementation struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+/* Generic struct for both POSIX and C11 mutexes.  New ports are expected
+   to use the default layout, however architecture can redefine it to
+   add arch-specific extension (such as lock-elision).  The struct have
+   a size of 32 bytes on LP32 and 40 bytes on LP64 architectures.  */
+
+struct __pthread_mutex_s
+{
+  int __lock __LOCK_ALIGNMENT;
+  unsigned int __count;
+  int __owner;
+#if __WORDSIZE == 64
+  unsigned int __nusers;
+#endif
+  /* KIND must stay at this position in the structure to maintain
+     binary compatibility with static initializers.
+
+     Concurrency notes:
+     The __kind of a mutex is initialized either by the static
+     PTHREAD_MUTEX_INITIALIZER or by a call to pthread_mutex_init.
+
+     After a mutex has been initialized, the __kind of a mutex is usually not
+     changed.  BUT it can be set to -1 in pthread_mutex_destroy or elision can
+     be enabled.  This is done concurrently in the pthread_mutex_*lock
+     functions by using the macro FORCE_ELISION. This macro is only defined
+     for architectures which supports lock elision.
+
+     For elision, there are the flags PTHREAD_MUTEX_ELISION_NP and
+     PTHREAD_MUTEX_NO_ELISION_NP which can be set in addition to the already
+     set type of a mutex.  Before a mutex is initialized, only
+     PTHREAD_MUTEX_NO_ELISION_NP can be set with pthread_mutexattr_settype.
+
+     After a mutex has been initialized, the functions pthread_mutex_*lock can
+     enable elision - if the mutex-type and the machine supports it - by
+     setting the flag PTHREAD_MUTEX_ELISION_NP. This is done concurrently.
+     Afterwards the lock / unlock functions are using specific elision
+     code-paths.  */
+  int __kind;
+#if __WORDSIZE != 64
+  unsigned int __nusers;
+#endif
+#if __WORDSIZE == 64
+  int __spins;
+  __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV      1
+#else
+  __extension__ union
+  {
+    int __spins;
+    __pthread_slist_t __list;
+  };
+# define __PTHREAD_MUTEX_HAVE_PREV      0
+#endif
+};
+
+#if __PTHREAD_MUTEX_HAVE_PREV == 1
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, 0, { 0, 0 }
+#else
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, { 0 }
+#endif
+
+#endif
index f7eb8d4c5dee978e3fb73924f3946b087238212e..0727fdec96dc4c8548b82fd6a9ea33b83cd225a6 100644 (file)
    __SIZEOF_PTHREAD_BARRIER_T     - size of pthread_barrier_t.
    __SIZEOF_PTHREAD_BARRIERATTR_T - size of pthread_barrierattr_t.
 
-   Also, the following macros must be define for internal pthread_mutex_t
-   struct definitions (struct __pthread_mutex_s):
-
-   __PTHREAD_COMPAT_PADDING_MID   - any additional members after 'kind'
-                                   and before '__spin' (for 64 bits) or
-                                   '__nusers' (for 32 bits).
-   __PTHREAD_COMPAT_PADDING_END   - any additional members at the end of
-                                   the internal structure.
-   __PTHREAD_MUTEX_LOCK_ELISION   - 1 if the architecture supports lock
-                                   elision or 0 otherwise.
-   __PTHREAD_MUTEX_NUSERS_AFTER_KIND - control where to put __nusers.  The
-                                      preferred value for new architectures
-                                      is 0.
-   __PTHREAD_MUTEX_USE_UNION      - control whether internal __spins and
-                                   __list will be place inside a union for
-                                   linuxthreads compatibility.
-                                   The preferred value for new architectures
-                                   is 0.
-
-   For a new port the preferred values for the required defines are:
-
-   #define __PTHREAD_COMPAT_PADDING_MID
-   #define __PTHREAD_COMPAT_PADDING_END
-   #define __PTHREAD_MUTEX_LOCK_ELISION         0
-   #define __PTHREAD_MUTEX_NUSERS_AFTER_KIND    0
-   #define __PTHREAD_MUTEX_USE_UNION            0
-
-   __PTHREAD_MUTEX_LOCK_ELISION can be set to 1 if the hardware plans to
-   eventually support lock elision using transactional memory.
-
    The additional macro defines any constraint for the lock alignment
    inside the thread structures:
 
    __ONCE_ALIGNMENT - for pthread_once_t/once_flag definition.
 
    And finally the internal pthread_rwlock_t (struct __pthread_rwlock_arch_t)
-   must be defined.
- */
+   must be defined.  */
+
 #include <bits/pthreadtypes-arch.h>
 
+
 /* Common definition of pthread_mutex_t. */
 
-#if !__PTHREAD_MUTEX_USE_UNION
 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
-
-/* Lock elision support.  */
-#if __PTHREAD_MUTEX_LOCK_ELISION
-# if !__PTHREAD_MUTEX_USE_UNION
-#  define __PTHREAD_SPINS_DATA \
-  short __spins;               \
-  short __elision
-#  define __PTHREAD_SPINS             0, 0
-# else
-#  define __PTHREAD_SPINS_DATA \
-  struct                       \
-  {                            \
-    short __espins;            \
-    short __eelision;          \
-  } __elision_data
-#  define __PTHREAD_SPINS         { 0, 0 }
-#  define __spins __elision_data.__espins
-#  define __elision __elision_data.__eelision
-# endif
-#else
-# define __PTHREAD_SPINS_DATA int __spins
-/* Mutex __spins initializer used by PTHREAD_MUTEX_INITIALIZER.  */
-# define __PTHREAD_SPINS 0
-#endif
-
-struct __pthread_mutex_s
-{
-  int __lock __LOCK_ALIGNMENT;
-  unsigned int __count;
-  int __owner;
-#if !__PTHREAD_MUTEX_NUSERS_AFTER_KIND
-  unsigned int __nusers;
-#endif
-  /* KIND must stay at this position in the structure to maintain
-     binary compatibility with static initializers.
-
-     Concurrency notes:
-     The __kind of a mutex is initialized either by the static
-     PTHREAD_MUTEX_INITIALIZER or by a call to pthread_mutex_init.
-
-     After a mutex has been initialized, the __kind of a mutex is usually not
-     changed.  BUT it can be set to -1 in pthread_mutex_destroy or elision can
-     be enabled.  This is done concurrently in the pthread_mutex_*lock functions
-     by using the macro FORCE_ELISION. This macro is only defined for
-     architectures which supports lock elision.
-
-     For elision, there are the flags PTHREAD_MUTEX_ELISION_NP and
-     PTHREAD_MUTEX_NO_ELISION_NP which can be set in addition to the already set
-     type of a mutex.
-     Before a mutex is initialized, only PTHREAD_MUTEX_NO_ELISION_NP can be set
-     with pthread_mutexattr_settype.
-     After a mutex has been initialized, the functions pthread_mutex_*lock can
-     enable elision - if the mutex-type and the machine supports it - by setting
-     the flag PTHREAD_MUTEX_ELISION_NP. This is done concurrently. Afterwards
-     the lock / unlock functions are using specific elision code-paths.  */
-  int __kind;
-  __PTHREAD_COMPAT_PADDING_MID
-#if __PTHREAD_MUTEX_NUSERS_AFTER_KIND
-  unsigned int __nusers;
-#endif
-#if !__PTHREAD_MUTEX_USE_UNION
-  __PTHREAD_SPINS_DATA;
-  __pthread_list_t __list;
-# define __PTHREAD_MUTEX_HAVE_PREV      1
-#else
-  __extension__ union
-  {
-    __PTHREAD_SPINS_DATA;
-    __pthread_slist_t __list;
-  };
-# define __PTHREAD_MUTEX_HAVE_PREV      0
-#endif
-  __PTHREAD_COMPAT_PADDING_END
-};
+
+/* Arch-specific mutex definitions.  A generic implementation is provided
+   by sysdeps/nptl/bits/struct_mutex.h.  If required, an architecture
+   can override it by defining:
+
+   1. struct __pthread_mutex_s (used on both pthread_mutex_t and mtx_t
+      definition).  It should contains at least the internal members
+      defined in the generic version.
+
+   2. __LOCK_ALIGNMENT for any extra attribute for internal lock used with
+      atomic operations.
+
+   3. The macro __PTHREAD_MUTEX_INITIALIZER used for static initialization.
+      It should initialize the mutex internal flag.  */
+
+#include <bits/struct_mutex.h>
 
 
 /* Common definition of pthread_cond_t. */
index f2b21accbca352ae57de386d9969b4dec89f4965..e673e3f4fc1d75186ad8562293614230d709a967 100644 (file)
@@ -83,30 +83,15 @@ enum
 #endif
 
 
-#if __PTHREAD_MUTEX_HAVE_PREV
-# define PTHREAD_MUTEX_INITIALIZER \
-  { { 0, 0, 0, 0, 0, __PTHREAD_SPINS, { 0, 0 } } }
-# ifdef __USE_GNU
-#  define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
-#  define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, __PTHREAD_SPINS, { 0, 0 } } }
-#  define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }
-
-# endif
-#else
-# define PTHREAD_MUTEX_INITIALIZER \
-  { { 0, 0, 0, 0, 0, { __PTHREAD_SPINS } } }
-# ifdef __USE_GNU
-#  define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { __PTHREAD_SPINS } } }
-#  define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, 0, { __PTHREAD_SPINS } } }
-#  define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, 0, { __PTHREAD_SPINS } } }
-
-# endif
+#define PTHREAD_MUTEX_INITIALIZER \
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_DEFAULT) } }
+#ifdef __USE_GNU
+# define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
+# define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
+# define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
 #endif
 
 
index 9810aab05753cc190974367fc52cdebc28b96fc2..429d42d77d3918557c2d15e44ab0e6edcafc2ca3 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    1
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  (__WORDSIZE != 64)
-#define __PTHREAD_MUTEX_USE_UNION          (__WORDSIZE != 64)
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/powerpc/nptl/bits/struct_mutex.h b/sysdeps/powerpc/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..a13dd58
--- /dev/null
@@ -0,0 +1,63 @@
+/* PowerPC internal mutex struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+struct __pthread_mutex_s
+{
+  int __lock;
+  unsigned int __count;
+  int __owner;
+#if __WORDSIZE == 64
+  unsigned int __nusers;
+#endif
+  /* KIND must stay at this position in the structure to maintain
+     binary compatibility with static initializers.  */
+  int __kind;
+#if __WORDSIZE == 64
+  short __spins;
+  short __elision;
+  __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV      1
+#else
+  unsigned int __nusers;
+  __extension__ union
+  {
+    struct
+    {
+      short __espins;
+      short __elision;
+# define __spins __elision_data.__espins
+# define __elision __elision_data.__elision
+    } __elision_data;
+    __pthread_slist_t __list;
+  };
+# define __PTHREAD_MUTEX_HAVE_PREV      0
+#endif
+};
+
+#if __WORDSIZE == 64
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, 0, 0, { 0, 0 }
+#else
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, __kind, 0, { { 0, 0 } }
+#endif
+
+#endif
index 77b60bf8f70a58ae6bc5c3a07e21cb55f4420db5..31f46ecbde7d51ace00161f63767114fa3c392d9 100644 (file)
 # error "rv32i-based systems are not supported"
 #endif
 
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION           0
-#define __PTHREAD_MUTEX_USE_UNION              0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND      0
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index 1cb1d7216a3a635c6bb173e7c6c12a5be7ce7d2f..5fd32ba75696526b041d5529fe3ad53fb16a9383 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    1
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  (__WORDSIZE != 64)
-#define __PTHREAD_MUTEX_USE_UNION          (__WORDSIZE != 64)
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/s390/nptl/bits/struct_mutex.h b/sysdeps/s390/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..8f3c755
--- /dev/null
@@ -0,0 +1,63 @@
+/* S390 internal mutex struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+struct __pthread_mutex_s
+{
+  int __lock;
+  unsigned int __count;
+  int __owner;
+#if __WORDSIZE == 64
+  unsigned int __nusers;
+#endif
+  /* KIND must stay at this position in the structure to maintain
+     binary compatibility with static initializers.  */
+  int __kind;
+#if __WORDSIZE == 64
+  short __spins;
+  short __elision;
+  __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV      1
+#else
+  unsigned int __nusers;
+  __extension__ union
+  {
+    struct
+    {
+      short __espins;
+      short __elision;
+    } _d;
+#  define __spins _d.__espins
+#  define __elision _d.__elision
+    __pthread_slist_t __list;
+  };
+# define __PTHREAD_MUTEX_HAVE_PREV      0
+#endif
+};
+
+#if __WORDSIZE == 64
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, 0, 0, { 0, 0 }
+#else
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, __kind, 0, { { 0, 0 } }
+#endif
+
+#endif
index e7bbfe1861fc77020418c76e093c88080ed5b07c..ec861133ca788f6026c24865295614f0afd8f569 100644 (file)
 #define __SIZEOF_PTHREAD_BARRIER_T 20
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-#define __PTHREAD_MUTEX_USE_UNION          1
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index 47093a57a29a99636d7b2cdb7b377bfdd351b28b..4cda2376d2ff736a00a13f7dfdd4cfad4f4462d6 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    0
-#define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  (__WORDSIZE != 64)
-#define __PTHREAD_MUTEX_USE_UNION          (__WORDSIZE != 64)
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
index 300c187759b91df1cdd410835f3300262983e369..3d1e7f3a45fcbb4cc7b9099f55968bc7cb0bc7d9 100644 (file)
@@ -83,17 +83,14 @@ enum
 
 
 #define PTHREAD_MUTEX_INITIALIZER \
 { { 0, 0, 0, 0, { 0, 0, 0, 0 }, 0, { __PTHREAD_SPINS }, { 0, 0 } } }
{ {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_DEFAULT) } }
 #ifdef __USE_GNU
 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, { 0, 0, 0, 0 }, 0, \
-      { __PTHREAD_SPINS }, { 0, 0 } } }
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_RECURSIVE_NP) } }
 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, { 0, 0, 0, 0 }, 0, \
-      { __PTHREAD_SPINS }, { 0, 0 } } }
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ERRORCHECK_NP) } }
 # define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP \
-  { { 0, 0, 0, PTHREAD_MUTEX_ADAPTIVE_NP, { 0, 0, 0, 0 }, 0, \
-      { __PTHREAD_SPINS }, { 0, 0 } } }
+ { {  __PTHREAD_MUTEX_INITIALIZER (PTHREAD_MUTEX_ADAPTIVE_NP) } }
 #endif
 
 
index 1f05ca088cfa0dd395b458772120cc1e97601113..668ec4a5d5f0cd25fdbbf173d033a7f5ff0671cd 100644 (file)
 #define __SIZEOF_PTHREAD_RWLOCKATTR_T 8
 #define __SIZEOF_PTHREAD_BARRIERATTR_T 4
 
-/* Definitions for internal mutex struct.  */
-#define __PTHREAD_COMPAT_PADDING_MID
-#define __PTHREAD_COMPAT_PADDING_END
-#define __PTHREAD_MUTEX_LOCK_ELISION    1
-#ifdef __x86_64__
-# define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  0
-# define __PTHREAD_MUTEX_USE_UNION          0
-#else
-# define __PTHREAD_MUTEX_NUSERS_AFTER_KIND  1
-# define __PTHREAD_MUTEX_USE_UNION          1
-#endif
-
 #define __LOCK_ALIGNMENT
 #define __ONCE_ALIGNMENT
 
diff --git a/sysdeps/x86/nptl/bits/struct_mutex.h b/sysdeps/x86/nptl/bits/struct_mutex.h
new file mode 100644 (file)
index 0000000..0938448
--- /dev/null
@@ -0,0 +1,63 @@
+/* x86 internal mutex struct definitions.
+   Copyright (C) 2019 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 _THREAD_MUTEX_INTERNAL_H
+#define _THREAD_MUTEX_INTERNAL_H 1
+
+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 with static initializers.  */
+  int __kind;
+#ifdef __x86_64__
+  short __spins;
+  short __elision;
+  __pthread_list_t __list;
+# define __PTHREAD_MUTEX_HAVE_PREV      1
+#else
+  unsigned int __nusers;
+  __extension__ union
+  {
+    struct
+    {
+      short __espins;
+      short __eelision;
+# define __spins __elision_data.__espins
+# define __elision __elision_data.__eelision
+    } __elision_data;
+    __pthread_slist_t __list;
+  };
+# define __PTHREAD_MUTEX_HAVE_PREV      0
+#endif
+};
+
+#ifdef __x86_64__
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, 0, __kind, 0, 0, { 0, 0 }
+#else
+# define __PTHREAD_MUTEX_INITIALIZER(__kind) \
+  0, 0, 0, __kind, 0, { { 0, 0 } }
+#endif
+
+#endif