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