1 /* This file is automatically generated. DO NOT EDIT! */
2 /* Generated from: NetBSD: mknative-gcc,v 1.22 2006/06/25 03:06:15 mrg Exp */
3 /* Generated from: NetBSD: mknative.common,v 1.9 2007/02/05 18:26:01 apb Exp */
5 /* Threads compatibility routines for libgcc2 and libobjc. */
6 /* Compile this one with gcc. */
7 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
8 Free Software Foundation, Inc.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 2, or (at your option) any later
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING. If not, write to the Free
24 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 /* As a special exception, if you link this library with other files,
28 some of which are compiled with GCC, to produce an executable,
29 this library does not by itself cause the resulting executable
30 to be covered by the GNU General Public License.
31 This exception does not however invalidate any other reasons why
32 the executable file might be covered by the GNU General Public License. */
34 #ifndef _GLIBCXX_GCC_GTHR_POSIX_H
35 #define _GLIBCXX_GCC_GTHR_POSIX_H
37 /* POSIX threads specific definitions.
38 Easy, since the interface is just one-to-one mapping. */
42 /* Some implementations of <pthread.h> require this to be defined. */
43 #if !defined(_REENTRANT) && defined(__osf__)
50 typedef pthread_key_t __gthread_key_t
;
51 typedef pthread_once_t __gthread_once_t
;
52 typedef pthread_mutex_t __gthread_mutex_t
;
53 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
55 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
56 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
57 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
59 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
62 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
65 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
66 # ifndef __gthrw_pragma
67 # define __gthrw_pragma(pragma)
69 # define __gthrw2(name,name2,type) \
70 extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \
71 __gthrw_pragma(weak type)
72 # define __gthrw_(name) __gthrw_ ## name
74 # define __gthrw2(name,name2,type)
75 # define __gthrw_(name) name
78 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
79 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
81 /* On Tru64, /usr/include/pthread.h uses #pragma extern_prefix "__" to
82 map a subset of the POSIX pthread API to mangled versions of their
84 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
85 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
86 __gthrw3(pthread_once
)
87 __gthrw3(pthread_getspecific
)
88 __gthrw3(pthread_setspecific
)
89 __gthrw3(pthread_create
)
90 __gthrw3(pthread_cancel
)
91 __gthrw3(pthread_mutex_lock
)
92 __gthrw3(pthread_mutex_trylock
)
93 __gthrw3(pthread_mutex_unlock
)
94 __gthrw3(pthread_mutex_init
)
97 __gthrw(pthread_getspecific
)
98 __gthrw(pthread_setspecific
)
99 __gthrw(pthread_create
)
100 __gthrw(pthread_cancel
)
101 __gthrw(pthread_mutex_lock
)
102 __gthrw(pthread_mutex_trylock
)
103 __gthrw(pthread_mutex_unlock
)
104 __gthrw(pthread_mutex_init
)
107 __gthrw(pthread_key_create
)
108 __gthrw(pthread_key_delete
)
109 __gthrw(pthread_mutexattr_init
)
110 __gthrw(pthread_mutexattr_settype
)
111 __gthrw(pthread_mutexattr_destroy
)
114 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
116 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
117 __gthrw3(pthread_cond_broadcast
)
118 __gthrw3(pthread_cond_destroy
)
119 __gthrw3(pthread_cond_init
)
120 __gthrw3(pthread_cond_signal
)
121 __gthrw3(pthread_cond_wait
)
122 __gthrw3(pthread_exit
)
123 __gthrw3(pthread_mutex_destroy
)
124 __gthrw3(pthread_self
)
126 __gthrw(pthread_cond_broadcast
)
127 __gthrw(pthread_cond_destroy
)
128 __gthrw(pthread_cond_init
)
129 __gthrw(pthread_cond_signal
)
130 __gthrw(pthread_cond_wait
)
131 __gthrw(pthread_exit
)
132 __gthrw(pthread_mutex_destroy
)
133 __gthrw(pthread_self
)
134 #endif /* __osf__ && _PTHREAD_USE_MANGLED_NAMES_ */
135 #ifdef _POSIX_PRIORITY_SCHEDULING
136 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
137 __gthrw(sched_get_priority_max
)
138 __gthrw(sched_get_priority_min
)
139 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
140 #endif /* _POSIX_PRIORITY_SCHEDULING */
142 __gthrw(pthread_attr_destroy
)
143 __gthrw(pthread_attr_init
)
144 __gthrw(pthread_attr_setdetachstate
)
145 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
146 __gthrw(pthread_getschedparam
)
147 __gthrw(pthread_setschedparam
)
148 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
149 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
151 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
154 __gthread_active_p (void)
156 static void *const __gthread_active_ptr
157 = __extension__ (void *) &__gthrw_(pthread_cancel
);
158 return __gthread_active_ptr
!= 0;
161 #else /* not __GXX_WEAK__ */
164 __gthread_active_p (void)
169 #endif /* __GXX_WEAK__ */
173 /* This is the config.h file in libobjc/ */
180 /* Key structure for maintaining thread specific storage */
181 static pthread_key_t _objc_thread_storage
;
182 static pthread_attr_t _objc_thread_attribs
;
184 /* Thread local storage for a single thread */
185 static void *thread_local_storage
= NULL
;
187 /* Backend initialization functions */
189 /* Initialize the threads subsystem. */
191 __gthread_objc_init_thread_system (void)
193 if (__gthread_active_p ())
195 /* Initialize the thread storage key. */
196 if (__gthrw_(pthread_key_create
) (&_objc_thread_storage
, NULL
) == 0)
198 /* The normal default detach state for threads is
199 * PTHREAD_CREATE_JOINABLE which causes threads to not die
200 * when you think they should. */
201 if (__gthrw_(pthread_attr_init
) (&_objc_thread_attribs
) == 0
202 && __gthrw_(pthread_attr_setdetachstate
) (&_objc_thread_attribs
,
203 PTHREAD_CREATE_DETACHED
) == 0)
211 /* Close the threads subsystem. */
213 __gthread_objc_close_thread_system (void)
215 if (__gthread_active_p ()
216 && __gthrw_(pthread_key_delete
) (_objc_thread_storage
) == 0
217 && __gthrw_(pthread_attr_destroy
) (&_objc_thread_attribs
) == 0)
223 /* Backend thread functions */
225 /* Create a new thread of execution. */
226 static inline objc_thread_t
227 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
229 objc_thread_t thread_id
;
230 pthread_t new_thread_handle
;
232 if (!__gthread_active_p ())
235 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, NULL
, (void *) func
, arg
)))
236 thread_id
= (objc_thread_t
) new_thread_handle
;
243 /* Set the current thread's priority. */
245 __gthread_objc_thread_set_priority (int priority
)
247 if (!__gthread_active_p ())
251 #ifdef _POSIX_PRIORITY_SCHEDULING
252 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
253 pthread_t thread_id
= __gthrw_(pthread_self
) ();
255 struct sched_param params
;
256 int priority_min
, priority_max
;
258 if (__gthrw_(pthread_getschedparam
) (thread_id
, &policy
, ¶ms
) == 0)
260 if ((priority_max
= __gthrw_(sched_get_priority_max
) (policy
)) == -1)
263 if ((priority_min
= __gthrw_(sched_get_priority_min
) (policy
)) == -1)
266 if (priority
> priority_max
)
267 priority
= priority_max
;
268 else if (priority
< priority_min
)
269 priority
= priority_min
;
270 params
.sched_priority
= priority
;
273 * The solaris 7 and several other man pages incorrectly state that
274 * this should be a pointer to policy but pthread.h is universally
277 if (__gthrw_(pthread_setschedparam
) (thread_id
, policy
, ¶ms
) == 0)
280 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
281 #endif /* _POSIX_PRIORITY_SCHEDULING */
286 /* Return the current thread's priority. */
288 __gthread_objc_thread_get_priority (void)
290 #ifdef _POSIX_PRIORITY_SCHEDULING
291 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
292 if (__gthread_active_p ())
295 struct sched_param params
;
297 if (__gthrw_(pthread_getschedparam
) (__gthrw_(pthread_self
) (), &policy
, ¶ms
) == 0)
298 return params
.sched_priority
;
303 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
304 #endif /* _POSIX_PRIORITY_SCHEDULING */
305 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
308 /* Yield our process time to another thread. */
310 __gthread_objc_thread_yield (void)
312 if (__gthread_active_p ())
313 __gthrw_(sched_yield
) ();
316 /* Terminate the current thread. */
318 __gthread_objc_thread_exit (void)
320 if (__gthread_active_p ())
321 /* exit the thread */
322 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
324 /* Failed if we reached here */
328 /* Returns an integer value which uniquely describes a thread. */
329 static inline objc_thread_t
330 __gthread_objc_thread_id (void)
332 if (__gthread_active_p ())
333 return (objc_thread_t
) __gthrw_(pthread_self
) ();
335 return (objc_thread_t
) 1;
338 /* Sets the thread's local storage pointer. */
340 __gthread_objc_thread_set_data (void *value
)
342 if (__gthread_active_p ())
343 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
346 thread_local_storage
= value
;
351 /* Returns the thread's local storage pointer. */
353 __gthread_objc_thread_get_data (void)
355 if (__gthread_active_p ())
356 return __gthrw_(pthread_getspecific
) (_objc_thread_storage
);
358 return thread_local_storage
;
361 /* Backend mutex functions */
363 /* Allocate a mutex. */
365 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
367 if (__gthread_active_p ())
369 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
371 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
, NULL
))
373 objc_free (mutex
->backend
);
374 mutex
->backend
= NULL
;
382 /* Deallocate a mutex. */
384 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
386 if (__gthread_active_p ())
391 * Posix Threads specifically require that the thread be unlocked
392 * for __gthrw_(pthread_mutex_destroy) to work.
397 count
= __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
403 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
406 objc_free (mutex
->backend
);
407 mutex
->backend
= NULL
;
412 /* Grab a lock on a mutex. */
414 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
416 if (__gthread_active_p ()
417 && __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
425 /* Try to grab a lock on a mutex. */
427 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
429 if (__gthread_active_p ()
430 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
438 /* Unlock the mutex */
440 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
442 if (__gthread_active_p ()
443 && __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
451 /* Backend condition mutex functions */
453 /* Allocate a condition. */
455 __gthread_objc_condition_allocate (objc_condition_t condition
)
457 if (__gthread_active_p ())
459 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
461 if (__gthrw_(pthread_cond_init
) ((pthread_cond_t
*) condition
->backend
, NULL
))
463 objc_free (condition
->backend
);
464 condition
->backend
= NULL
;
472 /* Deallocate a condition. */
474 __gthread_objc_condition_deallocate (objc_condition_t condition
)
476 if (__gthread_active_p ())
478 if (__gthrw_(pthread_cond_destroy
) ((pthread_cond_t
*) condition
->backend
))
481 objc_free (condition
->backend
);
482 condition
->backend
= NULL
;
487 /* Wait on the condition */
489 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
491 if (__gthread_active_p ())
492 return __gthrw_(pthread_cond_wait
) ((pthread_cond_t
*) condition
->backend
,
493 (pthread_mutex_t
*) mutex
->backend
);
498 /* Wake up all threads waiting on this condition. */
500 __gthread_objc_condition_broadcast (objc_condition_t condition
)
502 if (__gthread_active_p ())
503 return __gthrw_(pthread_cond_broadcast
) ((pthread_cond_t
*) condition
->backend
);
508 /* Wake up one thread waiting on this condition. */
510 __gthread_objc_condition_signal (objc_condition_t condition
)
512 if (__gthread_active_p ())
513 return __gthrw_(pthread_cond_signal
) ((pthread_cond_t
*) condition
->backend
);
521 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
523 if (__gthread_active_p ())
524 return __gthrw_(pthread_once
) (once
, func
);
530 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
532 return __gthrw_(pthread_key_create
) (key
, dtor
);
536 __gthread_key_delete (__gthread_key_t key
)
538 return __gthrw_(pthread_key_delete
) (key
);
542 __gthread_getspecific (__gthread_key_t key
)
544 return __gthrw_(pthread_getspecific
) (key
);
548 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
550 return __gthrw_(pthread_setspecific
) (key
, ptr
);
554 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
556 if (__gthread_active_p ())
557 return __gthrw_(pthread_mutex_lock
) (mutex
);
563 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
565 if (__gthread_active_p ())
566 return __gthrw_(pthread_mutex_trylock
) (mutex
);
572 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
574 if (__gthread_active_p ())
575 return __gthrw_(pthread_mutex_unlock
) (mutex
);
580 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
582 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
584 if (__gthread_active_p ())
586 pthread_mutexattr_t attr
;
589 r
= __gthrw_(pthread_mutexattr_init
) (&attr
);
591 r
= __gthrw_(pthread_mutexattr_settype
) (&attr
, PTHREAD_MUTEX_RECURSIVE
);
593 r
= __gthrw_(pthread_mutex_init
) (mutex
, &attr
);
595 r
= __gthrw_(pthread_mutexattr_destroy
) (&attr
);
603 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
605 return __gthread_mutex_lock (mutex
);
609 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
611 return __gthread_mutex_trylock (mutex
);
615 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
617 return __gthread_mutex_unlock (mutex
);
620 #endif /* _LIBOBJC */
622 #endif /* ! _GLIBCXX_GCC_GTHR_POSIX_H */