OSDN Git Service

* testsuite/lib/libgomp-dg.exp (libgomp_init): Compute multilib-related
[pf3gnuchains/gcc-fork.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, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* As a special exception, if you link this library with other files,
24    some of which are compiled with GCC, to produce an executable,
25    this library does not by itself cause the resulting executable
26    to be covered by the GNU General Public License.
27    This exception does not however invalidate any other reasons why
28    the executable file might be covered by the GNU General Public License.  */
29
30 #ifndef GCC_GTHR_POSIX_H
31 #define GCC_GTHR_POSIX_H
32
33 /* POSIX threads specific definitions.
34    Easy, since the interface is just one-to-one mapping.  */
35
36 #define __GTHREADS 1
37
38 /* Some implementations of <pthread.h> require this to be defined.  */
39 #ifndef _REENTRANT
40 #define _REENTRANT 1
41 #endif
42
43 #include <pthread.h>
44 #include <unistd.h>
45
46 typedef pthread_key_t __gthread_key_t;
47 typedef pthread_once_t __gthread_once_t;
48 typedef pthread_mutex_t __gthread_mutex_t;
49 typedef pthread_mutex_t __gthread_recursive_mutex_t;
50
51 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
52 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
54 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
55 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
56 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
57 #else
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #endif
60
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
62 # define __gthrw(name) \
63   static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
64 # define __gthrw_(name) __gthrw_ ## name
65 #else
66 # define __gthrw(name)
67 # define __gthrw_(name) 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
78 __gthrw(pthread_mutex_lock)
79 __gthrw(pthread_mutex_trylock)
80 __gthrw(pthread_mutex_unlock)
81 __gthrw(pthread_mutexattr_init)
82 __gthrw(pthread_mutexattr_settype)
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 __gthrw(pthread_self)
97 #ifdef _POSIX_PRIORITY_SCHEDULING
98 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
99 __gthrw(sched_get_priority_max)
100 __gthrw(sched_get_priority_min)
101 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
102 #endif /* _POSIX_PRIORITY_SCHEDULING */
103 __gthrw(sched_yield)
104 __gthrw(pthread_attr_destroy)
105 __gthrw(pthread_attr_init)
106 __gthrw(pthread_attr_setdetachstate)
107 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
108 __gthrw(pthread_getschedparam)
109 __gthrw(pthread_setschedparam)
110 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
111 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
112
113 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
114
115 static inline int
116 __gthread_active_p (void)
117 {
118   static void *const __gthread_active_ptr 
119     = __extension__ (void *) &__gthrw_(pthread_cancel);
120   return __gthread_active_ptr != 0;
121 }
122
123 #else /* not SUPPORTS_WEAK */
124
125 static inline int
126 __gthread_active_p (void)
127 {
128   return 1;
129 }
130
131 #endif /* SUPPORTS_WEAK */
132
133 #ifdef _LIBOBJC
134
135 /* This is the config.h file in libobjc/ */
136 #include <config.h>
137
138 #ifdef HAVE_SCHED_H
139 # include <sched.h>
140 #endif
141
142 /* Key structure for maintaining thread specific storage */
143 static pthread_key_t _objc_thread_storage;
144 static pthread_attr_t _objc_thread_attribs;
145
146 /* Thread local storage for a single thread */
147 static void *thread_local_storage = NULL;
148
149 /* Backend initialization functions */
150
151 /* Initialize the threads subsystem.  */
152 static inline int
153 __gthread_objc_init_thread_system (void)
154 {
155   if (__gthread_active_p ())
156     {
157       /* Initialize the thread storage key.  */
158       if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
159         {
160           /* The normal default detach state for threads is
161            * PTHREAD_CREATE_JOINABLE which causes threads to not die
162            * when you think they should.  */
163           if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
164               && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
165                                               PTHREAD_CREATE_DETACHED) == 0)
166             return 0;
167         }
168     }
169
170   return -1;
171 }
172
173 /* Close the threads subsystem.  */
174 static inline int
175 __gthread_objc_close_thread_system (void)
176 {
177   if (__gthread_active_p ()
178       && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
179       && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
180     return 0;
181
182   return -1;
183 }
184
185 /* Backend thread functions */
186
187 /* Create a new thread of execution.  */
188 static inline objc_thread_t
189 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
190 {
191   objc_thread_t thread_id;
192   pthread_t new_thread_handle;
193
194   if (!__gthread_active_p ())
195     return NULL;
196
197   if (!(__gthrw_(pthread_create) (&new_thread_handle, NULL, (void *) func, arg)))
198     thread_id = (objc_thread_t) new_thread_handle;
199   else
200     thread_id = NULL;
201
202   return thread_id;
203 }
204
205 /* Set the current thread's priority.  */
206 static inline int
207 __gthread_objc_thread_set_priority (int priority)
208 {
209   if (!__gthread_active_p ())
210     return -1;
211   else
212     {
213 #ifdef _POSIX_PRIORITY_SCHEDULING
214 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
215       pthread_t thread_id = __gthrw_(pthread_self) ();
216       int policy;
217       struct sched_param params;
218       int priority_min, priority_max;
219
220       if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
221         {
222           if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
223             return -1;
224
225           if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
226             return -1;
227
228           if (priority > priority_max)
229             priority = priority_max;
230           else if (priority < priority_min)
231             priority = priority_min;
232           params.sched_priority = priority;
233
234           /*
235            * The solaris 7 and several other man pages incorrectly state that
236            * this should be a pointer to policy but pthread.h is universally
237            * at odds with this.
238            */
239           if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
240             return 0;
241         }
242 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
243 #endif /* _POSIX_PRIORITY_SCHEDULING */
244       return -1;
245     }
246 }
247
248 /* Return the current thread's priority.  */
249 static inline int
250 __gthread_objc_thread_get_priority (void)
251 {
252 #ifdef _POSIX_PRIORITY_SCHEDULING
253 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
254   if (__gthread_active_p ())
255     {
256       int policy;
257       struct sched_param params;
258
259       if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
260         return params.sched_priority;
261       else
262         return -1;
263     }
264   else
265 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
266 #endif /* _POSIX_PRIORITY_SCHEDULING */
267     return OBJC_THREAD_INTERACTIVE_PRIORITY;
268 }
269
270 /* Yield our process time to another thread.  */
271 static inline void
272 __gthread_objc_thread_yield (void)
273 {
274   if (__gthread_active_p ())
275     __gthrw_(sched_yield) ();
276 }
277
278 /* Terminate the current thread.  */
279 static inline int
280 __gthread_objc_thread_exit (void)
281 {
282   if (__gthread_active_p ())
283     /* exit the thread */
284     __gthrw_(pthread_exit) (&__objc_thread_exit_status);
285
286   /* Failed if we reached here */
287   return -1;
288 }
289
290 /* Returns an integer value which uniquely describes a thread.  */
291 static inline objc_thread_t
292 __gthread_objc_thread_id (void)
293 {
294   if (__gthread_active_p ())
295     return (objc_thread_t) __gthrw_(pthread_self) ();
296   else
297     return (objc_thread_t) 1;
298 }
299
300 /* Sets the thread's local storage pointer.  */
301 static inline int
302 __gthread_objc_thread_set_data (void *value)
303 {
304   if (__gthread_active_p ())
305     return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
306   else
307     {
308       thread_local_storage = value;
309       return 0;
310     }
311 }
312
313 /* Returns the thread's local storage pointer.  */
314 static inline void *
315 __gthread_objc_thread_get_data (void)
316 {
317   if (__gthread_active_p ())
318     return __gthrw_(pthread_getspecific) (_objc_thread_storage);
319   else
320     return thread_local_storage;
321 }
322
323 /* Backend mutex functions */
324
325 /* Allocate a mutex.  */
326 static inline int
327 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
328 {
329   if (__gthread_active_p ())
330     {
331       mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
332
333       if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
334         {
335           objc_free (mutex->backend);
336           mutex->backend = NULL;
337           return -1;
338         }
339     }
340
341   return 0;
342 }
343
344 /* Deallocate a mutex.  */
345 static inline int
346 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
347 {
348   if (__gthread_active_p ())
349     {
350       int count;
351
352       /*
353        * Posix Threads specifically require that the thread be unlocked
354        * for __gthrw_(pthread_mutex_destroy) to work.
355        */
356
357       do
358         {
359           count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
360           if (count < 0)
361             return -1;
362         }
363       while (count);
364
365       if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
366         return -1;
367
368       objc_free (mutex->backend);
369       mutex->backend = NULL;
370     }
371   return 0;
372 }
373
374 /* Grab a lock on a mutex.  */
375 static inline int
376 __gthread_objc_mutex_lock (objc_mutex_t mutex)
377 {
378   if (__gthread_active_p ()
379       && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
380     {
381       return -1;
382     }
383
384   return 0;
385 }
386
387 /* Try to grab a lock on a mutex.  */
388 static inline int
389 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
390 {
391   if (__gthread_active_p ()
392       && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
393     {
394       return -1;
395     }
396
397   return 0;
398 }
399
400 /* Unlock the mutex */
401 static inline int
402 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
403 {
404   if (__gthread_active_p ()
405       && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
406     {
407       return -1;
408     }
409
410   return 0;
411 }
412
413 /* Backend condition mutex functions */
414
415 /* Allocate a condition.  */
416 static inline int
417 __gthread_objc_condition_allocate (objc_condition_t condition)
418 {
419   if (__gthread_active_p ())
420     {
421       condition->backend = objc_malloc (sizeof (pthread_cond_t));
422
423       if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
424         {
425           objc_free (condition->backend);
426           condition->backend = NULL;
427           return -1;
428         }
429     }
430
431   return 0;
432 }
433
434 /* Deallocate a condition.  */
435 static inline int
436 __gthread_objc_condition_deallocate (objc_condition_t condition)
437 {
438   if (__gthread_active_p ())
439     {
440       if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
441         return -1;
442
443       objc_free (condition->backend);
444       condition->backend = NULL;
445     }
446   return 0;
447 }
448
449 /* Wait on the condition */
450 static inline int
451 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
452 {
453   if (__gthread_active_p ())
454     return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
455                               (pthread_mutex_t *) mutex->backend);
456   else
457     return 0;
458 }
459
460 /* Wake up all threads waiting on this condition.  */
461 static inline int
462 __gthread_objc_condition_broadcast (objc_condition_t condition)
463 {
464   if (__gthread_active_p ())
465     return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
466   else
467     return 0;
468 }
469
470 /* Wake up one thread waiting on this condition.  */
471 static inline int
472 __gthread_objc_condition_signal (objc_condition_t condition)
473 {
474   if (__gthread_active_p ())
475     return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
476   else
477     return 0;
478 }
479
480 #else /* _LIBOBJC */
481
482 static inline int
483 __gthread_once (__gthread_once_t *once, void (*func) (void))
484 {
485   if (__gthread_active_p ())
486     return __gthrw_(pthread_once) (once, func);
487   else
488     return -1;
489 }
490
491 static inline int
492 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
493 {
494   return __gthrw_(pthread_key_create) (key, dtor);
495 }
496
497 static inline int
498 __gthread_key_delete (__gthread_key_t key)
499 {
500   return __gthrw_(pthread_key_delete) (key);
501 }
502
503 static inline void *
504 __gthread_getspecific (__gthread_key_t key)
505 {
506   return __gthrw_(pthread_getspecific) (key);
507 }
508
509 static inline int
510 __gthread_setspecific (__gthread_key_t key, const void *ptr)
511 {
512   return __gthrw_(pthread_setspecific) (key, ptr);
513 }
514
515 static inline int
516 __gthread_mutex_lock (__gthread_mutex_t *mutex)
517 {
518   if (__gthread_active_p ())
519     return __gthrw_(pthread_mutex_lock) (mutex);
520   else
521     return 0;
522 }
523
524 static inline int
525 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
526 {
527   if (__gthread_active_p ())
528     return __gthrw_(pthread_mutex_trylock) (mutex);
529   else
530     return 0;
531 }
532
533 static inline int
534 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
535 {
536   if (__gthread_active_p ())
537     return __gthrw_(pthread_mutex_unlock) (mutex);
538   else
539     return 0;
540 }
541
542 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
543 static inline int
544 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
545 {
546   if (__gthread_active_p ())
547     {
548       pthread_mutexattr_t attr;
549       int r;
550
551       r = __gthrw_(pthread_mutexattr_init) (&attr);
552       if (!r)
553         r = __gthrw_(pthread_mutexattr_settype) (&attr, PTHREAD_MUTEX_RECURSIVE);
554       if (!r)
555         r = __gthrw_(pthread_mutex_init) (mutex, &attr);
556       if (!r)
557         r = __gthrw_(pthread_mutexattr_destroy) (&attr);
558       return r;
559     }
560   return 0;
561 }
562 #endif
563
564 static inline int
565 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
566 {
567   return __gthread_mutex_lock (mutex);
568 }
569
570 static inline int
571 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
572 {
573   return __gthread_mutex_trylock (mutex);
574 }
575
576 static inline int
577 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
578 {
579   return __gthread_mutex_unlock (mutex);
580 }
581
582 #endif /* _LIBOBJC */
583
584 #endif /* ! GCC_GTHR_POSIX_H */