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