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