1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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 __gthr_posix_h
30 #define __gthr_posix_h
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
39 typedef pthread_key_t __gthread_key_t
;
40 typedef pthread_once_t __gthread_once_t
;
41 typedef pthread_mutex_t __gthread_mutex_t
;
43 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
46 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
48 #pragma weak pthread_once
49 #pragma weak pthread_key_create
50 #pragma weak pthread_key_delete
51 #pragma weak pthread_getspecific
52 #pragma weak pthread_setspecific
53 #pragma weak pthread_create
55 #pragma weak pthread_mutex_lock
56 #pragma weak pthread_mutex_trylock
57 #pragma weak pthread_mutex_unlock
61 #pragma weak pthread_cond_broadcast
62 #pragma weak pthread_cond_destroy
63 #pragma weak pthread_cond_init
64 #pragma weak pthread_cond_signal
65 #pragma weak pthread_cond_wait
66 #pragma weak pthread_exit
67 #pragma weak pthread_mutex_init
68 #pragma weak pthread_mutex_destroy
69 #pragma weak pthread_self
70 #pragma weak sched_yield
71 #pragma weak pthread_attr_destroy
72 #pragma weak pthread_attr_init
73 #pragma weak pthread_attr_setdetachstate
74 #pragma weak pthread_getschedparam
75 #pragma weak pthread_setschedparam
78 static void *__gthread_active_ptr
= &pthread_create
;
81 __gthread_active_p (void)
83 return __gthread_active_ptr
!= 0;
86 #else /* not SUPPORTS_WEAK */
89 __gthread_active_p (void)
94 #endif /* SUPPORTS_WEAK */
98 /* This is the config.h file in libobjc/ */
105 /* Key structure for maintaining thread specific storage */
106 static pthread_key_t _objc_thread_storage
;
107 static pthread_attr_t _objc_thread_attribs
;
109 /* Thread local storage for a single thread */
110 static void *thread_local_storage
= NULL
;
112 /* Backend initialization functions */
114 /* Initialize the threads subsystem. */
116 __gthread_objc_init_thread_system(void)
118 if (__gthread_active_p ())
120 /* Initialize the thread storage key */
121 if (pthread_key_create(&_objc_thread_storage
, NULL
) == 0)
123 /* The normal default detach state for threads is
124 * PTHREAD_CREATE_JOINABLE which causes threads to not die
125 * when you think they should. */
126 if (pthread_attr_init(&_objc_thread_attribs
) == 0
127 && pthread_attr_setdetachstate(&_objc_thread_attribs
,
128 PTHREAD_CREATE_DETACHED
) == 0)
136 /* Close the threads subsystem. */
138 __gthread_objc_close_thread_system(void)
140 if (__gthread_active_p ()
141 && pthread_key_delete(_objc_thread_storage
) == 0
142 && pthread_attr_destroy(&_objc_thread_attribs
) == 0)
148 /* Backend thread functions */
150 /* Create a new thread of execution. */
151 static inline objc_thread_t
152 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
154 objc_thread_t thread_id
;
155 pthread_t new_thread_handle
;
157 if (!__gthread_active_p ())
160 if ( !(pthread_create(&new_thread_handle
, NULL
, (void *)func
, arg
)) )
161 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
168 /* Set the current thread's priority. */
170 __gthread_objc_thread_set_priority(int priority
)
172 if (!__gthread_active_p())
175 pthread_t thread_id
= pthread_self();
177 struct sched_param params
;
178 int priority_min
, priority_max
;
180 if (pthread_getschedparam(thread_id
, &policy
, ¶ms
) == 0)
182 if ((priority_max
= sched_get_priority_max(policy
)) != 0)
185 if ((priority_min
= sched_get_priority_min(policy
)) != 0)
188 if (priority
> priority_max
)
189 priority
= priority_max
;
190 else if (priority
< priority_min
)
191 priority
= priority_min
;
192 params
.sched_priority
= priority
;
195 * The solaris 7 and several other man pages incorrectly state that
196 * this should be a pointer to policy but pthread.h is universally
199 if (pthread_setschedparam(thread_id
, policy
, ¶ms
) == 0)
206 /* Return the current thread's priority. */
208 __gthread_objc_thread_get_priority(void)
210 if (__gthread_active_p ())
213 struct sched_param params
;
215 if (pthread_getschedparam(pthread_self(), &policy
, ¶ms
) == 0)
216 return params
.sched_priority
;
221 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
224 /* Yield our process time to another thread. */
226 __gthread_objc_thread_yield(void)
228 if (__gthread_active_p ())
232 /* Terminate the current thread. */
234 __gthread_objc_thread_exit(void)
236 if (__gthread_active_p ())
237 /* exit the thread */
238 pthread_exit(&__objc_thread_exit_status
);
240 /* Failed if we reached here */
244 /* Returns an integer value which uniquely describes a thread. */
245 static inline objc_thread_t
246 __gthread_objc_thread_id(void)
248 if (__gthread_active_p ())
250 pthread_t self
= pthread_self();
252 return *(objc_thread_t
*)&self
;
255 return (objc_thread_t
)1;
258 /* Sets the thread's local storage pointer. */
260 __gthread_objc_thread_set_data(void *value
)
262 if (__gthread_active_p ())
263 return pthread_setspecific(_objc_thread_storage
, value
);
266 thread_local_storage
= value
;
271 /* Returns the thread's local storage pointer. */
273 __gthread_objc_thread_get_data(void)
275 if (__gthread_active_p ())
276 return pthread_getspecific(_objc_thread_storage
);
278 return thread_local_storage
;
281 /* Backend mutex functions */
283 /* Allocate a mutex. */
285 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
287 if (__gthread_active_p ())
289 mutex
->backend
= objc_malloc(sizeof(pthread_mutex_t
));
291 if (pthread_mutex_init((pthread_mutex_t
*)mutex
->backend
, NULL
))
293 objc_free(mutex
->backend
);
294 mutex
->backend
= NULL
;
302 /* Deallocate a mutex. */
304 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
306 if (__gthread_active_p ())
311 * Posix Threads specifically require that the thread be unlocked
312 * for pthread_mutex_destroy to work.
317 count
= pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
);
323 if (pthread_mutex_destroy((pthread_mutex_t
*)mutex
->backend
))
326 objc_free(mutex
->backend
);
327 mutex
->backend
= NULL
;
332 /* Grab a lock on a mutex. */
334 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
336 if (__gthread_active_p ())
337 return pthread_mutex_lock((pthread_mutex_t
*)mutex
->backend
);
342 /* Try to grab a lock on a mutex. */
344 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
346 if (__gthread_active_p ())
347 return pthread_mutex_trylock((pthread_mutex_t
*)mutex
->backend
);
352 /* Unlock the mutex */
354 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
356 if (__gthread_active_p ())
357 return pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
);
362 /* Backend condition mutex functions */
364 /* Allocate a condition. */
366 __gthread_objc_condition_allocate(objc_condition_t condition
)
368 if (__gthread_active_p ())
370 condition
->backend
= objc_malloc(sizeof(pthread_cond_t
));
372 if (pthread_cond_init((pthread_cond_t
*)condition
->backend
, NULL
))
374 objc_free(condition
->backend
);
375 condition
->backend
= NULL
;
383 /* Deallocate a condition. */
385 __gthread_objc_condition_deallocate(objc_condition_t condition
)
387 if (__gthread_active_p ())
389 if (pthread_cond_destroy((pthread_cond_t
*)condition
->backend
))
392 objc_free(condition
->backend
);
393 condition
->backend
= NULL
;
398 /* Wait on the condition */
400 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
402 if (__gthread_active_p ())
403 return pthread_cond_wait((pthread_cond_t
*)condition
->backend
,
404 (pthread_mutex_t
*)mutex
->backend
);
409 /* Wake up all threads waiting on this condition. */
411 __gthread_objc_condition_broadcast(objc_condition_t condition
)
413 if (__gthread_active_p ())
414 return pthread_cond_broadcast((pthread_cond_t
*)condition
->backend
);
419 /* Wake up one thread waiting on this condition. */
421 __gthread_objc_condition_signal(objc_condition_t condition
)
423 if (__gthread_active_p ())
424 return pthread_cond_signal((pthread_cond_t
*)condition
->backend
);
432 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
434 if (__gthread_active_p ())
435 return pthread_once (once
, func
);
441 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
443 return pthread_key_create (key
, dtor
);
447 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
449 /* Just reset the key value to zero. */
451 return pthread_setspecific (key
, 0);
457 __gthread_key_delete (__gthread_key_t key
)
459 return pthread_key_delete (key
);
463 __gthread_getspecific (__gthread_key_t key
)
465 return pthread_getspecific (key
);
469 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
471 return pthread_setspecific (key
, ptr
);
475 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
477 if (__gthread_active_p ())
478 return pthread_mutex_lock (mutex
);
484 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
486 if (__gthread_active_p ())
487 return pthread_mutex_trylock (mutex
);
493 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
495 if (__gthread_active_p ())
496 return pthread_mutex_unlock (mutex
);
501 #endif /* _LIBOBJC */
503 #endif /* not __gthr_posix_h */