Merge with gcc-4_3-branch up to revision 175516.
[official-gcc.git] / gcc / gthr-posix95.h
blob66a00af9e3239ff995c65b0e0a7a26cd74e74d83
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 #ifndef GCC_GTHR_POSIX_H
30 #define GCC_GTHR_POSIX_H
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
35 #define __GTHREADS 1
37 /* Some implementations of <pthread.h> require this to be defined. */
38 #ifndef _REENTRANT
39 #define _REENTRANT 1
40 #endif
42 #include <pthread.h>
43 #include <unistd.h>
45 typedef pthread_key_t __gthread_key_t;
46 typedef pthread_once_t __gthread_once_t;
47 typedef pthread_mutex_t __gthread_mutex_t;
48 typedef pthread_cond_t __gthread_cond_t;
50 /* POSIX like conditional variables are supported. Please look at comments
51 in gthr.h for details. */
52 #define __GTHREAD_HAS_COND 1
54 typedef struct {
55 long depth;
56 pthread_t owner;
57 pthread_mutex_t actual;
58 } __gthread_recursive_mutex_t;
60 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
61 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
62 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
63 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
65 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
66 # define __gthrw(name) \
67 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
68 # define __gthrw_(name) __gthrw_ ## name
69 #else
70 # define __gthrw(name)
71 # define __gthrw_(name) name
72 #endif
74 __gthrw(pthread_once)
75 __gthrw(pthread_key_create)
76 __gthrw(pthread_key_delete)
77 __gthrw(pthread_getspecific)
78 __gthrw(pthread_setspecific)
79 __gthrw(pthread_create)
80 __gthrw(pthread_cancel)
81 __gthrw(pthread_self)
83 __gthrw(pthread_mutex_lock)
84 __gthrw(pthread_mutex_trylock)
85 __gthrw(pthread_mutex_unlock)
86 __gthrw(pthread_mutexattr_init)
87 __gthrw(pthread_mutexattr_destroy)
89 __gthrw(pthread_mutex_init)
90 __gthrw(pthread_cond_broadcast)
91 __gthrw(pthread_cond_wait)
93 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
94 /* Objective-C. */
95 __gthrw(pthread_cond_destroy)
96 __gthrw(pthread_cond_init)
97 __gthrw(pthread_cond_signal)
98 __gthrw(pthread_exit)
99 __gthrw(pthread_mutex_destroy)
100 #ifdef _POSIX_PRIORITY_SCHEDULING
101 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
102 __gthrw(sched_get_priority_max)
103 __gthrw(sched_get_priority_min)
104 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
105 #endif /* _POSIX_PRIORITY_SCHEDULING */
106 __gthrw(sched_yield)
107 __gthrw(pthread_attr_destroy)
108 __gthrw(pthread_attr_init)
109 __gthrw(pthread_attr_setdetachstate)
110 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
111 __gthrw(pthread_getschedparam)
112 __gthrw(pthread_setschedparam)
113 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
114 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
116 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
118 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
119 -pthreads is not specified. The functions are dummies and most return an
120 error value. However pthread_once returns 0 without invoking the routine
121 it is passed so we cannot pretend that the interface is active if -pthreads
122 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
123 we need to play the usual game with weak symbols. On Solaris 10 and up, a
124 working interface is always exposed. On FreeBSD 6 and later, libc also
125 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
126 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
127 which means the alternate __gthread_active_p below cannot be used there. */
129 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
131 static volatile int __gthread_active = -1;
133 static void
134 __gthread_trigger (void)
136 __gthread_active = 1;
139 static inline int
140 __gthread_active_p (void)
142 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
143 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
145 /* Avoid reading __gthread_active twice on the main code path. */
146 int __gthread_active_latest_value = __gthread_active;
148 /* This test is not protected to avoid taking a lock on the main code
149 path so every update of __gthread_active in a threaded program must
150 be atomic with regard to the result of the test. */
151 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
153 if (__gthrw_(pthread_once))
155 /* If this really is a threaded program, then we must ensure that
156 __gthread_active has been set to 1 before exiting this block. */
157 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
158 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
159 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
162 /* Make sure we'll never enter this block again. */
163 if (__gthread_active < 0)
164 __gthread_active = 0;
166 __gthread_active_latest_value = __gthread_active;
169 return __gthread_active_latest_value != 0;
172 #else /* neither FreeBSD nor Solaris */
174 static inline int
175 __gthread_active_p (void)
177 static void *const __gthread_active_ptr
178 = __extension__ (void *) &__gthrw_(pthread_cancel);
179 return __gthread_active_ptr != 0;
182 #endif /* FreeBSD or Solaris */
184 #else /* not SUPPORTS_WEAK */
186 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
187 calls in shared flavors of the HP-UX C library. Most of the stubs
188 have no functionality. The details are described in the "libc cumulative
189 patch" for each subversion of HP-UX 11. There are two special interfaces
190 provided for checking whether an application is linked to a shared pthread
191 library or not. However, these interfaces aren't available in early
192 pthread libraries. We also need a test that works for archive
193 libraries. We can't use pthread_once as some libc versions call the
194 init function. We also can't use pthread_create or pthread_attr_init
195 as these create a thread and thereby prevent changing the default stack
196 size. The function pthread_default_stacksize_np is available in both
197 the archive and shared versions of libpthread. It can be used to
198 determine the default pthread stack size. There is a stub in some
199 shared libc versions which returns a zero size if pthreads are not
200 active. We provide an equivalent stub to handle cases where libc
201 doesn't provide one. */
203 #if defined(__hppa__) && defined(__hpux__)
205 static volatile int __gthread_active = -1;
207 static inline int
208 __gthread_active_p (void)
210 /* Avoid reading __gthread_active twice on the main code path. */
211 int __gthread_active_latest_value = __gthread_active;
212 size_t __s;
214 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
216 pthread_default_stacksize_np (0, &__s);
217 __gthread_active = __s ? 1 : 0;
218 __gthread_active_latest_value = __gthread_active;
221 return __gthread_active_latest_value != 0;
224 #else /* not hppa-hpux */
226 static inline int
227 __gthread_active_p (void)
229 return 1;
232 #endif /* hppa-hpux */
234 #endif /* SUPPORTS_WEAK */
236 #ifdef _LIBOBJC
238 /* This is the config.h file in libobjc/ */
239 #include <config.h>
241 #ifdef HAVE_SCHED_H
242 # include <sched.h>
243 #endif
245 /* Key structure for maintaining thread specific storage */
246 static pthread_key_t _objc_thread_storage;
247 static pthread_attr_t _objc_thread_attribs;
249 /* Thread local storage for a single thread */
250 static void *thread_local_storage = NULL;
252 /* Backend initialization functions */
254 /* Initialize the threads subsystem. */
255 static inline int
256 __gthread_objc_init_thread_system (void)
258 if (__gthread_active_p ())
260 /* Initialize the thread storage key. */
261 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
263 /* The normal default detach state for threads is
264 * PTHREAD_CREATE_JOINABLE which causes threads to not die
265 * when you think they should. */
266 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
267 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
268 PTHREAD_CREATE_DETACHED) == 0)
269 return 0;
273 return -1;
276 /* Close the threads subsystem. */
277 static inline int
278 __gthread_objc_close_thread_system (void)
280 if (__gthread_active_p ()
281 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
282 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
283 return 0;
285 return -1;
288 /* Backend thread functions */
290 /* Create a new thread of execution. */
291 static inline objc_thread_t
292 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
294 objc_thread_t thread_id;
295 pthread_t new_thread_handle;
297 if (!__gthread_active_p ())
298 return NULL;
300 if (!(__gthrw_(pthread_create) (&new_thread_handle, NULL, (void *) func, arg)))
301 thread_id = (objc_thread_t) new_thread_handle;
302 else
303 thread_id = NULL;
305 return thread_id;
308 /* Set the current thread's priority. */
309 static inline int
310 __gthread_objc_thread_set_priority (int priority)
312 if (!__gthread_active_p ())
313 return -1;
314 else
316 #ifdef _POSIX_PRIORITY_SCHEDULING
317 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
318 pthread_t thread_id = __gthrw_(pthread_self) ();
319 int policy;
320 struct sched_param params;
321 int priority_min, priority_max;
323 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
325 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
326 return -1;
328 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
329 return -1;
331 if (priority > priority_max)
332 priority = priority_max;
333 else if (priority < priority_min)
334 priority = priority_min;
335 params.sched_priority = priority;
338 * The solaris 7 and several other man pages incorrectly state that
339 * this should be a pointer to policy but pthread.h is universally
340 * at odds with this.
342 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
343 return 0;
345 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
346 #endif /* _POSIX_PRIORITY_SCHEDULING */
347 return -1;
351 /* Return the current thread's priority. */
352 static inline int
353 __gthread_objc_thread_get_priority (void)
355 #ifdef _POSIX_PRIORITY_SCHEDULING
356 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
357 if (__gthread_active_p ())
359 int policy;
360 struct sched_param params;
362 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
363 return params.sched_priority;
364 else
365 return -1;
367 else
368 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
369 #endif /* _POSIX_PRIORITY_SCHEDULING */
370 return OBJC_THREAD_INTERACTIVE_PRIORITY;
373 /* Yield our process time to another thread. */
374 static inline void
375 __gthread_objc_thread_yield (void)
377 if (__gthread_active_p ())
378 __gthrw_(sched_yield) ();
381 /* Terminate the current thread. */
382 static inline int
383 __gthread_objc_thread_exit (void)
385 if (__gthread_active_p ())
386 /* exit the thread */
387 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
389 /* Failed if we reached here */
390 return -1;
393 /* Returns an integer value which uniquely describes a thread. */
394 static inline objc_thread_t
395 __gthread_objc_thread_id (void)
397 if (__gthread_active_p ())
398 return (objc_thread_t) __gthrw_(pthread_self) ();
399 else
400 return (objc_thread_t) 1;
403 /* Sets the thread's local storage pointer. */
404 static inline int
405 __gthread_objc_thread_set_data (void *value)
407 if (__gthread_active_p ())
408 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
409 else
411 thread_local_storage = value;
412 return 0;
416 /* Returns the thread's local storage pointer. */
417 static inline void *
418 __gthread_objc_thread_get_data (void)
420 if (__gthread_active_p ())
421 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
422 else
423 return thread_local_storage;
426 /* Backend mutex functions */
428 /* Allocate a mutex. */
429 static inline int
430 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
432 if (__gthread_active_p ())
434 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
436 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
438 objc_free (mutex->backend);
439 mutex->backend = NULL;
440 return -1;
444 return 0;
447 /* Deallocate a mutex. */
448 static inline int
449 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
451 if (__gthread_active_p ())
453 int count;
456 * Posix Threads specifically require that the thread be unlocked
457 * for __gthrw_(pthread_mutex_destroy) to work.
462 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
463 if (count < 0)
464 return -1;
466 while (count);
468 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
469 return -1;
471 objc_free (mutex->backend);
472 mutex->backend = NULL;
474 return 0;
477 /* Grab a lock on a mutex. */
478 static inline int
479 __gthread_objc_mutex_lock (objc_mutex_t mutex)
481 if (__gthread_active_p ()
482 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
484 return -1;
487 return 0;
490 /* Try to grab a lock on a mutex. */
491 static inline int
492 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
494 if (__gthread_active_p ()
495 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
497 return -1;
500 return 0;
503 /* Unlock the mutex */
504 static inline int
505 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
507 if (__gthread_active_p ()
508 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
510 return -1;
513 return 0;
516 /* Backend condition mutex functions */
518 /* Allocate a condition. */
519 static inline int
520 __gthread_objc_condition_allocate (objc_condition_t condition)
522 if (__gthread_active_p ())
524 condition->backend = objc_malloc (sizeof (pthread_cond_t));
526 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
528 objc_free (condition->backend);
529 condition->backend = NULL;
530 return -1;
534 return 0;
537 /* Deallocate a condition. */
538 static inline int
539 __gthread_objc_condition_deallocate (objc_condition_t condition)
541 if (__gthread_active_p ())
543 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
544 return -1;
546 objc_free (condition->backend);
547 condition->backend = NULL;
549 return 0;
552 /* Wait on the condition */
553 static inline int
554 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
556 if (__gthread_active_p ())
557 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
558 (pthread_mutex_t *) mutex->backend);
559 else
560 return 0;
563 /* Wake up all threads waiting on this condition. */
564 static inline int
565 __gthread_objc_condition_broadcast (objc_condition_t condition)
567 if (__gthread_active_p ())
568 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
569 else
570 return 0;
573 /* Wake up one thread waiting on this condition. */
574 static inline int
575 __gthread_objc_condition_signal (objc_condition_t condition)
577 if (__gthread_active_p ())
578 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
579 else
580 return 0;
583 #else /* _LIBOBJC */
585 static inline int
586 __gthread_once (__gthread_once_t *once, void (*func) (void))
588 if (__gthread_active_p ())
589 return __gthrw_(pthread_once) (once, func);
590 else
591 return -1;
594 static inline int
595 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
597 return __gthrw_(pthread_key_create) (key, dtor);
600 static inline int
601 __gthread_key_delete (__gthread_key_t key)
603 return __gthrw_(pthread_key_delete) (key);
606 static inline void *
607 __gthread_getspecific (__gthread_key_t key)
609 return __gthrw_(pthread_getspecific) (key);
612 static inline int
613 __gthread_setspecific (__gthread_key_t key, const void *ptr)
615 return __gthrw_(pthread_setspecific) (key, ptr);
618 static inline int
619 __gthread_mutex_lock (__gthread_mutex_t *mutex)
621 if (__gthread_active_p ())
622 return __gthrw_(pthread_mutex_lock) (mutex);
623 else
624 return 0;
627 static inline int
628 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
630 if (__gthread_active_p ())
631 return __gthrw_(pthread_mutex_trylock) (mutex);
632 else
633 return 0;
636 static inline int
637 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
639 if (__gthread_active_p ())
640 return __gthrw_(pthread_mutex_unlock) (mutex);
641 else
642 return 0;
645 static inline int
646 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
648 mutex->depth = 0;
649 mutex->owner = (pthread_t) 0;
650 return __gthrw_(pthread_mutex_init) (&mutex->actual, NULL);
653 static inline int
654 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
656 if (__gthread_active_p ())
658 pthread_t me = __gthrw_(pthread_self) ();
660 if (mutex->owner != me)
662 __gthrw_(pthread_mutex_lock) (&mutex->actual);
663 mutex->owner = me;
666 mutex->depth++;
668 return 0;
671 static inline int
672 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
674 if (__gthread_active_p ())
676 pthread_t me = __gthrw_(pthread_self) ();
678 if (mutex->owner != me)
680 if (__gthrw_(pthread_mutex_trylock) (&mutex->actual))
681 return 1;
682 mutex->owner = me;
685 mutex->depth++;
687 return 0;
690 static inline int
691 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
693 if (__gthread_active_p ())
695 if (--mutex->depth == 0)
697 mutex->owner = (pthread_t) 0;
698 __gthrw_(pthread_mutex_unlock) (&mutex->actual);
701 return 0;
704 static inline int
705 __gthread_cond_broadcast (__gthread_cond_t *cond)
707 return __gthrw_(pthread_cond_broadcast) (cond);
710 static inline int
711 __gthread_cond_wait (__gthread_cond_t *cond, __gthread_mutex_t *mutex)
713 return __gthrw_(pthread_cond_wait) (cond, mutex);
716 static inline int
717 __gthread_cond_wait_recursive (__gthread_cond_t *cond,
718 __gthread_recursive_mutex_t *mutex)
720 return __gthrw_(pthread_cond_wait) (cond, &mutex->actual);
723 #endif /* _LIBOBJC */
725 #endif /* ! GCC_GTHR_POSIX_H */