1 1.1 mrg /* GNU Objective C Runtime Thread Interface 2 1.1.1.12 mrg Copyright (C) 1996-2024 Free Software Foundation, Inc. 3 1.1 mrg Contributed by Galen C. Hunt (gchunt (at) cs.rochester.edu) 4 1.1 mrg 5 1.1 mrg This file is part of GCC. 6 1.1 mrg 7 1.1 mrg GCC is free software; you can redistribute it and/or modify it under the 8 1.1 mrg terms of the GNU General Public License as published by the Free Software 9 1.1 mrg Foundation; either version 3, or (at your option) any later version. 10 1.1 mrg 11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13 1.1 mrg FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 14 1.1 mrg details. 15 1.1 mrg 16 1.1 mrg Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg You should have received a copy of the GNU General Public License and 21 1.1 mrg a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg <http://www.gnu.org/licenses/>. */ 24 1.1 mrg 25 1.1.1.2 mrg #include "objc-private/common.h" 26 1.1.1.2 mrg #include "objc-private/error.h" 27 1.1.1.2 mrg #define _LIBOBJC 28 1.1.1.2 mrg #include "config.h" 29 1.1.1.2 mrg #include "tconfig.h" 30 1.1.1.2 mrg #include "coretypes.h" 31 1.1.1.2 mrg #include "tm.h" 32 1.1.1.2 mrg #include "defaults.h" 33 1.1.1.2 mrg #include "objc/thr.h" 34 1.1.1.2 mrg #include "objc/message.h" /* For objc_msg_lookup(). */ 35 1.1.1.2 mrg #include "objc/runtime.h" 36 1.1.1.2 mrg #include "objc-private/module-abi-8.h" 37 1.1.1.2 mrg #include "objc-private/runtime.h" 38 1.1.1.2 mrg #include <gthr.h> 39 1.1 mrg 40 1.1 mrg #include <stdlib.h> 41 1.1 mrg 42 1.1 mrg /* Global exit status. */ 43 1.1 mrg int __objc_thread_exit_status = 0; 44 1.1 mrg 45 1.1.1.2 mrg /* Flag which lets us know if we ever became multi threaded. */ 46 1.1 mrg int __objc_is_multi_threaded = 0; 47 1.1 mrg 48 1.1.1.2 mrg /* The hook function called when the runtime becomes multi 49 1.1.1.2 mrg threaded. */ 50 1.1 mrg objc_thread_callback _objc_became_multi_threaded = NULL; 51 1.1 mrg 52 1.1.1.2 mrg /* Use this to set the hook function that will be called when the 53 1.1.1.2 mrg runtime initially becomes multi threaded. The hook function is 54 1.1.1.2 mrg only called once, meaning only when the 2nd thread is spawned, not 55 1.1.1.2 mrg for each and every thread. 56 1.1.1.2 mrg 57 1.1.1.2 mrg It returns the previous hook function or NULL if there is none. 58 1.1.1.2 mrg 59 1.1.1.2 mrg A program outside of the runtime could set this to some function so 60 1.1.1.2 mrg it can be informed; for example, the GNUstep Base Library sets it 61 1.1.1.2 mrg so it can implement the NSBecomingMultiThreaded notification. */ 62 1.1 mrg objc_thread_callback objc_set_thread_callback (objc_thread_callback func) 63 1.1 mrg { 64 1.1 mrg objc_thread_callback temp = _objc_became_multi_threaded; 65 1.1 mrg _objc_became_multi_threaded = func; 66 1.1 mrg return temp; 67 1.1 mrg } 68 1.1 mrg 69 1.1.1.2 mrg /* Private functions. 70 1.1.1.2 mrg 71 1.1.1.2 mrg These functions are utilized by the runtime, but they are not 72 1.1.1.2 mrg considered part of the public interface. */ 73 1.1 mrg 74 1.1.1.2 mrg /* Initialize the threads subsystem. */ 75 1.1.1.2 mrg int 76 1.1.1.2 mrg __objc_init_thread_system(void) 77 1.1.1.2 mrg { 78 1.1.1.2 mrg return __gthread_objc_init_thread_system (); 79 1.1.1.2 mrg } 80 1.1.1.2 mrg 81 1.1.1.2 mrg /* First function called in a thread, starts everything else. 82 1.1.1.2 mrg 83 1.1.1.2 mrg This function is passed to the backend by objc_thread_detach as the 84 1.1.1.2 mrg starting function for a new thread. */ 85 1.1 mrg struct __objc_thread_start_state 86 1.1 mrg { 87 1.1 mrg SEL selector; 88 1.1 mrg id object; 89 1.1 mrg id argument; 90 1.1 mrg }; 91 1.1 mrg 92 1.1 mrg static void __attribute__((noreturn)) 93 1.1 mrg __objc_thread_detach_function (struct __objc_thread_start_state *istate) 94 1.1 mrg { 95 1.1 mrg /* Valid state? */ 96 1.1.1.2 mrg if (istate) 97 1.1.1.2 mrg { 98 1.1.1.2 mrg id (*imp) (id, SEL, id); 99 1.1.1.2 mrg SEL selector = istate->selector; 100 1.1.1.2 mrg id object = istate->object; 101 1.1.1.2 mrg id argument = istate->argument; 102 1.1.1.2 mrg 103 1.1.1.2 mrg /* Don't need anymore so free it. */ 104 1.1.1.2 mrg objc_free (istate); 105 1.1 mrg 106 1.1.1.2 mrg /* Clear out the thread local storage. */ 107 1.1.1.2 mrg objc_thread_set_data (NULL); 108 1.1.1.2 mrg 109 1.1.1.2 mrg /* Check to see if we just became multi threaded. */ 110 1.1.1.2 mrg if (! __objc_is_multi_threaded) 111 1.1.1.2 mrg { 112 1.1.1.2 mrg __objc_is_multi_threaded = 1; 113 1.1.1.2 mrg 114 1.1.1.2 mrg /* Call the hook function. */ 115 1.1.1.2 mrg if (_objc_became_multi_threaded != NULL) 116 1.1.1.2 mrg (*_objc_became_multi_threaded) (); 117 1.1.1.2 mrg } 118 1.1.1.2 mrg 119 1.1.1.2 mrg /* Call the method. */ 120 1.1.1.2 mrg if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector))) 121 1.1 mrg (*imp) (object, selector, argument); 122 1.1.1.2 mrg else 123 1.1.1.2 mrg { 124 1.1.1.2 mrg /* FIXME: Should we abort here ? */ 125 1.1.1.2 mrg _objc_abort ("objc_thread_detach called with bad selector.\n"); 126 1.1.1.2 mrg } 127 1.1.1.2 mrg } 128 1.1 mrg else 129 1.1.1.2 mrg { 130 1.1.1.2 mrg /* FIXME: Should we abort here ? */ 131 1.1.1.2 mrg _objc_abort ("objc_thread_detach called with NULL state.\n"); 132 1.1.1.2 mrg } 133 1.1.1.2 mrg 134 1.1.1.2 mrg /* Exit the thread. */ 135 1.1 mrg objc_thread_exit (); 136 1.1 mrg 137 1.1 mrg /* Make sure compiler detects no return. */ 138 1.1 mrg __builtin_trap (); 139 1.1 mrg } 140 1.1 mrg 141 1.1.1.2 mrg /* Public functions. 142 1.1.1.2 mrg 143 1.1.1.2 mrg These functions constitute the public interface to the Objective-C 144 1.1.1.2 mrg thread and mutex functionality. */ 145 1.1 mrg 146 1.1.1.2 mrg /* Detach a new thread of execution and return its id. Returns NULL 147 1.1.1.2 mrg if fails. Thread is started by sending message with selector to 148 1.1.1.2 mrg object. Message takes a single argument. */ 149 1.1 mrg objc_thread_t 150 1.1 mrg objc_thread_detach (SEL selector, id object, id argument) 151 1.1 mrg { 152 1.1 mrg struct __objc_thread_start_state *istate; 153 1.1 mrg objc_thread_t thread_id = NULL; 154 1.1 mrg 155 1.1.1.2 mrg /* Allocate the state structure. */ 156 1.1.1.2 mrg if (!(istate = (struct __objc_thread_start_state *)objc_malloc 157 1.1.1.2 mrg (sizeof (*istate)))) 158 1.1 mrg return NULL; 159 1.1.1.2 mrg 160 1.1.1.2 mrg /* Initialize the state structure. */ 161 1.1 mrg istate->selector = selector; 162 1.1 mrg istate->object = object; 163 1.1 mrg istate->argument = argument; 164 1.1 mrg 165 1.1.1.2 mrg /* Lock access. */ 166 1.1 mrg objc_mutex_lock (__objc_runtime_mutex); 167 1.1 mrg 168 1.1.1.2 mrg /* Call the backend to spawn the thread. */ 169 1.1.1.2 mrg if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function, 170 1.1.1.2 mrg istate)) == NULL) 171 1.1 mrg { 172 1.1.1.2 mrg /* Failed! */ 173 1.1 mrg objc_mutex_unlock (__objc_runtime_mutex); 174 1.1 mrg objc_free (istate); 175 1.1 mrg return NULL; 176 1.1 mrg } 177 1.1 mrg 178 1.1.1.2 mrg /* Increment our thread counter. */ 179 1.1 mrg __objc_runtime_threads_alive++; 180 1.1 mrg objc_mutex_unlock (__objc_runtime_mutex); 181 1.1 mrg 182 1.1 mrg return thread_id; 183 1.1 mrg } 184 1.1 mrg 185 1.1.1.2 mrg /* Set the current thread's priority. */ 186 1.1 mrg int 187 1.1 mrg objc_thread_set_priority (int priority) 188 1.1 mrg { 189 1.1.1.2 mrg return __gthread_objc_thread_set_priority (priority); 190 1.1 mrg } 191 1.1 mrg 192 1.1.1.2 mrg /* Return the current thread's priority. */ 193 1.1 mrg int 194 1.1 mrg objc_thread_get_priority (void) 195 1.1 mrg { 196 1.1.1.2 mrg return __gthread_objc_thread_get_priority (); 197 1.1 mrg } 198 1.1 mrg 199 1.1.1.2 mrg /* Yield our process time to another thread. Any BUSY waiting that is 200 1.1.1.2 mrg done by a thread should use this function to make sure that other 201 1.1.1.2 mrg threads can make progress even on a lazy uniprocessor system. */ 202 1.1 mrg void 203 1.1 mrg objc_thread_yield (void) 204 1.1 mrg { 205 1.1.1.2 mrg __gthread_objc_thread_yield (); 206 1.1 mrg } 207 1.1 mrg 208 1.1.1.2 mrg /* Terminate the current tread. Doesn't return. Actually, if it 209 1.1.1.2 mrg failed returns -1. */ 210 1.1 mrg int 211 1.1 mrg objc_thread_exit (void) 212 1.1 mrg { 213 1.1.1.2 mrg /* Decrement our counter of the number of threads alive. */ 214 1.1 mrg objc_mutex_lock (__objc_runtime_mutex); 215 1.1 mrg __objc_runtime_threads_alive--; 216 1.1 mrg objc_mutex_unlock (__objc_runtime_mutex); 217 1.1 mrg 218 1.1.1.2 mrg /* Call the backend to terminate the thread. */ 219 1.1.1.2 mrg return __gthread_objc_thread_exit (); 220 1.1 mrg } 221 1.1 mrg 222 1.1.1.2 mrg /* Returns an integer value which uniquely describes a thread. Must 223 1.1.1.2 mrg not be NULL which is reserved as a marker for "no thread". */ 224 1.1 mrg objc_thread_t 225 1.1 mrg objc_thread_id (void) 226 1.1 mrg { 227 1.1.1.2 mrg return __gthread_objc_thread_id (); 228 1.1 mrg } 229 1.1 mrg 230 1.1.1.2 mrg /* Sets the thread's local storage pointer. Returns 0 if successful 231 1.1.1.2 mrg or -1 if failed. */ 232 1.1 mrg int 233 1.1 mrg objc_thread_set_data (void *value) 234 1.1 mrg { 235 1.1.1.2 mrg return __gthread_objc_thread_set_data (value); 236 1.1 mrg } 237 1.1 mrg 238 1.1.1.2 mrg /* Returns the thread's local storage pointer. Returns NULL on 239 1.1.1.2 mrg failure. */ 240 1.1 mrg void * 241 1.1 mrg objc_thread_get_data (void) 242 1.1 mrg { 243 1.1.1.2 mrg return __gthread_objc_thread_get_data (); 244 1.1 mrg } 245 1.1 mrg 246 1.1.1.2 mrg /* Public mutex functions */ 247 1.1 mrg 248 1.1.1.2 mrg /* Allocate a mutex. Return the mutex pointer if successful or NULL 249 1.1.1.2 mrg if the allocation failed for any reason. */ 250 1.1 mrg objc_mutex_t 251 1.1 mrg objc_mutex_allocate (void) 252 1.1 mrg { 253 1.1 mrg objc_mutex_t mutex; 254 1.1 mrg 255 1.1.1.2 mrg /* Allocate the mutex structure. */ 256 1.1 mrg if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex)))) 257 1.1 mrg return NULL; 258 1.1 mrg 259 1.1.1.2 mrg /* Call backend to create the mutex. */ 260 1.1.1.2 mrg if (__gthread_objc_mutex_allocate (mutex)) 261 1.1 mrg { 262 1.1.1.2 mrg /* Failed! */ 263 1.1 mrg objc_free (mutex); 264 1.1 mrg return NULL; 265 1.1 mrg } 266 1.1 mrg 267 1.1.1.2 mrg /* Initialize mutex. */ 268 1.1 mrg mutex->owner = NULL; 269 1.1 mrg mutex->depth = 0; 270 1.1 mrg return mutex; 271 1.1 mrg } 272 1.1 mrg 273 1.1.1.2 mrg /* Deallocate a mutex. Note that this includes an implicit mutex_lock 274 1.1.1.2 mrg to insure that no one else is using the lock. It is legal to 275 1.1.1.2 mrg deallocate a lock if we have a lock on it, but illegal to 276 1.1.1.2 mrg deallocate a lock held by anyone else. Returns the number of locks 277 1.1.1.2 mrg on the thread. (1 for deallocate). */ 278 1.1 mrg int 279 1.1 mrg objc_mutex_deallocate (objc_mutex_t mutex) 280 1.1 mrg { 281 1.1 mrg int depth; 282 1.1 mrg 283 1.1.1.2 mrg /* Valid mutex? */ 284 1.1 mrg if (! mutex) 285 1.1 mrg return -1; 286 1.1 mrg 287 1.1.1.2 mrg /* Acquire lock on mutex. */ 288 1.1 mrg depth = objc_mutex_lock (mutex); 289 1.1 mrg 290 1.1.1.2 mrg /* Call backend to destroy mutex. */ 291 1.1.1.2 mrg if (__gthread_objc_mutex_deallocate (mutex)) 292 1.1 mrg return -1; 293 1.1 mrg 294 1.1.1.2 mrg /* Free the mutex structure. */ 295 1.1 mrg objc_free (mutex); 296 1.1 mrg 297 1.1.1.2 mrg /* Return last depth. */ 298 1.1 mrg return depth; 299 1.1 mrg } 300 1.1 mrg 301 1.1.1.2 mrg /* Grab a lock on a mutex. If this thread already has a lock on this 302 1.1.1.2 mrg mutex then we increment the lock count. If another thread has a 303 1.1.1.2 mrg lock on the mutex we block and wait for the thread to release the 304 1.1.1.2 mrg lock. Returns the lock count on the mutex held by this thread. */ 305 1.1 mrg int 306 1.1 mrg objc_mutex_lock (objc_mutex_t mutex) 307 1.1 mrg { 308 1.1 mrg objc_thread_t thread_id; 309 1.1 mrg int status; 310 1.1 mrg 311 1.1.1.2 mrg /* Valid mutex? */ 312 1.1 mrg if (! mutex) 313 1.1 mrg return -1; 314 1.1 mrg 315 1.1.1.2 mrg /* If we already own the lock then increment depth. */ 316 1.1.1.2 mrg thread_id = __gthread_objc_thread_id (); 317 1.1 mrg if (mutex->owner == thread_id) 318 1.1 mrg return ++mutex->depth; 319 1.1 mrg 320 1.1.1.2 mrg /* Call the backend to lock the mutex. */ 321 1.1.1.2 mrg status = __gthread_objc_mutex_lock (mutex); 322 1.1 mrg 323 1.1.1.2 mrg /* Failed? */ 324 1.1 mrg if (status) 325 1.1 mrg return status; 326 1.1 mrg 327 1.1.1.2 mrg /* Successfully locked the thread. */ 328 1.1 mrg mutex->owner = thread_id; 329 1.1 mrg return mutex->depth = 1; 330 1.1 mrg } 331 1.1 mrg 332 1.1.1.2 mrg /* Try to grab a lock on a mutex. If this thread already has a lock 333 1.1.1.2 mrg on this mutex then we increment the lock count and return it. If 334 1.1.1.2 mrg another thread has a lock on the mutex returns -1. */ 335 1.1 mrg int 336 1.1 mrg objc_mutex_trylock (objc_mutex_t mutex) 337 1.1 mrg { 338 1.1 mrg objc_thread_t thread_id; 339 1.1 mrg int status; 340 1.1 mrg 341 1.1.1.2 mrg /* Valid mutex? */ 342 1.1 mrg if (! mutex) 343 1.1 mrg return -1; 344 1.1 mrg 345 1.1.1.2 mrg /* If we already own the lock then increment depth. */ 346 1.1.1.2 mrg thread_id = __gthread_objc_thread_id (); 347 1.1 mrg if (mutex->owner == thread_id) 348 1.1 mrg return ++mutex->depth; 349 1.1 mrg 350 1.1.1.2 mrg /* Call the backend to try to lock the mutex. */ 351 1.1.1.2 mrg status = __gthread_objc_mutex_trylock (mutex); 352 1.1 mrg 353 1.1.1.2 mrg /* Failed? */ 354 1.1 mrg if (status) 355 1.1 mrg return status; 356 1.1 mrg 357 1.1.1.2 mrg /* Successfully locked the thread. */ 358 1.1 mrg mutex->owner = thread_id; 359 1.1 mrg return mutex->depth = 1; 360 1.1 mrg } 361 1.1 mrg 362 1.1.1.2 mrg /* Unlocks the mutex by one level. Decrements the lock count on this 363 1.1.1.2 mrg mutex by one. If the lock count reaches zero, release the lock on 364 1.1.1.2 mrg the mutex. Returns the lock count on the mutex. It is an error to 365 1.1.1.2 mrg attempt to unlock a mutex which this thread doesn't hold in which 366 1.1.1.2 mrg case return -1 and the mutex is unaffected. */ 367 1.1 mrg int 368 1.1 mrg objc_mutex_unlock (objc_mutex_t mutex) 369 1.1 mrg { 370 1.1 mrg objc_thread_t thread_id; 371 1.1 mrg int status; 372 1.1 mrg 373 1.1.1.2 mrg /* Valid mutex? */ 374 1.1 mrg if (! mutex) 375 1.1 mrg return -1; 376 1.1 mrg 377 1.1.1.2 mrg /* If another thread owns the lock then abort. */ 378 1.1.1.2 mrg thread_id = __gthread_objc_thread_id (); 379 1.1 mrg if (mutex->owner != thread_id) 380 1.1 mrg return -1; 381 1.1 mrg 382 1.1.1.2 mrg /* Decrement depth and return. */ 383 1.1 mrg if (mutex->depth > 1) 384 1.1 mrg return --mutex->depth; 385 1.1 mrg 386 1.1.1.2 mrg /* Depth down to zero so we are no longer the owner. */ 387 1.1 mrg mutex->depth = 0; 388 1.1 mrg mutex->owner = NULL; 389 1.1 mrg 390 1.1.1.2 mrg /* Have the backend unlock the mutex. */ 391 1.1.1.2 mrg status = __gthread_objc_mutex_unlock (mutex); 392 1.1 mrg 393 1.1.1.2 mrg /* Failed? */ 394 1.1 mrg if (status) 395 1.1 mrg return status; 396 1.1 mrg 397 1.1 mrg return 0; 398 1.1 mrg } 399 1.1 mrg 400 1.1.1.2 mrg /* Public condition mutex functions */ 401 1.1 mrg 402 1.1.1.2 mrg /* Allocate a condition. Return the condition pointer if successful 403 1.1.1.2 mrg or NULL if the allocation failed for any reason. */ 404 1.1 mrg objc_condition_t 405 1.1 mrg objc_condition_allocate (void) 406 1.1 mrg { 407 1.1 mrg objc_condition_t condition; 408 1.1 mrg 409 1.1.1.2 mrg /* Allocate the condition mutex structure. */ 410 1.1 mrg if (! (condition = 411 1.1 mrg (objc_condition_t) objc_malloc (sizeof (struct objc_condition)))) 412 1.1 mrg return NULL; 413 1.1 mrg 414 1.1.1.2 mrg /* Call the backend to create the condition mutex. */ 415 1.1.1.2 mrg if (__gthread_objc_condition_allocate (condition)) 416 1.1 mrg { 417 1.1.1.2 mrg /* Failed! */ 418 1.1 mrg objc_free (condition); 419 1.1 mrg return NULL; 420 1.1 mrg } 421 1.1 mrg 422 1.1.1.2 mrg /* Success! */ 423 1.1 mrg return condition; 424 1.1 mrg } 425 1.1 mrg 426 1.1.1.2 mrg /* Deallocate a condition. Note that this includes an implicit 427 1.1.1.2 mrg condition_broadcast to insure that waiting threads have the 428 1.1.1.2 mrg opportunity to wake. It is legal to dealloc a condition only if no 429 1.1.1.2 mrg other thread is/will be using it. Here we do NOT check for other 430 1.1.1.2 mrg threads waiting but just wake them up. */ 431 1.1 mrg int 432 1.1 mrg objc_condition_deallocate (objc_condition_t condition) 433 1.1 mrg { 434 1.1.1.2 mrg /* Broadcast the condition. */ 435 1.1 mrg if (objc_condition_broadcast (condition)) 436 1.1 mrg return -1; 437 1.1 mrg 438 1.1.1.2 mrg /* Call the backend to destroy. */ 439 1.1.1.2 mrg if (__gthread_objc_condition_deallocate (condition)) 440 1.1 mrg return -1; 441 1.1 mrg 442 1.1.1.2 mrg /* Free the condition mutex structure. */ 443 1.1 mrg objc_free (condition); 444 1.1 mrg 445 1.1 mrg return 0; 446 1.1 mrg } 447 1.1 mrg 448 1.1.1.2 mrg /* Wait on the condition unlocking the mutex until 449 1.1.1.2 mrg objc_condition_signal () or objc_condition_broadcast () are called 450 1.1.1.2 mrg for the same condition. The given mutex *must* have the depth set 451 1.1.1.2 mrg to 1 so that it can be unlocked here, so that someone else can lock 452 1.1.1.2 mrg it and signal/broadcast the condition. The mutex is used to lock 453 1.1.1.2 mrg access to the shared data that make up the "condition" 454 1.1.1.2 mrg predicate. */ 455 1.1 mrg int 456 1.1 mrg objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex) 457 1.1 mrg { 458 1.1 mrg objc_thread_t thread_id; 459 1.1 mrg 460 1.1.1.2 mrg /* Valid arguments? */ 461 1.1 mrg if (! mutex || ! condition) 462 1.1 mrg return -1; 463 1.1 mrg 464 1.1.1.2 mrg /* Make sure we are owner of mutex. */ 465 1.1.1.2 mrg thread_id = __gthread_objc_thread_id (); 466 1.1 mrg if (mutex->owner != thread_id) 467 1.1 mrg return -1; 468 1.1 mrg 469 1.1.1.2 mrg /* Cannot be locked more than once. */ 470 1.1 mrg if (mutex->depth > 1) 471 1.1 mrg return -1; 472 1.1 mrg 473 1.1.1.2 mrg /* Virtually unlock the mutex. */ 474 1.1 mrg mutex->depth = 0; 475 1.1 mrg mutex->owner = (objc_thread_t)NULL; 476 1.1 mrg 477 1.1.1.2 mrg /* Call the backend to wait. */ 478 1.1.1.2 mrg __gthread_objc_condition_wait (condition, mutex); 479 1.1 mrg 480 1.1.1.2 mrg /* Make ourselves owner of the mutex. */ 481 1.1 mrg mutex->owner = thread_id; 482 1.1 mrg mutex->depth = 1; 483 1.1 mrg 484 1.1 mrg return 0; 485 1.1 mrg } 486 1.1 mrg 487 1.1.1.2 mrg /* Wake up all threads waiting on this condition. It is recommended 488 1.1.1.2 mrg that the called would lock the same mutex as the threads in 489 1.1.1.2 mrg objc_condition_wait before changing the "condition predicate" and 490 1.1.1.2 mrg make this call and unlock it right away after this call. */ 491 1.1 mrg int 492 1.1 mrg objc_condition_broadcast (objc_condition_t condition) 493 1.1 mrg { 494 1.1.1.2 mrg /* Valid condition mutex? */ 495 1.1 mrg if (! condition) 496 1.1 mrg return -1; 497 1.1 mrg 498 1.1.1.2 mrg return __gthread_objc_condition_broadcast (condition); 499 1.1 mrg } 500 1.1 mrg 501 1.1.1.2 mrg /* Wake up one thread waiting on this condition. It is recommended 502 1.1.1.2 mrg that the called would lock the same mutex as the threads in 503 1.1.1.2 mrg objc_condition_wait before changing the "condition predicate" and 504 1.1.1.2 mrg make this call and unlock it right away after this call. */ 505 1.1 mrg int 506 1.1 mrg objc_condition_signal (objc_condition_t condition) 507 1.1 mrg { 508 1.1.1.2 mrg /* Valid condition mutex? */ 509 1.1 mrg if (! condition) 510 1.1 mrg return -1; 511 1.1 mrg 512 1.1.1.2 mrg return __gthread_objc_condition_signal (condition); 513 1.1 mrg } 514 1.1 mrg 515 1.1 mrg /* Make the objc thread system aware that a thread which is managed 516 1.1 mrg (started, stopped) by external code could access objc facilities 517 1.1 mrg from now on. This is used when you are interfacing with some 518 1.1 mrg external non-objc-based environment/system - you must call 519 1.1 mrg objc_thread_add () before an alien thread makes any calls to 520 1.1 mrg Objective-C. Do not cause the _objc_became_multi_threaded hook to 521 1.1 mrg be executed. */ 522 1.1 mrg void 523 1.1 mrg objc_thread_add (void) 524 1.1 mrg { 525 1.1 mrg objc_mutex_lock (__objc_runtime_mutex); 526 1.1 mrg __objc_is_multi_threaded = 1; 527 1.1 mrg __objc_runtime_threads_alive++; 528 1.1 mrg objc_mutex_unlock (__objc_runtime_mutex); 529 1.1 mrg } 530 1.1 mrg 531 1.1 mrg /* Make the objc thread system aware that a thread managed (started, 532 1.1 mrg stopped) by some external code will no longer access objc and thus 533 1.1 mrg can be forgotten by the objc thread system. Call 534 1.1 mrg objc_thread_remove () when your alien thread is done with making 535 1.1 mrg calls to Objective-C. */ 536 1.1 mrg void 537 1.1 mrg objc_thread_remove (void) 538 1.1 mrg { 539 1.1 mrg objc_mutex_lock (__objc_runtime_mutex); 540 1.1 mrg __objc_runtime_threads_alive--; 541 1.1 mrg objc_mutex_unlock (__objc_runtime_mutex); 542 1.1 mrg } 543 1.1 mrg 544