1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005 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
;
52 pthread_mutex_t actual
;
53 } __gthread_recursive_mutex_t
;
55 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
56 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
63 # define __gthrw_asmname(cname) __gthrw_asmnamep (__USER_LABEL_PREFIX__, cname)
64 # define __gthrw_asmnamep(prefix, cname) __gthrw_string (prefix) cname
65 # define __gthrw_string(x) #x
66 # define __gthrw(name) \
67 extern __typeof(name) __gthrw_ ## name __asm (__gthrw_asmname (#name))
70 __gthrw(pthread_once
);
71 __gthrw(pthread_key_create
);
72 __gthrw(pthread_key_delete
);
73 __gthrw(pthread_getspecific
);
74 __gthrw(pthread_setspecific
);
75 __gthrw(pthread_create
);
76 __gthrw(pthread_cancel
);
77 __gthrw(pthread_self
);
79 __gthrw(pthread_mutex_lock
);
80 __gthrw(pthread_mutex_trylock
);
81 __gthrw(pthread_mutex_unlock
);
82 __gthrw(pthread_mutexattr_init
);
83 __gthrw(pthread_mutexattr_destroy
);
85 __gthrw(pthread_mutex_init
);
87 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
89 __gthrw(pthread_cond_broadcast
);
90 __gthrw(pthread_cond_destroy
);
91 __gthrw(pthread_cond_init
);
92 __gthrw(pthread_cond_signal
);
93 __gthrw(pthread_cond_wait
);
94 __gthrw(pthread_exit
);
95 __gthrw(pthread_mutex_destroy
);
96 #ifdef _POSIX_PRIORITY_SCHEDULING
97 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
98 __gthrw(sched_get_priority_max
);
99 __gthrw(sched_get_priority_min
);
100 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
101 #endif /* _POSIX_PRIORITY_SCHEDULING */
102 __gthrw(sched_yield
);
103 __gthrw(pthread_attr_destroy
);
104 __gthrw(pthread_attr_init
);
105 __gthrw(pthread_attr_setdetachstate
);
106 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
107 __gthrw(pthread_getschedparam
);
108 __gthrw(pthread_setschedparam
);
109 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
110 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
112 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
115 __gthread_active_p (void)
117 static void *const __gthread_active_ptr
118 = __extension__ (void *) &__gthrw_pthread_cancel
;
119 return __gthread_active_ptr
!= 0;
122 #else /* not SUPPORTS_WEAK */
125 __gthread_active_p (void)
130 #endif /* SUPPORTS_WEAK */
134 /* This is the config.h file in libobjc/ */
141 /* Key structure for maintaining thread specific storage */
142 static pthread_key_t _objc_thread_storage
;
143 static pthread_attr_t _objc_thread_attribs
;
145 /* Thread local storage for a single thread */
146 static void *thread_local_storage
= NULL
;
148 /* Backend initialization functions */
150 /* Initialize the threads subsystem. */
152 __gthread_objc_init_thread_system (void)
154 if (__gthread_active_p ())
156 /* Initialize the thread storage key. */
157 if (__gthrw_pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
159 /* The normal default detach state for threads is
160 * PTHREAD_CREATE_JOINABLE which causes threads to not die
161 * when you think they should. */
162 if (__gthrw_pthread_attr_init (&_objc_thread_attribs
) == 0
163 && __gthrw_pthread_attr_setdetachstate (&_objc_thread_attribs
,
164 PTHREAD_CREATE_DETACHED
) == 0)
172 /* Close the threads subsystem. */
174 __gthread_objc_close_thread_system (void)
176 if (__gthread_active_p ()
177 && __gthrw_pthread_key_delete (_objc_thread_storage
) == 0
178 && __gthrw_pthread_attr_destroy (&_objc_thread_attribs
) == 0)
184 /* Backend thread functions */
186 /* Create a new thread of execution. */
187 static inline objc_thread_t
188 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
190 objc_thread_t thread_id
;
191 pthread_t new_thread_handle
;
193 if (!__gthread_active_p ())
196 if (!(__gthrw_pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
197 thread_id
= (objc_thread_t
) new_thread_handle
;
204 /* Set the current thread's priority. */
206 __gthread_objc_thread_set_priority (int priority
)
208 if (!__gthread_active_p ())
212 #ifdef _POSIX_PRIORITY_SCHEDULING
213 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
214 pthread_t thread_id
= __gthrw_pthread_self ();
216 struct sched_param params
;
217 int priority_min
, priority_max
;
219 if (__gthrw_pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
221 if ((priority_max
= __gthrw_sched_get_priority_max (policy
)) == -1)
224 if ((priority_min
= __gthrw_sched_get_priority_min (policy
)) == -1)
227 if (priority
> priority_max
)
228 priority
= priority_max
;
229 else if (priority
< priority_min
)
230 priority
= priority_min
;
231 params
.sched_priority
= priority
;
234 * The solaris 7 and several other man pages incorrectly state that
235 * this should be a pointer to policy but pthread.h is universally
238 if (__gthrw_pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
241 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
242 #endif /* _POSIX_PRIORITY_SCHEDULING */
247 /* Return the current thread's priority. */
249 __gthread_objc_thread_get_priority (void)
251 #ifdef _POSIX_PRIORITY_SCHEDULING
252 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
253 if (__gthread_active_p ())
256 struct sched_param params
;
258 if (__gthrw_pthread_getschedparam (__gthrw_pthread_self (), &policy
, ¶ms
) == 0)
259 return params
.sched_priority
;
264 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
265 #endif /* _POSIX_PRIORITY_SCHEDULING */
266 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
269 /* Yield our process time to another thread. */
271 __gthread_objc_thread_yield (void)
273 if (__gthread_active_p ())
274 __gthrw_sched_yield ();
277 /* Terminate the current thread. */
279 __gthread_objc_thread_exit (void)
281 if (__gthread_active_p ())
282 /* exit the thread */
283 __gthrw_pthread_exit (&__objc_thread_exit_status
);
285 /* Failed if we reached here */
289 /* Returns an integer value which uniquely describes a thread. */
290 static inline objc_thread_t
291 __gthread_objc_thread_id (void)
293 if (__gthread_active_p ())
294 return (objc_thread_t
) __gthrw_pthread_self ();
296 return (objc_thread_t
) 1;
299 /* Sets the thread's local storage pointer. */
301 __gthread_objc_thread_set_data (void *value
)
303 if (__gthread_active_p ())
304 return __gthrw_pthread_setspecific (_objc_thread_storage
, value
);
307 thread_local_storage
= value
;
312 /* Returns the thread's local storage pointer. */
314 __gthread_objc_thread_get_data (void)
316 if (__gthread_active_p ())
317 return __gthrw_pthread_getspecific (_objc_thread_storage
);
319 return thread_local_storage
;
322 /* Backend mutex functions */
324 /* Allocate a mutex. */
326 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
328 if (__gthread_active_p ())
330 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
332 if (__gthrw_pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
334 objc_free (mutex
->backend
);
335 mutex
->backend
= NULL
;
343 /* Deallocate a mutex. */
345 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
347 if (__gthread_active_p ())
352 * Posix Threads specifically require that the thread be unlocked
353 * for __gthrw_pthread_mutex_destroy to work.
358 count
= __gthrw_pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
364 if (__gthrw_pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
367 objc_free (mutex
->backend
);
368 mutex
->backend
= NULL
;
373 /* Grab a lock on a mutex. */
375 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
377 if (__gthread_active_p ()
378 && __gthrw_pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
386 /* Try to grab a lock on a mutex. */
388 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
390 if (__gthread_active_p ()
391 && __gthrw_pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
399 /* Unlock the mutex */
401 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
403 if (__gthread_active_p ()
404 && __gthrw_pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
412 /* Backend condition mutex functions */
414 /* Allocate a condition. */
416 __gthread_objc_condition_allocate (objc_condition_t condition
)
418 if (__gthread_active_p ())
420 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
422 if (__gthrw_pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
424 objc_free (condition
->backend
);
425 condition
->backend
= NULL
;
433 /* Deallocate a condition. */
435 __gthread_objc_condition_deallocate (objc_condition_t condition
)
437 if (__gthread_active_p ())
439 if (__gthrw_pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
442 objc_free (condition
->backend
);
443 condition
->backend
= NULL
;
448 /* Wait on the condition */
450 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
452 if (__gthread_active_p ())
453 return __gthrw_pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
454 (pthread_mutex_t
*) mutex
->backend
);
459 /* Wake up all threads waiting on this condition. */
461 __gthread_objc_condition_broadcast (objc_condition_t condition
)
463 if (__gthread_active_p ())
464 return __gthrw_pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
469 /* Wake up one thread waiting on this condition. */
471 __gthread_objc_condition_signal (objc_condition_t condition
)
473 if (__gthread_active_p ())
474 return __gthrw_pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
482 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
484 if (__gthread_active_p ())
485 return __gthrw_pthread_once (once
, func
);
491 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
493 return __gthrw_pthread_key_create (key
, dtor
);
497 __gthread_key_delete (__gthread_key_t key
)
499 return __gthrw_pthread_key_delete (key
);
503 __gthread_getspecific (__gthread_key_t key
)
505 return __gthrw_pthread_getspecific (key
);
509 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
511 return __gthrw_pthread_setspecific (key
, ptr
);
515 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
517 if (__gthread_active_p ())
518 return __gthrw_pthread_mutex_lock (mutex
);
524 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
526 if (__gthread_active_p ())
527 return __gthrw_pthread_mutex_trylock (mutex
);
533 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
535 if (__gthread_active_p ())
536 return __gthrw_pthread_mutex_unlock (mutex
);
542 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
545 mutex
->owner
= (pthread_t
) 0;
546 return __gthrw_pthread_mutex_init (&mutex
->actual
, NULL
);
550 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
552 if (__gthread_active_p ())
554 pthread_t me
= __gthrw_pthread_self ();
556 if (mutex
->owner
!= me
)
558 __gthrw_pthread_mutex_lock (&mutex
->actual
);
568 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
570 if (__gthread_active_p ())
572 pthread_t me
= __gthrw_pthread_self ();
574 if (mutex
->owner
!= me
)
576 if (__gthrw_pthread_mutex_trylock (&mutex
->actual
))
587 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
589 if (__gthread_active_p ())
591 if (--mutex
->depth
== 0)
593 mutex
->owner
= (pthread_t
) 0;
594 __gthrw_pthread_mutex_unlock (&mutex
->actual
);
600 #endif /* _LIBOBJC */
602 #endif /* ! GCC_GTHR_POSIX_H */