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 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
00049 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
00050
00051 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
00052
00053 #pragma weak thr_keycreate
00054 #pragma weak thr_getspecific
00055 #pragma weak thr_setspecific
00056 #pragma weak thr_create
00057
00058 #pragma weak mutex_lock
00059 #pragma weak mutex_trylock
00060 #pragma weak mutex_unlock
00061
00062 #ifdef _LIBOBJC
00063 #pragma weak thr_exit
00064 #pragma weak thr_keycreate
00065 #pragma weak thr_getprio
00066 #pragma weak thr_self
00067 #pragma weak thr_setprio
00068 #pragma weak thr_yield
00069
00070 #pragma weak cond_init
00071 #pragma weak cond_destroy
00072 #pragma weak cond_wait
00073 #pragma weak cond_broadcast
00074 #pragma weak cond_signal
00075
00076 #pragma weak mutex_init
00077 #pragma weak mutex_destroy
00078 #endif
00079
00080
00081
00082
00083 static inline int
00084 __gthread_active_p (void)
00085 {
00086 static void *const __gthread_active_ptr = (void *) &thr_create;
00087 return __gthread_active_ptr != 0;
00088 }
00089
00090 #else
00091
00092 static inline int
00093 __gthread_active_p (void)
00094 {
00095 return 1;
00096 }
00097
00098 #endif
00099
00100 #ifdef _LIBOBJC
00101
00102
00103 static thread_key_t _objc_thread_storage;
00104
00105
00106 static void *thread_local_storage = NULL;
00107
00108
00109
00110
00111 static inline int
00112 __gthread_objc_init_thread_system (void)
00113 {
00114
00115 if (__gthread_active_p ()
00116 && thr_keycreate (&_objc_thread_storage, NULL) == 0)
00117 return 0;
00118
00119 return -1;
00120 }
00121
00122
00123 static inline int
00124 __gthread_objc_close_thread_system (void)
00125 {
00126 if (__gthread_active_p ())
00127 return 0;
00128 else
00129 return -1;
00130 }
00131
00132
00133
00134
00135 static inline objc_thread_t
00136 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
00137 {
00138 objc_thread_t thread_id;
00139 thread_t new_thread_id = 0;
00140
00141 if (!__gthread_active_p ())
00142 return NULL;
00143
00144 if (thr_create (NULL, 0, (void *) func, arg,
00145 THR_DETACHED | THR_NEW_LWP,
00146 &new_thread_id) == 0)
00147 thread_id = *(objc_thread_t *) &new_thread_id;
00148 else
00149 thread_id = NULL;
00150
00151 return thread_id;
00152 }
00153
00154
00155 static inline int
00156 __gthread_objc_thread_set_priority (int priority)
00157 {
00158 int sys_priority = 0;
00159
00160 if (!__gthread_active_p ())
00161 return -1;
00162
00163 switch (priority)
00164 {
00165 case OBJC_THREAD_INTERACTIVE_PRIORITY:
00166 sys_priority = 300;
00167 break;
00168 default:
00169 case OBJC_THREAD_BACKGROUND_PRIORITY:
00170 sys_priority = 200;
00171 break;
00172 case OBJC_THREAD_LOW_PRIORITY:
00173 sys_priority = 1000;
00174 break;
00175 }
00176
00177
00178 if (thr_setprio (thr_self (), sys_priority) == 0)
00179 return 0;
00180 else
00181 return -1;
00182 }
00183
00184
00185 static inline int
00186 __gthread_objc_thread_get_priority (void)
00187 {
00188 int sys_priority;
00189
00190 if (!__gthread_active_p ())
00191 return OBJC_THREAD_INTERACTIVE_PRIORITY;
00192
00193 if (thr_getprio (thr_self (), &sys_priority) == 0)
00194 {
00195 if (sys_priority >= 250)
00196 return OBJC_THREAD_INTERACTIVE_PRIORITY;
00197 else if (sys_priority >= 150)
00198 return OBJC_THREAD_BACKGROUND_PRIORITY;
00199 return OBJC_THREAD_LOW_PRIORITY;
00200 }
00201
00202
00203 return -1;
00204 }
00205
00206
00207 static inline void
00208 __gthread_objc_thread_yield (void)
00209 {
00210 if (__gthread_active_p ())
00211 thr_yield ();
00212 }
00213
00214
00215 static inline int
00216 __gthread_objc_thread_exit (void)
00217 {
00218 if (__gthread_active_p ())
00219
00220 thr_exit (&__objc_thread_exit_status);
00221
00222
00223 return -1;
00224 }
00225
00226
00227 static inline objc_thread_t
00228 __gthread_objc_thread_id (void)
00229 {
00230 if (__gthread_active_p ())
00231 return (objc_thread_t) thr_self ();
00232 else
00233 return (objc_thread_t) 1;
00234 }
00235
00236
00237 static inline int
00238 __gthread_objc_thread_set_data (void *value)
00239 {
00240 if (__gthread_active_p ())
00241 {
00242 if (thr_setspecific (_objc_thread_storage, value) == 0)
00243 return 0;
00244 else
00245 return -1;
00246 }
00247 else
00248 {
00249 thread_local_storage = value;
00250 return 0;
00251 }
00252 }
00253
00254
00255 static inline void *
00256 __gthread_objc_thread_get_data (void)
00257 {
00258 void *value = NULL;
00259
00260 if (__gthread_active_p ())
00261 {
00262 if (thr_getspecific (_objc_thread_storage, &value) == 0)
00263 return value;
00264 else
00265 return NULL;
00266 }
00267 else
00268 return thread_local_storage;
00269 }
00270
00271
00272
00273
00274 static inline int
00275 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
00276 {
00277 if (__gthread_active_p ()
00278 && mutex_init ((mutex_t *) (&(mutex->backend)), USYNC_THREAD, 0))
00279 return -1;
00280
00281 return 0;
00282 }
00283
00284
00285 static inline int
00286 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
00287 {
00288 if (__gthread_active_p ())
00289 mutex_destroy ((mutex_t *) (&(mutex->backend)));
00290
00291 return 0;
00292 }
00293
00294
00295 static inline int
00296 __gthread_objc_mutex_lock (objc_mutex_t mutex)
00297 {
00298 if (__gthread_active_p ()
00299 && mutex_lock ((mutex_t *) (&(mutex->backend))) != 0)
00300 return -1;
00301
00302 return 0;
00303 }
00304
00305
00306 static inline int
00307 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
00308 {
00309 if (__gthread_active_p ()
00310 && mutex_trylock ((mutex_t *) (&(mutex->backend))) != 0)
00311 return -1;
00312
00313 return 0;
00314 }
00315
00316
00317 static inline int
00318 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
00319 {
00320 if (__gthread_active_p ()
00321 && mutex_unlock ((mutex_t *) (&(mutex->backend))) != 0)
00322 return -1;
00323
00324 return 0;
00325 }
00326
00327
00328
00329
00330 static inline int
00331 __gthread_objc_condition_allocate (objc_condition_t condition)
00332 {
00333 if (__gthread_active_p ())
00334 return cond_init ((cond_t *) (&(condition->backend)), USYNC_THREAD,
00335 NULL);
00336 else
00337 return 0;
00338 }
00339
00340
00341 static inline int
00342 __gthread_objc_condition_deallocate (objc_condition_t condition)
00343 {
00344 if (__gthread_active_p ())
00345 return cond_destroy ((cond_t *) (&(condition->backend)));
00346 else
00347 return 0;
00348 }
00349
00350
00351 static inline int
00352 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
00353 {
00354 if (__gthread_active_p ())
00355 return cond_wait ((cond_t *) (&(condition->backend)),
00356 (mutex_t *) (&(mutex->backend)));
00357 else
00358 return 0;
00359 }
00360
00361
00362 static inline int
00363 __gthread_objc_condition_broadcast (objc_condition_t condition)
00364 {
00365 if (__gthread_active_p ())
00366 return cond_broadcast ((cond_t *) (&(condition->backend)));
00367 else
00368 return 0;
00369 }
00370
00371
00372 static inline int
00373 __gthread_objc_condition_signal (objc_condition_t condition)
00374 {
00375 if (__gthread_active_p ())
00376 return cond_signal ((cond_t *) (&(condition->backend)));
00377 else
00378 return 0;
00379 }
00380
00381 #else
00382
00383 static inline int
00384 __gthread_once (__gthread_once_t *once, void (*func) (void))
00385 {
00386 if (! __gthread_active_p ())
00387 return -1;
00388
00389 if (once == 0 || func == 0)
00390 return EINVAL;
00391
00392 if (once->once == 0)
00393 {
00394 int status = mutex_lock (&once->mutex);
00395 if (status != 0)
00396 return status;
00397 if (once->once == 0)
00398 {
00399 (*func) ();
00400 once->once++;
00401 }
00402 mutex_unlock (&once->mutex);
00403 }
00404 return 0;
00405 }
00406
00407 static inline int
00408 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
00409 {
00410
00411
00412 *key = -1;
00413 if (thr_keycreate (key, dtor) != 0 || *key == -1)
00414 return -1;
00415 else
00416 return 0;
00417 }
00418
00419 static inline int
00420 __gthread_key_dtor (__gthread_key_t key, void *ptr)
00421 {
00422
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 #endif
00477
00478 #endif