1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005 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_POSIX_H
30 #define GCC_GTHR_POSIX_H
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
37 /* Some implementations of <pthread.h> require this to be defined. */
45 typedef pthread_key_t __gthread_key_t
;
46 typedef pthread_once_t __gthread_once_t
;
47 typedef pthread_mutex_t __gthread_mutex_t
;
52 pthread_mutex_t actual
;
53 } __gthread_recursive_mutex_t
;
55 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
56 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
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_key_create
)
70 __gthrw(pthread_key_delete
)
71 __gthrw(pthread_getspecific
)
72 __gthrw(pthread_setspecific
)
73 __gthrw(pthread_create
)
74 __gthrw(pthread_cancel
)
77 __gthrw(pthread_mutex_lock
)
78 __gthrw(pthread_mutex_trylock
)
79 __gthrw(pthread_mutex_unlock
)
80 __gthrw(pthread_mutexattr_init
)
81 __gthrw(pthread_mutexattr_destroy
)
83 __gthrw(pthread_mutex_init
)
85 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
87 __gthrw(pthread_cond_broadcast
)
88 __gthrw(pthread_cond_destroy
)
89 __gthrw(pthread_cond_init
)
90 __gthrw(pthread_cond_signal
)
91 __gthrw(pthread_cond_wait
)
93 __gthrw(pthread_mutex_destroy
)
94 #ifdef _POSIX_PRIORITY_SCHEDULING
95 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
96 __gthrw(sched_get_priority_max
)
97 __gthrw(sched_get_priority_min
)
98 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
99 #endif /* _POSIX_PRIORITY_SCHEDULING */
101 __gthrw(pthread_attr_destroy
)
102 __gthrw(pthread_attr_init
)
103 __gthrw(pthread_attr_setdetachstate
)
104 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
105 __gthrw(pthread_getschedparam
)
106 __gthrw(pthread_setschedparam
)
107 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
108 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
110 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
113 __gthread_active_p (void)
115 static void *const __gthread_active_ptr
116 = __extension__ (void *) &__gthrw_(pthread_cancel
);
117 return __gthread_active_ptr
!= 0;
120 #else /* not SUPPORTS_WEAK */
123 __gthread_active_p (void)
128 #endif /* SUPPORTS_WEAK */
132 /* This is the config.h file in libobjc/ */
139 /* Key structure for maintaining thread specific storage */
140 static pthread_key_t _objc_thread_storage
;
141 static pthread_attr_t _objc_thread_attribs
;
143 /* Thread local storage for a single thread */
144 static void *thread_local_storage
= NULL
;
146 /* Backend initialization functions */
148 /* Initialize the threads subsystem. */
150 __gthread_objc_init_thread_system (void)
152 if (__gthread_active_p ())
154 /* Initialize the thread storage key. */
155 if (__gthrw_(pthread_key_create
) (&_objc_thread_storage
, NULL
) == 0)
157 /* The normal default detach state for threads is
158 * PTHREAD_CREATE_JOINABLE which causes threads to not die
159 * when you think they should. */
160 if (__gthrw_(pthread_attr_init
) (&_objc_thread_attribs
) == 0
161 && __gthrw_(pthread_attr_setdetachstate
) (&_objc_thread_attribs
,
162 PTHREAD_CREATE_DETACHED
) == 0)
170 /* Close the threads subsystem. */
172 __gthread_objc_close_thread_system (void)
174 if (__gthread_active_p ()
175 && __gthrw_(pthread_key_delete
) (_objc_thread_storage
) == 0
176 && __gthrw_(pthread_attr_destroy
) (&_objc_thread_attribs
) == 0)
182 /* Backend thread functions */
184 /* Create a new thread of execution. */
185 static inline objc_thread_t
186 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
188 objc_thread_t thread_id
;
189 pthread_t new_thread_handle
;
191 if (!__gthread_active_p ())
194 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, NULL
, (void *) func
, arg
)))
195 thread_id
= (objc_thread_t
) new_thread_handle
;
202 /* Set the current thread's priority. */
204 __gthread_objc_thread_set_priority (int priority
)
206 if (!__gthread_active_p ())
210 #ifdef _POSIX_PRIORITY_SCHEDULING
211 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
212 pthread_t thread_id
= __gthrw_(pthread_self
) ();
214 struct sched_param params
;
215 int priority_min
, priority_max
;
217 if (__gthrw_(pthread_getschedparam
) (thread_id
, &policy
, ¶ms
) == 0)
219 if ((priority_max
= __gthrw_(sched_get_priority_max
) (policy
)) == -1)
222 if ((priority_min
= __gthrw_(sched_get_priority_min
) (policy
)) == -1)
225 if (priority
> priority_max
)
226 priority
= priority_max
;
227 else if (priority
< priority_min
)
228 priority
= priority_min
;
229 params
.sched_priority
= priority
;
232 * The solaris 7 and several other man pages incorrectly state that
233 * this should be a pointer to policy but pthread.h is universally
236 if (__gthrw_(pthread_setschedparam
) (thread_id
, policy
, ¶ms
) == 0)
239 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
240 #endif /* _POSIX_PRIORITY_SCHEDULING */
245 /* Return the current thread's priority. */
247 __gthread_objc_thread_get_priority (void)
249 #ifdef _POSIX_PRIORITY_SCHEDULING
250 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
251 if (__gthread_active_p ())
254 struct sched_param params
;
256 if (__gthrw_(pthread_getschedparam
) (__gthrw_(pthread_self
) (), &policy
, ¶ms
) == 0)
257 return params
.sched_priority
;
262 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
263 #endif /* _POSIX_PRIORITY_SCHEDULING */
264 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
267 /* Yield our process time to another thread. */
269 __gthread_objc_thread_yield (void)
271 if (__gthread_active_p ())
272 __gthrw_(sched_yield
) ();
275 /* Terminate the current thread. */
277 __gthread_objc_thread_exit (void)
279 if (__gthread_active_p ())
280 /* exit the thread */
281 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
283 /* Failed if we reached here */
287 /* Returns an integer value which uniquely describes a thread. */
288 static inline objc_thread_t
289 __gthread_objc_thread_id (void)
291 if (__gthread_active_p ())
292 return (objc_thread_t
) __gthrw_(pthread_self
) ();
294 return (objc_thread_t
) 1;
297 /* Sets the thread's local storage pointer. */
299 __gthread_objc_thread_set_data (void *value
)
301 if (__gthread_active_p ())
302 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
305 thread_local_storage
= value
;
310 /* Returns the thread's local storage pointer. */
312 __gthread_objc_thread_get_data (void)
314 if (__gthread_active_p ())
315 return __gthrw_(pthread_getspecific
) (_objc_thread_storage
);
317 return thread_local_storage
;
320 /* Backend mutex functions */
322 /* Allocate a mutex. */
324 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
326 if (__gthread_active_p ())
328 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
330 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
, NULL
))
332 objc_free (mutex
->backend
);
333 mutex
->backend
= NULL
;
341 /* Deallocate a mutex. */
343 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
345 if (__gthread_active_p ())
350 * Posix Threads specifically require that the thread be unlocked
351 * for __gthrw_(pthread_mutex_destroy) to work.
356 count
= __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
362 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
365 objc_free (mutex
->backend
);
366 mutex
->backend
= NULL
;
371 /* Grab a lock on a mutex. */
373 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
375 if (__gthread_active_p ()
376 && __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
384 /* Try to grab a lock on a mutex. */
386 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
388 if (__gthread_active_p ()
389 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
397 /* Unlock the mutex */
399 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
401 if (__gthread_active_p ()
402 && __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
410 /* Backend condition mutex functions */
412 /* Allocate a condition. */
414 __gthread_objc_condition_allocate (objc_condition_t condition
)
416 if (__gthread_active_p ())
418 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
420 if (__gthrw_(pthread_cond_init
) ((pthread_cond_t
*) condition
->backend
, NULL
))
422 objc_free (condition
->backend
);
423 condition
->backend
= NULL
;
431 /* Deallocate a condition. */
433 __gthread_objc_condition_deallocate (objc_condition_t condition
)
435 if (__gthread_active_p ())
437 if (__gthrw_(pthread_cond_destroy
) ((pthread_cond_t
*) condition
->backend
))
440 objc_free (condition
->backend
);
441 condition
->backend
= NULL
;
446 /* Wait on the condition */
448 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
450 if (__gthread_active_p ())
451 return __gthrw_(pthread_cond_wait
) ((pthread_cond_t
*) condition
->backend
,
452 (pthread_mutex_t
*) mutex
->backend
);
457 /* Wake up all threads waiting on this condition. */
459 __gthread_objc_condition_broadcast (objc_condition_t condition
)
461 if (__gthread_active_p ())
462 return __gthrw_(pthread_cond_broadcast
) ((pthread_cond_t
*) condition
->backend
);
467 /* Wake up one thread waiting on this condition. */
469 __gthread_objc_condition_signal (objc_condition_t condition
)
471 if (__gthread_active_p ())
472 return __gthrw_(pthread_cond_signal
) ((pthread_cond_t
*) condition
->backend
);
480 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
482 if (__gthread_active_p ())
483 return __gthrw_(pthread_once
) (once
, func
);
489 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
491 return __gthrw_(pthread_key_create
) (key
, dtor
);
495 __gthread_key_delete (__gthread_key_t key
)
497 return __gthrw_(pthread_key_delete
) (key
);
501 __gthread_getspecific (__gthread_key_t key
)
503 return __gthrw_(pthread_getspecific
) (key
);
507 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
509 return __gthrw_(pthread_setspecific
) (key
, ptr
);
513 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
515 if (__gthread_active_p ())
516 return __gthrw_(pthread_mutex_lock
) (mutex
);
522 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
524 if (__gthread_active_p ())
525 return __gthrw_(pthread_mutex_trylock
) (mutex
);
531 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
533 if (__gthread_active_p ())
534 return __gthrw_(pthread_mutex_unlock
) (mutex
);
540 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
543 mutex
->owner
= (pthread_t
) 0;
544 return __gthrw_(pthread_mutex_init
) (&mutex
->actual
, NULL
);
548 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
550 if (__gthread_active_p ())
552 pthread_t me
= __gthrw_(pthread_self
) ();
554 if (mutex
->owner
!= me
)
556 __gthrw_(pthread_mutex_lock
) (&mutex
->actual
);
566 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
568 if (__gthread_active_p ())
570 pthread_t me
= __gthrw_(pthread_self
) ();
572 if (mutex
->owner
!= me
)
574 if (__gthrw_(pthread_mutex_trylock
) (&mutex
->actual
))
585 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
587 if (__gthread_active_p ())
589 if (--mutex
->depth
== 0)
591 mutex
->owner
= (pthread_t
) 0;
592 __gthrw_(pthread_mutex_unlock
) (&mutex
->actual
);
598 #endif /* _LIBOBJC */
600 #endif /* ! GCC_GTHR_POSIX_H */