(__gthread_objc_init_thread_system): If pthread_key_create fails, it's not clear...
[gcc.git] / gcc / gthr-posix.h
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.
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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.
16
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. */
21
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. */
28
29 #ifndef GCC_GTHR_POSIX_H
30 #define GCC_GTHR_POSIX_H
31
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
34
35 #define __GTHREADS 1
36
37 #include <pthread.h>
38
39 typedef pthread_key_t __gthread_key_t;
40 typedef pthread_once_t __gthread_once_t;
41 typedef pthread_mutex_t __gthread_mutex_t;
42
43 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
45
46 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
47
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
54
55 #pragma weak pthread_mutex_lock
56 #pragma weak pthread_mutex_trylock
57 #pragma weak pthread_mutex_unlock
58
59 #ifdef _LIBOBJC
60 /* Objective C. */
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_yield
71 #pragma weak pthread_attr_destroy
72 #pragma weak pthread_attr_init
73 #pragma weak pthread_attr_setdetachstate
74 #pragma weak pthread_getschedparam
75 #pragma weak pthread_setschedparam
76 #endif
77
78 static void *__gthread_active_ptr = (void *) &pthread_create;
79
80 static inline int
81 __gthread_active_p (void)
82 {
83 return __gthread_active_ptr != 0;
84 }
85
86 #else /* not SUPPORTS_WEAK */
87
88 static inline int
89 __gthread_active_p (void)
90 {
91 return 1;
92 }
93
94 #endif /* SUPPORTS_WEAK */
95
96 #ifdef _LIBOBJC
97
98 /* This is the config.h file in libobjc/ */
99 #include <config.h>
100
101 #ifdef HAVE_SCHED_H
102 # include <sched.h>
103 #endif
104
105 /* Key structure for maintaining thread specific storage */
106 static pthread_key_t _objc_thread_storage;
107 static pthread_attr_t _objc_thread_attribs;
108
109 /* Thread local storage for a single thread */
110 static void *thread_local_storage = NULL;
111
112 /* Backend initialization functions */
113
114 /* Initialize the threads subsystem. */
115 static inline int
116 __gthread_objc_init_thread_system(void)
117 {
118 if (__gthread_active_p ())
119 {
120 /* Initialize the thread storage key */
121 if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
122 {
123 /* The normal default detach state for threads is
124 * PTHREAD_CREATE_JOINABLE which causes threads to not die
125 * when you think they should. */
126 if (pthread_attr_init(&_objc_thread_attribs) == 0
127 && pthread_attr_setdetachstate(&_objc_thread_attribs,
128 PTHREAD_CREATE_DETACHED) == 0)
129 return 0;
130 }
131 }
132
133 return -1;
134 }
135
136 /* Close the threads subsystem. */
137 static inline int
138 __gthread_objc_close_thread_system(void)
139 {
140 if (__gthread_active_p ()
141 && pthread_key_delete(_objc_thread_storage) == 0
142 && pthread_attr_destroy(&_objc_thread_attribs) == 0)
143 return 0;
144
145 return -1;
146 }
147
148 /* Backend thread functions */
149
150 /* Create a new thread of execution. */
151 static inline objc_thread_t
152 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
153 {
154 objc_thread_t thread_id;
155 pthread_t new_thread_handle;
156
157 if (!__gthread_active_p ())
158 return NULL;
159
160 if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
161 thread_id = (objc_thread_t) new_thread_handle;
162 else
163 thread_id = NULL;
164
165 return thread_id;
166 }
167
168 /* Set the current thread's priority. */
169 static inline int
170 __gthread_objc_thread_set_priority(int priority)
171 {
172 if (!__gthread_active_p())
173 return -1;
174 else {
175 pthread_t thread_id = pthread_self();
176 int policy;
177 struct sched_param params;
178 int priority_min, priority_max;
179
180 if (pthread_getschedparam(thread_id, &policy, &params) == 0)
181 {
182 if ((priority_max = sched_get_priority_max(policy)) != 0)
183 return -1;
184
185 if ((priority_min = sched_get_priority_min(policy)) != 0)
186 return -1;
187
188 if (priority > priority_max)
189 priority = priority_max;
190 else if (priority < priority_min)
191 priority = priority_min;
192 params.sched_priority = priority;
193
194 /*
195 * The solaris 7 and several other man pages incorrectly state that
196 * this should be a pointer to policy but pthread.h is universally
197 * at odds with this.
198 */
199 if (pthread_setschedparam(thread_id, policy, &params) == 0)
200 return 0;
201 }
202 return -1;
203 }
204 }
205
206 /* Return the current thread's priority. */
207 static inline int
208 __gthread_objc_thread_get_priority(void)
209 {
210 if (__gthread_active_p ())
211 {
212 int policy;
213 struct sched_param params;
214
215 if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
216 return params.sched_priority;
217 else
218 return -1;
219 }
220 else
221 return OBJC_THREAD_INTERACTIVE_PRIORITY;
222 }
223
224 /* Yield our process time to another thread. */
225 static inline void
226 __gthread_objc_thread_yield(void)
227 {
228 if (__gthread_active_p ())
229 sched_yield();
230 }
231
232 /* Terminate the current thread. */
233 static inline int
234 __gthread_objc_thread_exit(void)
235 {
236 if (__gthread_active_p ())
237 /* exit the thread */
238 pthread_exit(&__objc_thread_exit_status);
239
240 /* Failed if we reached here */
241 return -1;
242 }
243
244 /* Returns an integer value which uniquely describes a thread. */
245 static inline objc_thread_t
246 __gthread_objc_thread_id(void)
247 {
248 if (__gthread_active_p ())
249 return (objc_thread_t) pthread_self();
250 else
251 return (objc_thread_t) 1;
252 }
253
254 /* Sets the thread's local storage pointer. */
255 static inline int
256 __gthread_objc_thread_set_data(void *value)
257 {
258 if (__gthread_active_p ())
259 return pthread_setspecific(_objc_thread_storage, value);
260 else
261 {
262 thread_local_storage = value;
263 return 0;
264 }
265 }
266
267 /* Returns the thread's local storage pointer. */
268 static inline void *
269 __gthread_objc_thread_get_data(void)
270 {
271 if (__gthread_active_p ())
272 return pthread_getspecific(_objc_thread_storage);
273 else
274 return thread_local_storage;
275 }
276
277 /* Backend mutex functions */
278
279 /* Allocate a mutex. */
280 static inline int
281 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
282 {
283 if (__gthread_active_p ())
284 {
285 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
286
287 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
288 {
289 objc_free(mutex->backend);
290 mutex->backend = NULL;
291 return -1;
292 }
293 }
294
295 return 0;
296 }
297
298 /* Deallocate a mutex. */
299 static inline int
300 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
301 {
302 if (__gthread_active_p ())
303 {
304 int count;
305
306 /*
307 * Posix Threads specifically require that the thread be unlocked
308 * for pthread_mutex_destroy to work.
309 */
310
311 do
312 {
313 count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
314 if (count < 0)
315 return -1;
316 }
317 while (count);
318
319 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
320 return -1;
321
322 objc_free(mutex->backend);
323 mutex->backend = NULL;
324 }
325 return 0;
326 }
327
328 /* Grab a lock on a mutex. */
329 static inline int
330 __gthread_objc_mutex_lock(objc_mutex_t mutex)
331 {
332 if (__gthread_active_p ())
333 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
334 else
335 return 0;
336 }
337
338 /* Try to grab a lock on a mutex. */
339 static inline int
340 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
341 {
342 if (__gthread_active_p ())
343 return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
344 else
345 return 0;
346 }
347
348 /* Unlock the mutex */
349 static inline int
350 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
351 {
352 if (__gthread_active_p ())
353 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
354 else
355 return 0;
356 }
357
358 /* Backend condition mutex functions */
359
360 /* Allocate a condition. */
361 static inline int
362 __gthread_objc_condition_allocate(objc_condition_t condition)
363 {
364 if (__gthread_active_p ())
365 {
366 condition->backend = objc_malloc(sizeof(pthread_cond_t));
367
368 if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
369 {
370 objc_free(condition->backend);
371 condition->backend = NULL;
372 return -1;
373 }
374 }
375
376 return 0;
377 }
378
379 /* Deallocate a condition. */
380 static inline int
381 __gthread_objc_condition_deallocate(objc_condition_t condition)
382 {
383 if (__gthread_active_p ())
384 {
385 if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
386 return -1;
387
388 objc_free(condition->backend);
389 condition->backend = NULL;
390 }
391 return 0;
392 }
393
394 /* Wait on the condition */
395 static inline int
396 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
397 {
398 if (__gthread_active_p ())
399 return pthread_cond_wait((pthread_cond_t *)condition->backend,
400 (pthread_mutex_t *)mutex->backend);
401 else
402 return 0;
403 }
404
405 /* Wake up all threads waiting on this condition. */
406 static inline int
407 __gthread_objc_condition_broadcast(objc_condition_t condition)
408 {
409 if (__gthread_active_p ())
410 return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
411 else
412 return 0;
413 }
414
415 /* Wake up one thread waiting on this condition. */
416 static inline int
417 __gthread_objc_condition_signal(objc_condition_t condition)
418 {
419 if (__gthread_active_p ())
420 return pthread_cond_signal((pthread_cond_t *)condition->backend);
421 else
422 return 0;
423 }
424
425 #else /* _LIBOBJC */
426
427 static inline int
428 __gthread_once (__gthread_once_t *once, void (*func) (void))
429 {
430 if (__gthread_active_p ())
431 return pthread_once (once, func);
432 else
433 return -1;
434 }
435
436 static inline int
437 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
438 {
439 return pthread_key_create (key, dtor);
440 }
441
442 static inline int
443 __gthread_key_dtor (__gthread_key_t key, void *ptr)
444 {
445 /* Just reset the key value to zero. */
446 if (ptr)
447 return pthread_setspecific (key, 0);
448 else
449 return 0;
450 }
451
452 static inline int
453 __gthread_key_delete (__gthread_key_t key)
454 {
455 return pthread_key_delete (key);
456 }
457
458 static inline void *
459 __gthread_getspecific (__gthread_key_t key)
460 {
461 return pthread_getspecific (key);
462 }
463
464 static inline int
465 __gthread_setspecific (__gthread_key_t key, const void *ptr)
466 {
467 return pthread_setspecific (key, ptr);
468 }
469
470 static inline int
471 __gthread_mutex_lock (__gthread_mutex_t *mutex)
472 {
473 if (__gthread_active_p ())
474 return pthread_mutex_lock (mutex);
475 else
476 return 0;
477 }
478
479 static inline int
480 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
481 {
482 if (__gthread_active_p ())
483 return pthread_mutex_trylock (mutex);
484 else
485 return 0;
486 }
487
488 static inline int
489 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
490 {
491 if (__gthread_active_p ())
492 return pthread_mutex_unlock (mutex);
493 else
494 return 0;
495 }
496
497 #endif /* _LIBOBJC */
498
499 #endif /* ! GCC_GTHR_POSIX_H */