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. */
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_get_priority_max
71 #pragma weak sched_get_priority_min
72 #pragma weak sched_yield
73 #pragma weak pthread_attr_destroy
74 #pragma weak pthread_attr_init
75 #pragma weak pthread_attr_setdetachstate
76 #pragma weak pthread_getschedparam
77 #pragma weak pthread_setschedparam
80 static void *__gthread_active_ptr
= (void *) &pthread_create
;
83 __gthread_active_p (void)
85 return __gthread_active_ptr
!= 0;
88 #else /* not SUPPORTS_WEAK */
91 __gthread_active_p (void)
96 #endif /* SUPPORTS_WEAK */
100 /* This is the config.h file in libobjc/ */
107 /* Key structure for maintaining thread specific storage */
108 static pthread_key_t _objc_thread_storage
;
109 static pthread_attr_t _objc_thread_attribs
;
111 /* Thread local storage for a single thread */
112 static void *thread_local_storage
= NULL
;
114 /* Backend initialization functions */
116 /* Initialize the threads subsystem. */
118 __gthread_objc_init_thread_system(void)
120 if (__gthread_active_p ())
122 /* Initialize the thread storage key */
123 if (pthread_key_create(&_objc_thread_storage
, NULL
) == 0)
125 /* The normal default detach state for threads is
126 * PTHREAD_CREATE_JOINABLE which causes threads to not die
127 * when you think they should. */
128 if (pthread_attr_init(&_objc_thread_attribs
) == 0
129 && pthread_attr_setdetachstate(&_objc_thread_attribs
,
130 PTHREAD_CREATE_DETACHED
) == 0)
138 /* Close the threads subsystem. */
140 __gthread_objc_close_thread_system(void)
142 if (__gthread_active_p ()
143 && pthread_key_delete(_objc_thread_storage
) == 0
144 && pthread_attr_destroy(&_objc_thread_attribs
) == 0)
150 /* Backend thread functions */
152 /* Create a new thread of execution. */
153 static inline objc_thread_t
154 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
156 objc_thread_t thread_id
;
157 pthread_t new_thread_handle
;
159 if (!__gthread_active_p ())
162 if ( !(pthread_create(&new_thread_handle
, NULL
, (void *)func
, arg
)) )
163 thread_id
= (objc_thread_t
) new_thread_handle
;
170 /* Set the current thread's priority. */
172 __gthread_objc_thread_set_priority(int priority
)
174 if (!__gthread_active_p())
177 pthread_t thread_id
= pthread_self();
179 struct sched_param params
;
180 int priority_min
, priority_max
;
182 if (pthread_getschedparam(thread_id
, &policy
, ¶ms
) == 0)
184 if ((priority_max
= sched_get_priority_max(policy
)) != 0)
187 if ((priority_min
= sched_get_priority_min(policy
)) != 0)
190 if (priority
> priority_max
)
191 priority
= priority_max
;
192 else if (priority
< priority_min
)
193 priority
= priority_min
;
194 params
.sched_priority
= priority
;
197 * The solaris 7 and several other man pages incorrectly state that
198 * this should be a pointer to policy but pthread.h is universally
201 if (pthread_setschedparam(thread_id
, policy
, ¶ms
) == 0)
208 /* Return the current thread's priority. */
210 __gthread_objc_thread_get_priority(void)
212 if (__gthread_active_p ())
215 struct sched_param params
;
217 if (pthread_getschedparam(pthread_self(), &policy
, ¶ms
) == 0)
218 return params
.sched_priority
;
223 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
226 /* Yield our process time to another thread. */
228 __gthread_objc_thread_yield(void)
230 if (__gthread_active_p ())
234 /* Terminate the current thread. */
236 __gthread_objc_thread_exit(void)
238 if (__gthread_active_p ())
239 /* exit the thread */
240 pthread_exit(&__objc_thread_exit_status
);
242 /* Failed if we reached here */
246 /* Returns an integer value which uniquely describes a thread. */
247 static inline objc_thread_t
248 __gthread_objc_thread_id(void)
250 if (__gthread_active_p ())
251 return (objc_thread_t
) pthread_self();
253 return (objc_thread_t
) 1;
256 /* Sets the thread's local storage pointer. */
258 __gthread_objc_thread_set_data(void *value
)
260 if (__gthread_active_p ())
261 return pthread_setspecific(_objc_thread_storage
, value
);
264 thread_local_storage
= value
;
269 /* Returns the thread's local storage pointer. */
271 __gthread_objc_thread_get_data(void)
273 if (__gthread_active_p ())
274 return pthread_getspecific(_objc_thread_storage
);
276 return thread_local_storage
;
279 /* Backend mutex functions */
281 /* Allocate a mutex. */
283 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
285 if (__gthread_active_p ())
287 mutex
->backend
= objc_malloc(sizeof(pthread_mutex_t
));
289 if (pthread_mutex_init((pthread_mutex_t
*)mutex
->backend
, NULL
))
291 objc_free(mutex
->backend
);
292 mutex
->backend
= NULL
;
300 /* Deallocate a mutex. */
302 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
304 if (__gthread_active_p ())
309 * Posix Threads specifically require that the thread be unlocked
310 * for pthread_mutex_destroy to work.
315 count
= pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
);
321 if (pthread_mutex_destroy((pthread_mutex_t
*)mutex
->backend
))
324 objc_free(mutex
->backend
);
325 mutex
->backend
= NULL
;
330 /* Grab a lock on a mutex. */
332 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
334 if (__gthread_active_p ()
335 && pthread_mutex_lock((pthread_mutex_t
*)mutex
->backend
) != 0)
343 /* Try to grab a lock on a mutex. */
345 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
347 if (__gthread_active_p ()
348 && pthread_mutex_trylock((pthread_mutex_t
*)mutex
->backend
) != 0)
356 /* Unlock the mutex */
358 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
360 if (__gthread_active_p ()
361 && pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
) != 0)
369 /* Backend condition mutex functions */
371 /* Allocate a condition. */
373 __gthread_objc_condition_allocate(objc_condition_t condition
)
375 if (__gthread_active_p ())
377 condition
->backend
= objc_malloc(sizeof(pthread_cond_t
));
379 if (pthread_cond_init((pthread_cond_t
*)condition
->backend
, NULL
))
381 objc_free(condition
->backend
);
382 condition
->backend
= NULL
;
390 /* Deallocate a condition. */
392 __gthread_objc_condition_deallocate(objc_condition_t condition
)
394 if (__gthread_active_p ())
396 if (pthread_cond_destroy((pthread_cond_t
*)condition
->backend
))
399 objc_free(condition
->backend
);
400 condition
->backend
= NULL
;
405 /* Wait on the condition */
407 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
409 if (__gthread_active_p ())
410 return pthread_cond_wait((pthread_cond_t
*)condition
->backend
,
411 (pthread_mutex_t
*)mutex
->backend
);
416 /* Wake up all threads waiting on this condition. */
418 __gthread_objc_condition_broadcast(objc_condition_t condition
)
420 if (__gthread_active_p ())
421 return pthread_cond_broadcast((pthread_cond_t
*)condition
->backend
);
426 /* Wake up one thread waiting on this condition. */
428 __gthread_objc_condition_signal(objc_condition_t condition
)
430 if (__gthread_active_p ())
431 return pthread_cond_signal((pthread_cond_t
*)condition
->backend
);
439 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
441 if (__gthread_active_p ())
442 return pthread_once (once
, func
);
448 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
450 return pthread_key_create (key
, dtor
);
454 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
456 /* Just reset the key value to zero. */
458 return pthread_setspecific (key
, 0);
464 __gthread_key_delete (__gthread_key_t key
)
466 return pthread_key_delete (key
);
470 __gthread_getspecific (__gthread_key_t key
)
472 return pthread_getspecific (key
);
476 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
478 return pthread_setspecific (key
, ptr
);
482 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
484 if (__gthread_active_p ())
485 return pthread_mutex_lock (mutex
);
491 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
493 if (__gthread_active_p ())
494 return pthread_mutex_trylock (mutex
);
500 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
502 if (__gthread_active_p ())
503 return pthread_mutex_unlock (mutex
);
508 #endif /* _LIBOBJC */
510 #endif /* ! GCC_GTHR_POSIX_H */