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_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
52 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
54 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
56 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
58 #pragma weak pthread_once
59 #pragma weak pthread_once_init
60 #pragma weak pthread_keycreate
61 #pragma weak pthread_key_delete
62 #pragma weak pthread_getspecific
63 #pragma weak pthread_setspecific
64 #pragma weak pthread_create
65 #pragma weak pthread_mutex_init
66 #pragma weak pthread_mutex_lock
67 #pragma weak pthread_mutex_trylock
68 #pragma weak pthread_mutex_unlock
72 #pragma weak pthread_cond_broadcast
73 #pragma weak pthread_cond_destroy
74 #pragma weak pthread_cond_init
75 #pragma weak pthread_cond_signal
76 #pragma weak pthread_cond_wait
77 #pragma weak pthread_exit
78 #pragma weak pthread_getunique_np
79 #pragma weak pthread_mutex_destroy
80 #pragma weak pthread_self
81 #pragma weak pthread_yield
84 static void *__gthread_active_ptr
= (void *) &pthread_create
;
87 __gthread_active_p (void)
89 return __gthread_active_ptr
!= 0;
92 #else /* not SUPPORTS_WEAK */
95 __gthread_active_p (void)
100 #endif /* SUPPORTS_WEAK */
104 /* Key structure for maintaining thread specific storage */
105 static pthread_key_t _objc_thread_storage
;
107 /* Thread local storage for a single thread */
108 static void *thread_local_storage
= NULL
;
110 /* Backend initialization functions */
112 /* Initialize the threads subsystem. */
114 __gthread_objc_init_thread_system(void)
116 if (__gthread_active_p ())
117 /* Initialize the thread storage key */
118 return pthread_keycreate (&_objc_thread_storage
, NULL
);
123 /* Close the threads subsystem. */
125 __gthread_objc_close_thread_system(void)
127 if (__gthread_active_p ())
133 /* Backend thread functions */
135 /* Create a new thread of execution. */
136 static inline objc_thread_t
137 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
139 objc_thread_t thread_id
;
140 pthread_t new_thread_handle
;
142 if (!__gthread_active_p ())
145 if ( !(pthread_create(&new_thread_handle
, pthread_attr_default
,
146 (void *)func
, arg
)) )
148 /* ??? May not work! (64bit) */
149 thread_id
= *(objc_thread_t
*)&new_thread_handle
;
150 pthread_detach(&new_thread_handle
); /* Fully detach thread. */
158 /* Set the current thread's priority. */
160 __gthread_objc_thread_set_priority(int priority
)
162 int sys_priority
= 0;
164 if (!__gthread_active_p ())
169 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
170 sys_priority
= (PRI_FG_MIN_NP
+ PRI_FG_MAX_NP
) / 2;
173 case OBJC_THREAD_BACKGROUND_PRIORITY
:
174 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
176 case OBJC_THREAD_LOW_PRIORITY
:
177 sys_priority
= (PRI_BG_MIN_NP
+ PRI_BG_MAX_NP
) / 2;
181 /* Change the priority. */
182 if (pthread_setprio(pthread_self(), sys_priority
) >= 0)
189 /* Return the current thread's priority. */
191 __gthread_objc_thread_get_priority(void)
195 if (__gthread_active_p ())
197 if ((sys_priority
= pthread_getprio(pthread_self())) >= 0)
199 if (sys_priority
>= PRI_FG_MIN_NP
200 && sys_priority
<= PRI_FG_MAX_NP
)
201 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
202 if (sys_priority
>= PRI_BG_MIN_NP
203 && sys_priority
<= PRI_BG_MAX_NP
)
204 return OBJC_THREAD_BACKGROUND_PRIORITY
;
205 return OBJC_THREAD_LOW_PRIORITY
;
212 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
215 /* Yield our process time to another thread. */
217 __gthread_objc_thread_yield(void)
219 if (__gthread_active_p ())
223 /* Terminate the current thread. */
225 __gthread_objc_thread_exit(void)
227 if (__gthread_active_p ())
228 /* exit the thread */
229 pthread_exit(&__objc_thread_exit_status
);
231 /* Failed if we reached here */
235 /* Returns an integer value which uniquely describes a thread. */
236 static inline objc_thread_t
237 __gthread_objc_thread_id(void)
239 if (__gthread_active_p ())
241 pthread_t self
= pthread_self();
243 return (objc_thread_t
) pthread_getunique_np (&self
);
246 return (objc_thread_t
)1;
249 /* Sets the thread's local storage pointer. */
251 __gthread_objc_thread_set_data(void *value
)
253 if (__gthread_active_p ())
254 return pthread_setspecific(_objc_thread_storage
, value
);
257 thread_local_storage
= value
;
262 /* Returns the thread's local storage pointer. */
264 __gthread_objc_thread_get_data(void)
268 if (__gthread_active_p ())
270 if ( !(pthread_getspecific(_objc_thread_storage
, &value
)) )
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
,
290 pthread_mutexattr_default
))
292 objc_free(mutex
->backend
);
293 mutex
->backend
= NULL
;
301 /* Deallocate a mutex. */
303 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
305 if (__gthread_active_p ())
307 if (pthread_mutex_destroy((pthread_mutex_t
*)mutex
->backend
))
310 objc_free(mutex
->backend
);
311 mutex
->backend
= NULL
;
317 /* Grab a lock on a mutex. */
319 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
321 if (__gthread_active_p ())
322 return pthread_mutex_lock((pthread_mutex_t
*)mutex
->backend
);
327 /* Try to grab a lock on a mutex. */
329 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
331 if (__gthread_active_p ()
332 && pthread_mutex_trylock((pthread_mutex_t
*)mutex
->backend
) != 1)
338 /* Unlock the mutex */
340 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
342 if (__gthread_active_p ())
343 return pthread_mutex_unlock((pthread_mutex_t
*)mutex
->backend
);
348 /* Backend condition mutex functions */
350 /* Allocate a condition. */
352 __gthread_objc_condition_allocate(objc_condition_t condition
)
354 if (__gthread_active_p ())
361 /* Deallocate a condition. */
363 __gthread_objc_condition_deallocate(objc_condition_t condition
)
365 if (__gthread_active_p ())
372 /* Wait on the condition */
374 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
376 if (__gthread_active_p ())
383 /* Wake up all threads waiting on this condition. */
385 __gthread_objc_condition_broadcast(objc_condition_t condition
)
387 if (__gthread_active_p ())
394 /* Wake up one thread waiting on this condition. */
396 __gthread_objc_condition_signal(objc_condition_t condition
)
398 if (__gthread_active_p ())
408 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
410 if (__gthread_active_p ())
411 return pthread_once (once
, func
);
417 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
419 return pthread_keycreate (key
, dtor
);
423 __gthread_key_dtor (UNUSED (__gthread_key_t key
), UNUSED (void *ptr
))
425 /* Nothing needed. */
430 __gthread_key_delete (UNUSED (__gthread_key_t key
))
432 /* Operation is not supported. */
437 __gthread_getspecific (__gthread_key_t key
)
440 if (pthread_getspecific (key
, &ptr
) == 0)
447 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
449 return pthread_setspecific (key
, (void *) ptr
);
453 __gthread_mutex_init_function (__gthread_mutex_t
*mutex
)
455 if (__gthread_active_p ())
456 pthread_mutex_init (mutex
, pthread_mutexattr_default
);
460 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
462 if (__gthread_active_p ())
463 return pthread_mutex_lock (mutex
);
469 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
471 if (__gthread_active_p ())
472 return pthread_mutex_trylock (mutex
);
478 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
480 if (__gthread_active_p ())
481 return pthread_mutex_unlock (mutex
);
486 #endif /* _LIBOBJC */
490 #endif /* ! GCC_GTHR_DCE_H */