1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2004 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_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
77 #pragma weak pthread_mutexattr_create
78 #pragma weak pthread_mutexattr_setkind_np
79 #pragma weak pthread_mutexattr_delete
83 #pragma weak pthread_cond_broadcast
84 #pragma weak pthread_cond_destroy
85 #pragma weak pthread_cond_init
86 #pragma weak pthread_cond_signal
87 #pragma weak pthread_cond_wait
88 #pragma weak pthread_exit
89 #pragma weak pthread_getunique_np
90 #pragma weak pthread_mutex_destroy
91 #pragma weak pthread_self
92 #pragma weak pthread_yield
96 __gthread_active_p (void)
98 static void *const __gthread_active_ptr
= (void *) &pthread_create
;
99 return __gthread_active_ptr
!= 0;
102 #else /* not SUPPORTS_WEAK */
105 __gthread_active_p (void)
110 #endif /* SUPPORTS_WEAK */
114 /* Key structure for maintaining thread specific storage */
115 static pthread_key_t _objc_thread_storage
;
117 /* Thread local storage for a single thread */
118 static void *thread_local_storage
= NULL
;
120 /* Backend initialization functions */
122 /* Initialize the threads subsystem. */
124 __gthread_objc_init_thread_system (void)
126 if (__gthread_active_p ())
127 /* Initialize the thread storage key. */
128 return pthread_keycreate (&_objc_thread_storage
, NULL
);
133 /* Close the threads subsystem. */
135 __gthread_objc_close_thread_system (void)
137 if (__gthread_active_p ())
143 /* Backend thread functions */
145 /* Create a new thread of execution. */
146 static inline objc_thread_t
147 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
149 objc_thread_t thread_id
;
150 pthread_t new_thread_handle
;
152 if (!__gthread_active_p ())
155 if (!(pthread_create (&new_thread_handle
, pthread_attr_default
,
156 (void *) func
, arg
)))
158 /* ??? May not work! (64bit) */
159 thread_id
= *(objc_thread_t
*) &new_thread_handle
;
160 pthread_detach (&new_thread_handle
); /* Fully detach thread. */
168 /* Set the current thread's priority. */
170 __gthread_objc_thread_set_priority (int priority
)
172 int sys_priority
= 0;
174 if (!__gthread_active_p ())
179 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
180 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
183 case OBJC_THREAD_BACKGROUND_PRIORITY
:
184 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
186 case OBJC_THREAD_LOW_PRIORITY
:
187 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
191 /* Change the priority. */
192 if (pthread_setprio (pthread_self (), sys_priority
) >= 0)
199 /* Return the current thread's priority. */
201 __gthread_objc_thread_get_priority (void)
205 if (__gthread_active_p ())
207 if ((sys_priority
= pthread_getprio (pthread_self ())) >= 0)
209 if (sys_priority
>= PRI_FG_MIN_NP
210 && sys_priority
<= PRI_FG_MAX_NP
)
211 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
212 if (sys_priority
>= PRI_BG_MIN_NP
213 && sys_priority
<= PRI_BG_MAX_NP
)
214 return OBJC_THREAD_BACKGROUND_PRIORITY
;
215 return OBJC_THREAD_LOW_PRIORITY
;
222 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
225 /* Yield our process time to another thread. */
227 __gthread_objc_thread_yield (void)
229 if (__gthread_active_p ())
233 /* Terminate the current thread. */
235 __gthread_objc_thread_exit (void)
237 if (__gthread_active_p ())
238 /* exit the thread */
239 pthread_exit (&__objc_thread_exit_status
);
241 /* Failed if we reached here */
245 /* Returns an integer value which uniquely describes a thread. */
246 static inline objc_thread_t
247 __gthread_objc_thread_id (void)
249 if (__gthread_active_p ())
251 pthread_t self
= pthread_self ();
253 return (objc_thread_t
) pthread_getunique_np (&self
);
256 return (objc_thread_t
) 1;
259 /* Sets the thread's local storage pointer. */
261 __gthread_objc_thread_set_data (void *value
)
263 if (__gthread_active_p ())
264 return pthread_setspecific (_objc_thread_storage
, value
);
267 thread_local_storage
= value
;
272 /* Returns the thread's local storage pointer. */
274 __gthread_objc_thread_get_data (void)
278 if (__gthread_active_p ())
280 if (!(pthread_getspecific (_objc_thread_storage
, &value
)))
286 return thread_local_storage
;
289 /* Backend mutex functions */
291 /* Allocate a mutex. */
293 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
295 if (__gthread_active_p ())
297 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
299 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
,
300 pthread_mutexattr_default
))
302 objc_free (mutex
->backend
);
303 mutex
->backend
= NULL
;
311 /* Deallocate a mutex. */
313 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
315 if (__gthread_active_p ())
317 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
320 objc_free (mutex
->backend
);
321 mutex
->backend
= NULL
;
327 /* Grab a lock on a mutex. */
329 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
331 if (__gthread_active_p ())
332 return pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
);
337 /* Try to grab a lock on a mutex. */
339 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
341 if (__gthread_active_p ()
342 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 1)
348 /* Unlock the mutex */
350 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
352 if (__gthread_active_p ())
353 return pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
358 /* Backend condition mutex functions */
360 /* Allocate a condition. */
362 __gthread_objc_condition_allocate (objc_condition_t condition
)
364 if (__gthread_active_p ())
371 /* Deallocate a condition. */
373 __gthread_objc_condition_deallocate (objc_condition_t condition
)
375 if (__gthread_active_p ())
382 /* Wait on the condition */
384 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
386 if (__gthread_active_p ())
393 /* Wake up all threads waiting on this condition. */
395 __gthread_objc_condition_broadcast (objc_condition_t condition
)
397 if (__gthread_active_p ())
404 /* Wake up one thread waiting on this condition. */
406 __gthread_objc_condition_signal (objc_condition_t condition
)
408 if (__gthread_active_p ())
418 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
420 if (__gthread_active_p ())
421 return pthread_once (once
, func
);
427 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
429 return pthread_keycreate (key
, dtor
);
433 __gthread_key_delete (UNUSED (__gthread_key_t key
))
435 /* Operation is not supported. */
440 __gthread_getspecific (__gthread_key_t key
)
443 if (pthread_getspecific (key
, &ptr
) == 0)
450 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
452 return pthread_setspecific (key
, (void *) ptr
);
456 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
458 if (__gthread_active_p ())
459 pthread_mutex_init (mutex
, pthread_mutexattr_default
);
463 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
465 if (__gthread_active_p ())
466 return pthread_mutex_lock (mutex
);
472 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
474 if (__gthread_active_p ())
475 return pthread_mutex_trylock (mutex
);
481 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
483 if (__gthread_active_p ())
484 return pthread_mutex_unlock (mutex
);
490 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
492 if (__gthread_active_p ())
494 pthread_mutexattr_t attr
;
497 r
= pthread_mutexattr_create (&attr
);
499 r
= pthread_mutexattr_setkind_np (&attr
, MUTEX_RECURSIVE_NP
);
501 r
= pthread_mutex_init (mutex
, attr
);
503 r
= pthread_mutexattr_delete (&attr
);
509 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
511 return __gthread_mutex_lock (mutex
);
515 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
517 return __gthread_mutex_trylock (mutex
);
521 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
523 return __gthread_mutex_unlock (mutex
);
526 #endif /* _LIBOBJC */
531 #endif /* ! GCC_GTHR_DCE_H */