2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001 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 GCC_GTHR_DCE_H
30 #define GCC_GTHR_DCE_H
32 /* DCE threads interface.
33 DCE threads are based on POSIX threads draft 4, and many things
34 have changed since then. */
43 #define UNUSED(x) x __attribute__((unused))
46 typedef pthread_key_t __gthread_key_t
;
47 typedef pthread_once_t __gthread_once_t
;
48 typedef pthread_mutex_t __gthread_mutex_t
;
50 #define __GTHREAD_ONCE_INIT pthread_once_init
51 /* Howto define __GTHREAD_MUTEX_INIT? */
53 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
55 #pragma weak pthread_once
56 #pragma weak pthread_once_init
57 #pragma weak pthread_keycreate
58 #pragma weak pthread_key_delete
59 #pragma weak pthread_getspecific
60 #pragma weak pthread_setspecific
61 #pragma weak pthread_create
63 #pragma weak pthread_mutex_lock
64 #pragma weak pthread_mutex_trylock
65 #pragma weak pthread_mutex_unlock
69 #pragma weak pthread_cond_broadcast
70 #pragma weak pthread_cond_destroy
71 #pragma weak pthread_cond_init
72 #pragma weak pthread_cond_signal
73 #pragma weak pthread_cond_wait
74 #pragma weak pthread_exit
75 #pragma weak pthread_getunique_np
76 #pragma weak pthread_mutex_init
77 #pragma weak pthread_mutex_destroy
78 #pragma weak pthread_self
79 #pragma weak pthread_yield
82 static void *__gthread_active_ptr
= (void *) &pthread_create
;
85 __gthread_active_p (void)
87 return __gthread_active_ptr
!= 0;
90 #else /* not SUPPORTS_WEAK */
93 __gthread_active_p (void)
98 #endif /* SUPPORTS_WEAK */
102 /* Key structure for maintaining thread specific storage */
103 static pthread_key_t _objc_thread_storage
;
105 /* Thread local storage for a single thread */
106 static void *thread_local_storage
= NULL
;
108 /* Backend initialization functions */
110 /* Initialize the threads subsystem. */
112 __gthread_objc_init_thread_system(void)
114 if (__gthread_active_p ())
115 /* Initialize the thread storage key */
116 return pthread_keycreate (&_objc_thread_storage
, NULL
);
121 /* Close the threads subsystem. */
123 __gthread_objc_close_thread_system(void)
125 if (__gthread_active_p ())
131 /* Backend thread functions */
133 /* Create a new thread of execution. */
134 static inline objc_thread_t
135 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
137 objc_thread_t thread_id
;
138 pthread_t new_thread_handle
;
140 if (!__gthread_active_p ())
143 if ( !(pthread_create(&new_thread_handle
, pthread_attr_default
,
144 (void *)func
, arg
)) )
146 /* ??? May not work! (64bit) */
147 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
148 pthread_detach(&new_thread_handle
); /* Fully detach thread. */
156 /* Set the current thread's priority. */
158 __gthread_objc_thread_set_priority(int priority
)
160 int sys_priority
= 0;
162 if (!__gthread_active_p ())
167 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
168 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
171 case OBJC_THREAD_BACKGROUND_PRIORITY
:
172 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
174 case OBJC_THREAD_LOW_PRIORITY
:
175 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
179 /* Change the priority. */
180 if (pthread_setprio(pthread_self(), sys_priority
) >= 0)
187 /* Return the current thread's priority. */
189 __gthread_objc_thread_get_priority(void)
193 if (__gthread_active_p ())
195 if ((sys_priority
= pthread_getprio(pthread_self())) >= 0)
197 if (sys_priority
>= PRI_FG_MIN_NP
198 && sys_priority
<= PRI_FG_MAX_NP
)
199 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
200 if (sys_priority
>= PRI_BG_MIN_NP
201 && sys_priority
<= PRI_BG_MAX_NP
)
202 return OBJC_THREAD_BACKGROUND_PRIORITY
;
203 return OBJC_THREAD_LOW_PRIORITY
;
210 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
213 /* Yield our process time to another thread. */
215 __gthread_objc_thread_yield(void)
217 if (__gthread_active_p ())
221 /* Terminate the current thread. */
223 __gthread_objc_thread_exit(void)
225 if (__gthread_active_p ())
226 /* exit the thread */
227 pthread_exit(&__objc_thread_exit_status
);
229 /* Failed if we reached here */
233 /* Returns an integer value which uniquely describes a thread. */
234 static inline objc_thread_t
235 __gthread_objc_thread_id(void)
237 if (__gthread_active_p ())
239 pthread_t self
= pthread_self();
241 return (objc_thread_t
) pthread_getunique_np (&self
);
244 return (objc_thread_t
)1;
247 /* Sets the thread's local storage pointer. */
249 __gthread_objc_thread_set_data(void *value
)
251 if (__gthread_active_p ())
252 return pthread_setspecific(_objc_thread_storage
, value
);
255 thread_local_storage
= value
;
260 /* Returns the thread's local storage pointer. */
262 __gthread_objc_thread_get_data(void)
266 if (__gthread_active_p ())
268 if ( !(pthread_getspecific(_objc_thread_storage
, &value
)) )
274 return thread_local_storage
;
277 /* Backend mutex functions */
279 /* Allocate a mutex. */
281 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
283 if (__gthread_active_p ())
285 mutex
->backend
= objc_malloc(sizeof(pthread_mutex_t
));
287 if (pthread_mutex_init((pthread_mutex_t
*)mutex
->backend
,
288 pthread_mutexattr_default
))
290 objc_free(mutex
->backend
);
291 mutex
->backend
= NULL
;
299 /* Deallocate a mutex. */
301 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
303 if (__gthread_active_p ())
305 if (pthread_mutex_destroy((pthread_mutex_t
*)mutex
->backend
))
308 objc_free(mutex
->backend
);
309 mutex
->backend
= NULL
;
315 /* Grab a lock on a mutex. */
317 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
319 if (__gthread_active_p ())
320 return pthread_mutex_lock((pthread_mutex_t
*)mutex
->backend
);
325 /* Try to grab a lock on a mutex. */
327 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
329 if (__gthread_active_p ()
330 && pthread_mutex_trylock((pthread_mutex_t
*)mutex
->backend
) != 1)
336 /* Unlock the mutex */
338 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
340 if (__gthread_active_p ())
341 return pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
);
346 /* Backend condition mutex functions */
348 /* Allocate a condition. */
350 __gthread_objc_condition_allocate(objc_condition_t condition
)
352 if (__gthread_active_p ())
359 /* Deallocate a condition. */
361 __gthread_objc_condition_deallocate(objc_condition_t condition
)
363 if (__gthread_active_p ())
370 /* Wait on the condition */
372 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
374 if (__gthread_active_p ())
381 /* Wake up all threads waiting on this condition. */
383 __gthread_objc_condition_broadcast(objc_condition_t condition
)
385 if (__gthread_active_p ())
392 /* Wake up one thread waiting on this condition. */
394 __gthread_objc_condition_signal(objc_condition_t condition
)
396 if (__gthread_active_p ())
406 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
408 if (__gthread_active_p ())
409 return pthread_once (once
, func
);
415 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
417 return pthread_keycreate (key
, dtor
);
421 __gthread_key_dtor (UNUSED (__gthread_key_t key
), UNUSED (void *ptr
))
423 /* Nothing needed. */
427 #if defined (__PTHREAD_LIBRARY_VERSION_1) && __PTHREAD_LIBRARY_VERSION_1 >= 1
429 __gthread_key_delete (__gthread_key_t key
)
431 return pthread_key_delete (key
);
435 __gthread_key_delete (UNUSED (__gthread_key_t key
))
437 /* Operation is not supported. */
443 __gthread_getspecific (__gthread_key_t key
)
446 if (pthread_getspecific (key
, &ptr
) == 0)
453 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
455 return pthread_setspecific (key
, (void *) ptr
);
459 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
461 if (__gthread_active_p ())
462 return pthread_mutex_lock (mutex
);
468 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
470 if (__gthread_active_p ())
471 return pthread_mutex_trylock (mutex
);
477 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
479 if (__gthread_active_p ())
480 return pthread_mutex_unlock (mutex
);
485 #endif /* _LIBOBJC */
489 #endif /* ! GCC_GTHR_DCE_H */