]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gthr-posix.h
46054f6a7c212a054879952932d8e598da686674
[thirdparty/gcc.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 /* Android's C library does not provide pthread_cancel, check for
248 `pthread_create' instead. */
249 #ifndef __BIONIC__
250 static void *const __gthread_active_ptr
251 = __extension__ (void *) &__gthrw_(pthread_cancel);
252 #else
253 static void *const __gthread_active_ptr
254 = __extension__ (void *) &__gthrw_(pthread_create);
255 #endif
256 return __gthread_active_ptr != 0;
257 }
258
259 #endif /* FreeBSD or Solaris */
260
261 #else /* not SUPPORTS_WEAK */
262
263 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
264 calls in shared flavors of the HP-UX C library. Most of the stubs
265 have no functionality. The details are described in the "libc cumulative
266 patch" for each subversion of HP-UX 11. There are two special interfaces
267 provided for checking whether an application is linked to a shared pthread
268 library or not. However, these interfaces aren't available in early
269 libpthread libraries. We also need a test that works for archive
270 libraries. We can't use pthread_once as some libc versions call the
271 init function. We also can't use pthread_create or pthread_attr_init
272 as these create a thread and thereby prevent changing the default stack
273 size. The function pthread_default_stacksize_np is available in both
274 the archive and shared versions of libpthread. It can be used to
275 determine the default pthread stack size. There is a stub in some
276 shared libc versions which returns a zero size if pthreads are not
277 active. We provide an equivalent stub to handle cases where libc
278 doesn't provide one. */
279
280 #if defined(__hppa__) && defined(__hpux__)
281
282 static volatile int __gthread_active = -1;
283
284 static inline int
285 __gthread_active_p (void)
286 {
287 /* Avoid reading __gthread_active twice on the main code path. */
288 int __gthread_active_latest_value = __gthread_active;
289 size_t __s;
290
291 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
292 {
293 pthread_default_stacksize_np (0, &__s);
294 __gthread_active = __s ? 1 : 0;
295 __gthread_active_latest_value = __gthread_active;
296 }
297
298 return __gthread_active_latest_value != 0;
299 }
300
301 #else /* not hppa-hpux */
302
303 static inline int
304 __gthread_active_p (void)
305 {
306 return 1;
307 }
308
309 #endif /* hppa-hpux */
310
311 #endif /* SUPPORTS_WEAK */
312
313 #ifdef _LIBOBJC
314
315 /* This is the config.h file in libobjc/ */
316 #include <config.h>
317
318 #ifdef HAVE_SCHED_H
319 # include <sched.h>
320 #endif
321
322 /* Key structure for maintaining thread specific storage */
323 static pthread_key_t _objc_thread_storage;
324 static pthread_attr_t _objc_thread_attribs;
325
326 /* Thread local storage for a single thread */
327 static void *thread_local_storage = NULL;
328
329 /* Backend initialization functions */
330
331 /* Initialize the threads subsystem. */
332 static inline int
333 __gthread_objc_init_thread_system (void)
334 {
335 if (__gthread_active_p ())
336 {
337 /* Initialize the thread storage key. */
338 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
339 {
340 /* The normal default detach state for threads is
341 * PTHREAD_CREATE_JOINABLE which causes threads to not die
342 * when you think they should. */
343 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
344 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
345 PTHREAD_CREATE_DETACHED) == 0)
346 return 0;
347 }
348 }
349
350 return -1;
351 }
352
353 /* Close the threads subsystem. */
354 static inline int
355 __gthread_objc_close_thread_system (void)
356 {
357 if (__gthread_active_p ()
358 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
359 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
360 return 0;
361
362 return -1;
363 }
364
365 /* Backend thread functions */
366
367 /* Create a new thread of execution. */
368 static inline objc_thread_t
369 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
370 {
371 objc_thread_t thread_id;
372 pthread_t new_thread_handle;
373
374 if (!__gthread_active_p ())
375 return NULL;
376
377 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
378 (void *) func, arg)))
379 thread_id = (objc_thread_t) new_thread_handle;
380 else
381 thread_id = NULL;
382
383 return thread_id;
384 }
385
386 /* Set the current thread's priority. */
387 static inline int
388 __gthread_objc_thread_set_priority (int priority)
389 {
390 if (!__gthread_active_p ())
391 return -1;
392 else
393 {
394 #ifdef _POSIX_PRIORITY_SCHEDULING
395 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
396 pthread_t thread_id = __gthrw_(pthread_self) ();
397 int policy;
398 struct sched_param params;
399 int priority_min, priority_max;
400
401 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
402 {
403 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
404 return -1;
405
406 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
407 return -1;
408
409 if (priority > priority_max)
410 priority = priority_max;
411 else if (priority < priority_min)
412 priority = priority_min;
413 params.sched_priority = priority;
414
415 /*
416 * The solaris 7 and several other man pages incorrectly state that
417 * this should be a pointer to policy but pthread.h is universally
418 * at odds with this.
419 */
420 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
421 return 0;
422 }
423 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
424 #endif /* _POSIX_PRIORITY_SCHEDULING */
425 return -1;
426 }
427 }
428
429 /* Return the current thread's priority. */
430 static inline int
431 __gthread_objc_thread_get_priority (void)
432 {
433 #ifdef _POSIX_PRIORITY_SCHEDULING
434 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
435 if (__gthread_active_p ())
436 {
437 int policy;
438 struct sched_param params;
439
440 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
441 return params.sched_priority;
442 else
443 return -1;
444 }
445 else
446 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
447 #endif /* _POSIX_PRIORITY_SCHEDULING */
448 return OBJC_THREAD_INTERACTIVE_PRIORITY;
449 }
450
451 /* Yield our process time to another thread. */
452 static inline void
453 __gthread_objc_thread_yield (void)
454 {
455 if (__gthread_active_p ())
456 __gthrw_(sched_yield) ();
457 }
458
459 /* Terminate the current thread. */
460 static inline int
461 __gthread_objc_thread_exit (void)
462 {
463 if (__gthread_active_p ())
464 /* exit the thread */
465 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
466
467 /* Failed if we reached here */
468 return -1;
469 }
470
471 /* Returns an integer value which uniquely describes a thread. */
472 static inline objc_thread_t
473 __gthread_objc_thread_id (void)
474 {
475 if (__gthread_active_p ())
476 return (objc_thread_t) __gthrw_(pthread_self) ();
477 else
478 return (objc_thread_t) 1;
479 }
480
481 /* Sets the thread's local storage pointer. */
482 static inline int
483 __gthread_objc_thread_set_data (void *value)
484 {
485 if (__gthread_active_p ())
486 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
487 else
488 {
489 thread_local_storage = value;
490 return 0;
491 }
492 }
493
494 /* Returns the thread's local storage pointer. */
495 static inline void *
496 __gthread_objc_thread_get_data (void)
497 {
498 if (__gthread_active_p ())
499 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
500 else
501 return thread_local_storage;
502 }
503
504 /* Backend mutex functions */
505
506 /* Allocate a mutex. */
507 static inline int
508 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
509 {
510 if (__gthread_active_p ())
511 {
512 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
513
514 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
515 {
516 objc_free (mutex->backend);
517 mutex->backend = NULL;
518 return -1;
519 }
520 }
521
522 return 0;
523 }
524
525 /* Deallocate a mutex. */
526 static inline int
527 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
528 {
529 if (__gthread_active_p ())
530 {
531 int count;
532
533 /*
534 * Posix Threads specifically require that the thread be unlocked
535 * for __gthrw_(pthread_mutex_destroy) to work.
536 */
537
538 do
539 {
540 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
541 if (count < 0)
542 return -1;
543 }
544 while (count);
545
546 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
547 return -1;
548
549 objc_free (mutex->backend);
550 mutex->backend = NULL;
551 }
552 return 0;
553 }
554
555 /* Grab a lock on a mutex. */
556 static inline int
557 __gthread_objc_mutex_lock (objc_mutex_t mutex)
558 {
559 if (__gthread_active_p ()
560 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
561 {
562 return -1;
563 }
564
565 return 0;
566 }
567
568 /* Try to grab a lock on a mutex. */
569 static inline int
570 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
571 {
572 if (__gthread_active_p ()
573 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
574 {
575 return -1;
576 }
577
578 return 0;
579 }
580
581 /* Unlock the mutex */
582 static inline int
583 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
584 {
585 if (__gthread_active_p ()
586 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
587 {
588 return -1;
589 }
590
591 return 0;
592 }
593
594 /* Backend condition mutex functions */
595
596 /* Allocate a condition. */
597 static inline int
598 __gthread_objc_condition_allocate (objc_condition_t condition)
599 {
600 if (__gthread_active_p ())
601 {
602 condition->backend = objc_malloc (sizeof (pthread_cond_t));
603
604 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
605 {
606 objc_free (condition->backend);
607 condition->backend = NULL;
608 return -1;
609 }
610 }
611
612 return 0;
613 }
614
615 /* Deallocate a condition. */
616 static inline int
617 __gthread_objc_condition_deallocate (objc_condition_t condition)
618 {
619 if (__gthread_active_p ())
620 {
621 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
622 return -1;
623
624 objc_free (condition->backend);
625 condition->backend = NULL;
626 }
627 return 0;
628 }
629
630 /* Wait on the condition */
631 static inline int
632 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
633 {
634 if (__gthread_active_p ())
635 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
636 (pthread_mutex_t *) mutex->backend);
637 else
638 return 0;
639 }
640
641 /* Wake up all threads waiting on this condition. */
642 static inline int
643 __gthread_objc_condition_broadcast (objc_condition_t condition)
644 {
645 if (__gthread_active_p ())
646 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
647 else
648 return 0;
649 }
650
651 /* Wake up one thread waiting on this condition. */
652 static inline int
653 __gthread_objc_condition_signal (objc_condition_t condition)
654 {
655 if (__gthread_active_p ())
656 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
657 else
658 return 0;
659 }
660
661 #else /* _LIBOBJC */
662
663 static inline int
664 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
665 void *__args)
666 {
667 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
668 }
669
670 static inline int
671 __gthread_join (__gthread_t __threadid, void **__value_ptr)
672 {
673 return __gthrw_(pthread_join) (__threadid, __value_ptr);
674 }
675
676 static inline int
677 __gthread_detach (__gthread_t __threadid)
678 {
679 return __gthrw_(pthread_detach) (__threadid);
680 }
681
682 static inline int
683 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
684 {
685 return __gthrw_(pthread_equal) (__t1, __t2);
686 }
687
688 static inline __gthread_t
689 __gthread_self (void)
690 {
691 return __gthrw_(pthread_self) ();
692 }
693
694 static inline int
695 __gthread_yield (void)
696 {
697 return __gthrw_(sched_yield) ();
698 }
699
700 static inline int
701 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
702 {
703 if (__gthread_active_p ())
704 return __gthrw_(pthread_once) (__once, __func);
705 else
706 return -1;
707 }
708
709 static inline int
710 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
711 {
712 return __gthrw_(pthread_key_create) (__key, __dtor);
713 }
714
715 static inline int
716 __gthread_key_delete (__gthread_key_t __key)
717 {
718 return __gthrw_(pthread_key_delete) (__key);
719 }
720
721 static inline void *
722 __gthread_getspecific (__gthread_key_t __key)
723 {
724 return __gthrw_(pthread_getspecific) (__key);
725 }
726
727 static inline int
728 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
729 {
730 return __gthrw_(pthread_setspecific) (__key, __ptr);
731 }
732
733 static inline int
734 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
735 {
736 if (__gthread_active_p ())
737 return __gthrw_(pthread_mutex_destroy) (__mutex);
738 else
739 return 0;
740 }
741
742 static inline int
743 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
744 {
745 if (__gthread_active_p ())
746 return __gthrw_(pthread_mutex_lock) (__mutex);
747 else
748 return 0;
749 }
750
751 static inline int
752 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
753 {
754 if (__gthread_active_p ())
755 return __gthrw_(pthread_mutex_trylock) (__mutex);
756 else
757 return 0;
758 }
759
760 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
761 static inline int
762 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
763 const __gthread_time_t *__abs_timeout)
764 {
765 if (__gthread_active_p ())
766 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
767 else
768 return 0;
769 }
770 #endif
771
772 static inline int
773 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
774 {
775 if (__gthread_active_p ())
776 return __gthrw_(pthread_mutex_unlock) (__mutex);
777 else
778 return 0;
779 }
780
781 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
782 static inline int
783 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
784 {
785 if (__gthread_active_p ())
786 {
787 pthread_mutexattr_t __attr;
788 int __r;
789
790 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
791 if (!__r)
792 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
793 PTHREAD_MUTEX_RECURSIVE);
794 if (!__r)
795 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
796 if (!__r)
797 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
798 return __r;
799 }
800 return 0;
801 }
802 #endif
803
804 static inline int
805 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
806 {
807 return __gthread_mutex_lock (__mutex);
808 }
809
810 static inline int
811 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
812 {
813 return __gthread_mutex_trylock (__mutex);
814 }
815
816 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
817 static inline int
818 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
819 const __gthread_time_t *__abs_timeout)
820 {
821 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
822 }
823 #endif
824
825 static inline int
826 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
827 {
828 return __gthread_mutex_unlock (__mutex);
829 }
830
831 static inline int
832 __gthread_cond_broadcast (__gthread_cond_t *__cond)
833 {
834 return __gthrw_(pthread_cond_broadcast) (__cond);
835 }
836
837 static inline int
838 __gthread_cond_signal (__gthread_cond_t *__cond)
839 {
840 return __gthrw_(pthread_cond_signal) (__cond);
841 }
842
843 static inline int
844 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
845 {
846 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
847 }
848
849 static inline int
850 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
851 const __gthread_time_t *__abs_timeout)
852 {
853 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
854 }
855
856 static inline int
857 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
858 __gthread_recursive_mutex_t *__mutex)
859 {
860 return __gthread_cond_wait (__cond, __mutex);
861 }
862
863 static inline int
864 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
865 __gthread_recursive_mutex_t *__mutex,
866 const __gthread_time_t *__abs_timeout)
867 {
868 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
869 }
870
871 static inline int
872 __gthread_cond_destroy (__gthread_cond_t* __cond)
873 {
874 return __gthrw_(pthread_cond_destroy) (__cond);
875 }
876
877 #endif /* _LIBOBJC */
878
879 #endif /* ! GCC_GTHR_POSIX_H */