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