1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004 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. */
37 /* Some implementations of <pthread.h> require this to be defined. */
45 typedef pthread_key_t __gthread_key_t
;
46 typedef pthread_once_t __gthread_once_t
;
47 typedef pthread_mutex_t __gthread_mutex_t
;
52 pthread_mutex_t actual
;
53 } __gthread_recursive_mutex_t
;
55 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
56 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
61 #pragma weak pthread_once
62 #pragma weak pthread_key_create
63 #pragma weak pthread_key_delete
64 #pragma weak pthread_getspecific
65 #pragma weak pthread_setspecific
66 #pragma weak pthread_create
67 #pragma weak pthread_self
69 #pragma weak pthread_mutex_lock
70 #pragma weak pthread_mutex_trylock
71 #pragma weak pthread_mutex_unlock
72 #pragma weak pthread_mutexattr_init
73 #pragma weak pthread_mutexattr_settype
74 #pragma weak pthread_mutexattr_destroy
76 #pragma weak pthread_mutex_init
78 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
80 #pragma weak pthread_cond_broadcast
81 #pragma weak pthread_cond_destroy
82 #pragma weak pthread_cond_init
83 #pragma weak pthread_cond_signal
84 #pragma weak pthread_cond_wait
85 #pragma weak pthread_exit
86 #pragma weak pthread_mutex_destroy
87 #pragma weak pthread_self
88 #ifdef _POSIX_PRIORITY_SCHEDULING
89 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
90 #pragma weak sched_get_priority_max
91 #pragma weak sched_get_priority_min
92 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
93 #endif /* _POSIX_PRIORITY_SCHEDULING */
94 #pragma weak sched_yield
95 #pragma weak pthread_attr_destroy
96 #pragma weak pthread_attr_init
97 #pragma weak pthread_attr_setdetachstate
98 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
99 #pragma weak pthread_getschedparam
100 #pragma weak pthread_setschedparam
101 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
102 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
105 __gthread_active_p (void)
107 static void *const __gthread_active_ptr
108 = __extension__ (void *) &pthread_create
;
109 return __gthread_active_ptr
!= 0;
112 #else /* not SUPPORTS_WEAK */
115 __gthread_active_p (void)
120 #endif /* SUPPORTS_WEAK */
124 /* This is the config.h file in libobjc/ */
131 /* Key structure for maintaining thread specific storage */
132 static pthread_key_t _objc_thread_storage
;
133 static pthread_attr_t _objc_thread_attribs
;
135 /* Thread local storage for a single thread */
136 static void *thread_local_storage
= NULL
;
138 /* Backend initialization functions */
140 /* Initialize the threads subsystem. */
142 __gthread_objc_init_thread_system (void)
144 if (__gthread_active_p ())
146 /* Initialize the thread storage key. */
147 if (pthread_key_create (&_objc_thread_storage
, NULL
) == 0)
149 /* The normal default detach state for threads is
150 * PTHREAD_CREATE_JOINABLE which causes threads to not die
151 * when you think they should. */
152 if (pthread_attr_init (&_objc_thread_attribs
) == 0
153 && pthread_attr_setdetachstate (&_objc_thread_attribs
,
154 PTHREAD_CREATE_DETACHED
) == 0)
162 /* Close the threads subsystem. */
164 __gthread_objc_close_thread_system (void)
166 if (__gthread_active_p ()
167 && pthread_key_delete (_objc_thread_storage
) == 0
168 && pthread_attr_destroy (&_objc_thread_attribs
) == 0)
174 /* Backend thread functions */
176 /* Create a new thread of execution. */
177 static inline objc_thread_t
178 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
180 objc_thread_t thread_id
;
181 pthread_t new_thread_handle
;
183 if (!__gthread_active_p ())
186 if (!(pthread_create (&new_thread_handle
, NULL
, (void *) func
, arg
)))
187 thread_id
= (objc_thread_t
) new_thread_handle
;
194 /* Set the current thread's priority. */
196 __gthread_objc_thread_set_priority (int priority
)
198 if (!__gthread_active_p ())
202 #ifdef _POSIX_PRIORITY_SCHEDULING
203 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
204 pthread_t thread_id
= pthread_self ();
206 struct sched_param params
;
207 int priority_min
, priority_max
;
209 if (pthread_getschedparam (thread_id
, &policy
, ¶ms
) == 0)
211 if ((priority_max
= sched_get_priority_max (policy
)) == -1)
214 if ((priority_min
= sched_get_priority_min (policy
)) == -1)
217 if (priority
> priority_max
)
218 priority
= priority_max
;
219 else if (priority
< priority_min
)
220 priority
= priority_min
;
221 params
.sched_priority
= priority
;
224 * The solaris 7 and several other man pages incorrectly state that
225 * this should be a pointer to policy but pthread.h is universally
228 if (pthread_setschedparam (thread_id
, policy
, ¶ms
) == 0)
231 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
232 #endif /* _POSIX_PRIORITY_SCHEDULING */
237 /* Return the current thread's priority. */
239 __gthread_objc_thread_get_priority (void)
241 #ifdef _POSIX_PRIORITY_SCHEDULING
242 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
243 if (__gthread_active_p ())
246 struct sched_param params
;
248 if (pthread_getschedparam (pthread_self (), &policy
, ¶ms
) == 0)
249 return params
.sched_priority
;
254 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
255 #endif /* _POSIX_PRIORITY_SCHEDULING */
256 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
259 /* Yield our process time to another thread. */
261 __gthread_objc_thread_yield (void)
263 if (__gthread_active_p ())
267 /* Terminate the current thread. */
269 __gthread_objc_thread_exit (void)
271 if (__gthread_active_p ())
272 /* exit the thread */
273 pthread_exit (&__objc_thread_exit_status
);
275 /* Failed if we reached here */
279 /* Returns an integer value which uniquely describes a thread. */
280 static inline objc_thread_t
281 __gthread_objc_thread_id (void)
283 if (__gthread_active_p ())
284 return (objc_thread_t
) pthread_self ();
286 return (objc_thread_t
) 1;
289 /* Sets the thread's local storage pointer. */
291 __gthread_objc_thread_set_data (void *value
)
293 if (__gthread_active_p ())
294 return pthread_setspecific (_objc_thread_storage
, value
);
297 thread_local_storage
= value
;
302 /* Returns the thread's local storage pointer. */
304 __gthread_objc_thread_get_data (void)
306 if (__gthread_active_p ())
307 return pthread_getspecific (_objc_thread_storage
);
309 return thread_local_storage
;
312 /* Backend mutex functions */
314 /* Allocate a mutex. */
316 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
318 if (__gthread_active_p ())
320 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
322 if (pthread_mutex_init ((pthread_mutex_t
*) mutex
->backend
, NULL
))
324 objc_free (mutex
->backend
);
325 mutex
->backend
= NULL
;
333 /* Deallocate a mutex. */
335 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
337 if (__gthread_active_p ())
342 * Posix Threads specifically require that the thread be unlocked
343 * for pthread_mutex_destroy to work.
348 count
= pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
);
354 if (pthread_mutex_destroy ((pthread_mutex_t
*) mutex
->backend
))
357 objc_free (mutex
->backend
);
358 mutex
->backend
= NULL
;
363 /* Grab a lock on a mutex. */
365 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
367 if (__gthread_active_p ()
368 && pthread_mutex_lock ((pthread_mutex_t
*) mutex
->backend
) != 0)
376 /* Try to grab a lock on a mutex. */
378 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
380 if (__gthread_active_p ()
381 && pthread_mutex_trylock ((pthread_mutex_t
*) mutex
->backend
) != 0)
389 /* Unlock the mutex */
391 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
393 if (__gthread_active_p ()
394 && pthread_mutex_unlock ((pthread_mutex_t
*) mutex
->backend
) != 0)
402 /* Backend condition mutex functions */
404 /* Allocate a condition. */
406 __gthread_objc_condition_allocate (objc_condition_t condition
)
408 if (__gthread_active_p ())
410 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
412 if (pthread_cond_init ((pthread_cond_t
*) condition
->backend
, NULL
))
414 objc_free (condition
->backend
);
415 condition
->backend
= NULL
;
423 /* Deallocate a condition. */
425 __gthread_objc_condition_deallocate (objc_condition_t condition
)
427 if (__gthread_active_p ())
429 if (pthread_cond_destroy ((pthread_cond_t
*) condition
->backend
))
432 objc_free (condition
->backend
);
433 condition
->backend
= NULL
;
438 /* Wait on the condition */
440 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
442 if (__gthread_active_p ())
443 return pthread_cond_wait ((pthread_cond_t
*) condition
->backend
,
444 (pthread_mutex_t
*) mutex
->backend
);
449 /* Wake up all threads waiting on this condition. */
451 __gthread_objc_condition_broadcast (objc_condition_t condition
)
453 if (__gthread_active_p ())
454 return pthread_cond_broadcast ((pthread_cond_t
*) condition
->backend
);
459 /* Wake up one thread waiting on this condition. */
461 __gthread_objc_condition_signal (objc_condition_t condition
)
463 if (__gthread_active_p ())
464 return pthread_cond_signal ((pthread_cond_t
*) condition
->backend
);
472 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
474 if (__gthread_active_p ())
475 return pthread_once (once
, func
);
481 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
483 return pthread_key_create (key
, dtor
);
487 __gthread_key_delete (__gthread_key_t key
)
489 return pthread_key_delete (key
);
493 __gthread_getspecific (__gthread_key_t key
)
495 return pthread_getspecific (key
);
499 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
501 return pthread_setspecific (key
, ptr
);
505 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
507 if (__gthread_active_p ())
508 return pthread_mutex_lock (mutex
);
514 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
516 if (__gthread_active_p ())
517 return pthread_mutex_trylock (mutex
);
523 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
525 if (__gthread_active_p ())
526 return pthread_mutex_unlock (mutex
);
532 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
535 mutex
->owner
= (pthread_t
) 0;
536 return pthread_mutex_init (&mutex
->actual
, NULL
);
540 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
542 if (__gthread_active_p ())
544 pthread_t me
= pthread_self ();
546 if (mutex
->owner
!= me
)
548 pthread_mutex_lock (&mutex
->actual
);
558 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
560 if (__gthread_active_p ())
562 pthread_t me
= pthread_self ();
564 if (mutex
->owner
!= me
)
566 if (pthread_mutex_trylock (&mutex
->actual
))
577 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
579 if (__gthread_active_p ())
581 if (--mutex
->depth
== 0)
583 mutex
->owner
= (pthread_t
) 0;
584 pthread_mutex_unlock (&mutex
->actual
);
590 #endif /* _LIBOBJC */
592 #endif /* ! GCC_GTHR_POSIX_H */