1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2004, 2005, 2008, 2009
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 3, 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 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #ifndef GCC_GTHR_DCE_H
28 #define GCC_GTHR_DCE_H
30 /* If _DCE_THREADS is not defined, then we're building the single
31 threaded version of the libraries and do not want to reference
32 anything related to pthreads or dce. */
34 #include "gthr-single.h"
36 /* DCE threads interface.
37 DCE threads are based on POSIX threads draft 4, and many things
38 have changed since then. */
40 /* Make sure CONST_CAST2 (original in system.h) is defined. */
43 #define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
45 #define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q; TOTYPE _nq;})(X))._nq)
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 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 pthread_once_init
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_keycreate
)
76 __gthrw(pthread_getspecific
)
77 __gthrw(pthread_setspecific
)
78 __gthrw(pthread_create
)
79 __gthrw(pthread_mutex_init
)
80 __gthrw(pthread_mutex_destroy
)
81 __gthrw(pthread_mutex_lock
)
82 __gthrw(pthread_mutex_trylock
)
83 __gthrw(pthread_mutex_unlock
)
84 __gthrw(pthread_mutexattr_create
)
85 __gthrw(pthread_mutexattr_setkind_np
)
86 __gthrw(pthread_mutexattr_delete
)
90 __gthrw(pthread_cond_broadcast
)
91 __gthrw(pthread_cond_destroy
)
92 __gthrw(pthread_cond_init
)
93 __gthrw(pthread_cond_signal
)
94 __gthrw(pthread_cond_wait
)
97 #ifdef pthread_getunique_np
98 # define __gthrw_pthread_getunique_np pthread_getunique_np
100 __gthrw(pthread_getunique_np
)
101 # define __gthrw_pthread_getunique_np __gthrw_(pthread_getunique_np)
104 __gthrw(pthread_mutex_destroy
)
105 __gthrw(pthread_self
)
106 __gthrw(pthread_yield
)
109 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
112 __gthread_active_p (void)
114 static void *const __gthread_active_ptr
= (void *) &__gthrw_(pthread_create
);
115 return __gthread_active_ptr
!= 0;
118 #else /* not SUPPORTS_WEAK */
121 __gthread_active_p (void)
126 #endif /* SUPPORTS_WEAK */
130 /* Key structure for maintaining thread specific storage */
131 static pthread_key_t _objc_thread_storage
;
133 /* Thread local storage for a single thread */
134 static void *thread_local_storage
= NULL
;
136 /* Backend initialization functions */
138 /* Initialize the threads subsystem. */
140 __gthread_objc_init_thread_system (void)
142 if (__gthread_active_p ())
143 /* Initialize the thread storage key. */
144 return __gthrw_(pthread_keycreate
) (&_objc_thread_storage
, NULL
);
149 /* Close the threads subsystem. */
151 __gthread_objc_close_thread_system (void)
153 if (__gthread_active_p ())
159 /* Backend thread functions */
161 /* Create a new thread of execution. */
162 static inline objc_thread_t
163 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
165 objc_thread_t thread_id
;
166 pthread_t new_thread_handle
;
168 if (!__gthread_active_p ())
171 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, pthread_attr_default
,
172 (void *) func
, arg
)))
174 /* ??? May not work! (64bit) */
175 thread_id
= *(objc_thread_t
*) &new_thread_handle
;
176 pthread_detach (&new_thread_handle
); /* Fully detach thread. */
184 /* Set the current thread's priority. */
186 __gthread_objc_thread_set_priority (int priority
)
188 int sys_priority
= 0;
190 if (!__gthread_active_p ())
195 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
196 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
199 case OBJC_THREAD_BACKGROUND_PRIORITY
:
200 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
202 case OBJC_THREAD_LOW_PRIORITY
:
203 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
207 /* Change the priority. */
208 if (pthread_setprio (__gthrw_(pthread_self
) (), sys_priority
) >= 0)
215 /* Return the current thread's priority. */
217 __gthread_objc_thread_get_priority (void)
221 if (__gthread_active_p ())
223 if ((sys_priority
= pthread_getprio (__gthrw_(pthread_self
) ())) >= 0)
225 if (sys_priority
>= PRI_FG_MIN_NP
226 && sys_priority
<= PRI_FG_MAX_NP
)
227 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
228 if (sys_priority
>= PRI_BG_MIN_NP
229 && sys_priority
<= PRI_BG_MAX_NP
)
230 return OBJC_THREAD_BACKGROUND_PRIORITY
;
231 return OBJC_THREAD_LOW_PRIORITY
;
238 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
241 /* Yield our process time to another thread. */
243 __gthread_objc_thread_yield (void)
245 if (__gthread_active_p ())
246 __gthrw_(pthread_yield
) ();
249 /* Terminate the current thread. */
251 __gthread_objc_thread_exit (void)
253 if (__gthread_active_p ())
254 /* exit the thread */
255 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
257 /* Failed if we reached here */
261 /* Returns an integer value which uniquely describes a thread. */
262 static inline objc_thread_t
263 __gthread_objc_thread_id (void)
265 if (__gthread_active_p ())
267 pthread_t self
= __gthrw_(pthread_self
) ();
269 return (objc_thread_t
) __gthrw_pthread_getunique_np (&self
);
272 return (objc_thread_t
) 1;
275 /* Sets the thread's local storage pointer. */
277 __gthread_objc_thread_set_data (void *value
)
279 if (__gthread_active_p ())
280 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
283 thread_local_storage
= value
;
288 /* Returns the thread's local storage pointer. */
290 __gthread_objc_thread_get_data (void)
294 if (__gthread_active_p ())
296 if (!(__gthrw_(pthread_getspecific
) (_objc_thread_storage
, &value
)))
302 return thread_local_storage
;
305 /* Backend mutex functions */
307 /* Allocate a mutex. */
309 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
311 if (__gthread_active_p ())
313 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
315 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
,
316 pthread_mutexattr_default
))
318 objc_free (mutex
->backend
);
319 mutex
->backend
= NULL
;
327 /* Deallocate a mutex. */
329 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
331 if (__gthread_active_p ())
333 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
336 objc_free (mutex
->backend
);
337 mutex
->backend
= NULL
;
343 /* Grab a lock on a mutex. */
345 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
347 if (__gthread_active_p ())
348 return __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
);
353 /* Try to grab a lock on a mutex. */
355 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
357 if (__gthread_active_p ()
358 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 1)
364 /* Unlock the mutex */
366 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
368 if (__gthread_active_p ())
369 return __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
374 /* Backend condition mutex functions */
376 /* Allocate a condition. */
378 __gthread_objc_condition_allocate (objc_condition_t condition
379 __attribute__ ((__unused__
)))
381 if (__gthread_active_p ())
388 /* Deallocate a condition. */
390 __gthread_objc_condition_deallocate (objc_condition_t condition
391 __attribute__ ((__unused__
)))
393 if (__gthread_active_p ())
400 /* Wait on the condition */
402 __gthread_objc_condition_wait (objc_condition_t condition
403 __attribute__ ((__unused__
)),
404 objc_mutex_t mutex
__attribute__ ((__unused__
)))
406 if (__gthread_active_p ())
413 /* Wake up all threads waiting on this condition. */
415 __gthread_objc_condition_broadcast (objc_condition_t condition
416 __attribute__ ((__unused__
)))
418 if (__gthread_active_p ())
425 /* Wake up one thread waiting on this condition. */
427 __gthread_objc_condition_signal (objc_condition_t condition
428 __attribute__ ((__unused__
)))
430 if (__gthread_active_p ())
440 __gthread_once (__gthread_once_t
*__once
, void (*__func
) (void))
442 if (__gthread_active_p ())
443 return __gthrw_(pthread_once
) (__once
, __func
);
449 __gthread_key_create (__gthread_key_t
*__key
, void (*__dtor
) (void *))
451 return __gthrw_(pthread_keycreate
) (__key
, __dtor
);
455 __gthread_key_delete (__gthread_key_t __key
__attribute__ ((__unused__
)))
457 /* Operation is not supported. */
462 __gthread_getspecific (__gthread_key_t __key
)
465 if (__gthrw_(pthread_getspecific
) (__key
, &__ptr
) == 0)
472 __gthread_setspecific (__gthread_key_t __key
, const void *__ptr
)
474 return __gthrw_(pthread_setspecific
)
475 (__key
, CONST_CAST2(void *, const void *, __ptr
));
479 __gthread_mutex_init_function (__gthread_mutex_t
*__mutex
)
481 if (__gthread_active_p ())
482 __gthrw_(pthread_mutex_init
) (__mutex
, pthread_mutexattr_default
);
486 __gthread_mutex_destroy (__gthread_mutex_t
*__mutex
)
488 if (__gthread_active_p ())
489 return __gthrw_(pthread_mutex_destroy
) (__mutex
);
495 __gthread_mutex_lock (__gthread_mutex_t
*__mutex
)
497 if (__gthread_active_p ())
498 return __gthrw_(pthread_mutex_lock
) (__mutex
);
504 __gthread_mutex_trylock (__gthread_mutex_t
*__mutex
)
506 if (__gthread_active_p ())
507 return __gthrw_(pthread_mutex_trylock
) (__mutex
);
513 __gthread_mutex_unlock (__gthread_mutex_t
*__mutex
)
515 if (__gthread_active_p ())
516 return __gthrw_(pthread_mutex_unlock
) (__mutex
);
522 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*__mutex
)
524 if (__gthread_active_p ())
526 pthread_mutexattr_t __attr
;
529 __r
= __gthrw_(pthread_mutexattr_create
) (&__attr
);
531 __r
= __gthrw_(pthread_mutexattr_setkind_np
) (&__attr
,
534 __r
= __gthrw_(pthread_mutex_init
) (__mutex
, __attr
);
536 __r
= __gthrw_(pthread_mutexattr_delete
) (&__attr
);
543 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*__mutex
)
545 return __gthread_mutex_lock (__mutex
);
549 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*__mutex
)
551 return __gthread_mutex_trylock (__mutex
);
555 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*__mutex
)
557 return __gthread_mutex_unlock (__mutex
);
560 #endif /* _LIBOBJC */
563 #endif /* ! GCC_GTHR_DCE_H */