1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 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_POSIX_H
31 #define GCC_GTHR_POSIX_H
33 /* POSIX threads specific definitions.
34 Easy, since the interface is just one-to-one mapping. */
38 /* Some implementations of <pthread.h> require this to be defined. */
46 typedef pthread_key_t __gthread_key_t
;
47 typedef pthread_once_t __gthread_once_t
;
48 typedef pthread_mutex_t __gthread_mutex_t
;
49 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
51 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
52 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
54 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
55 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
56 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
63 #pragma weak pthread_once
64 #pragma weak pthread_key_create
65 #pragma weak pthread_key_delete
66 #pragma weak pthread_getspecific
67 #pragma weak pthread_setspecific
68 #pragma weak pthread_create
69 #pragma weak pthread_cancel
71 #pragma weak pthread_mutex_lock
72 #pragma weak pthread_mutex_trylock
73 #pragma weak pthread_mutex_unlock
74 #pragma weak pthread_mutexattr_init
75 #pragma weak pthread_mutexattr_settype
76 #pragma weak pthread_mutexattr_destroy
78 #pragma weak pthread_mutex_init
80 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
82 #pragma weak pthread_cond_broadcast
83 #pragma weak pthread_cond_destroy
84 #pragma weak pthread_cond_init
85 #pragma weak pthread_cond_signal
86 #pragma weak pthread_cond_wait
87 #pragma weak pthread_exit
88 #pragma weak pthread_mutex_destroy
89 #pragma weak pthread_self
90 #ifdef _POSIX_PRIORITY_SCHEDULING
91 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
92 #pragma weak sched_get_priority_max
93 #pragma weak sched_get_priority_min
94 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
95 #endif /* _POSIX_PRIORITY_SCHEDULING */
96 #pragma weak sched_yield
97 #pragma weak pthread_attr_destroy
98 #pragma weak pthread_attr_init
99 #pragma weak pthread_attr_setdetachstate
100 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
101 #pragma weak pthread_getschedparam
102 #pragma weak pthread_setschedparam
103 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
104 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
107 __gthread_active_p (void)
109 static void *const __gthread_active_ptr
110 = __extension__ (void *) &pthread_cancel
;
111 return __gthread_active_ptr
!= 0;
114 #else /* not SUPPORTS_WEAK */
117 __gthread_active_p (void)
122 #endif /* SUPPORTS_WEAK */
126 /* This is the config.h file in libobjc/ */
133 /* Key structure for maintaining thread specific storage */
134 static pthread_key_t _objc_thread_storage
;
135 static pthread_attr_t _objc_thread_attribs
;
137 /* Thread local storage for a single thread */
138 static void *thread_local_storage
= NULL
;
140 /* Backend initialization functions */
142 /* Initialize the threads subsystem. */
144 __gthread_objc_init_thread_system (void)
146 if (__gthread_active_p ())
148 /* Initialize the thread storage key. */
149 if (pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
151 /* The normal default detach state for threads is
152 * PTHREAD_CREATE_JOINABLE which causes threads to not die
153 * when you think they should. */
154 if (pthread_attr_init (&_objc_thread_attribs
) == 0
155 && pthread_attr_setdetachstate (&_objc_thread_attribs
,
156 PTHREAD_CREATE_DETACHED
) == 0)
164 /* Close the threads subsystem. */
166 __gthread_objc_close_thread_system (void)
168 if (__gthread_active_p ()
169 && pthread_key_delete (_objc_thread_storage
) == 0
170 && pthread_attr_destroy (&_objc_thread_attribs
) == 0)
176 /* Backend thread functions */
178 /* Create a new thread of execution. */
179 static inline objc_thread_t
180 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
182 objc_thread_t thread_id
;
183 pthread_t new_thread_handle
;
185 if (!__gthread_active_p ())
188 if (!(pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
189 thread_id
= (objc_thread_t
) new_thread_handle
;
196 /* Set the current thread's priority. */
198 __gthread_objc_thread_set_priority (int priority
)
200 if (!__gthread_active_p ())
204 #ifdef _POSIX_PRIORITY_SCHEDULING
205 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
206 pthread_t thread_id
= pthread_self ();
208 struct sched_param params
;
209 int priority_min
, priority_max
;
211 if (pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
213 if ((priority_max
= sched_get_priority_max (policy
)) == -1)
216 if ((priority_min
= sched_get_priority_min (policy
)) == -1)
219 if (priority
> priority_max
)
220 priority
= priority_max
;
221 else if (priority
< priority_min
)
222 priority
= priority_min
;
223 params
.sched_priority
= priority
;
226 * The solaris 7 and several other man pages incorrectly state that
227 * this should be a pointer to policy but pthread.h is universally
230 if (pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
233 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
234 #endif /* _POSIX_PRIORITY_SCHEDULING */
239 /* Return the current thread's priority. */
241 __gthread_objc_thread_get_priority (void)
243 #ifdef _POSIX_PRIORITY_SCHEDULING
244 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
245 if (__gthread_active_p ())
248 struct sched_param params
;
250 if (pthread_getschedparam (pthread_self (), &policy
, ¶ms
) == 0)
251 return params
.sched_priority
;
256 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
257 #endif /* _POSIX_PRIORITY_SCHEDULING */
258 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
261 /* Yield our process time to another thread. */
263 __gthread_objc_thread_yield (void)
265 if (__gthread_active_p ())
269 /* Terminate the current thread. */
271 __gthread_objc_thread_exit (void)
273 if (__gthread_active_p ())
274 /* exit the thread */
275 pthread_exit (&__objc_thread_exit_status
);
277 /* Failed if we reached here */
281 /* Returns an integer value which uniquely describes a thread. */
282 static inline objc_thread_t
283 __gthread_objc_thread_id (void)
285 if (__gthread_active_p ())
286 return (objc_thread_t
) pthread_self ();
288 return (objc_thread_t
) 1;
291 /* Sets the thread's local storage pointer. */
293 __gthread_objc_thread_set_data (void *value
)
295 if (__gthread_active_p ())
296 return pthread_setspecific (_objc_thread_storage
, value
);
299 thread_local_storage
= value
;
304 /* Returns the thread's local storage pointer. */
306 __gthread_objc_thread_get_data (void)
308 if (__gthread_active_p ())
309 return pthread_getspecific (_objc_thread_storage
);
311 return thread_local_storage
;
314 /* Backend mutex functions */
316 /* Allocate a mutex. */
318 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
320 if (__gthread_active_p ())
322 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
324 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
326 objc_free (mutex
->backend
);
327 mutex
->backend
= NULL
;
335 /* Deallocate a mutex. */
337 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
339 if (__gthread_active_p ())
344 * Posix Threads specifically require that the thread be unlocked
345 * for pthread_mutex_destroy to work.
350 count
= pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
356 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
359 objc_free (mutex
->backend
);
360 mutex
->backend
= NULL
;
365 /* Grab a lock on a mutex. */
367 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
369 if (__gthread_active_p ()
370 && pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
378 /* Try to grab a lock on a mutex. */
380 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
382 if (__gthread_active_p ()
383 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
391 /* Unlock the mutex */
393 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
395 if (__gthread_active_p ()
396 && pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
404 /* Backend condition mutex functions */
406 /* Allocate a condition. */
408 __gthread_objc_condition_allocate (objc_condition_t condition
)
410 if (__gthread_active_p ())
412 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
414 if (pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
416 objc_free (condition
->backend
);
417 condition
->backend
= NULL
;
425 /* Deallocate a condition. */
427 __gthread_objc_condition_deallocate (objc_condition_t condition
)
429 if (__gthread_active_p ())
431 if (pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
434 objc_free (condition
->backend
);
435 condition
->backend
= NULL
;
440 /* Wait on the condition */
442 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
444 if (__gthread_active_p ())
445 return pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
446 (pthread_mutex_t
*) mutex
->backend
);
451 /* Wake up all threads waiting on this condition. */
453 __gthread_objc_condition_broadcast (objc_condition_t condition
)
455 if (__gthread_active_p ())
456 return pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
461 /* Wake up one thread waiting on this condition. */
463 __gthread_objc_condition_signal (objc_condition_t condition
)
465 if (__gthread_active_p ())
466 return pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
474 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
476 if (__gthread_active_p ())
477 return pthread_once (once
, func
);
483 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
485 return pthread_key_create (key
, dtor
);
489 __gthread_key_delete (__gthread_key_t key
)
491 return pthread_key_delete (key
);
495 __gthread_getspecific (__gthread_key_t key
)
497 return pthread_getspecific (key
);
501 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
503 return pthread_setspecific (key
, ptr
);
507 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
509 if (__gthread_active_p ())
510 return pthread_mutex_lock (mutex
);
516 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
518 if (__gthread_active_p ())
519 return pthread_mutex_trylock (mutex
);
525 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
527 if (__gthread_active_p ())
528 return pthread_mutex_unlock (mutex
);
533 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
535 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
537 if (__gthread_active_p ())
539 pthread_mutexattr_t attr
;
542 r
= pthread_mutexattr_init (&attr
);
544 r
= pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
546 r
= pthread_mutex_init (mutex
, &attr
);
548 r
= pthread_mutexattr_destroy (&attr
);
556 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
558 return __gthread_mutex_lock (mutex
);
562 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
564 return __gthread_mutex_trylock (mutex
);
568 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
570 return __gthread_mutex_unlock (mutex
);
573 #endif /* _LIBOBJC */
575 #endif /* ! GCC_GTHR_POSIX_H */