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