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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
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 2, or (at your option) any later
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
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 #ifndef GCC_GTHR_POSIX_H
31 #define GCC_GTHR_POSIX_H
33 /* POSIX threads specific definitions.
34 Easy, since the interface is just one-to-one mapping. */
38 /* Some implementations of <pthread.h> require this to be defined. */
46 typedef pthread_key_t __gthread_key_t
;
47 typedef pthread_once_t __gthread_once_t
;
48 typedef pthread_mutex_t __gthread_mutex_t
;
49 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
51 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
52 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
54 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
55 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
56 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
62 # define __gthrw(name) \
63 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
65 # define __gthrw_asmname(cname) __gthrw_asmnamep (__USER_LABEL_PREFIX__, cname)
66 # define __gthrw_asmnamep(prefix, cname) __gthrw_string (prefix) cname
67 # define __gthrw_string(x) #x
68 # define __gthrw(name) \
69 extern __typeof(name) __gthrw_ ## name __asm (__gthrw_asmname (#name))
72 __gthrw(pthread_once
);
73 __gthrw(pthread_key_create
);
74 __gthrw(pthread_key_delete
);
75 __gthrw(pthread_getspecific
);
76 __gthrw(pthread_setspecific
);
77 __gthrw(pthread_create
);
78 __gthrw(pthread_cancel
);
80 __gthrw(pthread_mutex_lock
);
81 __gthrw(pthread_mutex_trylock
);
82 __gthrw(pthread_mutex_unlock
);
83 __gthrw(pthread_mutexattr_init
);
84 __gthrw(pthread_mutexattr_settype
);
85 __gthrw(pthread_mutexattr_destroy
);
87 __gthrw(pthread_mutex_init
);
89 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
91 __gthrw(pthread_cond_broadcast
);
92 __gthrw(pthread_cond_destroy
);
93 __gthrw(pthread_cond_init
);
94 __gthrw(pthread_cond_signal
);
95 __gthrw(pthread_cond_wait
);
96 __gthrw(pthread_exit
);
97 __gthrw(pthread_mutex_destroy
);
98 __gthrw(pthread_self
);
99 #ifdef _POSIX_PRIORITY_SCHEDULING
100 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
101 __gthrw(sched_get_priority_max
);
102 __gthrw(sched_get_priority_min
);
103 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
104 #endif /* _POSIX_PRIORITY_SCHEDULING */
105 __gthrw(sched_yield
);
106 __gthrw(pthread_attr_destroy
);
107 __gthrw(pthread_attr_init
);
108 __gthrw(pthread_attr_setdetachstate
);
109 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
110 __gthrw(pthread_getschedparam
);
111 __gthrw(pthread_setschedparam
);
112 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
113 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
115 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
118 __gthread_active_p (void)
120 static void *const __gthread_active_ptr
121 = __extension__ (void *) &__gthrw_pthread_cancel
;
122 return __gthread_active_ptr
!= 0;
125 #else /* not SUPPORTS_WEAK */
128 __gthread_active_p (void)
133 #endif /* SUPPORTS_WEAK */
137 /* This is the config.h file in libobjc/ */
144 /* Key structure for maintaining thread specific storage */
145 static pthread_key_t _objc_thread_storage
;
146 static pthread_attr_t _objc_thread_attribs
;
148 /* Thread local storage for a single thread */
149 static void *thread_local_storage
= NULL
;
151 /* Backend initialization functions */
153 /* Initialize the threads subsystem. */
155 __gthread_objc_init_thread_system (void)
157 if (__gthread_active_p ())
159 /* Initialize the thread storage key. */
160 if (__gthrw_pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
162 /* The normal default detach state for threads is
163 * PTHREAD_CREATE_JOINABLE which causes threads to not die
164 * when you think they should. */
165 if (__gthrw_pthread_attr_init (&_objc_thread_attribs
) == 0
166 && __gthrw_pthread_attr_setdetachstate (&_objc_thread_attribs
,
167 PTHREAD_CREATE_DETACHED
) == 0)
175 /* Close the threads subsystem. */
177 __gthread_objc_close_thread_system (void)
179 if (__gthread_active_p ()
180 && __gthrw_pthread_key_delete (_objc_thread_storage
) == 0
181 && __gthrw_pthread_attr_destroy (&_objc_thread_attribs
) == 0)
187 /* Backend thread functions */
189 /* Create a new thread of execution. */
190 static inline objc_thread_t
191 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
193 objc_thread_t thread_id
;
194 pthread_t new_thread_handle
;
196 if (!__gthread_active_p ())
199 if (!(__gthrw_pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
200 thread_id
= (objc_thread_t
) new_thread_handle
;
207 /* Set the current thread's priority. */
209 __gthread_objc_thread_set_priority (int priority
)
211 if (!__gthread_active_p ())
215 #ifdef _POSIX_PRIORITY_SCHEDULING
216 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
217 pthread_t thread_id
= __gthrw_pthread_self ();
219 struct sched_param params
;
220 int priority_min
, priority_max
;
222 if (__gthrw_pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
224 if ((priority_max
= __gthrw_sched_get_priority_max (policy
)) == -1)
227 if ((priority_min
= __gthrw_sched_get_priority_min (policy
)) == -1)
230 if (priority
> priority_max
)
231 priority
= priority_max
;
232 else if (priority
< priority_min
)
233 priority
= priority_min
;
234 params
.sched_priority
= priority
;
237 * The solaris 7 and several other man pages incorrectly state that
238 * this should be a pointer to policy but pthread.h is universally
241 if (__gthrw_pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
244 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
245 #endif /* _POSIX_PRIORITY_SCHEDULING */
250 /* Return the current thread's priority. */
252 __gthread_objc_thread_get_priority (void)
254 #ifdef _POSIX_PRIORITY_SCHEDULING
255 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
256 if (__gthread_active_p ())
259 struct sched_param params
;
261 if (__gthrw_pthread_getschedparam (__gthrw_pthread_self (), &policy
, ¶ms
) == 0)
262 return params
.sched_priority
;
267 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
268 #endif /* _POSIX_PRIORITY_SCHEDULING */
269 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
272 /* Yield our process time to another thread. */
274 __gthread_objc_thread_yield (void)
276 if (__gthread_active_p ())
277 __gthrw_sched_yield ();
280 /* Terminate the current thread. */
282 __gthread_objc_thread_exit (void)
284 if (__gthread_active_p ())
285 /* exit the thread */
286 __gthrw_pthread_exit (&__objc_thread_exit_status
);
288 /* Failed if we reached here */
292 /* Returns an integer value which uniquely describes a thread. */
293 static inline objc_thread_t
294 __gthread_objc_thread_id (void)
296 if (__gthread_active_p ())
297 return (objc_thread_t
) __gthrw_pthread_self ();
299 return (objc_thread_t
) 1;
302 /* Sets the thread's local storage pointer. */
304 __gthread_objc_thread_set_data (void *value
)
306 if (__gthread_active_p ())
307 return __gthrw_pthread_setspecific (_objc_thread_storage
, value
);
310 thread_local_storage
= value
;
315 /* Returns the thread's local storage pointer. */
317 __gthread_objc_thread_get_data (void)
319 if (__gthread_active_p ())
320 return __gthrw_pthread_getspecific (_objc_thread_storage
);
322 return thread_local_storage
;
325 /* Backend mutex functions */
327 /* Allocate a mutex. */
329 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
331 if (__gthread_active_p ())
333 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
335 if (__gthrw_pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
337 objc_free (mutex
->backend
);
338 mutex
->backend
= NULL
;
346 /* Deallocate a mutex. */
348 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
350 if (__gthread_active_p ())
355 * Posix Threads specifically require that the thread be unlocked
356 * for __gthrw_pthread_mutex_destroy to work.
361 count
= __gthrw_pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
367 if (__gthrw_pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
370 objc_free (mutex
->backend
);
371 mutex
->backend
= NULL
;
376 /* Grab a lock on a mutex. */
378 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
380 if (__gthread_active_p ()
381 && __gthrw_pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
389 /* Try to grab a lock on a mutex. */
391 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
393 if (__gthread_active_p ()
394 && __gthrw_pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
402 /* Unlock the mutex */
404 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
406 if (__gthread_active_p ()
407 && __gthrw_pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
415 /* Backend condition mutex functions */
417 /* Allocate a condition. */
419 __gthread_objc_condition_allocate (objc_condition_t condition
)
421 if (__gthread_active_p ())
423 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
425 if (__gthrw_pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
427 objc_free (condition
->backend
);
428 condition
->backend
= NULL
;
436 /* Deallocate a condition. */
438 __gthread_objc_condition_deallocate (objc_condition_t condition
)
440 if (__gthread_active_p ())
442 if (__gthrw_pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
445 objc_free (condition
->backend
);
446 condition
->backend
= NULL
;
451 /* Wait on the condition */
453 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
455 if (__gthread_active_p ())
456 return __gthrw_pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
457 (pthread_mutex_t
*) mutex
->backend
);
462 /* Wake up all threads waiting on this condition. */
464 __gthread_objc_condition_broadcast (objc_condition_t condition
)
466 if (__gthread_active_p ())
467 return __gthrw_pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
472 /* Wake up one thread waiting on this condition. */
474 __gthread_objc_condition_signal (objc_condition_t condition
)
476 if (__gthread_active_p ())
477 return __gthrw_pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
485 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
487 if (__gthread_active_p ())
488 return __gthrw_pthread_once (once
, func
);
494 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
496 return __gthrw_pthread_key_create (key
, dtor
);
500 __gthread_key_delete (__gthread_key_t key
)
502 return __gthrw_pthread_key_delete (key
);
506 __gthread_getspecific (__gthread_key_t key
)
508 return __gthrw_pthread_getspecific (key
);
512 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
514 return __gthrw_pthread_setspecific (key
, ptr
);
518 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
520 if (__gthread_active_p ())
521 return __gthrw_pthread_mutex_lock (mutex
);
527 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
529 if (__gthread_active_p ())
530 return __gthrw_pthread_mutex_trylock (mutex
);
536 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
538 if (__gthread_active_p ())
539 return __gthrw_pthread_mutex_unlock (mutex
);
544 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
546 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
548 if (__gthread_active_p ())
550 pthread_mutexattr_t attr
;
553 r
= __gthrw_pthread_mutexattr_init (&attr
);
555 r
= __gthrw_pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
557 r
= __gthrw_pthread_mutex_init (mutex
, &attr
);
559 r
= __gthrw_pthread_mutexattr_destroy (&attr
);
567 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
569 return __gthread_mutex_lock (mutex
);
573 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
575 return __gthread_mutex_trylock (mutex
);
579 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
581 return __gthread_mutex_unlock (mutex
);
584 #endif /* _LIBOBJC */
586 #endif /* ! GCC_GTHR_POSIX_H */