gthr-posix95.h: Remove declaration of pthread_mutexattr_settype and duplicate declara...
[gcc.git] / gcc / gthr-posix95.h
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 for more details.
16
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 /* Some implementations of <pthread.h> require this to be defined. */
38 #ifndef _REENTRANT
39 #define _REENTRANT 1
40 #endif
41
42 #include <pthread.h>
43 #include <unistd.h>
44
45 typedef pthread_key_t __gthread_key_t;
46 typedef pthread_once_t __gthread_once_t;
47 typedef pthread_mutex_t __gthread_mutex_t;
48
49 typedef struct {
50 long depth;
51 pthread_t owner;
52 pthread_mutex_t actual;
53 } __gthread_recursive_mutex_t;
54
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
58
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 extern __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
62 #else
63 # define __gthrw_asmname(cname) __gthrw_asmnamep (__USER_LABEL_PREFIX__, cname)
64 # define __gthrw_asmnamep(prefix, cname) __gthrw_string (prefix) cname
65 # define __gthrw_string(x) #x
66 # define __gthrw(name) \
67 extern __typeof(name) __gthrw_ ## name __asm (__gthrw_asmname (#name))
68 #endif
69
70 __gthrw(pthread_once);
71 __gthrw(pthread_key_create);
72 __gthrw(pthread_key_delete);
73 __gthrw(pthread_getspecific);
74 __gthrw(pthread_setspecific);
75 __gthrw(pthread_create);
76 __gthrw(pthread_cancel);
77 __gthrw(pthread_self);
78
79 __gthrw(pthread_mutex_lock);
80 __gthrw(pthread_mutex_trylock);
81 __gthrw(pthread_mutex_unlock);
82 __gthrw(pthread_mutexattr_init);
83 __gthrw(pthread_mutexattr_destroy);
84
85 __gthrw(pthread_mutex_init);
86
87 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
88 /* Objective-C. */
89 __gthrw(pthread_cond_broadcast);
90 __gthrw(pthread_cond_destroy);
91 __gthrw(pthread_cond_init);
92 __gthrw(pthread_cond_signal);
93 __gthrw(pthread_cond_wait);
94 __gthrw(pthread_exit);
95 __gthrw(pthread_mutex_destroy);
96 #ifdef _POSIX_PRIORITY_SCHEDULING
97 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
98 __gthrw(sched_get_priority_max);
99 __gthrw(sched_get_priority_min);
100 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
101 #endif /* _POSIX_PRIORITY_SCHEDULING */
102 __gthrw(sched_yield);
103 __gthrw(pthread_attr_destroy);
104 __gthrw(pthread_attr_init);
105 __gthrw(pthread_attr_setdetachstate);
106 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
107 __gthrw(pthread_getschedparam);
108 __gthrw(pthread_setschedparam);
109 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
110 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
111
112 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
113
114 static inline int
115 __gthread_active_p (void)
116 {
117 static void *const __gthread_active_ptr
118 = __extension__ (void *) &__gthrw_pthread_cancel;
119 return __gthread_active_ptr != 0;
120 }
121
122 #else /* not SUPPORTS_WEAK */
123
124 static inline int
125 __gthread_active_p (void)
126 {
127 return 1;
128 }
129
130 #endif /* SUPPORTS_WEAK */
131
132 #ifdef _LIBOBJC
133
134 /* This is the config.h file in libobjc/ */
135 #include <config.h>
136
137 #ifdef HAVE_SCHED_H
138 # include <sched.h>
139 #endif
140
141 /* Key structure for maintaining thread specific storage */
142 static pthread_key_t _objc_thread_storage;
143 static pthread_attr_t _objc_thread_attribs;
144
145 /* Thread local storage for a single thread */
146 static void *thread_local_storage = NULL;
147
148 /* Backend initialization functions */
149
150 /* Initialize the threads subsystem. */
151 static inline int
152 __gthread_objc_init_thread_system (void)
153 {
154 if (__gthread_active_p ())
155 {
156 /* Initialize the thread storage key. */
157 if (__gthrw_pthread_key_create (&_objc_thread_storage, NULL) == 0)
158 {
159 /* The normal default detach state for threads is
160 * PTHREAD_CREATE_JOINABLE which causes threads to not die
161 * when you think they should. */
162 if (__gthrw_pthread_attr_init (&_objc_thread_attribs) == 0
163 && __gthrw_pthread_attr_setdetachstate (&_objc_thread_attribs,
164 PTHREAD_CREATE_DETACHED) == 0)
165 return 0;
166 }
167 }
168
169 return -1;
170 }
171
172 /* Close the threads subsystem. */
173 static inline int
174 __gthread_objc_close_thread_system (void)
175 {
176 if (__gthread_active_p ()
177 && __gthrw_pthread_key_delete (_objc_thread_storage) == 0
178 && __gthrw_pthread_attr_destroy (&_objc_thread_attribs) == 0)
179 return 0;
180
181 return -1;
182 }
183
184 /* Backend thread functions */
185
186 /* Create a new thread of execution. */
187 static inline objc_thread_t
188 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
189 {
190 objc_thread_t thread_id;
191 pthread_t new_thread_handle;
192
193 if (!__gthread_active_p ())
194 return NULL;
195
196 if (!(__gthrw_pthread_create (&new_thread_handle, NULL, (void *) func, arg)))
197 thread_id = (objc_thread_t) new_thread_handle;
198 else
199 thread_id = NULL;
200
201 return thread_id;
202 }
203
204 /* Set the current thread's priority. */
205 static inline int
206 __gthread_objc_thread_set_priority (int priority)
207 {
208 if (!__gthread_active_p ())
209 return -1;
210 else
211 {
212 #ifdef _POSIX_PRIORITY_SCHEDULING
213 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
214 pthread_t thread_id = __gthrw_pthread_self ();
215 int policy;
216 struct sched_param params;
217 int priority_min, priority_max;
218
219 if (__gthrw_pthread_getschedparam (thread_id, &policy, &params) == 0)
220 {
221 if ((priority_max = __gthrw_sched_get_priority_max (policy)) == -1)
222 return -1;
223
224 if ((priority_min = __gthrw_sched_get_priority_min (policy)) == -1)
225 return -1;
226
227 if (priority > priority_max)
228 priority = priority_max;
229 else if (priority < priority_min)
230 priority = priority_min;
231 params.sched_priority = priority;
232
233 /*
234 * The solaris 7 and several other man pages incorrectly state that
235 * this should be a pointer to policy but pthread.h is universally
236 * at odds with this.
237 */
238 if (__gthrw_pthread_setschedparam (thread_id, policy, &params) == 0)
239 return 0;
240 }
241 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
242 #endif /* _POSIX_PRIORITY_SCHEDULING */
243 return -1;
244 }
245 }
246
247 /* Return the current thread's priority. */
248 static inline int
249 __gthread_objc_thread_get_priority (void)
250 {
251 #ifdef _POSIX_PRIORITY_SCHEDULING
252 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
253 if (__gthread_active_p ())
254 {
255 int policy;
256 struct sched_param params;
257
258 if (__gthrw_pthread_getschedparam (__gthrw_pthread_self (), &policy, &params) == 0)
259 return params.sched_priority;
260 else
261 return -1;
262 }
263 else
264 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
265 #endif /* _POSIX_PRIORITY_SCHEDULING */
266 return OBJC_THREAD_INTERACTIVE_PRIORITY;
267 }
268
269 /* Yield our process time to another thread. */
270 static inline void
271 __gthread_objc_thread_yield (void)
272 {
273 if (__gthread_active_p ())
274 __gthrw_sched_yield ();
275 }
276
277 /* Terminate the current thread. */
278 static inline int
279 __gthread_objc_thread_exit (void)
280 {
281 if (__gthread_active_p ())
282 /* exit the thread */
283 __gthrw_pthread_exit (&__objc_thread_exit_status);
284
285 /* Failed if we reached here */
286 return -1;
287 }
288
289 /* Returns an integer value which uniquely describes a thread. */
290 static inline objc_thread_t
291 __gthread_objc_thread_id (void)
292 {
293 if (__gthread_active_p ())
294 return (objc_thread_t) __gthrw_pthread_self ();
295 else
296 return (objc_thread_t) 1;
297 }
298
299 /* Sets the thread's local storage pointer. */
300 static inline int
301 __gthread_objc_thread_set_data (void *value)
302 {
303 if (__gthread_active_p ())
304 return __gthrw_pthread_setspecific (_objc_thread_storage, value);
305 else
306 {
307 thread_local_storage = value;
308 return 0;
309 }
310 }
311
312 /* Returns the thread's local storage pointer. */
313 static inline void *
314 __gthread_objc_thread_get_data (void)
315 {
316 if (__gthread_active_p ())
317 return __gthrw_pthread_getspecific (_objc_thread_storage);
318 else
319 return thread_local_storage;
320 }
321
322 /* Backend mutex functions */
323
324 /* Allocate a mutex. */
325 static inline int
326 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
327 {
328 if (__gthread_active_p ())
329 {
330 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
331
332 if (__gthrw_pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL))
333 {
334 objc_free (mutex->backend);
335 mutex->backend = NULL;
336 return -1;
337 }
338 }
339
340 return 0;
341 }
342
343 /* Deallocate a mutex. */
344 static inline int
345 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
346 {
347 if (__gthread_active_p ())
348 {
349 int count;
350
351 /*
352 * Posix Threads specifically require that the thread be unlocked
353 * for __gthrw_pthread_mutex_destroy to work.
354 */
355
356 do
357 {
358 count = __gthrw_pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
359 if (count < 0)
360 return -1;
361 }
362 while (count);
363
364 if (__gthrw_pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
365 return -1;
366
367 objc_free (mutex->backend);
368 mutex->backend = NULL;
369 }
370 return 0;
371 }
372
373 /* Grab a lock on a mutex. */
374 static inline int
375 __gthread_objc_mutex_lock (objc_mutex_t mutex)
376 {
377 if (__gthread_active_p ()
378 && __gthrw_pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
379 {
380 return -1;
381 }
382
383 return 0;
384 }
385
386 /* Try to grab a lock on a mutex. */
387 static inline int
388 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
389 {
390 if (__gthread_active_p ()
391 && __gthrw_pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
392 {
393 return -1;
394 }
395
396 return 0;
397 }
398
399 /* Unlock the mutex */
400 static inline int
401 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
402 {
403 if (__gthread_active_p ()
404 && __gthrw_pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
405 {
406 return -1;
407 }
408
409 return 0;
410 }
411
412 /* Backend condition mutex functions */
413
414 /* Allocate a condition. */
415 static inline int
416 __gthread_objc_condition_allocate (objc_condition_t condition)
417 {
418 if (__gthread_active_p ())
419 {
420 condition->backend = objc_malloc (sizeof (pthread_cond_t));
421
422 if (__gthrw_pthread_cond_init ((pthread_cond_t *) condition->backend, NULL))
423 {
424 objc_free (condition->backend);
425 condition->backend = NULL;
426 return -1;
427 }
428 }
429
430 return 0;
431 }
432
433 /* Deallocate a condition. */
434 static inline int
435 __gthread_objc_condition_deallocate (objc_condition_t condition)
436 {
437 if (__gthread_active_p ())
438 {
439 if (__gthrw_pthread_cond_destroy ((pthread_cond_t *) condition->backend))
440 return -1;
441
442 objc_free (condition->backend);
443 condition->backend = NULL;
444 }
445 return 0;
446 }
447
448 /* Wait on the condition */
449 static inline int
450 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
451 {
452 if (__gthread_active_p ())
453 return __gthrw_pthread_cond_wait ((pthread_cond_t *) condition->backend,
454 (pthread_mutex_t *) mutex->backend);
455 else
456 return 0;
457 }
458
459 /* Wake up all threads waiting on this condition. */
460 static inline int
461 __gthread_objc_condition_broadcast (objc_condition_t condition)
462 {
463 if (__gthread_active_p ())
464 return __gthrw_pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
465 else
466 return 0;
467 }
468
469 /* Wake up one thread waiting on this condition. */
470 static inline int
471 __gthread_objc_condition_signal (objc_condition_t condition)
472 {
473 if (__gthread_active_p ())
474 return __gthrw_pthread_cond_signal ((pthread_cond_t *) condition->backend);
475 else
476 return 0;
477 }
478
479 #else /* _LIBOBJC */
480
481 static inline int
482 __gthread_once (__gthread_once_t *once, void (*func) (void))
483 {
484 if (__gthread_active_p ())
485 return __gthrw_pthread_once (once, func);
486 else
487 return -1;
488 }
489
490 static inline int
491 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
492 {
493 return __gthrw_pthread_key_create (key, dtor);
494 }
495
496 static inline int
497 __gthread_key_delete (__gthread_key_t key)
498 {
499 return __gthrw_pthread_key_delete (key);
500 }
501
502 static inline void *
503 __gthread_getspecific (__gthread_key_t key)
504 {
505 return __gthrw_pthread_getspecific (key);
506 }
507
508 static inline int
509 __gthread_setspecific (__gthread_key_t key, const void *ptr)
510 {
511 return __gthrw_pthread_setspecific (key, ptr);
512 }
513
514 static inline int
515 __gthread_mutex_lock (__gthread_mutex_t *mutex)
516 {
517 if (__gthread_active_p ())
518 return __gthrw_pthread_mutex_lock (mutex);
519 else
520 return 0;
521 }
522
523 static inline int
524 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
525 {
526 if (__gthread_active_p ())
527 return __gthrw_pthread_mutex_trylock (mutex);
528 else
529 return 0;
530 }
531
532 static inline int
533 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
534 {
535 if (__gthread_active_p ())
536 return __gthrw_pthread_mutex_unlock (mutex);
537 else
538 return 0;
539 }
540
541 static inline int
542 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
543 {
544 mutex->depth = 0;
545 mutex->owner = (pthread_t) 0;
546 return __gthrw_pthread_mutex_init (&mutex->actual, NULL);
547 }
548
549 static inline int
550 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
551 {
552 if (__gthread_active_p ())
553 {
554 pthread_t me = __gthrw_pthread_self ();
555
556 if (mutex->owner != me)
557 {
558 __gthrw_pthread_mutex_lock (&mutex->actual);
559 mutex->owner = me;
560 }
561
562 mutex->depth++;
563 }
564 return 0;
565 }
566
567 static inline int
568 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
569 {
570 if (__gthread_active_p ())
571 {
572 pthread_t me = __gthrw_pthread_self ();
573
574 if (mutex->owner != me)
575 {
576 if (__gthrw_pthread_mutex_trylock (&mutex->actual))
577 return 1;
578 mutex->owner = me;
579 }
580
581 mutex->depth++;
582 }
583 return 0;
584 }
585
586 static inline int
587 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
588 {
589 if (__gthread_active_p ())
590 {
591 if (--mutex->depth == 0)
592 {
593 mutex->owner = (pthread_t) 0;
594 __gthrw_pthread_mutex_unlock (&mutex->actual);
595 }
596 }
597 return 0;
598 }
599
600 #endif /* _LIBOBJC */
601
602 #endif /* ! GCC_GTHR_POSIX_H */