1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003
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, 59 Temple Place - Suite 330, 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
;
50 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
51 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
55 #pragma weak pthread_once
56 #pragma weak pthread_key_create
57 #pragma weak pthread_key_delete
58 #pragma weak pthread_getspecific
59 #pragma weak pthread_setspecific
60 #pragma weak pthread_create
62 #pragma weak pthread_mutex_lock
63 #pragma weak pthread_mutex_trylock
64 #pragma weak pthread_mutex_unlock
66 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
68 #pragma weak pthread_cond_broadcast
69 #pragma weak pthread_cond_destroy
70 #pragma weak pthread_cond_init
71 #pragma weak pthread_cond_signal
72 #pragma weak pthread_cond_wait
73 #pragma weak pthread_exit
74 #pragma weak pthread_mutex_init
75 #pragma weak pthread_mutex_destroy
76 #pragma weak pthread_self
77 #ifdef _POSIX_PRIORITY_SCHEDULING
78 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
79 #pragma weak sched_get_priority_max
80 #pragma weak sched_get_priority_min
81 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
82 #endif /* _POSIX_PRIORITY_SCHEDULING */
83 #pragma weak sched_yield
84 #pragma weak pthread_attr_destroy
85 #pragma weak pthread_attr_init
86 #pragma weak pthread_attr_setdetachstate
87 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
88 #pragma weak pthread_getschedparam
89 #pragma weak pthread_setschedparam
90 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
91 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
94 __gthread_active_p (void)
96 static void *const __gthread_active_ptr
= (void *) &pthread_create
;
97 return __gthread_active_ptr
!= 0;
100 #else /* not SUPPORTS_WEAK */
103 __gthread_active_p (void)
108 #endif /* SUPPORTS_WEAK */
112 /* This is the config.h file in libobjc/ */
119 /* Key structure for maintaining thread specific storage */
120 static pthread_key_t _objc_thread_storage
;
121 static pthread_attr_t _objc_thread_attribs
;
123 /* Thread local storage for a single thread */
124 static void *thread_local_storage
= NULL
;
126 /* Backend initialization functions */
128 /* Initialize the threads subsystem. */
130 __gthread_objc_init_thread_system (void)
132 if (__gthread_active_p ())
134 /* Initialize the thread storage key */
135 if (pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
137 /* The normal default detach state for threads is
138 * PTHREAD_CREATE_JOINABLE which causes threads to not die
139 * when you think they should. */
140 if (pthread_attr_init (&_objc_thread_attribs
) == 0
141 && pthread_attr_setdetachstate (&_objc_thread_attribs
,
142 PTHREAD_CREATE_DETACHED
) == 0)
150 /* Close the threads subsystem. */
152 __gthread_objc_close_thread_system (void)
154 if (__gthread_active_p ()
155 && pthread_key_delete (_objc_thread_storage
) == 0
156 && pthread_attr_destroy (&_objc_thread_attribs
) == 0)
162 /* Backend thread functions */
164 /* Create a new thread of execution. */
165 static inline objc_thread_t
166 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
168 objc_thread_t thread_id
;
169 pthread_t new_thread_handle
;
171 if (!__gthread_active_p ())
174 if (!(pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
175 thread_id
= (objc_thread_t
) new_thread_handle
;
182 /* Set the current thread's priority. */
184 __gthread_objc_thread_set_priority (int priority
)
186 if (!__gthread_active_p ())
190 #ifdef _POSIX_PRIORITY_SCHEDULING
191 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
192 pthread_t thread_id
= pthread_self ();
194 struct sched_param params
;
195 int priority_min
, priority_max
;
197 if (pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
199 if ((priority_max
= sched_get_priority_max (policy
)) == -1)
202 if ((priority_min
= sched_get_priority_min (policy
)) == -1)
205 if (priority
> priority_max
)
206 priority
= priority_max
;
207 else if (priority
< priority_min
)
208 priority
= priority_min
;
209 params
.sched_priority
= priority
;
212 * The solaris 7 and several other man pages incorrectly state that
213 * this should be a pointer to policy but pthread.h is universally
216 if (pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
219 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
220 #endif /* _POSIX_PRIORITY_SCHEDULING */
225 /* Return the current thread's priority. */
227 __gthread_objc_thread_get_priority (void)
229 #ifdef _POSIX_PRIORITY_SCHEDULING
230 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
231 if (__gthread_active_p ())
234 struct sched_param params
;
236 if (pthread_getschedparam (pthread_self (), &policy
, ¶ms
) == 0)
237 return params
.sched_priority
;
242 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
243 #endif /* _POSIX_PRIORITY_SCHEDULING */
244 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
247 /* Yield our process time to another thread. */
249 __gthread_objc_thread_yield (void)
251 if (__gthread_active_p ())
255 /* Terminate the current thread. */
257 __gthread_objc_thread_exit (void)
259 if (__gthread_active_p ())
260 /* exit the thread */
261 pthread_exit (&__objc_thread_exit_status
);
263 /* Failed if we reached here */
267 /* Returns an integer value which uniquely describes a thread. */
268 static inline objc_thread_t
269 __gthread_objc_thread_id (void)
271 if (__gthread_active_p ())
272 return (objc_thread_t
) pthread_self ();
274 return (objc_thread_t
) 1;
277 /* Sets the thread's local storage pointer. */
279 __gthread_objc_thread_set_data (void *value
)
281 if (__gthread_active_p ())
282 return pthread_setspecific (_objc_thread_storage
, value
);
285 thread_local_storage
= value
;
290 /* Returns the thread's local storage pointer. */
292 __gthread_objc_thread_get_data (void)
294 if (__gthread_active_p ())
295 return pthread_getspecific (_objc_thread_storage
);
297 return thread_local_storage
;
300 /* Backend mutex functions */
302 /* Allocate a mutex. */
304 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
306 if (__gthread_active_p ())
308 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
310 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
312 objc_free (mutex
->backend
);
313 mutex
->backend
= NULL
;
321 /* Deallocate a mutex. */
323 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
325 if (__gthread_active_p ())
330 * Posix Threads specifically require that the thread be unlocked
331 * for pthread_mutex_destroy to work.
336 count
= pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
342 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
345 objc_free (mutex
->backend
);
346 mutex
->backend
= NULL
;
351 /* Grab a lock on a mutex. */
353 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
355 if (__gthread_active_p ()
356 && pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
364 /* Try to grab a lock on a mutex. */
366 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
368 if (__gthread_active_p ()
369 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
377 /* Unlock the mutex */
379 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
381 if (__gthread_active_p ()
382 && pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
390 /* Backend condition mutex functions */
392 /* Allocate a condition. */
394 __gthread_objc_condition_allocate (objc_condition_t condition
)
396 if (__gthread_active_p ())
398 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
400 if (pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
402 objc_free (condition
->backend
);
403 condition
->backend
= NULL
;
411 /* Deallocate a condition. */
413 __gthread_objc_condition_deallocate (objc_condition_t condition
)
415 if (__gthread_active_p ())
417 if (pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
420 objc_free (condition
->backend
);
421 condition
->backend
= NULL
;
426 /* Wait on the condition */
428 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
430 if (__gthread_active_p ())
431 return pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
432 (pthread_mutex_t
*) mutex
->backend
);
437 /* Wake up all threads waiting on this condition. */
439 __gthread_objc_condition_broadcast (objc_condition_t condition
)
441 if (__gthread_active_p ())
442 return pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
447 /* Wake up one thread waiting on this condition. */
449 __gthread_objc_condition_signal (objc_condition_t condition
)
451 if (__gthread_active_p ())
452 return pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
460 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
462 if (__gthread_active_p ())
463 return pthread_once (once
, func
);
469 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
471 return pthread_key_create (key
, dtor
);
475 __gthread_key_delete (__gthread_key_t key
)
477 return pthread_key_delete (key
);
481 __gthread_getspecific (__gthread_key_t key
)
483 return pthread_getspecific (key
);
487 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
489 return pthread_setspecific (key
, ptr
);
493 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
495 if (__gthread_active_p ())
496 return pthread_mutex_lock (mutex
);
502 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
504 if (__gthread_active_p ())
505 return pthread_mutex_trylock (mutex
);
511 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
513 if (__gthread_active_p ())
514 return pthread_mutex_unlock (mutex
);
519 #endif /* _LIBOBJC */
521 #endif /* ! GCC_GTHR_POSIX_H */