OSDN Git Service

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