1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001 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, 59 Temple Place - Suite 330, 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_DCE_H
30 #define GCC_GTHR_DCE_H
32 /* If _DCE_THREADS is not defined, then we're building the single
33 threaded version of the libraries and do not want to reference
34 anything related to pthreads or dce. */
36 #include "gthr-single.h"
38 /* DCE threads interface.
39 DCE threads are based on POSIX threads draft 4, and many things
40 have changed since then. */
49 #define UNUSED(x) x __attribute__((unused))
52 typedef pthread_key_t __gthread_key_t
;
53 typedef pthread_once_t __gthread_once_t
;
54 typedef pthread_mutex_t __gthread_mutex_t
;
55 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
57 #define __GTHREAD_ONCE_INIT pthread_once_init
59 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
62 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
64 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
66 #pragma weak pthread_once
67 #pragma weak pthread_once_init
68 #pragma weak pthread_keycreate
69 #pragma weak pthread_key_delete
70 #pragma weak pthread_getspecific
71 #pragma weak pthread_setspecific
72 #pragma weak pthread_create
73 #pragma weak pthread_mutex_init
74 #pragma weak pthread_mutex_lock
75 #pragma weak pthread_mutex_trylock
76 #pragma weak pthread_mutex_unlock
80 #pragma weak pthread_cond_broadcast
81 #pragma weak pthread_cond_destroy
82 #pragma weak pthread_cond_init
83 #pragma weak pthread_cond_signal
84 #pragma weak pthread_cond_wait
85 #pragma weak pthread_exit
86 #pragma weak pthread_getunique_np
87 #pragma weak pthread_mutex_destroy
88 #pragma weak pthread_self
89 #pragma weak pthread_yield
93 __gthread_active_p (void)
95 static void *const __gthread_active_ptr
= (void *) &pthread_create
;
96 return __gthread_active_ptr
!= 0;
99 #else /* not SUPPORTS_WEAK */
102 __gthread_active_p (void)
107 #endif /* SUPPORTS_WEAK */
111 /* Key structure for maintaining thread specific storage */
112 static pthread_key_t _objc_thread_storage
;
114 /* Thread local storage for a single thread */
115 static void *thread_local_storage
= NULL
;
117 /* Backend initialization functions */
119 /* Initialize the threads subsystem. */
121 __gthread_objc_init_thread_system (void)
123 if (__gthread_active_p ())
124 /* Initialize the thread storage key */
125 return pthread_keycreate (&_objc_thread_storage
, NULL
);
130 /* Close the threads subsystem. */
132 __gthread_objc_close_thread_system (void)
134 if (__gthread_active_p ())
140 /* Backend thread functions */
142 /* Create a new thread of execution. */
143 static inline objc_thread_t
144 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
146 objc_thread_t thread_id
;
147 pthread_t new_thread_handle
;
149 if (!__gthread_active_p ())
152 if (!(pthread_create (&new_thread_handle
, pthread_attr_default
,
153 (void *) func
, arg
)))
155 /* ??? May not work! (64bit) */
156 thread_id
= *(objc_thread_t
*) &new_thread_handle
;
157 pthread_detach (&new_thread_handle
); /* Fully detach thread. */
165 /* Set the current thread's priority. */
167 __gthread_objc_thread_set_priority (int priority
)
169 int sys_priority
= 0;
171 if (!__gthread_active_p ())
176 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
177 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
180 case OBJC_THREAD_BACKGROUND_PRIORITY
:
181 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
183 case OBJC_THREAD_LOW_PRIORITY
:
184 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
188 /* Change the priority. */
189 if (pthread_setprio (pthread_self (), sys_priority
) >= 0)
196 /* Return the current thread's priority. */
198 __gthread_objc_thread_get_priority (void)
202 if (__gthread_active_p ())
204 if ((sys_priority
= pthread_getprio (pthread_self ())) >= 0)
206 if (sys_priority
>= PRI_FG_MIN_NP
207 && sys_priority
<= PRI_FG_MAX_NP
)
208 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
209 if (sys_priority
>= PRI_BG_MIN_NP
210 && sys_priority
<= PRI_BG_MAX_NP
)
211 return OBJC_THREAD_BACKGROUND_PRIORITY
;
212 return OBJC_THREAD_LOW_PRIORITY
;
219 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
222 /* Yield our process time to another thread. */
224 __gthread_objc_thread_yield (void)
226 if (__gthread_active_p ())
230 /* Terminate the current thread. */
232 __gthread_objc_thread_exit (void)
234 if (__gthread_active_p ())
235 /* exit the thread */
236 pthread_exit (&__objc_thread_exit_status
);
238 /* Failed if we reached here */
242 /* Returns an integer value which uniquely describes a thread. */
243 static inline objc_thread_t
244 __gthread_objc_thread_id (void)
246 if (__gthread_active_p ())
248 pthread_t self
= pthread_self ();
250 return (objc_thread_t
) pthread_getunique_np (&self
);
253 return (objc_thread_t
) 1;
256 /* Sets the thread's local storage pointer. */
258 __gthread_objc_thread_set_data (void *value
)
260 if (__gthread_active_p ())
261 return pthread_setspecific (_objc_thread_storage
, value
);
264 thread_local_storage
= value
;
269 /* Returns the thread's local storage pointer. */
271 __gthread_objc_thread_get_data (void)
275 if (__gthread_active_p ())
277 if (!(pthread_getspecific (_objc_thread_storage
, &value
)))
283 return thread_local_storage
;
286 /* Backend mutex functions */
288 /* Allocate a mutex. */
290 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
292 if (__gthread_active_p ())
294 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
296 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
,
297 pthread_mutexattr_default
))
299 objc_free (mutex
->backend
);
300 mutex
->backend
= NULL
;
308 /* Deallocate a mutex. */
310 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
312 if (__gthread_active_p ())
314 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
317 objc_free (mutex
->backend
);
318 mutex
->backend
= NULL
;
324 /* Grab a lock on a mutex. */
326 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
328 if (__gthread_active_p ())
329 return pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
);
334 /* Try to grab a lock on a mutex. */
336 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
338 if (__gthread_active_p ()
339 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 1)
345 /* Unlock the mutex */
347 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
349 if (__gthread_active_p ())
350 return pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
355 /* Backend condition mutex functions */
357 /* Allocate a condition. */
359 __gthread_objc_condition_allocate (objc_condition_t condition
)
361 if (__gthread_active_p ())
368 /* Deallocate a condition. */
370 __gthread_objc_condition_deallocate (objc_condition_t condition
)
372 if (__gthread_active_p ())
379 /* Wait on the condition */
381 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
383 if (__gthread_active_p ())
390 /* Wake up all threads waiting on this condition. */
392 __gthread_objc_condition_broadcast (objc_condition_t condition
)
394 if (__gthread_active_p ())
401 /* Wake up one thread waiting on this condition. */
403 __gthread_objc_condition_signal (objc_condition_t condition
)
405 if (__gthread_active_p ())
415 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
417 if (__gthread_active_p ())
418 return pthread_once (once
, func
);
424 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
426 return pthread_keycreate (key
, dtor
);
430 __gthread_key_delete (UNUSED (__gthread_key_t key
))
432 /* Operation is not supported. */
437 __gthread_getspecific (__gthread_key_t key
)
440 if (pthread_getspecific (key
, &ptr
) == 0)
447 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
449 return pthread_setspecific (key
, (void *) ptr
);
453 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
455 if (__gthread_active_p ())
456 pthread_mutex_init (mutex
, pthread_mutexattr_default
);
460 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
462 if (__gthread_active_p ())
463 return pthread_mutex_lock (mutex
);
469 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
471 if (__gthread_active_p ())
472 return pthread_mutex_trylock (mutex
);
478 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
480 if (__gthread_active_p ())
481 return pthread_mutex_unlock (mutex
);
487 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
489 if (__gthread_active_p ())
491 pthread_mutexattr_t attr
;
494 r
= pthread_mutexattr_create (&attr
);
496 r
= pthread_mutexattr_setkind_np (&attr
, MUTEX_RECURSIVE_NP
);
498 r
= pthread_mutex_init (mutex
, attr
);
500 r
= pthread_mutexattr_delete (&attr
);
506 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
508 return __gthread_mutex_lock (mutex
);
512 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
514 return __gthread_mutex_trylock (mutex
);
518 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
520 return __gthread_mutex_unlock (mutex
);
523 #endif /* _LIBOBJC */
528 #endif /* ! GCC_GTHR_DCE_H */