1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 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_DCE_H
31 #define GCC_GTHR_DCE_H
33 /* If _DCE_THREADS is not defined, then we're building the single
34 threaded version of the libraries and do not want to reference
35 anything related to pthreads or dce. */
37 #include "gthr-single.h"
39 /* DCE threads interface.
40 DCE threads are based on POSIX threads draft 4, and many things
41 have changed since then. */
50 #define UNUSED(x) x __attribute__((unused))
53 typedef pthread_key_t __gthread_key_t
;
54 typedef pthread_once_t __gthread_once_t
;
55 typedef pthread_mutex_t __gthread_mutex_t
;
56 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
58 #define __GTHREAD_ONCE_INIT __gthrw_pthread_once_init
60 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
61 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
63 #define __GTHREAD_MUTEX_INIT_DEFAULT __gthrw_pthread_once_init
65 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
66 # define __gthrw(name) \
67 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
69 # define __gthrw_asmname(cname) __gthrw_asmnamep (__USER_LABEL_PREFIX__, cname)
70 # define __gthrw_asmnamep(prefix, cname) __gthrw_string (prefix) cname
71 # define __gthrw_string(x) #x
72 # define __gthrw(name) \
73 extern __typeof(name) __gthrw_ ## name __asm (__gthrw_asmname (#name))
76 __gthrw(pthread_once
);
77 __gthrw(pthread_once_init
);
78 __gthrw(pthread_keycreate
);
79 __gthrw(pthread_key_delete
);
80 __gthrw(pthread_getspecific
);
81 __gthrw(pthread_setspecific
);
82 __gthrw(pthread_create
);
83 __gthrw(pthread_mutex_init
);
84 __gthrw(pthread_mutex_lock
);
85 __gthrw(pthread_mutex_trylock
);
86 __gthrw(pthread_mutex_unlock
);
87 __gthrw(pthread_mutexattr_create
);
88 __gthrw(pthread_mutexattr_setkind_np
);
89 __gthrw(pthread_mutexattr_delete
);
93 __gthrw(pthread_cond_broadcast
);
94 __gthrw(pthread_cond_destroy
);
95 __gthrw(pthread_cond_init
);
96 __gthrw(pthread_cond_signal
);
97 __gthrw(pthread_cond_wait
);
98 __gthrw(pthread_exit
);
99 __gthrw(pthread_getunique_np
);
100 __gthrw(pthread_mutex_destroy
);
101 __gthrw(pthread_self
);
102 __gthrw(pthread_yield
);
105 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
108 __gthread_active_p (void)
110 static void *const __gthread_active_ptr
= (void *) &__gthrw_pthread_create
;
111 return __gthread_active_ptr
!= 0;
114 #else /* not SUPPORTS_WEAK */
117 __gthread_active_p (void)
122 #endif /* SUPPORTS_WEAK */
126 /* Key structure for maintaining thread specific storage */
127 static pthread_key_t _objc_thread_storage
;
129 /* Thread local storage for a single thread */
130 static void *thread_local_storage
= NULL
;
132 /* Backend initialization functions */
134 /* Initialize the threads subsystem. */
136 __gthread_objc_init_thread_system (void)
138 if (__gthread_active_p ())
139 /* Initialize the thread storage key. */
140 return __gthrw_pthread_keycreate (&_objc_thread_storage
, NULL
);
145 /* Close the threads subsystem. */
147 __gthread_objc_close_thread_system (void)
149 if (__gthread_active_p ())
155 /* Backend thread functions */
157 /* Create a new thread of execution. */
158 static inline objc_thread_t
159 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
161 objc_thread_t thread_id
;
162 pthread_t new_thread_handle
;
164 if (!__gthread_active_p ())
167 if (!(__gthrw_pthread_create (&new_thread_handle
, pthread_attr_default
,
168 (void *) func
, arg
)))
170 /* ??? May not work! (64bit) */
171 thread_id
= *(objc_thread_t
*) &new_thread_handle
;
172 pthread_detach (&new_thread_handle
); /* Fully detach thread. */
180 /* Set the current thread's priority. */
182 __gthread_objc_thread_set_priority (int priority
)
184 int sys_priority
= 0;
186 if (!__gthread_active_p ())
191 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
192 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
195 case OBJC_THREAD_BACKGROUND_PRIORITY
:
196 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
198 case OBJC_THREAD_LOW_PRIORITY
:
199 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
203 /* Change the priority. */
204 if (pthread_setprio (__gthrw_pthread_self (), sys_priority
) >= 0)
211 /* Return the current thread's priority. */
213 __gthread_objc_thread_get_priority (void)
217 if (__gthread_active_p ())
219 if ((sys_priority
= pthread_getprio (__gthrw_pthread_self ())) >= 0)
221 if (sys_priority
>= PRI_FG_MIN_NP
222 && sys_priority
<= PRI_FG_MAX_NP
)
223 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
224 if (sys_priority
>= PRI_BG_MIN_NP
225 && sys_priority
<= PRI_BG_MAX_NP
)
226 return OBJC_THREAD_BACKGROUND_PRIORITY
;
227 return OBJC_THREAD_LOW_PRIORITY
;
234 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
237 /* Yield our process time to another thread. */
239 __gthread_objc_thread_yield (void)
241 if (__gthread_active_p ())
242 __gthrw_pthread_yield ();
245 /* Terminate the current thread. */
247 __gthread_objc_thread_exit (void)
249 if (__gthread_active_p ())
250 /* exit the thread */
251 __gthrw_pthread_exit (&__objc_thread_exit_status
);
253 /* Failed if we reached here */
257 /* Returns an integer value which uniquely describes a thread. */
258 static inline objc_thread_t
259 __gthread_objc_thread_id (void)
261 if (__gthread_active_p ())
263 pthread_t self
= __gthrw_pthread_self ();
265 return (objc_thread_t
) __gthrw_pthread_getunique_np (&self
);
268 return (objc_thread_t
) 1;
271 /* Sets the thread's local storage pointer. */
273 __gthread_objc_thread_set_data (void *value
)
275 if (__gthread_active_p ())
276 return __gthrw_pthread_setspecific (_objc_thread_storage
, value
);
279 thread_local_storage
= value
;
284 /* Returns the thread's local storage pointer. */
286 __gthread_objc_thread_get_data (void)
290 if (__gthread_active_p ())
292 if (!(__gthrw_pthread_getspecific (_objc_thread_storage
, &value
)))
298 return thread_local_storage
;
301 /* Backend mutex functions */
303 /* Allocate a mutex. */
305 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
307 if (__gthread_active_p ())
309 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
311 if (__gthrw_pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
,
312 pthread_mutexattr_default
))
314 objc_free (mutex
->backend
);
315 mutex
->backend
= NULL
;
323 /* Deallocate a mutex. */
325 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
327 if (__gthread_active_p ())
329 if (__gthrw_pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
332 objc_free (mutex
->backend
);
333 mutex
->backend
= NULL
;
339 /* Grab a lock on a mutex. */
341 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
343 if (__gthread_active_p ())
344 return __gthrw_pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
);
349 /* Try to grab a lock on a mutex. */
351 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
353 if (__gthread_active_p ()
354 && __gthrw_pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 1)
360 /* Unlock the mutex */
362 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
364 if (__gthread_active_p ())
365 return __gthrw_pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
370 /* Backend condition mutex functions */
372 /* Allocate a condition. */
374 __gthread_objc_condition_allocate (objc_condition_t condition
)
376 if (__gthread_active_p ())
383 /* Deallocate a condition. */
385 __gthread_objc_condition_deallocate (objc_condition_t condition
)
387 if (__gthread_active_p ())
394 /* Wait on the condition */
396 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
398 if (__gthread_active_p ())
405 /* Wake up all threads waiting on this condition. */
407 __gthread_objc_condition_broadcast (objc_condition_t condition
)
409 if (__gthread_active_p ())
416 /* Wake up one thread waiting on this condition. */
418 __gthread_objc_condition_signal (objc_condition_t condition
)
420 if (__gthread_active_p ())
430 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
432 if (__gthread_active_p ())
433 return __gthrw_pthread_once (once
, func
);
439 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
441 return __gthrw_pthread_keycreate (key
, dtor
);
445 __gthread_key_delete (UNUSED (__gthread_key_t key
))
447 /* Operation is not supported. */
452 __gthread_getspecific (__gthread_key_t key
)
455 if (__gthrw_pthread_getspecific (key
, &ptr
) == 0)
462 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
464 return __gthrw_pthread_setspecific (key
, (void *) ptr
);
468 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
470 if (__gthread_active_p ())
471 __gthrw_pthread_mutex_init (mutex
, pthread_mutexattr_default
);
475 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
477 if (__gthread_active_p ())
478 return __gthrw_pthread_mutex_lock (mutex
);
484 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
486 if (__gthread_active_p ())
487 return __gthrw_pthread_mutex_trylock (mutex
);
493 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
495 if (__gthread_active_p ())
496 return __gthrw_pthread_mutex_unlock (mutex
);
502 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
504 if (__gthread_active_p ())
506 pthread_mutexattr_t attr
;
509 r
= __gthrw_pthread_mutexattr_create (&attr
);
511 r
= __gthrw_pthread_mutexattr_setkind_np (&attr
, MUTEX_RECURSIVE_NP
);
513 r
= __gthrw_pthread_mutex_init (mutex
, attr
);
515 r
= __gthrw_pthread_mutexattr_delete (&attr
);
521 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
523 return __gthread_mutex_lock (mutex
);
527 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
529 return __gthread_mutex_trylock (mutex
);
533 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
535 return __gthread_mutex_unlock (mutex
);
538 #endif /* _LIBOBJC */
543 #endif /* ! GCC_GTHR_DCE_H */