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_POSIX_H
30 #define GCC_GTHR_POSIX_H
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
40 typedef pthread_key_t __gthread_key_t
;
41 typedef pthread_once_t __gthread_once_t
;
42 typedef pthread_mutex_t __gthread_mutex_t
;
44 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
45 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
47 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
49 #pragma weak pthread_once
50 #pragma weak pthread_key_create
51 #pragma weak pthread_key_delete
52 #pragma weak pthread_getspecific
53 #pragma weak pthread_setspecific
54 #pragma weak pthread_create
56 #pragma weak pthread_mutex_lock
57 #pragma weak pthread_mutex_trylock
58 #pragma weak pthread_mutex_unlock
62 #pragma weak pthread_cond_broadcast
63 #pragma weak pthread_cond_destroy
64 #pragma weak pthread_cond_init
65 #pragma weak pthread_cond_signal
66 #pragma weak pthread_cond_wait
67 #pragma weak pthread_exit
68 #pragma weak pthread_mutex_init
69 #pragma weak pthread_mutex_destroy
70 #pragma weak pthread_self
71 /* These really should be protected by _POSIX_PRIORITY_SCHEDULING, but
72 we use them inside a _POSIX_THREAD_PRIORITY_SCHEDULING block. */
73 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
74 #pragma weak sched_get_priority_max
75 #pragma weak sched_get_priority_min
76 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
77 #pragma weak sched_yield
78 #pragma weak pthread_attr_destroy
79 #pragma weak pthread_attr_init
80 #pragma weak pthread_attr_setdetachstate
81 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
82 #pragma weak pthread_getschedparam
83 #pragma weak pthread_setschedparam
84 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
88 __gthread_active_p (void)
90 static void *const __gthread_active_ptr
= (void *) &pthread_create
;
91 return __gthread_active_ptr
!= 0;
94 #else /* not SUPPORTS_WEAK */
97 __gthread_active_p (void)
102 #endif /* SUPPORTS_WEAK */
106 /* This is the config.h file in libobjc/ */
113 /* Key structure for maintaining thread specific storage */
114 static pthread_key_t _objc_thread_storage
;
115 static pthread_attr_t _objc_thread_attribs
;
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 ())
128 /* Initialize the thread storage key */
129 if (pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
131 /* The normal default detach state for threads is
132 * PTHREAD_CREATE_JOINABLE which causes threads to not die
133 * when you think they should. */
134 if (pthread_attr_init (&_objc_thread_attribs
) == 0
135 && pthread_attr_setdetachstate (&_objc_thread_attribs
,
136 PTHREAD_CREATE_DETACHED
) == 0)
144 /* Close the threads subsystem. */
146 __gthread_objc_close_thread_system (void)
148 if (__gthread_active_p ()
149 && pthread_key_delete (_objc_thread_storage
) == 0
150 && pthread_attr_destroy (&_objc_thread_attribs
) == 0)
156 /* Backend thread functions */
158 /* Create a new thread of execution. */
159 static inline objc_thread_t
160 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
162 objc_thread_t thread_id
;
163 pthread_t new_thread_handle
;
165 if (!__gthread_active_p ())
168 if (!(pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
169 thread_id
= (objc_thread_t
) new_thread_handle
;
176 /* Set the current thread's priority. */
178 __gthread_objc_thread_set_priority (int priority
)
180 if (!__gthread_active_p ())
184 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
185 pthread_t thread_id
= pthread_self ();
187 struct sched_param params
;
188 int priority_min
, priority_max
;
190 if (pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
192 if ((priority_max
= sched_get_priority_max (policy
)) == -1)
195 if ((priority_min
= sched_get_priority_min (policy
)) == -1)
198 if (priority
> priority_max
)
199 priority
= priority_max
;
200 else if (priority
< priority_min
)
201 priority
= priority_min
;
202 params
.sched_priority
= priority
;
205 * The solaris 7 and several other man pages incorrectly state that
206 * this should be a pointer to policy but pthread.h is universally
209 if (pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
212 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
217 /* Return the current thread's priority. */
219 __gthread_objc_thread_get_priority (void)
221 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
222 if (__gthread_active_p ())
225 struct sched_param params
;
227 if (pthread_getschedparam (pthread_self (), &policy
, ¶ms
) == 0)
228 return params
.sched_priority
;
233 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
234 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
237 /* Yield our process time to another thread. */
239 __gthread_objc_thread_yield (void)
241 if (__gthread_active_p ())
245 /* Terminate the current thread. */
247 __gthread_objc_thread_exit (void)
249 if (__gthread_active_p ())
250 /* exit the thread */
251 pthread_exit (&__objc_thread_exit_status
);
253 /* Failed if we reached here */
257 /* Returns an integer value which uniquely describes a thread. */
258 static inline objc_thread_t
259 __gthread_objc_thread_id (void)
261 if (__gthread_active_p ())
262 return (objc_thread_t
) pthread_self ();
264 return (objc_thread_t
) 1;
267 /* Sets the thread's local storage pointer. */
269 __gthread_objc_thread_set_data (void *value
)
271 if (__gthread_active_p ())
272 return pthread_setspecific (_objc_thread_storage
, value
);
275 thread_local_storage
= value
;
280 /* Returns the thread's local storage pointer. */
282 __gthread_objc_thread_get_data (void)
284 if (__gthread_active_p ())
285 return pthread_getspecific (_objc_thread_storage
);
287 return thread_local_storage
;
290 /* Backend mutex functions */
292 /* Allocate a mutex. */
294 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
296 if (__gthread_active_p ())
298 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
300 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
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 ())
320 * Posix Threads specifically require that the thread be unlocked
321 * for pthread_mutex_destroy to work.
326 count
= pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
332 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
335 objc_free (mutex
->backend
);
336 mutex
->backend
= NULL
;
341 /* Grab a lock on a mutex. */
343 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
345 if (__gthread_active_p ()
346 && pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
354 /* Try to grab a lock on a mutex. */
356 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
358 if (__gthread_active_p ()
359 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
367 /* Unlock the mutex */
369 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
371 if (__gthread_active_p ()
372 && pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
380 /* Backend condition mutex functions */
382 /* Allocate a condition. */
384 __gthread_objc_condition_allocate (objc_condition_t condition
)
386 if (__gthread_active_p ())
388 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
390 if (pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
392 objc_free (condition
->backend
);
393 condition
->backend
= NULL
;
401 /* Deallocate a condition. */
403 __gthread_objc_condition_deallocate (objc_condition_t condition
)
405 if (__gthread_active_p ())
407 if (pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
410 objc_free (condition
->backend
);
411 condition
->backend
= NULL
;
416 /* Wait on the condition */
418 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
420 if (__gthread_active_p ())
421 return pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
422 (pthread_mutex_t
*) mutex
->backend
);
427 /* Wake up all threads waiting on this condition. */
429 __gthread_objc_condition_broadcast (objc_condition_t condition
)
431 if (__gthread_active_p ())
432 return pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
437 /* Wake up one thread waiting on this condition. */
439 __gthread_objc_condition_signal (objc_condition_t condition
)
441 if (__gthread_active_p ())
442 return pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
450 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
452 if (__gthread_active_p ())
453 return pthread_once (once
, func
);
459 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
461 return pthread_key_create (key
, dtor
);
465 __gthread_key_delete (__gthread_key_t key
)
467 return pthread_key_delete (key
);
471 __gthread_getspecific (__gthread_key_t key
)
473 return pthread_getspecific (key
);
477 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
479 return pthread_setspecific (key
, ptr
);
483 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
485 if (__gthread_active_p ())
486 return pthread_mutex_lock (mutex
);
492 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
494 if (__gthread_active_p ())
495 return pthread_mutex_trylock (mutex
);
501 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
503 if (__gthread_active_p ())
504 return pthread_mutex_unlock (mutex
);
509 #endif /* _LIBOBJC */
511 #endif /* ! GCC_GTHR_POSIX_H */