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
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
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
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. */
37 /* Some implementations of <pthread.h> require this to be defined. */
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
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
70 # define __gthrw(name)
71 # define __gthrw_(name) name
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
)
83 __gthrw(pthread_mutex_init
)
84 __gthrw(pthread_mutex_destroy
)
85 __gthrw(pthread_mutex_lock
)
86 __gthrw(pthread_mutex_trylock
)
87 __gthrw(pthread_mutex_unlock
)
88 __gthrw(pthread_mutexattr_init
)
89 __gthrw(pthread_mutexattr_destroy
)
91 __gthrw(pthread_cond_broadcast
)
92 __gthrw(pthread_cond_wait
)
94 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
96 __gthrw(pthread_cond_destroy
)
97 __gthrw(pthread_cond_init
)
98 __gthrw(pthread_cond_signal
)
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 */
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;
134 __gthread_trigger (void)
136 __gthread_active
= 1;
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 */
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 pthread
191 library or not. However, these interfaces aren't available in early
192 libc versions. We also can't use pthread_once as some libc versions
193 call the init function. So, we use pthread_create to check whether it
194 is possible to create a thread or not. The stub implementation returns
195 the error number ENOSYS. */
197 #if defined(__hppa__) && defined(__hpux__)
201 static volatile int __gthread_active
= -1;
204 __gthread_start (void *arg
__attribute__((unused
)))
209 static void __gthread_active_init (void) __attribute__((noinline
));
211 __gthread_active_init (void)
213 static pthread_mutex_t __gthread_active_mutex
= PTHREAD_MUTEX_INITIALIZER
;
218 __gthrw_(pthread_mutex_lock
) (&__gthread_active_mutex
);
219 if (__gthread_active
< 0)
221 __gthrw_(pthread_attr_init
) (&a
);
222 __gthrw_(pthread_attr_setdetachstate
) (&a
, PTHREAD_CREATE_DETACHED
);
223 result
= __gthrw_(pthread_create
) (&t
, &a
, __gthread_start
, NULL
);
224 if (result
!= ENOSYS
)
225 __gthread_active
= 1;
227 __gthread_active
= 0;
228 __gthrw_(pthread_attr_destroy
) (&a
);
230 __gthrw_(pthread_mutex_unlock
) (&__gthread_active_mutex
);
234 __gthread_active_p (void)
236 /* Avoid reading __gthread_active twice on the main code path. */
237 int __gthread_active_latest_value
= __gthread_active
;
239 /* This test is not protected to avoid taking a lock on the main code
240 path so every update of __gthread_active in a threaded program must
241 be atomic with regard to the result of the test. */
242 if (__builtin_expect (__gthread_active_latest_value
< 0, 0))
244 __gthread_active_init ();
245 __gthread_active_latest_value
= __gthread_active
;
248 return __gthread_active_latest_value
!= 0;
251 #else /* not hppa-hpux */
254 __gthread_active_p (void)
259 #endif /* hppa-hpux */
261 #endif /* SUPPORTS_WEAK */
265 /* This is the config.h file in libobjc/ */
272 /* Key structure for maintaining thread specific storage */
273 static pthread_key_t _objc_thread_storage
;
274 static pthread_attr_t _objc_thread_attribs
;
276 /* Thread local storage for a single thread */
277 static void *thread_local_storage
= NULL
;
279 /* Backend initialization functions */
281 /* Initialize the threads subsystem. */
283 __gthread_objc_init_thread_system (void)
285 if (__gthread_active_p ())
287 /* Initialize the thread storage key. */
288 if (__gthrw_(pthread_key_create
) (&_objc_thread_storage
, NULL
) == 0)
290 /* The normal default detach state for threads is
291 * PTHREAD_CREATE_JOINABLE which causes threads to not die
292 * when you think they should. */
293 if (__gthrw_(pthread_attr_init
) (&_objc_thread_attribs
) == 0
294 && __gthrw_(pthread_attr_setdetachstate
) (&_objc_thread_attribs
,
295 PTHREAD_CREATE_DETACHED
) == 0)
303 /* Close the threads subsystem. */
305 __gthread_objc_close_thread_system (void)
307 if (__gthread_active_p ()
308 && __gthrw_(pthread_key_delete
) (_objc_thread_storage
) == 0
309 && __gthrw_(pthread_attr_destroy
) (&_objc_thread_attribs
) == 0)
315 /* Backend thread functions */
317 /* Create a new thread of execution. */
318 static inline objc_thread_t
319 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
321 objc_thread_t thread_id
;
322 pthread_t new_thread_handle
;
324 if (!__gthread_active_p ())
327 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, NULL
, (void *) func
, arg
)))
328 thread_id
= (objc_thread_t
) new_thread_handle
;
335 /* Set the current thread's priority. */
337 __gthread_objc_thread_set_priority (int priority
)
339 if (!__gthread_active_p ())
343 #ifdef _POSIX_PRIORITY_SCHEDULING
344 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
345 pthread_t thread_id
= __gthrw_(pthread_self
) ();
347 struct sched_param params
;
348 int priority_min
, priority_max
;
350 if (__gthrw_(pthread_getschedparam
) (thread_id
, &policy
, ¶ms
) == 0)
352 if ((priority_max
= __gthrw_(sched_get_priority_max
) (policy
)) == -1)
355 if ((priority_min
= __gthrw_(sched_get_priority_min
) (policy
)) == -1)
358 if (priority
> priority_max
)
359 priority
= priority_max
;
360 else if (priority
< priority_min
)
361 priority
= priority_min
;
362 params
.sched_priority
= priority
;
365 * The solaris 7 and several other man pages incorrectly state that
366 * this should be a pointer to policy but pthread.h is universally
369 if (__gthrw_(pthread_setschedparam
) (thread_id
, policy
, ¶ms
) == 0)
372 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
373 #endif /* _POSIX_PRIORITY_SCHEDULING */
378 /* Return the current thread's priority. */
380 __gthread_objc_thread_get_priority (void)
382 #ifdef _POSIX_PRIORITY_SCHEDULING
383 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
384 if (__gthread_active_p ())
387 struct sched_param params
;
389 if (__gthrw_(pthread_getschedparam
) (__gthrw_(pthread_self
) (), &policy
, ¶ms
) == 0)
390 return params
.sched_priority
;
395 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
396 #endif /* _POSIX_PRIORITY_SCHEDULING */
397 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
400 /* Yield our process time to another thread. */
402 __gthread_objc_thread_yield (void)
404 if (__gthread_active_p ())
405 __gthrw_(sched_yield
) ();
408 /* Terminate the current thread. */
410 __gthread_objc_thread_exit (void)
412 if (__gthread_active_p ())
413 /* exit the thread */
414 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
416 /* Failed if we reached here */
420 /* Returns an integer value which uniquely describes a thread. */
421 static inline objc_thread_t
422 __gthread_objc_thread_id (void)
424 if (__gthread_active_p ())
425 return (objc_thread_t
) __gthrw_(pthread_self
) ();
427 return (objc_thread_t
) 1;
430 /* Sets the thread's local storage pointer. */
432 __gthread_objc_thread_set_data (void *value
)
434 if (__gthread_active_p ())
435 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
438 thread_local_storage
= value
;
443 /* Returns the thread's local storage pointer. */
445 __gthread_objc_thread_get_data (void)
447 if (__gthread_active_p ())
448 return __gthrw_(pthread_getspecific
) (_objc_thread_storage
);
450 return thread_local_storage
;
453 /* Backend mutex functions */
455 /* Allocate a mutex. */
457 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
459 if (__gthread_active_p ())
461 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
463 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
, NULL
))
465 objc_free (mutex
->backend
);
466 mutex
->backend
= NULL
;
474 /* Deallocate a mutex. */
476 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
478 if (__gthread_active_p ())
483 * Posix Threads specifically require that the thread be unlocked
484 * for __gthrw_(pthread_mutex_destroy) to work.
489 count
= __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
495 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
498 objc_free (mutex
->backend
);
499 mutex
->backend
= NULL
;
504 /* Grab a lock on a mutex. */
506 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
508 if (__gthread_active_p ()
509 && __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
517 /* Try to grab a lock on a mutex. */
519 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
521 if (__gthread_active_p ()
522 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
530 /* Unlock the mutex */
532 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
534 if (__gthread_active_p ()
535 && __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
543 /* Backend condition mutex functions */
545 /* Allocate a condition. */
547 __gthread_objc_condition_allocate (objc_condition_t condition
)
549 if (__gthread_active_p ())
551 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
553 if (__gthrw_(pthread_cond_init
) ((pthread_cond_t
*) condition
->backend
, NULL
))
555 objc_free (condition
->backend
);
556 condition
->backend
= NULL
;
564 /* Deallocate a condition. */
566 __gthread_objc_condition_deallocate (objc_condition_t condition
)
568 if (__gthread_active_p ())
570 if (__gthrw_(pthread_cond_destroy
) ((pthread_cond_t
*) condition
->backend
))
573 objc_free (condition
->backend
);
574 condition
->backend
= NULL
;
579 /* Wait on the condition */
581 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
583 if (__gthread_active_p ())
584 return __gthrw_(pthread_cond_wait
) ((pthread_cond_t
*) condition
->backend
,
585 (pthread_mutex_t
*) mutex
->backend
);
590 /* Wake up all threads waiting on this condition. */
592 __gthread_objc_condition_broadcast (objc_condition_t condition
)
594 if (__gthread_active_p ())
595 return __gthrw_(pthread_cond_broadcast
) ((pthread_cond_t
*) condition
->backend
);
600 /* Wake up one thread waiting on this condition. */
602 __gthread_objc_condition_signal (objc_condition_t condition
)
604 if (__gthread_active_p ())
605 return __gthrw_(pthread_cond_signal
) ((pthread_cond_t
*) condition
->backend
);
613 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
615 if (__gthread_active_p ())
616 return __gthrw_(pthread_once
) (once
, func
);
622 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
624 return __gthrw_(pthread_key_create
) (key
, dtor
);
628 __gthread_key_delete (__gthread_key_t key
)
630 return __gthrw_(pthread_key_delete
) (key
);
634 __gthread_getspecific (__gthread_key_t key
)
636 return __gthrw_(pthread_getspecific
) (key
);
640 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
642 return __gthrw_(pthread_setspecific
) (key
, ptr
);
646 __gthread_mutex_destroy (__gthread_mutex_t
*mutex
)
648 if (__gthread_active_p ())
649 return __gthrw_(pthread_mutex_destroy
) (mutex
);
655 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
657 if (__gthread_active_p ())
658 return __gthrw_(pthread_mutex_lock
) (mutex
);
664 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
666 if (__gthread_active_p ())
667 return __gthrw_(pthread_mutex_trylock
) (mutex
);
673 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
675 if (__gthread_active_p ())
676 return __gthrw_(pthread_mutex_unlock
) (mutex
);
682 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
685 mutex
->owner
= (pthread_t
) 0;
686 return __gthrw_(pthread_mutex_init
) (&mutex
->actual
, NULL
);
690 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
692 if (__gthread_active_p ())
694 pthread_t me
= __gthrw_(pthread_self
) ();
696 if (mutex
->owner
!= me
)
698 __gthrw_(pthread_mutex_lock
) (&mutex
->actual
);
708 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
710 if (__gthread_active_p ())
712 pthread_t me
= __gthrw_(pthread_self
) ();
714 if (mutex
->owner
!= me
)
716 if (__gthrw_(pthread_mutex_trylock
) (&mutex
->actual
))
727 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
729 if (__gthread_active_p ())
731 if (--mutex
->depth
== 0)
733 mutex
->owner
= (pthread_t
) 0;
734 __gthrw_(pthread_mutex_unlock
) (&mutex
->actual
);
741 __gthread_cond_broadcast (__gthread_cond_t
*cond
)
743 return __gthrw_(pthread_cond_broadcast
) (cond
);
747 __gthread_cond_wait (__gthread_cond_t
*cond
, __gthread_mutex_t
*mutex
)
749 return __gthrw_(pthread_cond_wait
) (cond
, mutex
);
753 __gthread_cond_wait_recursive (__gthread_cond_t
*cond
,
754 __gthread_recursive_mutex_t
*mutex
)
756 return __gthrw_(pthread_cond_wait
) (cond
, &mutex
->actual
);
759 #endif /* _LIBOBJC */
761 #endif /* ! GCC_GTHR_POSIX_H */