]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/thr.c
[Ada] Improved support for aspect alignment in CCG
[thirdparty/gcc.git] / libobjc / thr.c
CommitLineData
88e17b57 1/* GNU Objective C Runtime Thread Interface
8d9254fc 2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
88e17b57
BE
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4
38709cad 5This file is part of GCC.
88e17b57 6
38709cad 7GCC is free software; you can redistribute it and/or modify it under the
88e17b57 8terms of the GNU General Public License as published by the Free Software
748086b7 9Foundation; either version 3, or (at your option) any later version.
88e17b57 10
38709cad 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
88e17b57
BE
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14details.
15
748086b7
JJ
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/>. */
88e17b57 24
6dead247 25#include "objc-private/common.h"
7b869986 26#include "objc-private/error.h"
88a2722e 27#define _LIBOBJC
88a2722e
NP
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"
5ec582f9 34#include "objc/message.h" /* For objc_msg_lookup(). */
718a8e53 35#include "objc/runtime.h"
80e4b9e5 36#include "objc-private/module-abi-8.h"
a19fac96 37#include "objc-private/runtime.h"
88a2722e 38#include <gthr.h>
88e17b57
BE
39
40#include <stdlib.h>
88e17b57
BE
41
42/* Global exit status. */
43int __objc_thread_exit_status = 0;
44
575584a9 45/* Flag which lets us know if we ever became multi threaded. */
88e17b57
BE
46int __objc_is_multi_threaded = 0;
47
575584a9
NP
48/* The hook function called when the runtime becomes multi
49 threaded. */
88e17b57
BE
50objc_thread_callback _objc_became_multi_threaded = NULL;
51
575584a9
NP
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.
88e17b57 56
575584a9 57 It returns the previous hook function or NULL if there is none.
88e17b57 58
575584a9
NP
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. */
40165636 62objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
88e17b57
BE
63{
64 objc_thread_callback temp = _objc_became_multi_threaded;
65 _objc_became_multi_threaded = func;
66 return temp;
67}
68
575584a9
NP
69/* Private functions.
70
71 These functions are utilized by the runtime, but they are not
72 considered part of the public interface. */
88e17b57 73
575584a9 74/* Initialize the threads subsystem. */
88a2722e
NP
75int
76__objc_init_thread_system(void)
77{
78 return __gthread_objc_init_thread_system ();
79}
80
575584a9 81/* First function called in a thread, starts everything else.
88e17b57 82
575584a9
NP
83 This function is passed to the backend by objc_thread_detach as the
84 starting function for a new thread. */
88e17b57
BE
85struct __objc_thread_start_state
86{
87 SEL selector;
88 id object;
89 id argument;
90};
91
bc012a44
AP
92static void __attribute__((noreturn))
93__objc_thread_detach_function (struct __objc_thread_start_state *istate)
88e17b57
BE
94{
95 /* Valid state? */
575584a9
NP
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)))
40165636 121 (*imp) (object, selector, argument);
575584a9
NP
122 else
123 {
124 /* FIXME: Should we abort here ? */
125 _objc_abort ("objc_thread_detach called with bad selector.\n");
126 }
127 }
88e17b57 128 else
7b869986
NP
129 {
130 /* FIXME: Should we abort here ? */
131 _objc_abort ("objc_thread_detach called with NULL state.\n");
132 }
575584a9
NP
133
134 /* Exit the thread. */
40165636 135 objc_thread_exit ();
b15b7ef8
KT
136
137 /* Make sure compiler detects no return. */
138 __builtin_trap ();
88e17b57
BE
139}
140
575584a9 141/* Public functions.
88e17b57 142
575584a9
NP
143 These functions constitute the public interface to the Objective-C
144 thread and mutex functionality. */
88e17b57 145
575584a9
NP
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. */
88e17b57 149objc_thread_t
40165636 150objc_thread_detach (SEL selector, id object, id argument)
88e17b57
BE
151{
152 struct __objc_thread_start_state *istate;
153 objc_thread_t thread_id = NULL;
154
575584a9
NP
155 /* Allocate the state structure. */
156 if (!(istate = (struct __objc_thread_start_state *)objc_malloc
157 (sizeof (*istate))))
88e17b57 158 return NULL;
575584a9
NP
159
160 /* Initialize the state structure. */
88e17b57
BE
161 istate->selector = selector;
162 istate->object = object;
163 istate->argument = argument;
164
575584a9 165 /* Lock access. */
40165636 166 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 167
575584a9 168 /* Call the backend to spawn the thread. */
88a2722e
NP
169 if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
170 istate)) == NULL)
88e17b57 171 {
575584a9 172 /* Failed! */
40165636
RB
173 objc_mutex_unlock (__objc_runtime_mutex);
174 objc_free (istate);
88e17b57
BE
175 return NULL;
176 }
177
575584a9 178 /* Increment our thread counter. */
88e17b57 179 __objc_runtime_threads_alive++;
40165636 180 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
181
182 return thread_id;
183}
184
575584a9 185/* Set the current thread's priority. */
88e17b57 186int
40165636 187objc_thread_set_priority (int priority)
88e17b57 188{
88a2722e 189 return __gthread_objc_thread_set_priority (priority);
88e17b57
BE
190}
191
575584a9 192/* Return the current thread's priority. */
88e17b57 193int
40165636 194objc_thread_get_priority (void)
88e17b57 195{
88a2722e 196 return __gthread_objc_thread_get_priority ();
88e17b57
BE
197}
198
575584a9
NP
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. */
88e17b57 202void
40165636 203objc_thread_yield (void)
88e17b57 204{
88a2722e 205 __gthread_objc_thread_yield ();
88e17b57
BE
206}
207
575584a9
NP
208/* Terminate the current tread. Doesn't return. Actually, if it
209 failed returns -1. */
88e17b57 210int
40165636 211objc_thread_exit (void)
88e17b57 212{
575584a9 213 /* Decrement our counter of the number of threads alive. */
40165636 214 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 215 __objc_runtime_threads_alive--;
40165636 216 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57 217
575584a9 218 /* Call the backend to terminate the thread. */
88a2722e 219 return __gthread_objc_thread_exit ();
88e17b57
BE
220}
221
575584a9
NP
222/* Returns an integer value which uniquely describes a thread. Must
223 not be NULL which is reserved as a marker for "no thread". */
88e17b57 224objc_thread_t
40165636 225objc_thread_id (void)
88e17b57 226{
88a2722e 227 return __gthread_objc_thread_id ();
88e17b57
BE
228}
229
575584a9
NP
230/* Sets the thread's local storage pointer. Returns 0 if successful
231 or -1 if failed. */
88e17b57 232int
40165636 233objc_thread_set_data (void *value)
88e17b57 234{
88a2722e 235 return __gthread_objc_thread_set_data (value);
88e17b57
BE
236}
237
575584a9
NP
238/* Returns the thread's local storage pointer. Returns NULL on
239 failure. */
88e17b57 240void *
40165636 241objc_thread_get_data (void)
88e17b57 242{
88a2722e 243 return __gthread_objc_thread_get_data ();
88e17b57
BE
244}
245
575584a9 246/* Public mutex functions */
88e17b57 247
575584a9
NP
248/* Allocate a mutex. Return the mutex pointer if successful or NULL
249 if the allocation failed for any reason. */
88e17b57 250objc_mutex_t
40165636 251objc_mutex_allocate (void)
88e17b57
BE
252{
253 objc_mutex_t mutex;
254
575584a9 255 /* Allocate the mutex structure. */
40165636 256 if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
88e17b57
BE
257 return NULL;
258
575584a9 259 /* Call backend to create the mutex. */
88a2722e 260 if (__gthread_objc_mutex_allocate (mutex))
88e17b57 261 {
575584a9 262 /* Failed! */
40165636 263 objc_free (mutex);
88e17b57
BE
264 return NULL;
265 }
266
575584a9 267 /* Initialize mutex. */
88e17b57
BE
268 mutex->owner = NULL;
269 mutex->depth = 0;
270 return mutex;
271}
272
575584a9
NP
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). */
88e17b57 278int
40165636 279objc_mutex_deallocate (objc_mutex_t mutex)
88e17b57
BE
280{
281 int depth;
282
575584a9 283 /* Valid mutex? */
40165636 284 if (! mutex)
88e17b57
BE
285 return -1;
286
575584a9 287 /* Acquire lock on mutex. */
40165636 288 depth = objc_mutex_lock (mutex);
88e17b57 289
575584a9 290 /* Call backend to destroy mutex. */
88a2722e 291 if (__gthread_objc_mutex_deallocate (mutex))
88e17b57
BE
292 return -1;
293
575584a9 294 /* Free the mutex structure. */
40165636 295 objc_free (mutex);
88e17b57 296
575584a9 297 /* Return last depth. */
88e17b57
BE
298 return depth;
299}
300
575584a9
NP
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. */
88e17b57 305int
40165636 306objc_mutex_lock (objc_mutex_t mutex)
88e17b57
BE
307{
308 objc_thread_t thread_id;
309 int status;
310
575584a9 311 /* Valid mutex? */
40165636 312 if (! mutex)
88e17b57
BE
313 return -1;
314
575584a9 315 /* If we already own the lock then increment depth. */
88a2722e 316 thread_id = __gthread_objc_thread_id ();
88e17b57
BE
317 if (mutex->owner == thread_id)
318 return ++mutex->depth;
319
575584a9 320 /* Call the backend to lock the mutex. */
88a2722e 321 status = __gthread_objc_mutex_lock (mutex);
88e17b57 322
575584a9 323 /* Failed? */
88e17b57
BE
324 if (status)
325 return status;
326
575584a9 327 /* Successfully locked the thread. */
88e17b57
BE
328 mutex->owner = thread_id;
329 return mutex->depth = 1;
330}
331
575584a9
NP
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. */
88e17b57 335int
40165636 336objc_mutex_trylock (objc_mutex_t mutex)
88e17b57
BE
337{
338 objc_thread_t thread_id;
339 int status;
340
575584a9 341 /* Valid mutex? */
40165636 342 if (! mutex)
88e17b57
BE
343 return -1;
344
575584a9 345 /* If we already own the lock then increment depth. */
88a2722e 346 thread_id = __gthread_objc_thread_id ();
88e17b57
BE
347 if (mutex->owner == thread_id)
348 return ++mutex->depth;
349
575584a9 350 /* Call the backend to try to lock the mutex. */
88a2722e 351 status = __gthread_objc_mutex_trylock (mutex);
88e17b57 352
575584a9 353 /* Failed? */
88e17b57
BE
354 if (status)
355 return status;
356
575584a9 357 /* Successfully locked the thread. */
88e17b57
BE
358 mutex->owner = thread_id;
359 return mutex->depth = 1;
360}
361
575584a9
NP
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. */
88e17b57 367int
40165636 368objc_mutex_unlock (objc_mutex_t mutex)
88e17b57
BE
369{
370 objc_thread_t thread_id;
371 int status;
372
575584a9 373 /* Valid mutex? */
40165636 374 if (! mutex)
88e17b57
BE
375 return -1;
376
575584a9 377 /* If another thread owns the lock then abort. */
88a2722e 378 thread_id = __gthread_objc_thread_id ();
88e17b57
BE
379 if (mutex->owner != thread_id)
380 return -1;
381
575584a9 382 /* Decrement depth and return. */
88e17b57
BE
383 if (mutex->depth > 1)
384 return --mutex->depth;
385
575584a9 386 /* Depth down to zero so we are no longer the owner. */
88e17b57
BE
387 mutex->depth = 0;
388 mutex->owner = NULL;
389
575584a9 390 /* Have the backend unlock the mutex. */
88a2722e 391 status = __gthread_objc_mutex_unlock (mutex);
88e17b57 392
575584a9 393 /* Failed? */
88e17b57
BE
394 if (status)
395 return status;
396
397 return 0;
398}
399
575584a9 400/* Public condition mutex functions */
88e17b57 401
575584a9
NP
402/* Allocate a condition. Return the condition pointer if successful
403 or NULL if the allocation failed for any reason. */
88e17b57 404objc_condition_t
40165636 405objc_condition_allocate (void)
88e17b57
BE
406{
407 objc_condition_t condition;
408
575584a9 409 /* Allocate the condition mutex structure. */
40165636
RB
410 if (! (condition =
411 (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
88e17b57
BE
412 return NULL;
413
575584a9 414 /* Call the backend to create the condition mutex. */
88a2722e 415 if (__gthread_objc_condition_allocate (condition))
88e17b57 416 {
575584a9 417 /* Failed! */
40165636 418 objc_free (condition);
88e17b57
BE
419 return NULL;
420 }
421
575584a9 422 /* Success! */
88e17b57
BE
423 return condition;
424}
425
575584a9
NP
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. */
88e17b57 431int
40165636 432objc_condition_deallocate (objc_condition_t condition)
88e17b57 433{
575584a9 434 /* Broadcast the condition. */
40165636 435 if (objc_condition_broadcast (condition))
88e17b57
BE
436 return -1;
437
575584a9 438 /* Call the backend to destroy. */
88a2722e 439 if (__gthread_objc_condition_deallocate (condition))
88e17b57
BE
440 return -1;
441
575584a9 442 /* Free the condition mutex structure. */
40165636 443 objc_free (condition);
88e17b57
BE
444
445 return 0;
446}
447
575584a9
NP
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. */
88e17b57 455int
40165636 456objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
88e17b57
BE
457{
458 objc_thread_t thread_id;
459
575584a9 460 /* Valid arguments? */
40165636 461 if (! mutex || ! condition)
88e17b57
BE
462 return -1;
463
575584a9 464 /* Make sure we are owner of mutex. */
88a2722e 465 thread_id = __gthread_objc_thread_id ();
88e17b57
BE
466 if (mutex->owner != thread_id)
467 return -1;
468
575584a9 469 /* Cannot be locked more than once. */
88e17b57
BE
470 if (mutex->depth > 1)
471 return -1;
472
575584a9 473 /* Virtually unlock the mutex. */
88e17b57
BE
474 mutex->depth = 0;
475 mutex->owner = (objc_thread_t)NULL;
476
575584a9 477 /* Call the backend to wait. */
88a2722e 478 __gthread_objc_condition_wait (condition, mutex);
88e17b57 479
575584a9 480 /* Make ourselves owner of the mutex. */
88e17b57
BE
481 mutex->owner = thread_id;
482 mutex->depth = 1;
483
484 return 0;
485}
486
575584a9
NP
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. */
88e17b57 491int
40165636 492objc_condition_broadcast (objc_condition_t condition)
88e17b57 493{
575584a9 494 /* Valid condition mutex? */
40165636 495 if (! condition)
88e17b57
BE
496 return -1;
497
88a2722e 498 return __gthread_objc_condition_broadcast (condition);
88e17b57
BE
499}
500
575584a9
NP
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. */
88e17b57 505int
40165636 506objc_condition_signal (objc_condition_t condition)
88e17b57 507{
575584a9 508 /* Valid condition mutex? */
40165636 509 if (! condition)
88e17b57
BE
510 return -1;
511
88a2722e 512 return __gthread_objc_condition_signal (condition);
88e17b57
BE
513}
514
b894530e
NP
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
40165636 519 objc_thread_add () before an alien thread makes any calls to
b894530e
NP
520 Objective-C. Do not cause the _objc_became_multi_threaded hook to
521 be executed. */
522void
40165636 523objc_thread_add (void)
b894530e 524{
40165636 525 objc_mutex_lock (__objc_runtime_mutex);
b894530e
NP
526 __objc_is_multi_threaded = 1;
527 __objc_runtime_threads_alive++;
40165636 528 objc_mutex_unlock (__objc_runtime_mutex);
b894530e
NP
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
40165636 534 objc_thread_remove () when your alien thread is done with making
b894530e
NP
535 calls to Objective-C. */
536void
40165636 537objc_thread_remove (void)
b894530e 538{
40165636 539 objc_mutex_lock (__objc_runtime_mutex);
b894530e 540 __objc_runtime_threads_alive--;
40165636 541 objc_mutex_unlock (__objc_runtime_mutex);
b894530e
NP
542}
543