00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef GCC_GTHR_SOLARIS_H
00030 #define GCC_GTHR_SOLARIS_H
00031
00032
00033
00034
00035
00036 #define __GTHREADS 1
00037
00038 #include <thread.h>
00039 #include <errno.h>
00040
00041 typedef thread_key_t __gthread_key_t;
00042 typedef struct {
00043 mutex_t mutex;
00044 int once;
00045 } __gthread_once_t;
00046 typedef mutex_t __gthread_mutex_t;
00047
00048 typedef struct {
00049 long depth;
00050 thread_t owner;
00051 mutex_t actual;
00052 } __gthread_recursive_mutex_t;
00053
00054 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
00055 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
00056 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
00057
00058 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
00059
00060 #pragma weak thr_keycreate
00061 #pragma weak thr_getspecific
00062 #pragma weak thr_setspecific
00063 #pragma weak thr_create
00064
00065 #pragma weak mutex_lock
00066 #pragma weak mutex_trylock
00067 #pragma weak mutex_unlock
00068
00069 #ifdef _LIBOBJC
00070 #pragma weak thr_exit
00071 #pragma weak thr_keycreate
00072 #pragma weak thr_getprio
00073 #pragma weak thr_self
00074 #pragma weak thr_setprio
00075 #pragma weak thr_yield
00076
00077 #pragma weak cond_init
00078 #pragma weak cond_destroy
00079 #pragma weak cond_wait
00080 #pragma weak cond_broadcast
00081 #pragma weak cond_signal
00082
00083 #pragma weak mutex_init
00084 #pragma weak mutex_destroy
00085 #endif
00086
00087
00088
00089
00090 static inline int
00091 __gthread_active_p (void)
00092 {
00093 static void *const __gthread_active_ptr = (void *) &thr_create;
00094 return __gthread_active_ptr != 0;
00095 }
00096
00097 #else
00098
00099 static inline int
00100 __gthread_active_p (void)
00101 {
00102 return 1;
00103 }
00104
00105 #endif
00106
00107 #ifdef _LIBOBJC
00108
00109
00110 static thread_key_t _objc_thread_storage;
00111
00112
00113 static void *thread_local_storage = NULL;
00114
00115
00116
00117
00118 static inline int
00119 __gthread_objc_init_thread_system (void)
00120 {
00121
00122 if (__gthread_active_p ()
00123 && thr_keycreate (&_objc_thread_storage, NULL) == 0)
00124 return 0;
00125
00126 return -1;
00127 }
00128
00129
00130 static inline int
00131 __gthread_objc_close_thread_system (void)
00132 {
00133 if (__gthread_active_p ())
00134 return 0;
00135 else
00136 return -1;
00137 }
00138
00139
00140
00141
00142 static inline objc_thread_t
00143 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
00144 {
00145 objc_thread_t thread_id;
00146 thread_t new_thread_id = 0;
00147
00148 if (!__gthread_active_p ())
00149 return NULL;
00150
00151 if (thr_create (NULL, 0, (void *) func, arg,
00152 THR_DETACHED | THR_NEW_LWP,
00153 &new_thread_id) == 0)
00154 thread_id = *(objc_thread_t *) &new_thread_id;
00155 else
00156 thread_id = NULL;
00157
00158 return thread_id;
00159 }
00160
00161
00162 static inline int
00163 __gthread_objc_thread_set_priority (int priority)
00164 {
00165 int sys_priority = 0;
00166
00167 if (!__gthread_active_p ())
00168 return -1;
00169
00170 switch (priority)
00171 {
00172 case OBJC_THREAD_INTERACTIVE_PRIORITY:
00173 sys_priority = 300;
00174 break;
00175 default:
00176 case OBJC_THREAD_BACKGROUND_PRIORITY:
00177 sys_priority = 200;
00178 break;
00179 case OBJC_THREAD_LOW_PRIORITY:
00180 sys_priority = 1000;
00181 break;
00182 }
00183
00184
00185 if (thr_setprio (thr_self (), sys_priority) == 0)
00186 return 0;
00187 else
00188 return -1;
00189 }
00190
00191
00192 static inline int
00193 __gthread_objc_thread_get_priority (void)
00194 {
00195 int sys_priority;
00196
00197 if (!__gthread_active_p ())
00198 return OBJC_THREAD_INTERACTIVE_PRIORITY;
00199
00200 if (thr_getprio (thr_self (), &sys_priority) == 0)
00201 {
00202 if (sys_priority >= 250)
00203 return OBJC_THREAD_INTERACTIVE_PRIORITY;
00204 else if (sys_priority >= 150)
00205 return OBJC_THREAD_BACKGROUND_PRIORITY;
00206 return OBJC_THREAD_LOW_PRIORITY;
00207 }
00208
00209
00210 return -1;
00211 }
00212
00213
00214 static inline void
00215 __gthread_objc_thread_yield (void)
00216 {
00217 if (__gthread_active_p ())
00218 thr_yield ();
00219 }
00220
00221
00222 static inline int
00223 __gthread_objc_thread_exit (void)
00224 {
00225 if (__gthread_active_p ())
00226
00227 thr_exit (&__objc_thread_exit_status);
00228
00229
00230 return -1;
00231 }
00232
00233
00234 static inline objc_thread_t
00235 __gthread_objc_thread_id (void)
00236 {
00237 if (__gthread_active_p ())
00238 return (objc_thread_t) thr_self ();
00239 else
00240 return (objc_thread_t) 1;
00241 }
00242
00243
00244 static inline int
00245 __gthread_objc_thread_set_data (void *value)
00246 {
00247 if (__gthread_active_p ())
00248 {
00249 if (thr_setspecific (_objc_thread_storage, value) == 0)
00250 return 0;
00251 else
00252 return -1;
00253 }
00254 else
00255 {
00256 thread_local_storage = value;
00257 return 0;
00258 }
00259 }
00260
00261
00262 static inline void *
00263 __gthread_objc_thread_get_data (void)
00264 {
00265 void *value = NULL;
00266
00267 if (__gthread_active_p ())
00268 {
00269 if (thr_getspecific (_objc_thread_storage, &value) == 0)
00270 return value;
00271 else
00272 return NULL;
00273 }
00274 else
00275 return thread_local_storage;
00276 }
00277
00278
00279
00280
00281 static inline int
00282 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
00283 {
00284 if (__gthread_active_p ()
00285 && mutex_init ((mutex_t *) (&(mutex->backend)), USYNC_THREAD, 0))
00286 return -1;
00287
00288 return 0;
00289 }
00290
00291
00292 static inline int
00293 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
00294 {
00295 if (__gthread_active_p ())
00296 mutex_destroy ((mutex_t *) (&(mutex->backend)));
00297
00298 return 0;
00299 }
00300
00301
00302 static inline int
00303 __gthread_objc_mutex_lock (objc_mutex_t mutex)
00304 {
00305 if (__gthread_active_p ()
00306 && mutex_lock ((mutex_t *) (&(mutex->backend))) != 0)
00307 return -1;
00308
00309 return 0;
00310 }
00311
00312
00313 static inline int
00314 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
00315 {
00316 if (__gthread_active_p ()
00317 && mutex_trylock ((mutex_t *) (&(mutex->backend))) != 0)
00318 return -1;
00319
00320 return 0;
00321 }
00322
00323
00324 static inline int
00325 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
00326 {
00327 if (__gthread_active_p ()
00328 && mutex_unlock ((mutex_t *) (&(mutex->backend))) != 0)
00329 return -1;
00330
00331 return 0;
00332 }
00333
00334
00335
00336
00337 static inline int
00338 __gthread_objc_condition_allocate (objc_condition_t condition)
00339 {
00340 if (__gthread_active_p ())
00341 return cond_init ((cond_t *) (&(condition->backend)), USYNC_THREAD,
00342 NULL);
00343 else
00344 return 0;
00345 }
00346
00347
00348 static inline int
00349 __gthread_objc_condition_deallocate (objc_condition_t condition)
00350 {
00351 if (__gthread_active_p ())
00352 return cond_destroy ((cond_t *) (&(condition->backend)));
00353 else
00354 return 0;
00355 }
00356
00357
00358 static inline int
00359 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
00360 {
00361 if (__gthread_active_p ())
00362 return cond_wait ((cond_t *) (&(condition->backend)),
00363 (mutex_t *) (&(mutex->backend)));
00364 else
00365 return 0;
00366 }
00367
00368
00369 static inline int
00370 __gthread_objc_condition_broadcast (objc_condition_t condition)
00371 {
00372 if (__gthread_active_p ())
00373 return cond_broadcast ((cond_t *) (&(condition->backend)));
00374 else
00375 return 0;
00376 }
00377
00378
00379 static inline int
00380 __gthread_objc_condition_signal (objc_condition_t condition)
00381 {
00382 if (__gthread_active_p ())
00383 return cond_signal ((cond_t *) (&(condition->backend)));
00384 else
00385 return 0;
00386 }
00387
00388 #else
00389
00390 static inline int
00391 __gthread_once (__gthread_once_t *once, void (*func) (void))
00392 {
00393 if (! __gthread_active_p ())
00394 return -1;
00395
00396 if (once == 0 || func == 0)
00397 return EINVAL;
00398
00399 if (once->once == 0)
00400 {
00401 int status = mutex_lock (&once->mutex);
00402 if (status != 0)
00403 return status;
00404 if (once->once == 0)
00405 {
00406 (*func) ();
00407 once->once++;
00408 }
00409 mutex_unlock (&once->mutex);
00410 }
00411 return 0;
00412 }
00413
00414 static inline int
00415 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
00416 {
00417
00418
00419 *key = (__gthread_key_t)-1;
00420 if (thr_keycreate (key, dtor) != 0 || *key == (__gthread_key_t)-1)
00421 return -1;
00422 else
00423 return 0;
00424 }
00425
00426 static inline int
00427 __gthread_key_delete (__gthread_key_t key)
00428 {
00429
00430 return -1;
00431 }
00432
00433 static inline void *
00434 __gthread_getspecific (__gthread_key_t key)
00435 {
00436 void *ptr;
00437 if (thr_getspecific (key, &ptr) == 0)
00438 return ptr;
00439 else
00440 return 0;
00441 }
00442
00443 static inline int
00444 __gthread_setspecific (__gthread_key_t key, const void *ptr)
00445 {
00446 return thr_setspecific (key, (void *) ptr);
00447 }
00448
00449 static inline int
00450 __gthread_mutex_lock (__gthread_mutex_t *mutex)
00451 {
00452 if (__gthread_active_p ())
00453 return mutex_lock (mutex);
00454 else
00455 return 0;
00456 }
00457
00458 static inline int
00459 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
00460 {
00461 if (__gthread_active_p ())
00462 return mutex_trylock (mutex);
00463 else
00464 return 0;
00465 }
00466
00467 static inline int
00468 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
00469 {
00470 if (__gthread_active_p ())
00471 return mutex_unlock (mutex);
00472 else
00473 return 0;
00474 }
00475
00476 static inline int
00477 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
00478 {
00479 mutex->depth = 0;
00480 mutex->owner = (thread_t) 0;
00481 return mutex_init (&mutex->actual, USYNC_THREAD, 0);
00482 }
00483
00484 static inline int
00485 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
00486 {
00487 if (__gthread_active_p ())
00488 {
00489 thread_t me = thr_self ();
00490
00491 if (mutex->owner != me)
00492 {
00493 mutex_lock (&mutex->actual);
00494 mutex->owner = me;
00495 }
00496
00497 mutex->depth++;
00498 }
00499 return 0;
00500 }
00501
00502 static inline int
00503 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
00504 {
00505 if (__gthread_active_p ())
00506 {
00507 thread_t me = thr_self ();
00508
00509 if (mutex->owner != me)
00510 {
00511 if (mutex_trylock (&mutex->actual))
00512 return 1;
00513 mutex->owner = me;
00514 }
00515
00516 mutex->depth++;
00517 }
00518 return 0;
00519 }
00520
00521 static inline int
00522 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
00523 {
00524 if (__gthread_active_p ())
00525 {
00526 if (--mutex->depth == 0)
00527 {
00528 mutex->owner = (thread_t) 0;
00529 mutex_unlock (&mutex->actual);
00530 }
00531 }
00532 return 0;
00533 }
00534
00535 #endif
00536
00537 #endif