OSDN Git Service

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