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. */
47 typedef pthread_key_t __gthread_key_t
;
48 typedef pthread_once_t __gthread_once_t
;
49 typedef pthread_mutex_t __gthread_mutex_t
;
50 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
52 #define __GTHREAD_ONCE_INIT pthread_once_init
54 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
55 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
57 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
62 # define __gthrw_(name) __gthrw_ ## name
64 # define __gthrw(name)
65 # define __gthrw_(name) name
69 __gthrw(pthread_keycreate
)
70 __gthrw(pthread_getspecific
)
71 __gthrw(pthread_setspecific
)
72 __gthrw(pthread_create
)
73 __gthrw(pthread_mutex_init
)
74 __gthrw(pthread_mutex_lock
)
75 __gthrw(pthread_mutex_trylock
)
76 __gthrw(pthread_mutex_unlock
)
77 __gthrw(pthread_mutexattr_create
)
78 __gthrw(pthread_mutexattr_setkind_np
)
79 __gthrw(pthread_mutexattr_delete
)
83 __gthrw(pthread_cond_broadcast
)
84 __gthrw(pthread_cond_destroy
)
85 __gthrw(pthread_cond_init
)
86 __gthrw(pthread_cond_signal
)
87 __gthrw(pthread_cond_wait
)
90 #ifdef pthread_getunique_np
91 # define __gthrw_pthread_getunique_np pthread_getunique_np
93 __gthrw(pthread_getunique_np
)
94 # define __gthrw_pthread_getunique_np __gthrw_(pthread_getunique_np)
97 __gthrw(pthread_mutex_destroy
)
99 __gthrw(pthread_yield
)
102 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
105 __gthread_active_p (void)
107 static void *const __gthread_active_ptr
= (void *) &__gthrw_(pthread_create
);
108 return __gthread_active_ptr
!= 0;
111 #else /* not SUPPORTS_WEAK */
114 __gthread_active_p (void)
119 #endif /* SUPPORTS_WEAK */
123 /* Key structure for maintaining thread specific storage */
124 static pthread_key_t _objc_thread_storage
;
126 /* Thread local storage for a single thread */
127 static void *thread_local_storage
= NULL
;
129 /* Backend initialization functions */
131 /* Initialize the threads subsystem. */
133 __gthread_objc_init_thread_system (void)
135 if (__gthread_active_p ())
136 /* Initialize the thread storage key. */
137 return __gthrw_(pthread_keycreate
) (&_objc_thread_storage
, NULL
);
142 /* Close the threads subsystem. */
144 __gthread_objc_close_thread_system (void)
146 if (__gthread_active_p ())
152 /* Backend thread functions */
154 /* Create a new thread of execution. */
155 static inline objc_thread_t
156 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
158 objc_thread_t thread_id
;
159 pthread_t new_thread_handle
;
161 if (!__gthread_active_p ())
164 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, pthread_attr_default
,
165 (void *) func
, arg
)))
167 /* ??? May not work! (64bit) */
168 thread_id
= *(objc_thread_t
*) &new_thread_handle
;
169 pthread_detach (&new_thread_handle
); /* Fully detach thread. */
177 /* Set the current thread's priority. */
179 __gthread_objc_thread_set_priority (int priority
)
181 int sys_priority
= 0;
183 if (!__gthread_active_p ())
188 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
189 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
192 case OBJC_THREAD_BACKGROUND_PRIORITY
:
193 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
195 case OBJC_THREAD_LOW_PRIORITY
:
196 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
200 /* Change the priority. */
201 if (pthread_setprio (__gthrw_(pthread_self
) (), sys_priority
) >= 0)
208 /* Return the current thread's priority. */
210 __gthread_objc_thread_get_priority (void)
214 if (__gthread_active_p ())
216 if ((sys_priority
= pthread_getprio (__gthrw_(pthread_self
) ())) >= 0)
218 if (sys_priority
>= PRI_FG_MIN_NP
219 && sys_priority
<= PRI_FG_MAX_NP
)
220 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
221 if (sys_priority
>= PRI_BG_MIN_NP
222 && sys_priority
<= PRI_BG_MAX_NP
)
223 return OBJC_THREAD_BACKGROUND_PRIORITY
;
224 return OBJC_THREAD_LOW_PRIORITY
;
231 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
234 /* Yield our process time to another thread. */
236 __gthread_objc_thread_yield (void)
238 if (__gthread_active_p ())
239 __gthrw_(pthread_yield
) ();
242 /* Terminate the current thread. */
244 __gthread_objc_thread_exit (void)
246 if (__gthread_active_p ())
247 /* exit the thread */
248 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
250 /* Failed if we reached here */
254 /* Returns an integer value which uniquely describes a thread. */
255 static inline objc_thread_t
256 __gthread_objc_thread_id (void)
258 if (__gthread_active_p ())
260 pthread_t self
= __gthrw_(pthread_self
) ();
262 return (objc_thread_t
) __gthrw_pthread_getunique_np (&self
);
265 return (objc_thread_t
) 1;
268 /* Sets the thread's local storage pointer. */
270 __gthread_objc_thread_set_data (void *value
)
272 if (__gthread_active_p ())
273 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
276 thread_local_storage
= value
;
281 /* Returns the thread's local storage pointer. */
283 __gthread_objc_thread_get_data (void)
287 if (__gthread_active_p ())
289 if (!(__gthrw_(pthread_getspecific
) (_objc_thread_storage
, &value
)))
295 return thread_local_storage
;
298 /* Backend mutex functions */
300 /* Allocate a mutex. */
302 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
304 if (__gthread_active_p ())
306 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
308 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
,
309 pthread_mutexattr_default
))
311 objc_free (mutex
->backend
);
312 mutex
->backend
= NULL
;
320 /* Deallocate a mutex. */
322 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
324 if (__gthread_active_p ())
326 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
329 objc_free (mutex
->backend
);
330 mutex
->backend
= NULL
;
336 /* Grab a lock on a mutex. */
338 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
340 if (__gthread_active_p ())
341 return __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
);
346 /* Try to grab a lock on a mutex. */
348 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
350 if (__gthread_active_p ()
351 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 1)
357 /* Unlock the mutex */
359 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
361 if (__gthread_active_p ())
362 return __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
367 /* Backend condition mutex functions */
369 /* Allocate a condition. */
371 __gthread_objc_condition_allocate (objc_condition_t condition
372 __attribute__ ((__unused__
)))
374 if (__gthread_active_p ())
381 /* Deallocate a condition. */
383 __gthread_objc_condition_deallocate (objc_condition_t condition
384 __attribute__ ((__unused__
)))
386 if (__gthread_active_p ())
393 /* Wait on the condition */
395 __gthread_objc_condition_wait (objc_condition_t condition
396 __attribute__ ((__unused__
)),
397 objc_mutex_t mutex
__attribute__ ((__unused__
)))
399 if (__gthread_active_p ())
406 /* Wake up all threads waiting on this condition. */
408 __gthread_objc_condition_broadcast (objc_condition_t condition
409 __attribute__ ((__unused__
)))
411 if (__gthread_active_p ())
418 /* Wake up one thread waiting on this condition. */
420 __gthread_objc_condition_signal (objc_condition_t condition
421 __attribute__ ((__unused__
)))
423 if (__gthread_active_p ())
433 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
435 if (__gthread_active_p ())
436 return __gthrw_(pthread_once
) (once
, func
);
442 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
444 return __gthrw_(pthread_keycreate
) (key
, dtor
);
448 __gthread_key_delete (__gthread_key_t key
__attribute__ ((__unused__
)))
450 /* Operation is not supported. */
455 __gthread_getspecific (__gthread_key_t key
)
458 if (__gthrw_(pthread_getspecific
) (key
, &ptr
) == 0)
465 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
467 return __gthrw_(pthread_setspecific
) (key
, (void *) ptr
);
471 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
473 if (__gthread_active_p ())
474 __gthrw_(pthread_mutex_init
) (mutex
, pthread_mutexattr_default
);
478 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
480 if (__gthread_active_p ())
481 return __gthrw_(pthread_mutex_lock
) (mutex
);
487 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
489 if (__gthread_active_p ())
490 return __gthrw_(pthread_mutex_trylock
) (mutex
);
496 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
498 if (__gthread_active_p ())
499 return __gthrw_(pthread_mutex_unlock
) (mutex
);
505 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
507 if (__gthread_active_p ())
509 pthread_mutexattr_t attr
;
512 r
= __gthrw_(pthread_mutexattr_create
) (&attr
);
514 r
= __gthrw_(pthread_mutexattr_setkind_np
) (&attr
, MUTEX_RECURSIVE_NP
);
516 r
= __gthrw_(pthread_mutex_init
) (mutex
, attr
);
518 r
= __gthrw_(pthread_mutexattr_delete
) (&attr
);
525 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
527 return __gthread_mutex_lock (mutex
);
531 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
533 return __gthread_mutex_trylock (mutex
);
537 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
539 return __gthread_mutex_unlock (mutex
);
542 #endif /* _LIBOBJC */
545 #endif /* ! GCC_GTHR_DCE_H */