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