1 /* Copyright (C) 2002-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
22 #include <kernel-features.h>
25 #include <pthread-offsets.h>
26 #include <futex-internal.h>
27 #include <shlib-compat.h>
29 #include <stap-probe.h>
31 static const struct pthread_mutexattr default_mutexattr
=
33 /* Default is a normal mutex, not shared between processes. */
34 .mutexkind
= PTHREAD_MUTEX_NORMAL
39 prio_inherit_missing (void)
41 static int tpi_supported
;
42 if (__glibc_unlikely (atomic_load_relaxed (&tpi_supported
) == 0))
44 int e
= futex_unlock_pi (&(unsigned int){0}, 0);
45 atomic_store_relaxed (&tpi_supported
, e
== ENOSYS
? -1 : 1);
47 return __glibc_unlikely (tpi_supported
< 0);
51 ___pthread_mutex_init (pthread_mutex_t
*mutex
,
52 const pthread_mutexattr_t
*mutexattr
)
54 const struct pthread_mutexattr
*imutexattr
;
56 ASSERT_TYPE_SIZE (pthread_mutex_t
, __SIZEOF_PTHREAD_MUTEX_T
);
58 /* __kind is the only field where its offset should be checked to
59 avoid ABI breakage with static initializers. */
60 ASSERT_PTHREAD_INTERNAL_OFFSET (pthread_mutex_t
, __data
.__kind
,
61 __PTHREAD_MUTEX_KIND_OFFSET
);
62 ASSERT_PTHREAD_INTERNAL_MEMBER_SIZE (pthread_mutex_t
, __data
.__kind
, int);
64 imutexattr
= ((const struct pthread_mutexattr
*) mutexattr
65 ?: &default_mutexattr
);
68 switch (__builtin_expect (imutexattr
->mutexkind
69 & PTHREAD_MUTEXATTR_PROTOCOL_MASK
,
71 << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
))
73 case PTHREAD_PRIO_NONE
<< PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
:
76 case PTHREAD_PRIO_INHERIT
<< PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
:
77 if (__glibc_unlikely (prio_inherit_missing ()))
82 /* XXX: For now we don't support robust priority protected mutexes. */
83 if (imutexattr
->mutexkind
& PTHREAD_MUTEXATTR_FLAG_ROBUST
)
88 /* Clear the whole variable. */
89 memset (mutex
, '\0', __SIZEOF_PTHREAD_MUTEX_T
);
91 /* Copy the values from the attribute. */
92 int mutex_kind
= imutexattr
->mutexkind
& ~PTHREAD_MUTEXATTR_FLAG_BITS
;
94 if ((imutexattr
->mutexkind
& PTHREAD_MUTEXATTR_FLAG_ROBUST
) != 0)
96 #ifndef __ASSUME_SET_ROBUST_LIST
97 if ((imutexattr
->mutexkind
& PTHREAD_MUTEXATTR_FLAG_PSHARED
) != 0
98 && !__nptl_set_robust_list_avail
)
102 mutex_kind
|= PTHREAD_MUTEX_ROBUST_NORMAL_NP
;
105 switch (imutexattr
->mutexkind
& PTHREAD_MUTEXATTR_PROTOCOL_MASK
)
107 case PTHREAD_PRIO_INHERIT
<< PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
:
108 mutex_kind
|= PTHREAD_MUTEX_PRIO_INHERIT_NP
;
111 case PTHREAD_PRIO_PROTECT
<< PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
:
112 mutex_kind
|= PTHREAD_MUTEX_PRIO_PROTECT_NP
;
114 int ceiling
= (imutexattr
->mutexkind
115 & PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
)
116 >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT
;
119 /* See __init_sched_fifo_prio. */
120 if (atomic_load_relaxed (&__sched_fifo_min_prio
) == -1)
121 __init_sched_fifo_prio ();
122 if (ceiling
< atomic_load_relaxed (&__sched_fifo_min_prio
))
123 ceiling
= atomic_load_relaxed (&__sched_fifo_min_prio
);
125 mutex
->__data
.__lock
= ceiling
<< PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
132 /* The kernel when waking robust mutexes on exit never uses
133 FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
134 if ((imutexattr
->mutexkind
& (PTHREAD_MUTEXATTR_FLAG_PSHARED
135 | PTHREAD_MUTEXATTR_FLAG_ROBUST
)) != 0)
136 mutex_kind
|= PTHREAD_MUTEX_PSHARED_BIT
;
138 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
139 in sysdeps/nptl/bits/thread-shared-types.h. */
140 atomic_store_relaxed (&(mutex
->__data
.__kind
), mutex_kind
);
142 /* Default values: mutex not used yet. */
143 // mutex->__count = 0; already done by memset
144 // mutex->__owner = 0; already done by memset
145 // mutex->__nusers = 0; already done by memset
146 // mutex->__spins = 0; already done by memset
147 // mutex->__next = NULL; already done by memset
149 LIBC_PROBE (mutex_init
, 1, mutex
);
153 versioned_symbol (libpthread
, ___pthread_mutex_init
, pthread_mutex_init
,
155 libc_hidden_ver (___pthread_mutex_init
, __pthread_mutex_init
)
157 strong_alias (___pthread_mutex_init
, __pthread_mutex_init
)
160 #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_34)
161 compat_symbol (libpthread
, ___pthread_mutex_init
, __pthread_mutex_init
,