]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/gthr-posix.h
Update copyright years.
[thirdparty/gcc.git] / libgcc / gthr-posix.h
CommitLineData
15794a95 1/* Threads compatibility routines for libgcc2 and libobjc. */
f24af81b 2/* Compile this one with gcc. */
a945c346 3/* Copyright (C) 1997-2024 Free Software Foundation, Inc.
f24af81b 4
1322177d 5This file is part of GCC.
f24af81b 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
748086b7 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
f24af81b 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
f24af81b 16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
f24af81b 25
88657302
RH
26#ifndef GCC_GTHR_POSIX_H
27#define GCC_GTHR_POSIX_H
f24af81b
TT
28
29/* POSIX threads specific definitions.
71287280 30 Easy, since the interface is just one-to-one mapping. */
f24af81b
TT
31
32#define __GTHREADS 1
a75150ae 33#define __GTHREADS_CXX0X 1
f24af81b
TT
34
35#include <pthread.h>
6545e33e
PC
36
37#if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
38 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
39# include <unistd.h>
40# if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
41# define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
42# else
43# define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
44# endif
45#endif
f24af81b 46
a75150ae 47typedef pthread_t __gthread_t;
f24af81b
TT
48typedef pthread_key_t __gthread_key_t;
49typedef pthread_once_t __gthread_once_t;
50typedef pthread_mutex_t __gthread_mutex_t;
b806c88f
LZ
51#ifndef __cplusplus
52typedef pthread_rwlock_t __gthread_rwlock_t;
53#endif
40aac948 54typedef pthread_mutex_t __gthread_recursive_mutex_t;
afd82ef5 55typedef pthread_cond_t __gthread_cond_t;
a75150ae 56typedef struct timespec __gthread_time_t;
afd82ef5
DK
57
58/* POSIX like conditional variables are supported. Please look at comments
59 in gthr.h for details. */
b8698a0f 60#define __GTHREAD_HAS_COND 1
f24af81b
TT
61
62#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
33e3e24d 63#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
b806c88f
LZ
64#ifndef __cplusplus
65#define __GTHREAD_RWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER
66#endif
f24af81b 67#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
40aac948
JM
68#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
69#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
70#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
71#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
e95dc99b 72#else
ab3d1049 73#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
40aac948 74#endif
afd82ef5 75#define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
a75150ae 76#define __GTHREAD_TIME_INIT {0,0}
f24af81b 77
b59cbd50
JW
78#ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
79# undef __GTHREAD_MUTEX_INIT
b59cbd50
JW
80#endif
81#ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
82# undef __GTHREAD_RECURSIVE_MUTEX_INIT
83# undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
84# define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
85#endif
86#ifdef _GTHREAD_USE_COND_INIT_FUNC
87# undef __GTHREAD_COND_INIT
88# define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
89#endif
90
f24af81b 91#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
b36e79e5
RO
92# ifndef __gthrw_pragma
93# define __gthrw_pragma(pragma)
94# endif
a11dd268 95# define __gthrw2(name,name2,type) \
29d24852
MS
96 static __typeof(type) name \
97 __attribute__ ((__weakref__(#name2), __copy__ (type))); \
b36e79e5 98 __gthrw_pragma(weak type)
72b16773 99# define __gthrw_(name) __gthrw_ ## name
7ef67393 100#else
a11dd268 101# define __gthrw2(name,name2,type)
72b16773 102# define __gthrw_(name) name
7ef67393 103#endif
f24af81b 104
efbbbb61 105/* Typically, __gthrw_foo is a weak reference to symbol foo. */
a11dd268 106#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
c2a8530e 107
72b16773 108__gthrw(pthread_once)
72b16773
AO
109__gthrw(pthread_getspecific)
110__gthrw(pthread_setspecific)
a75150ae 111
72b16773 112__gthrw(pthread_create)
a75150ae
CF
113__gthrw(pthread_join)
114__gthrw(pthread_equal)
115__gthrw(pthread_self)
116__gthrw(pthread_detach)
3714764d 117#ifndef __BIONIC__
72b16773 118__gthrw(pthread_cancel)
3714764d 119#endif
a75150ae
CF
120__gthrw(sched_yield)
121
72b16773
AO
122__gthrw(pthread_mutex_lock)
123__gthrw(pthread_mutex_trylock)
6545e33e 124#if _GTHREAD_USE_MUTEX_TIMEDLOCK
a75150ae 125__gthrw(pthread_mutex_timedlock)
1cd8b853 126#endif
72b16773 127__gthrw(pthread_mutex_unlock)
c2a8530e 128__gthrw(pthread_mutex_init)
4dabf736 129__gthrw(pthread_mutex_destroy)
a75150ae 130
b59cbd50 131__gthrw(pthread_cond_init)
afd82ef5 132__gthrw(pthread_cond_broadcast)
a75150ae 133__gthrw(pthread_cond_signal)
afd82ef5 134__gthrw(pthread_cond_wait)
a75150ae
CF
135__gthrw(pthread_cond_timedwait)
136__gthrw(pthread_cond_destroy)
c2a8530e
RS
137
138__gthrw(pthread_key_create)
139__gthrw(pthread_key_delete)
72b16773
AO
140__gthrw(pthread_mutexattr_init)
141__gthrw(pthread_mutexattr_settype)
142__gthrw(pthread_mutexattr_destroy)
70008293 143
b806c88f
LZ
144#ifndef __cplusplus
145__gthrw(pthread_rwlock_rdlock)
146__gthrw(pthread_rwlock_tryrdlock)
147__gthrw(pthread_rwlock_wrlock)
148__gthrw(pthread_rwlock_trywrlock)
149__gthrw(pthread_rwlock_unlock)
150#endif
15794a95 151
2a4e8ebc 152#if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
40f03658 153/* Objective-C. */
72b16773 154__gthrw(pthread_exit)
45863ba3 155#ifdef _POSIX_PRIORITY_SCHEDULING
c95d07f8 156#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
72b16773
AO
157__gthrw(sched_get_priority_max)
158__gthrw(sched_get_priority_min)
c95d07f8 159#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
45863ba3 160#endif /* _POSIX_PRIORITY_SCHEDULING */
72b16773
AO
161__gthrw(pthread_attr_destroy)
162__gthrw(pthread_attr_init)
163__gthrw(pthread_attr_setdetachstate)
c95d07f8 164#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
72b16773
AO
165__gthrw(pthread_getschedparam)
166__gthrw(pthread_setschedparam)
c95d07f8 167#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
2a4e8ebc 168#endif /* _LIBOBJC || _LIBOBJC_WEAK */
f24af81b 169
7ef67393
AO
170#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
171
efd6ef80
EB
172/* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
173 -pthreads is not specified. The functions are dummies and most return an
174 error value. However pthread_once returns 0 without invoking the routine
175 it is passed so we cannot pretend that the interface is active if -pthreads
176 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
177 we need to play the usual game with weak symbols. On Solaris 10 and up, a
799cff46
MS
178 working interface is always exposed. On FreeBSD 6 and later, libc also
179 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
180 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
181 which means the alternate __gthread_active_p below cannot be used there. */
efd6ef80 182
799cff46 183#if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
efd6ef80
EB
184
185static volatile int __gthread_active = -1;
186
187static void
188__gthread_trigger (void)
189{
190 __gthread_active = 1;
191}
192
193static inline int
194__gthread_active_p (void)
195{
196 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
198
199 /* Avoid reading __gthread_active twice on the main code path. */
200 int __gthread_active_latest_value = __gthread_active;
201
202 /* This test is not protected to avoid taking a lock on the main code
203 path so every update of __gthread_active in a threaded program must
204 be atomic with regard to the result of the test. */
205 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
206 {
207 if (__gthrw_(pthread_once))
208 {
209 /* If this really is a threaded program, then we must ensure that
210 __gthread_active has been set to 1 before exiting this block. */
211 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
212 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
213 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
214 }
215
216 /* Make sure we'll never enter this block again. */
217 if (__gthread_active < 0)
218 __gthread_active = 0;
219
220 __gthread_active_latest_value = __gthread_active;
221 }
222
223 return __gthread_active_latest_value != 0;
224}
225
799cff46 226#else /* neither FreeBSD nor Solaris */
efd6ef80 227
67a218c5
RM
228/* For a program to be multi-threaded the only thing that it certainly must
229 be using is pthread_create. However, there may be other libraries that
230 intercept pthread_create with their own definitions to wrap pthreads
231 functionality for some purpose. In those cases, pthread_create being
232 defined might not necessarily mean that libpthread is actually linked
233 in.
234
235 For the GNU C library, we can use a known internal name. This is always
236 available in the ABI, but no other library would define it. That is
237 ideal, since any public pthread function might be intercepted just as
238 pthread_create might be. __pthread_key_create is an "internal"
239 implementation symbol, but it is part of the public exported ABI. Also,
240 it's among the symbols that the static libpthread.a always links in
241 whenever pthread_create is used, so there is no danger of a false
242 negative result in any statically-linked, multi-threaded program.
243
244 For others, we choose pthread_cancel as a function that seems unlikely
245 to be redefined by an interceptor library. The bionic (Android) C
246 library does not provide pthread_cancel, so we do use pthread_create
247 there (and interceptor libraries lose). */
248
249#ifdef __GLIBC__
250__gthrw2(__gthrw_(__pthread_key_create),
251 __pthread_key_create,
252 pthread_key_create)
253# define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
254#elif defined (__BIONIC__)
255# define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
256#else
257# define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
258#endif
259
f24af81b 260static inline int
d1e51320 261__gthread_active_p (void)
f24af81b 262{
e621f530 263 static void *const __gthread_active_ptr
67a218c5 264 = __extension__ (void *) &GTHR_ACTIVE_PROXY;
f24af81b
TT
265 return __gthread_active_ptr != 0;
266}
267
799cff46 268#endif /* FreeBSD or Solaris */
efd6ef80 269
f24af81b
TT
270#else /* not SUPPORTS_WEAK */
271
f74eeed4
JDA
272/* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
273 calls in shared flavors of the HP-UX C library. Most of the stubs
274 have no functionality. The details are described in the "libc cumulative
275 patch" for each subversion of HP-UX 11. There are two special interfaces
8c157f90 276 provided for checking whether an application is linked to a shared pthread
f74eeed4 277 library or not. However, these interfaces aren't available in early
8c157f90
JDA
278 libpthread libraries. We also need a test that works for archive
279 libraries. We can't use pthread_once as some libc versions call the
280 init function. We also can't use pthread_create or pthread_attr_init
281 as these create a thread and thereby prevent changing the default stack
282 size. The function pthread_default_stacksize_np is available in both
283 the archive and shared versions of libpthread. It can be used to
284 determine the default pthread stack size. There is a stub in some
285 shared libc versions which returns a zero size if pthreads are not
286 active. We provide an equivalent stub to handle cases where libc
287 doesn't provide one. */
f74eeed4
JDA
288
289#if defined(__hppa__) && defined(__hpux__)
290
f74eeed4
JDA
291static volatile int __gthread_active = -1;
292
f74eeed4
JDA
293static inline int
294__gthread_active_p (void)
295{
296 /* Avoid reading __gthread_active twice on the main code path. */
297 int __gthread_active_latest_value = __gthread_active;
8c157f90 298 size_t __s;
f74eeed4 299
f74eeed4
JDA
300 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
301 {
8c157f90
JDA
302 pthread_default_stacksize_np (0, &__s);
303 __gthread_active = __s ? 1 : 0;
f74eeed4
JDA
304 __gthread_active_latest_value = __gthread_active;
305 }
306
307 return __gthread_active_latest_value != 0;
308}
309
310#else /* not hppa-hpux */
311
f24af81b 312static inline int
d1e51320 313__gthread_active_p (void)
f24af81b
TT
314{
315 return 1;
316}
317
f74eeed4
JDA
318#endif /* hppa-hpux */
319
f24af81b
TT
320#endif /* SUPPORTS_WEAK */
321
15794a95
L
322#ifdef _LIBOBJC
323
5f974826
OP
324/* This is the config.h file in libobjc/ */
325#include <config.h>
326
327#ifdef HAVE_SCHED_H
328# include <sched.h>
329#endif
330
15794a95
L
331/* Key structure for maintaining thread specific storage */
332static pthread_key_t _objc_thread_storage;
447c11a5 333static pthread_attr_t _objc_thread_attribs;
15794a95
L
334
335/* Thread local storage for a single thread */
336static void *thread_local_storage = NULL;
337
338/* Backend initialization functions */
339
71287280 340/* Initialize the threads subsystem. */
15794a95 341static inline int
fe83a9ce 342__gthread_objc_init_thread_system (void)
15794a95
L
343{
344 if (__gthread_active_p ())
447c11a5 345 {
ea4b7848 346 /* Initialize the thread storage key. */
72b16773 347 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
589005ff
KH
348 {
349 /* The normal default detach state for threads is
350 * PTHREAD_CREATE_JOINABLE which causes threads to not die
351 * when you think they should. */
72b16773
AO
352 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
353 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
fe83a9ce 354 PTHREAD_CREATE_DETACHED) == 0)
589005ff
KH
355 return 0;
356 }
447c11a5 357 }
abc0360c
NP
358
359 return -1;
15794a95
L
360}
361
71287280 362/* Close the threads subsystem. */
15794a95 363static inline int
fe83a9ce 364__gthread_objc_close_thread_system (void)
15794a95 365{
447c11a5 366 if (__gthread_active_p ()
72b16773
AO
367 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
368 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
15794a95 369 return 0;
447c11a5
OP
370
371 return -1;
15794a95
L
372}
373
374/* Backend thread functions */
375
71287280 376/* Create a new thread of execution. */
15794a95 377static inline objc_thread_t
fe83a9ce 378__gthread_objc_thread_detach (void (*func)(void *), void *arg)
15794a95
L
379{
380 objc_thread_t thread_id;
381 pthread_t new_thread_handle;
382
383 if (!__gthread_active_p ())
384 return NULL;
589005ff 385
708e07fb
NP
386 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
387 (void *) func, arg)))
fee013ef 388 thread_id = (objc_thread_t) new_thread_handle;
15794a95
L
389 else
390 thread_id = NULL;
589005ff 391
15794a95
L
392 return thread_id;
393}
394
71287280 395/* Set the current thread's priority. */
15794a95 396static inline int
fe83a9ce 397__gthread_objc_thread_set_priority (int priority)
15794a95 398{
fe83a9ce 399 if (!__gthread_active_p ())
447c11a5 400 return -1;
fe83a9ce
KH
401 else
402 {
45863ba3 403#ifdef _POSIX_PRIORITY_SCHEDULING
c95d07f8 404#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
72b16773 405 pthread_t thread_id = __gthrw_(pthread_self) ();
fe83a9ce
KH
406 int policy;
407 struct sched_param params;
408 int priority_min, priority_max;
589005ff 409
72b16773 410 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
fe83a9ce 411 {
72b16773 412 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
fe83a9ce
KH
413 return -1;
414
72b16773 415 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
fe83a9ce 416 return -1;
589005ff 417
fe83a9ce
KH
418 if (priority > priority_max)
419 priority = priority_max;
420 else if (priority < priority_min)
421 priority = priority_min;
422 params.sched_priority = priority;
423
424 /*
425 * The solaris 7 and several other man pages incorrectly state that
426 * this should be a pointer to policy but pthread.h is universally
427 * at odds with this.
428 */
72b16773 429 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
fe83a9ce
KH
430 return 0;
431 }
c95d07f8 432#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
45863ba3 433#endif /* _POSIX_PRIORITY_SCHEDULING */
fe83a9ce
KH
434 return -1;
435 }
15794a95
L
436}
437
71287280 438/* Return the current thread's priority. */
15794a95 439static inline int
fe83a9ce 440__gthread_objc_thread_get_priority (void)
15794a95 441{
45863ba3 442#ifdef _POSIX_PRIORITY_SCHEDULING
c95d07f8 443#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
15794a95 444 if (__gthread_active_p ())
447c11a5
OP
445 {
446 int policy;
447 struct sched_param params;
448
72b16773 449 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
589005ff 450 return params.sched_priority;
447c11a5 451 else
589005ff 452 return -1;
447c11a5 453 }
15794a95 454 else
c95d07f8 455#endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
45863ba3 456#endif /* _POSIX_PRIORITY_SCHEDULING */
15794a95
L
457 return OBJC_THREAD_INTERACTIVE_PRIORITY;
458}
459
71287280 460/* Yield our process time to another thread. */
15794a95 461static inline void
fe83a9ce 462__gthread_objc_thread_yield (void)
15794a95
L
463{
464 if (__gthread_active_p ())
72b16773 465 __gthrw_(sched_yield) ();
15794a95
L
466}
467
71287280 468/* Terminate the current thread. */
15794a95 469static inline int
fe83a9ce 470__gthread_objc_thread_exit (void)
15794a95
L
471{
472 if (__gthread_active_p ())
473 /* exit the thread */
72b16773 474 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
15794a95
L
475
476 /* Failed if we reached here */
477 return -1;
478}
479
71287280 480/* Returns an integer value which uniquely describes a thread. */
15794a95 481static inline objc_thread_t
fe83a9ce 482__gthread_objc_thread_id (void)
15794a95
L
483{
484 if (__gthread_active_p ())
72b16773 485 return (objc_thread_t) __gthrw_(pthread_self) ();
15794a95 486 else
fee013ef 487 return (objc_thread_t) 1;
15794a95
L
488}
489
71287280 490/* Sets the thread's local storage pointer. */
15794a95 491static inline int
fe83a9ce 492__gthread_objc_thread_set_data (void *value)
15794a95
L
493{
494 if (__gthread_active_p ())
72b16773 495 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
15794a95
L
496 else
497 {
498 thread_local_storage = value;
499 return 0;
500 }
501}
502
71287280 503/* Returns the thread's local storage pointer. */
15794a95 504static inline void *
fe83a9ce 505__gthread_objc_thread_get_data (void)
15794a95
L
506{
507 if (__gthread_active_p ())
72b16773 508 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
15794a95
L
509 else
510 return thread_local_storage;
511}
512
513/* Backend mutex functions */
514
71287280 515/* Allocate a mutex. */
15794a95 516static inline int
fe83a9ce 517__gthread_objc_mutex_allocate (objc_mutex_t mutex)
15794a95
L
518{
519 if (__gthread_active_p ())
520 {
fe83a9ce 521 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
15794a95 522
72b16773 523 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
15794a95 524 {
fe83a9ce 525 objc_free (mutex->backend);
15794a95
L
526 mutex->backend = NULL;
527 return -1;
528 }
529 }
530
531 return 0;
532}
533
71287280 534/* Deallocate a mutex. */
15794a95 535static inline int
fe83a9ce 536__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
15794a95
L
537{
538 if (__gthread_active_p ())
539 {
540 int count;
541
542 /*
543 * Posix Threads specifically require that the thread be unlocked
72b16773 544 * for __gthrw_(pthread_mutex_destroy) to work.
15794a95
L
545 */
546
547 do
548 {
72b16773 549 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
15794a95
L
550 if (count < 0)
551 return -1;
552 }
553 while (count);
554
72b16773 555 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
15794a95
L
556 return -1;
557
fe83a9ce 558 objc_free (mutex->backend);
15794a95
L
559 mutex->backend = NULL;
560 }
561 return 0;
562}
563
71287280 564/* Grab a lock on a mutex. */
15794a95 565static inline int
fe83a9ce 566__gthread_objc_mutex_lock (objc_mutex_t mutex)
15794a95 567{
589005ff 568 if (__gthread_active_p ()
72b16773 569 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
054af139
NP
570 {
571 return -1;
572 }
573
574 return 0;
15794a95
L
575}
576
71287280 577/* Try to grab a lock on a mutex. */
15794a95 578static inline int
fe83a9ce 579__gthread_objc_mutex_trylock (objc_mutex_t mutex)
15794a95 580{
589005ff 581 if (__gthread_active_p ()
72b16773 582 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
054af139
NP
583 {
584 return -1;
585 }
586
587 return 0;
15794a95
L
588}
589
590/* Unlock the mutex */
591static inline int
fe83a9ce 592__gthread_objc_mutex_unlock (objc_mutex_t mutex)
15794a95 593{
589005ff 594 if (__gthread_active_p ()
72b16773 595 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
054af139
NP
596 {
597 return -1;
598 }
599
600 return 0;
15794a95
L
601}
602
603/* Backend condition mutex functions */
604
71287280 605/* Allocate a condition. */
15794a95 606static inline int
fe83a9ce 607__gthread_objc_condition_allocate (objc_condition_t condition)
15794a95
L
608{
609 if (__gthread_active_p ())
610 {
fe83a9ce 611 condition->backend = objc_malloc (sizeof (pthread_cond_t));
15794a95 612
72b16773 613 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
15794a95 614 {
fe83a9ce 615 objc_free (condition->backend);
15794a95
L
616 condition->backend = NULL;
617 return -1;
618 }
619 }
620
621 return 0;
622}
623
71287280 624/* Deallocate a condition. */
15794a95 625static inline int
fe83a9ce 626__gthread_objc_condition_deallocate (objc_condition_t condition)
15794a95
L
627{
628 if (__gthread_active_p ())
629 {
72b16773 630 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
15794a95
L
631 return -1;
632
fe83a9ce 633 objc_free (condition->backend);
15794a95
L
634 condition->backend = NULL;
635 }
636 return 0;
637}
638
639/* Wait on the condition */
640static inline int
fe83a9ce 641__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
15794a95
L
642{
643 if (__gthread_active_p ())
72b16773 644 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
fe83a9ce 645 (pthread_mutex_t *) mutex->backend);
15794a95
L
646 else
647 return 0;
648}
649
71287280 650/* Wake up all threads waiting on this condition. */
15794a95 651static inline int
fe83a9ce 652__gthread_objc_condition_broadcast (objc_condition_t condition)
15794a95
L
653{
654 if (__gthread_active_p ())
72b16773 655 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
15794a95
L
656 else
657 return 0;
658}
659
71287280 660/* Wake up one thread waiting on this condition. */
15794a95 661static inline int
fe83a9ce 662__gthread_objc_condition_signal (objc_condition_t condition)
15794a95
L
663{
664 if (__gthread_active_p ())
72b16773 665 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
15794a95
L
666 else
667 return 0;
668}
669
670#else /* _LIBOBJC */
671
a75150ae 672static inline int
09e361bb
JJ
673__gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
674 void *__args)
a75150ae 675{
09e361bb 676 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
a75150ae
CF
677}
678
679static inline int
09e361bb 680__gthread_join (__gthread_t __threadid, void **__value_ptr)
a75150ae 681{
09e361bb 682 return __gthrw_(pthread_join) (__threadid, __value_ptr);
a75150ae
CF
683}
684
685static inline int
09e361bb 686__gthread_detach (__gthread_t __threadid)
a75150ae 687{
09e361bb 688 return __gthrw_(pthread_detach) (__threadid);
a75150ae
CF
689}
690
691static inline int
09e361bb 692__gthread_equal (__gthread_t __t1, __gthread_t __t2)
a75150ae 693{
09e361bb 694 return __gthrw_(pthread_equal) (__t1, __t2);
a75150ae
CF
695}
696
697static inline __gthread_t
698__gthread_self (void)
699{
700 return __gthrw_(pthread_self) ();
701}
702
703static inline int
704__gthread_yield (void)
705{
706 return __gthrw_(sched_yield) ();
707}
708
f24af81b 709static inline int
09e361bb 710__gthread_once (__gthread_once_t *__once, void (*__func) (void))
f24af81b
TT
711{
712 if (__gthread_active_p ())
09e361bb 713 return __gthrw_(pthread_once) (__once, __func);
f24af81b
TT
714 else
715 return -1;
716}
717
718static inline int
09e361bb 719__gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
f24af81b 720{
09e361bb 721 return __gthrw_(pthread_key_create) (__key, __dtor);
f24af81b
TT
722}
723
f24af81b 724static inline int
09e361bb 725__gthread_key_delete (__gthread_key_t __key)
f24af81b 726{
09e361bb 727 return __gthrw_(pthread_key_delete) (__key);
f24af81b
TT
728}
729
730static inline void *
09e361bb 731__gthread_getspecific (__gthread_key_t __key)
f24af81b 732{
09e361bb 733 return __gthrw_(pthread_getspecific) (__key);
f24af81b
TT
734}
735
736static inline int
09e361bb 737__gthread_setspecific (__gthread_key_t __key, const void *__ptr)
f24af81b 738{
09e361bb 739 return __gthrw_(pthread_setspecific) (__key, __ptr);
f24af81b
TT
740}
741
b59cbd50
JW
742static inline void
743__gthread_mutex_init_function (__gthread_mutex_t *__mutex)
744{
745 if (__gthread_active_p ())
746 __gthrw_(pthread_mutex_init) (__mutex, NULL);
747}
33e3e24d 748
4dabf736 749static inline int
09e361bb 750__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
4dabf736
JB
751{
752 if (__gthread_active_p ())
09e361bb 753 return __gthrw_(pthread_mutex_destroy) (__mutex);
4dabf736
JB
754 else
755 return 0;
756}
757
f24af81b 758static inline int
09e361bb 759__gthread_mutex_lock (__gthread_mutex_t *__mutex)
f24af81b
TT
760{
761 if (__gthread_active_p ())
09e361bb 762 return __gthrw_(pthread_mutex_lock) (__mutex);
f24af81b
TT
763 else
764 return 0;
765}
766
767static inline int
09e361bb 768__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
f24af81b
TT
769{
770 if (__gthread_active_p ())
09e361bb 771 return __gthrw_(pthread_mutex_trylock) (__mutex);
f24af81b
TT
772 else
773 return 0;
774}
775
6545e33e 776#if _GTHREAD_USE_MUTEX_TIMEDLOCK
a75150ae 777static inline int
09e361bb
JJ
778__gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
779 const __gthread_time_t *__abs_timeout)
a75150ae
CF
780{
781 if (__gthread_active_p ())
09e361bb 782 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
a75150ae
CF
783 else
784 return 0;
785}
786#endif
787
f24af81b 788static inline int
09e361bb 789__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
f24af81b
TT
790{
791 if (__gthread_active_p ())
09e361bb 792 return __gthrw_(pthread_mutex_unlock) (__mutex);
f24af81b
TT
793 else
794 return 0;
795}
796
b59cbd50
JW
797#if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
798 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
40aac948 799static inline int
09e361bb 800__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
40aac948
JM
801{
802 if (__gthread_active_p ())
803 {
09e361bb
JJ
804 pthread_mutexattr_t __attr;
805 int __r;
806
807 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
808 if (!__r)
809 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
810 PTHREAD_MUTEX_RECURSIVE);
811 if (!__r)
812 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
813 if (!__r)
814 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
815 return __r;
40aac948 816 }
7c68fabb 817 return 0;
40aac948
JM
818}
819#endif
820
821static inline int
09e361bb 822__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
40aac948 823{
09e361bb 824 return __gthread_mutex_lock (__mutex);
40aac948
JM
825}
826
827static inline int
09e361bb 828__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
40aac948 829{
09e361bb 830 return __gthread_mutex_trylock (__mutex);
40aac948
JM
831}
832
6545e33e 833#if _GTHREAD_USE_MUTEX_TIMEDLOCK
a75150ae 834static inline int
09e361bb
JJ
835__gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
836 const __gthread_time_t *__abs_timeout)
a75150ae 837{
09e361bb 838 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
a75150ae
CF
839}
840#endif
841
40aac948 842static inline int
09e361bb 843__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
40aac948 844{
09e361bb 845 return __gthread_mutex_unlock (__mutex);
40aac948
JM
846}
847
1504e3e1
JW
848static inline int
849__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
850{
851 return __gthread_mutex_destroy (__mutex);
852}
853
b59cbd50
JW
854#ifdef _GTHREAD_USE_COND_INIT_FUNC
855static inline void
856__gthread_cond_init_function (__gthread_cond_t *__cond)
857{
858 if (__gthread_active_p ())
859 __gthrw_(pthread_cond_init) (__cond, NULL);
860}
861#endif
862
afd82ef5 863static inline int
09e361bb 864__gthread_cond_broadcast (__gthread_cond_t *__cond)
afd82ef5 865{
09e361bb 866 return __gthrw_(pthread_cond_broadcast) (__cond);
afd82ef5
DK
867}
868
a75150ae 869static inline int
09e361bb 870__gthread_cond_signal (__gthread_cond_t *__cond)
a75150ae 871{
09e361bb 872 return __gthrw_(pthread_cond_signal) (__cond);
a75150ae
CF
873}
874
afd82ef5 875static inline int
09e361bb 876__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
afd82ef5 877{
09e361bb 878 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
afd82ef5
DK
879}
880
a75150ae 881static inline int
09e361bb
JJ
882__gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
883 const __gthread_time_t *__abs_timeout)
a75150ae 884{
09e361bb 885 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
a75150ae
CF
886}
887
afd82ef5 888static inline int
09e361bb
JJ
889__gthread_cond_wait_recursive (__gthread_cond_t *__cond,
890 __gthread_recursive_mutex_t *__mutex)
afd82ef5 891{
09e361bb 892 return __gthread_cond_wait (__cond, __mutex);
afd82ef5
DK
893}
894
a75150ae 895static inline int
09e361bb 896__gthread_cond_destroy (__gthread_cond_t* __cond)
a75150ae 897{
09e361bb 898 return __gthrw_(pthread_cond_destroy) (__cond);
a75150ae
CF
899}
900
b806c88f
LZ
901#ifndef __cplusplus
902static inline int
903__gthread_rwlock_rdlock (__gthread_rwlock_t *__rwlock)
904{
905 if (__gthread_active_p ())
906 return __gthrw_(pthread_rwlock_rdlock) (__rwlock);
907 else
908 return 0;
909}
910
911static inline int
912__gthread_rwlock_tryrdlock (__gthread_rwlock_t *__rwlock)
913{
914 if (__gthread_active_p ())
915 return __gthrw_(pthread_rwlock_tryrdlock) (__rwlock);
916 else
917 return 0;
918}
919
920static inline int
921__gthread_rwlock_wrlock (__gthread_rwlock_t *__rwlock)
922{
923 if (__gthread_active_p ())
924 return __gthrw_(pthread_rwlock_wrlock) (__rwlock);
925 else
926 return 0;
927}
928
929static inline int
930__gthread_rwlock_trywrlock (__gthread_rwlock_t *__rwlock)
931{
932 if (__gthread_active_p ())
933 return __gthrw_(pthread_rwlock_trywrlock) (__rwlock);
934 else
935 return 0;
936}
937
938static inline int
939__gthread_rwlock_unlock (__gthread_rwlock_t *__rwlock)
940{
941 if (__gthread_active_p ())
942 return __gthrw_(pthread_rwlock_unlock) (__rwlock);
943 else
944 return 0;
945}
946#endif
947
15794a95
L
948#endif /* _LIBOBJC */
949
88657302 950#endif /* ! GCC_GTHR_POSIX_H */