blob: 8908414521669376b2d926a5134aeb4efbb93d97 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Linuxthreads - a simple clone()-based implementation of Posix */
2/* threads for Linux. */
3/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4/* */
5/* This program is free software; you can redistribute it and/or */
6/* modify it under the terms of the GNU Library General Public License */
7/* as published by the Free Software Foundation; either version 2 */
8/* of the License, or (at your option) any later version. */
9/* */
10/* This program is distributed in the hope that it will be useful, */
11/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13/* GNU Library General Public License for more details. */
14
15/* Mutexes */
16
17#include <errno.h>
18#include <sched.h>
19#include <stddef.h>
20#include <limits.h>
21#include "pthread.h"
22#include "internals.h"
23#include "spinlock.h"
24#include "queue.h"
25#include "restart.h"
26
27int attribute_hidden __pthread_mutex_init(pthread_mutex_t * mutex,
28 const pthread_mutexattr_t * mutex_attr)
29{
30 __pthread_init_lock(&mutex->__m_lock);
31 mutex->__m_kind =
32 mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
33 mutex->__m_count = 0;
34 mutex->__m_owner = NULL;
35 return 0;
36}
37strong_alias (__pthread_mutex_init, pthread_mutex_init)
38
39int attribute_hidden __pthread_mutex_destroy(pthread_mutex_t * mutex)
40{
41 switch (mutex->__m_kind) {
42 case PTHREAD_MUTEX_ADAPTIVE_NP:
43 case PTHREAD_MUTEX_RECURSIVE_NP:
44 if ((mutex->__m_lock.__status & 1) != 0)
45 return EBUSY;
46 return 0;
47 case PTHREAD_MUTEX_ERRORCHECK_NP:
48 case PTHREAD_MUTEX_TIMED_NP:
49 if (mutex->__m_lock.__status != 0)
50 return EBUSY;
51 return 0;
52 default:
53 return EINVAL;
54 }
55}
56strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
57
58int attribute_hidden __pthread_mutex_trylock(pthread_mutex_t * mutex)
59{
60 pthread_descr self;
61 int retcode;
62
63 switch(mutex->__m_kind) {
64 case PTHREAD_MUTEX_ADAPTIVE_NP:
65 retcode = __pthread_trylock(&mutex->__m_lock);
66 return retcode;
67 case PTHREAD_MUTEX_RECURSIVE_NP:
68 self = thread_self();
69 if (mutex->__m_owner == self) {
70 mutex->__m_count++;
71 return 0;
72 }
73 retcode = __pthread_trylock(&mutex->__m_lock);
74 if (retcode == 0) {
75 mutex->__m_owner = self;
76 mutex->__m_count = 0;
77 }
78 return retcode;
79 case PTHREAD_MUTEX_ERRORCHECK_NP:
80 retcode = __pthread_alt_trylock(&mutex->__m_lock);
81 if (retcode == 0) {
82 mutex->__m_owner = thread_self();
83 }
84 return retcode;
85 case PTHREAD_MUTEX_TIMED_NP:
86 retcode = __pthread_alt_trylock(&mutex->__m_lock);
87 return retcode;
88 default:
89 return EINVAL;
90 }
91}
92strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
93
94int attribute_hidden __pthread_mutex_lock(pthread_mutex_t * mutex)
95{
96 pthread_descr self;
97
98 switch(mutex->__m_kind) {
99 case PTHREAD_MUTEX_ADAPTIVE_NP:
100 __pthread_lock(&mutex->__m_lock, NULL);
101 return 0;
102 case PTHREAD_MUTEX_RECURSIVE_NP:
103 self = thread_self();
104 if (mutex->__m_owner == self) {
105 mutex->__m_count++;
106 return 0;
107 }
108 __pthread_lock(&mutex->__m_lock, self);
109 mutex->__m_owner = self;
110 mutex->__m_count = 0;
111 return 0;
112 case PTHREAD_MUTEX_ERRORCHECK_NP:
113 self = thread_self();
114 if (mutex->__m_owner == self) return EDEADLK;
115 __pthread_alt_lock(&mutex->__m_lock, self);
116 mutex->__m_owner = self;
117 return 0;
118 case PTHREAD_MUTEX_TIMED_NP:
119 __pthread_alt_lock(&mutex->__m_lock, NULL);
120 return 0;
121 default:
122 return EINVAL;
123 }
124}
125strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
126
127int pthread_mutex_timedlock (pthread_mutex_t *mutex,
128 const struct timespec *abstime)
129{
130 pthread_descr self;
131 int res;
132
133 if (__builtin_expect (abstime->tv_nsec, 0) < 0
134 || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
135 return EINVAL;
136
137 switch(mutex->__m_kind) {
138 case PTHREAD_MUTEX_ADAPTIVE_NP:
139 __pthread_lock(&mutex->__m_lock, NULL);
140 return 0;
141 case PTHREAD_MUTEX_RECURSIVE_NP:
142 self = thread_self();
143 if (mutex->__m_owner == self) {
144 mutex->__m_count++;
145 return 0;
146 }
147 __pthread_lock(&mutex->__m_lock, self);
148 mutex->__m_owner = self;
149 mutex->__m_count = 0;
150 return 0;
151 case PTHREAD_MUTEX_ERRORCHECK_NP:
152 self = thread_self();
153 if (mutex->__m_owner == self) return EDEADLK;
154 res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
155 if (res != 0)
156 {
157 mutex->__m_owner = self;
158 return 0;
159 }
160 return ETIMEDOUT;
161 case PTHREAD_MUTEX_TIMED_NP:
162 /* Only this type supports timed out lock. */
163 return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
164 ? 0 : ETIMEDOUT);
165 default:
166 return EINVAL;
167 }
168}
169
170int attribute_hidden __pthread_mutex_unlock(pthread_mutex_t * mutex)
171{
172 switch (mutex->__m_kind) {
173 case PTHREAD_MUTEX_ADAPTIVE_NP:
174 __pthread_unlock(&mutex->__m_lock);
175 return 0;
176 case PTHREAD_MUTEX_RECURSIVE_NP:
177 if (mutex->__m_owner != thread_self())
178 return EPERM;
179 if (mutex->__m_count > 0) {
180 mutex->__m_count--;
181 return 0;
182 }
183 mutex->__m_owner = NULL;
184 __pthread_unlock(&mutex->__m_lock);
185 return 0;
186 case PTHREAD_MUTEX_ERRORCHECK_NP:
187 if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
188 return EPERM;
189 mutex->__m_owner = NULL;
190 __pthread_alt_unlock(&mutex->__m_lock);
191 return 0;
192 case PTHREAD_MUTEX_TIMED_NP:
193 __pthread_alt_unlock(&mutex->__m_lock);
194 return 0;
195 default:
196 return EINVAL;
197 }
198}
199strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
200
201int attribute_hidden __pthread_mutexattr_init(pthread_mutexattr_t *attr)
202{
203 attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
204 return 0;
205}
206strong_alias(__pthread_mutexattr_init,pthread_mutexattr_init)
207
208int attribute_hidden __pthread_mutexattr_destroy(pthread_mutexattr_t *attr attribute_unused)
209{
210 return 0;
211}
212strong_alias(__pthread_mutexattr_destroy,pthread_mutexattr_destroy)
213
214int attribute_hidden __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
215{
216 if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
217 && kind != PTHREAD_MUTEX_RECURSIVE_NP
218 && kind != PTHREAD_MUTEX_ERRORCHECK_NP
219 && kind != PTHREAD_MUTEX_TIMED_NP)
220 return EINVAL;
221 attr->__mutexkind = kind;
222 return 0;
223}
224strong_alias(__pthread_mutexattr_settype,pthread_mutexattr_settype)
225strong_alias (__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
226weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
227
228int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind) attribute_hidden;
229int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
230{
231 *kind = attr->__mutexkind;
232 return 0;
233}
234weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
235strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
236weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
237
238int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr attribute_unused,
239 int *pshared) attribute_hidden;
240int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr attribute_unused,
241 int *pshared)
242{
243 *pshared = PTHREAD_PROCESS_PRIVATE;
244 return 0;
245}
246weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
247
248int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr attribute_unused, int pshared) attribute_hidden;
249int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr attribute_unused, int pshared)
250{
251 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
252 return EINVAL;
253
254 /* For now it is not possible to shared a conditional variable. */
255 if (pshared != PTHREAD_PROCESS_PRIVATE)
256 return ENOSYS;
257
258 return 0;
259}
260weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
261
262/* Once-only execution */
263
264static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
265static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
266static int fork_generation = 0; /* Child process increments this after fork. */
267
268enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
269
270/* If a thread is canceled while calling the init_routine out of
271 pthread once, this handler will reset the once_control variable
272 to the NEVER state. */
273
274static void pthread_once_cancelhandler(void *arg)
275{
276 pthread_once_t *once_control = arg;
277
278 __pthread_mutex_lock(&once_masterlock);
279 *once_control = NEVER;
280 __pthread_mutex_unlock(&once_masterlock);
281 pthread_cond_broadcast(&once_finished);
282}
283
284int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
285{
286 /* flag for doing the condition broadcast outside of mutex */
287 int state_changed;
288
289 /* Test without locking first for speed */
290 if (*once_control == DONE) {
291 READ_MEMORY_BARRIER();
292 return 0;
293 }
294 /* Lock and test again */
295
296 state_changed = 0;
297
298 __pthread_mutex_lock(&once_masterlock);
299
300 /* If this object was left in an IN_PROGRESS state in a parent
301 process (indicated by stale generation field), reset it to NEVER. */
302 if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
303 *once_control = NEVER;
304
305 /* If init_routine is being called from another routine, wait until
306 it completes. */
307 while ((*once_control & 3) == IN_PROGRESS) {
308 pthread_cond_wait(&once_finished, &once_masterlock);
309 }
310 /* Here *once_control is stable and either NEVER or DONE. */
311 if (*once_control == NEVER) {
312 *once_control = IN_PROGRESS | fork_generation;
313 __pthread_mutex_unlock(&once_masterlock);
314 pthread_cleanup_push(pthread_once_cancelhandler, once_control);
315 init_routine();
316 pthread_cleanup_pop(0);
317 __pthread_mutex_lock(&once_masterlock);
318 WRITE_MEMORY_BARRIER();
319 *once_control = DONE;
320 state_changed = 1;
321 }
322 __pthread_mutex_unlock(&once_masterlock);
323
324 if (state_changed)
325 pthread_cond_broadcast(&once_finished);
326
327 return 0;
328}
329strong_alias (__pthread_once, pthread_once)
330
331/*
332 * Handle the state of the pthread_once mechanism across forks. The
333 * once_masterlock is acquired in the parent process prior to a fork to ensure
334 * that no thread is in the critical region protected by the lock. After the
335 * fork, the lock is released. In the child, the lock and the condition
336 * variable are simply reset. The child also increments its generation
337 * counter which lets pthread_once calls detect stale IN_PROGRESS states
338 * and reset them back to NEVER.
339 */
340
341void __pthread_once_fork_prepare(void);
342void __pthread_once_fork_prepare(void)
343{
344 __pthread_mutex_lock(&once_masterlock);
345}
346
347void __pthread_once_fork_parent(void);
348void __pthread_once_fork_parent(void)
349{
350 __pthread_mutex_unlock(&once_masterlock);
351}
352
353void __pthread_once_fork_child(void);
354void __pthread_once_fork_child(void)
355{
356 __pthread_mutex_init(&once_masterlock, NULL);
357 pthread_cond_init(&once_finished, NULL);
358 if (fork_generation <= INT_MAX - 4)
359 fork_generation += 4; /* leave least significant two bits zero */
360 else
361 fork_generation = 0;
362}