]>
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 | ||
88657302 RH |
29 | #ifndef GCC_GTHR_SOLARIS_H |
30 | #define GCC_GTHR_SOLARIS_H | |
f24af81b TT |
31 | |
32 | /* Solaris threads as found in Solaris 2.[456]. | |
33 | Actually these are Unix International (UI) threads, but I don't | |
34 | know if anyone else implements these. */ | |
35 | ||
36 | #define __GTHREADS 1 | |
37 | ||
38 | #include <thread.h> | |
39 | #include <errno.h> | |
40 | ||
41 | typedef thread_key_t __gthread_key_t; | |
42 | typedef struct | |
43 | { | |
44 | mutex_t mutex; | |
45 | int once; | |
46 | } __gthread_once_t; | |
47 | typedef mutex_t __gthread_mutex_t; | |
48 | ||
49 | #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 } | |
50 | #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX | |
51 | ||
52 | #if SUPPORTS_WEAK && GTHREAD_USE_WEAK | |
53 | ||
54 | #pragma weak thr_keycreate | |
55 | #pragma weak thr_getspecific | |
56 | #pragma weak thr_setspecific | |
57 | #pragma weak thr_create | |
58 | ||
59 | #pragma weak mutex_lock | |
60 | #pragma weak mutex_trylock | |
61 | #pragma weak mutex_unlock | |
62 | ||
15794a95 L |
63 | #ifdef _LIBOBJC |
64 | #pragma weak thr_exit | |
65 | #pragma weak thr_keycreate | |
66 | #pragma weak thr_getprio | |
67 | #pragma weak thr_self | |
68 | #pragma weak thr_setprio | |
69 | #pragma weak thr_yield | |
70 | ||
71 | #pragma weak cond_init | |
72 | #pragma weak cond_destroy | |
73 | #pragma weak cond_wait | |
74 | #pragma weak cond_broadcast | |
75 | #pragma weak cond_signal | |
76 | ||
77 | #pragma weak mutex_init | |
78 | #pragma weak mutex_destroy | |
79 | #endif | |
80 | ||
f24af81b TT |
81 | /* This will not actually work in Solaris 2.5, since libc contains |
82 | dummy symbols of all thr_* routines. */ | |
83 | ||
84 | static void *__gthread_active_ptr = &thr_create; | |
85 | ||
86 | static inline int | |
d1e51320 | 87 | __gthread_active_p (void) |
f24af81b TT |
88 | { |
89 | return __gthread_active_ptr != 0; | |
90 | } | |
91 | ||
92 | #else /* not SUPPORTS_WEAK */ | |
93 | ||
94 | static inline int | |
d1e51320 | 95 | __gthread_active_p (void) |
f24af81b TT |
96 | { |
97 | return 1; | |
98 | } | |
99 | ||
100 | #endif /* SUPPORTS_WEAK */ | |
101 | ||
15794a95 L |
102 | #ifdef _LIBOBJC |
103 | ||
104 | /* Key structure for maintaining thread specific storage */ | |
105 | static thread_key_t _objc_thread_storage; | |
106 | ||
107 | /* Thread local storage for a single thread */ | |
108 | static void *thread_local_storage = NULL; | |
109 | ||
110 | /* Backend initialization functions */ | |
111 | ||
112 | /* Initialize the threads subsystem. */ | |
113 | static inline int | |
114 | __gthread_objc_init_thread_system(void) | |
115 | { | |
116 | /* Initialize the thread storage key */ | |
117 | if (__gthread_active_p () | |
118 | && thr_keycreate(&_objc_thread_storage, NULL) == 0) | |
119 | return 0; | |
120 | ||
121 | return -1; | |
122 | } | |
123 | ||
124 | /* Close the threads subsystem. */ | |
125 | static inline int | |
126 | __gthread_objc_close_thread_system(void) | |
127 | { | |
128 | if (__gthread_active_p ()) | |
129 | return 0; | |
130 | else | |
131 | return -1; | |
132 | } | |
133 | ||
134 | /* Backend thread functions */ | |
135 | ||
136 | /* Create a new thread of execution. */ | |
137 | static inline objc_thread_t | |
138 | __gthread_objc_thread_detach(void (*func)(void *), void *arg) | |
139 | { | |
140 | objc_thread_t thread_id; | |
141 | thread_t new_thread_id = 0; | |
142 | ||
143 | if (!__gthread_active_p ()) | |
144 | return NULL; | |
145 | ||
146 | if (thr_create(NULL, 0, (void *)func, arg, | |
147 | THR_DETACHED | THR_NEW_LWP, | |
148 | &new_thread_id) == 0) | |
149 | thread_id = *(objc_thread_t *)&new_thread_id; | |
150 | else | |
151 | thread_id = NULL; | |
152 | ||
153 | return thread_id; | |
154 | } | |
155 | ||
156 | /* Set the current thread's priority. */ | |
157 | static inline int | |
158 | __gthread_objc_thread_set_priority(int priority) | |
159 | { | |
160 | int sys_priority = 0; | |
161 | ||
162 | if (!__gthread_active_p ()) | |
163 | return -1; | |
164 | ||
165 | switch (priority) | |
166 | { | |
167 | case OBJC_THREAD_INTERACTIVE_PRIORITY: | |
168 | sys_priority = 300; | |
169 | break; | |
170 | default: | |
171 | case OBJC_THREAD_BACKGROUND_PRIORITY: | |
172 | sys_priority = 200; | |
173 | break; | |
174 | case OBJC_THREAD_LOW_PRIORITY: | |
175 | sys_priority = 1000; | |
176 | break; | |
177 | } | |
178 | ||
179 | /* Change priority */ | |
180 | if (thr_setprio(thr_self(), sys_priority) == 0) | |
181 | return 0; | |
182 | else | |
183 | return -1; | |
184 | } | |
185 | ||
186 | /* Return the current thread's priority. */ | |
187 | static inline int | |
188 | __gthread_objc_thread_get_priority(void) | |
189 | { | |
190 | int sys_priority; | |
191 | ||
192 | if (!__gthread_active_p ()) | |
193 | return OBJC_THREAD_INTERACTIVE_PRIORITY; | |
194 | ||
195 | if (thr_getprio(thr_self(), &sys_priority) == 0) | |
196 | { | |
197 | if (sys_priority >= 250) | |
198 | return OBJC_THREAD_INTERACTIVE_PRIORITY; | |
199 | else if (sys_priority >= 150) | |
200 | return OBJC_THREAD_BACKGROUND_PRIORITY; | |
201 | return OBJC_THREAD_LOW_PRIORITY; | |
202 | } | |
203 | ||
204 | /* Couldn't get priority. */ | |
205 | return -1; | |
206 | } | |
207 | ||
208 | /* Yield our process time to another thread. */ | |
209 | static inline void | |
210 | __gthread_objc_thread_yield(void) | |
211 | { | |
212 | if (__gthread_active_p ()) | |
213 | thr_yield(); | |
214 | } | |
215 | ||
216 | /* Terminate the current thread. */ | |
217 | static inline int | |
218 | __gthread_objc_thread_exit(void) | |
219 | { | |
220 | if (__gthread_active_p ()) | |
221 | /* exit the thread */ | |
222 | thr_exit(&__objc_thread_exit_status); | |
223 | ||
224 | /* Failed if we reached here */ | |
225 | return -1; | |
226 | } | |
227 | ||
228 | /* Returns an integer value which uniquely describes a thread. */ | |
229 | static inline objc_thread_t | |
230 | __gthread_objc_thread_id(void) | |
231 | { | |
232 | if (__gthread_active_p ()) | |
233 | return (objc_thread_t)thr_self(); | |
234 | else | |
235 | return (objc_thread_t)1; | |
236 | } | |
237 | ||
238 | /* Sets the thread's local storage pointer. */ | |
239 | static inline int | |
240 | __gthread_objc_thread_set_data(void *value) | |
241 | { | |
242 | if (__gthread_active_p ()) | |
243 | { | |
244 | if (thr_setspecific(_objc_thread_storage, value) == 0) | |
245 | return 0; | |
246 | else | |
247 | return -1; | |
248 | } | |
249 | else | |
250 | { | |
251 | thread_local_storage = value; | |
252 | return 0; | |
253 | } | |
254 | } | |
255 | ||
256 | /* Returns the thread's local storage pointer. */ | |
257 | static inline void * | |
258 | __gthread_objc_thread_get_data(void) | |
259 | { | |
260 | void *value = NULL; | |
261 | ||
262 | if (__gthread_active_p ()) | |
263 | { | |
264 | if (thr_getspecific(_objc_thread_storage, &value) == 0) | |
265 | return value; | |
266 | else | |
267 | return NULL; | |
268 | } | |
269 | else | |
270 | return thread_local_storage; | |
271 | } | |
272 | ||
273 | /* Backend mutex functions */ | |
274 | ||
275 | /* Allocate a mutex. */ | |
276 | static inline int | |
277 | __gthread_objc_mutex_allocate(objc_mutex_t mutex) | |
278 | { | |
279 | if (__gthread_active_p () | |
280 | && mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0)) | |
281 | return -1; | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
286 | /* Deallocate a mutex. */ | |
287 | static inline int | |
288 | __gthread_objc_mutex_deallocate(objc_mutex_t mutex) | |
289 | { | |
290 | if (__gthread_active_p ()) | |
291 | mutex_destroy((mutex_t *)(&(mutex->backend))); | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | /* Grab a lock on a mutex. */ | |
297 | static inline int | |
298 | __gthread_objc_mutex_lock(objc_mutex_t mutex) | |
299 | { | |
300 | if (__gthread_active_p () | |
301 | && mutex_lock((mutex_t *)(&(mutex->backend))) != 0) | |
302 | return -1; | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | /* Try to grab a lock on a mutex. */ | |
308 | static inline int | |
309 | __gthread_objc_mutex_trylock(objc_mutex_t mutex) | |
310 | { | |
311 | if (__gthread_active_p () | |
312 | && mutex_trylock((mutex_t *)(&(mutex->backend))) != 0) | |
313 | return -1; | |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
318 | /* Unlock the mutex */ | |
319 | static inline int | |
320 | __gthread_objc_mutex_unlock(objc_mutex_t mutex) | |
321 | { | |
322 | if (__gthread_active_p () | |
323 | && mutex_unlock((mutex_t *)(&(mutex->backend))) != 0) | |
324 | return -1; | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
329 | /* Backend condition mutex functions */ | |
330 | ||
331 | /* Allocate a condition. */ | |
332 | static inline int | |
333 | __gthread_objc_condition_allocate(objc_condition_t condition) | |
334 | { | |
335 | if (__gthread_active_p ()) | |
336 | return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD, | |
337 | NULL); | |
338 | else | |
339 | return 0; | |
340 | } | |
341 | ||
342 | /* Deallocate a condition. */ | |
343 | static inline int | |
344 | __gthread_objc_condition_deallocate(objc_condition_t condition) | |
345 | { | |
346 | if (__gthread_active_p ()) | |
347 | return cond_destroy((cond_t *)(&(condition->backend))); | |
348 | else | |
349 | return 0; | |
350 | } | |
351 | ||
352 | /* Wait on the condition */ | |
353 | static inline int | |
354 | __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex) | |
355 | { | |
356 | if (__gthread_active_p ()) | |
357 | return cond_wait((cond_t *)(&(condition->backend)), | |
358 | (mutex_t *)(&(mutex->backend))); | |
359 | else | |
360 | return 0; | |
361 | } | |
362 | ||
363 | /* Wake up all threads waiting on this condition. */ | |
364 | static inline int | |
365 | __gthread_objc_condition_broadcast(objc_condition_t condition) | |
366 | { | |
367 | if (__gthread_active_p ()) | |
368 | return cond_broadcast((cond_t *)(&(condition->backend))); | |
369 | else | |
370 | return 0; | |
371 | } | |
372 | ||
373 | /* Wake up one thread waiting on this condition. */ | |
374 | static inline int | |
375 | __gthread_objc_condition_signal(objc_condition_t condition) | |
376 | { | |
377 | if (__gthread_active_p ()) | |
378 | return cond_signal((cond_t *)(&(condition->backend))); | |
379 | else | |
380 | return 0; | |
381 | } | |
382 | ||
383 | #else /* _LIBOBJC */ | |
384 | ||
f24af81b | 385 | static inline int |
d1e51320 | 386 | __gthread_once (__gthread_once_t *once, void (*func) (void)) |
f24af81b TT |
387 | { |
388 | if (! __gthread_active_p ()) | |
389 | return -1; | |
390 | ||
391 | if (once == 0 || func == 0) | |
754d1a92 | 392 | return EINVAL; |
f24af81b TT |
393 | |
394 | if (once->once == 0) | |
395 | { | |
754d1a92 TT |
396 | int status = mutex_lock (&once->mutex); |
397 | if (status != 0) | |
398 | return status; | |
f24af81b TT |
399 | if (once->once == 0) |
400 | { | |
401 | (*func) (); | |
402 | once->once ++; | |
403 | } | |
404 | mutex_unlock (&once->mutex); | |
405 | } | |
406 | return 0; | |
407 | } | |
408 | ||
409 | static inline int | |
410 | __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *)) | |
411 | { | |
412 | /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually | |
413 | got a reasonable key value, and if not, fail. */ | |
414 | *key = -1; | |
754d1a92 | 415 | if (thr_keycreate (key, dtor) != 0 || *key == -1) |
f24af81b TT |
416 | return -1; |
417 | else | |
418 | return 0; | |
419 | } | |
420 | ||
421 | static inline int | |
422 | __gthread_key_dtor (__gthread_key_t key, void *ptr) | |
423 | { | |
424 | /* Nothing needed. */ | |
425 | return 0; | |
426 | } | |
427 | ||
428 | static inline int | |
429 | __gthread_key_delete (__gthread_key_t key) | |
430 | { | |
431 | /* Not possible. */ | |
432 | return -1; | |
433 | } | |
434 | ||
435 | static inline void * | |
436 | __gthread_getspecific (__gthread_key_t key) | |
437 | { | |
438 | void *ptr; | |
439 | if (thr_getspecific (key, &ptr) == 0) | |
440 | return ptr; | |
441 | else | |
442 | return 0; | |
443 | } | |
444 | ||
445 | static inline int | |
446 | __gthread_setspecific (__gthread_key_t key, const void *ptr) | |
447 | { | |
448 | return thr_setspecific (key, (void *) ptr); | |
449 | } | |
450 | ||
451 | static inline int | |
452 | __gthread_mutex_lock (__gthread_mutex_t *mutex) | |
453 | { | |
454 | if (__gthread_active_p ()) | |
455 | return mutex_lock (mutex); | |
456 | else | |
457 | return 0; | |
458 | } | |
459 | ||
460 | static inline int | |
461 | __gthread_mutex_trylock (__gthread_mutex_t *mutex) | |
462 | { | |
463 | if (__gthread_active_p ()) | |
464 | return mutex_trylock (mutex); | |
465 | else | |
466 | return 0; | |
467 | } | |
468 | ||
469 | static inline int | |
470 | __gthread_mutex_unlock (__gthread_mutex_t *mutex) | |
471 | { | |
472 | if (__gthread_active_p ()) | |
473 | return mutex_unlock (mutex); | |
474 | else | |
475 | return 0; | |
476 | } | |
477 | ||
15794a95 L |
478 | #endif /* _LIBOBJC */ |
479 | ||
88657302 | 480 | #endif /* ! GCC_GTHR_SOLARIS_H */ |