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