pr65270-1.c: Add -fno-common to dg-options on hppa*-*-hpux*.
[official-gcc.git] / libobjc / thr.c
blob4b16d4b059c43d91dd473892b883e3cddba94469
1 /* GNU Objective C Runtime Thread Interface
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3, or (at your option) any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "objc-private/common.h"
26 #include "objc-private/error.h"
27 #define _LIBOBJC
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"
34 #include "objc/message.h" /* For objc_msg_lookup(). */
35 #include "objc/runtime.h"
36 #include "objc-private/module-abi-8.h"
37 #include "objc-private/runtime.h"
38 #include <gthr.h>
40 #include <stdlib.h>
42 /* Global exit status. */
43 int __objc_thread_exit_status = 0;
45 /* Flag which lets us know if we ever became multi threaded. */
46 int __objc_is_multi_threaded = 0;
48 /* The hook function called when the runtime becomes multi
49 threaded. */
50 objc_thread_callback _objc_became_multi_threaded = NULL;
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.
57 It returns the previous hook function or NULL if there is none.
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. */
62 objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
64 objc_thread_callback temp = _objc_became_multi_threaded;
65 _objc_became_multi_threaded = func;
66 return temp;
69 /* Private functions.
71 These functions are utilized by the runtime, but they are not
72 considered part of the public interface. */
74 /* Initialize the threads subsystem. */
75 int
76 __objc_init_thread_system(void)
78 return __gthread_objc_init_thread_system ();
81 /* First function called in a thread, starts everything else.
83 This function is passed to the backend by objc_thread_detach as the
84 starting function for a new thread. */
85 struct __objc_thread_start_state
87 SEL selector;
88 id object;
89 id argument;
92 static void __attribute__((noreturn))
93 __objc_thread_detach_function (struct __objc_thread_start_state *istate)
95 /* Valid state? */
96 if (istate)
98 id (*imp) (id, SEL, id);
99 SEL selector = istate->selector;
100 id object = istate->object;
101 id argument = istate->argument;
103 /* Don't need anymore so free it. */
104 objc_free (istate);
106 /* Clear out the thread local storage. */
107 objc_thread_set_data (NULL);
109 /* Check to see if we just became multi threaded. */
110 if (! __objc_is_multi_threaded)
112 __objc_is_multi_threaded = 1;
114 /* Call the hook function. */
115 if (_objc_became_multi_threaded != NULL)
116 (*_objc_became_multi_threaded) ();
119 /* Call the method. */
120 if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
121 (*imp) (object, selector, argument);
122 else
124 /* FIXME: Should we abort here ? */
125 _objc_abort ("objc_thread_detach called with bad selector.\n");
128 else
130 /* FIXME: Should we abort here ? */
131 _objc_abort ("objc_thread_detach called with NULL state.\n");
134 /* Exit the thread. */
135 objc_thread_exit ();
137 /* Make sure compiler detects no return. */
138 __builtin_trap ();
141 /* Public functions.
143 These functions constitute the public interface to the Objective-C
144 thread and mutex functionality. */
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. */
149 objc_thread_t
150 objc_thread_detach (SEL selector, id object, id argument)
152 struct __objc_thread_start_state *istate;
153 objc_thread_t thread_id = NULL;
155 /* Allocate the state structure. */
156 if (!(istate = (struct __objc_thread_start_state *)objc_malloc
157 (sizeof (*istate))))
158 return NULL;
160 /* Initialize the state structure. */
161 istate->selector = selector;
162 istate->object = object;
163 istate->argument = argument;
165 /* Lock access. */
166 objc_mutex_lock (__objc_runtime_mutex);
168 /* Call the backend to spawn the thread. */
169 if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
170 istate)) == NULL)
172 /* Failed! */
173 objc_mutex_unlock (__objc_runtime_mutex);
174 objc_free (istate);
175 return NULL;
178 /* Increment our thread counter. */
179 __objc_runtime_threads_alive++;
180 objc_mutex_unlock (__objc_runtime_mutex);
182 return thread_id;
185 /* Set the current thread's priority. */
187 objc_thread_set_priority (int priority)
189 return __gthread_objc_thread_set_priority (priority);
192 /* Return the current thread's priority. */
194 objc_thread_get_priority (void)
196 return __gthread_objc_thread_get_priority ();
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. */
202 void
203 objc_thread_yield (void)
205 __gthread_objc_thread_yield ();
208 /* Terminate the current tread. Doesn't return. Actually, if it
209 failed returns -1. */
211 objc_thread_exit (void)
213 /* Decrement our counter of the number of threads alive. */
214 objc_mutex_lock (__objc_runtime_mutex);
215 __objc_runtime_threads_alive--;
216 objc_mutex_unlock (__objc_runtime_mutex);
218 /* Call the backend to terminate the thread. */
219 return __gthread_objc_thread_exit ();
222 /* Returns an integer value which uniquely describes a thread. Must
223 not be NULL which is reserved as a marker for "no thread". */
224 objc_thread_t
225 objc_thread_id (void)
227 return __gthread_objc_thread_id ();
230 /* Sets the thread's local storage pointer. Returns 0 if successful
231 or -1 if failed. */
233 objc_thread_set_data (void *value)
235 return __gthread_objc_thread_set_data (value);
238 /* Returns the thread's local storage pointer. Returns NULL on
239 failure. */
240 void *
241 objc_thread_get_data (void)
243 return __gthread_objc_thread_get_data ();
246 /* Public mutex functions */
248 /* Allocate a mutex. Return the mutex pointer if successful or NULL
249 if the allocation failed for any reason. */
250 objc_mutex_t
251 objc_mutex_allocate (void)
253 objc_mutex_t mutex;
255 /* Allocate the mutex structure. */
256 if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
257 return NULL;
259 /* Call backend to create the mutex. */
260 if (__gthread_objc_mutex_allocate (mutex))
262 /* Failed! */
263 objc_free (mutex);
264 return NULL;
267 /* Initialize mutex. */
268 mutex->owner = NULL;
269 mutex->depth = 0;
270 return mutex;
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). */
279 objc_mutex_deallocate (objc_mutex_t mutex)
281 int depth;
283 /* Valid mutex? */
284 if (! mutex)
285 return -1;
287 /* Acquire lock on mutex. */
288 depth = objc_mutex_lock (mutex);
290 /* Call backend to destroy mutex. */
291 if (__gthread_objc_mutex_deallocate (mutex))
292 return -1;
294 /* Free the mutex structure. */
295 objc_free (mutex);
297 /* Return last depth. */
298 return depth;
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. */
306 objc_mutex_lock (objc_mutex_t mutex)
308 objc_thread_t thread_id;
309 int status;
311 /* Valid mutex? */
312 if (! mutex)
313 return -1;
315 /* If we already own the lock then increment depth. */
316 thread_id = __gthread_objc_thread_id ();
317 if (mutex->owner == thread_id)
318 return ++mutex->depth;
320 /* Call the backend to lock the mutex. */
321 status = __gthread_objc_mutex_lock (mutex);
323 /* Failed? */
324 if (status)
325 return status;
327 /* Successfully locked the thread. */
328 mutex->owner = thread_id;
329 return mutex->depth = 1;
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. */
336 objc_mutex_trylock (objc_mutex_t mutex)
338 objc_thread_t thread_id;
339 int status;
341 /* Valid mutex? */
342 if (! mutex)
343 return -1;
345 /* If we already own the lock then increment depth. */
346 thread_id = __gthread_objc_thread_id ();
347 if (mutex->owner == thread_id)
348 return ++mutex->depth;
350 /* Call the backend to try to lock the mutex. */
351 status = __gthread_objc_mutex_trylock (mutex);
353 /* Failed? */
354 if (status)
355 return status;
357 /* Successfully locked the thread. */
358 mutex->owner = thread_id;
359 return mutex->depth = 1;
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. */
368 objc_mutex_unlock (objc_mutex_t mutex)
370 objc_thread_t thread_id;
371 int status;
373 /* Valid mutex? */
374 if (! mutex)
375 return -1;
377 /* If another thread owns the lock then abort. */
378 thread_id = __gthread_objc_thread_id ();
379 if (mutex->owner != thread_id)
380 return -1;
382 /* Decrement depth and return. */
383 if (mutex->depth > 1)
384 return --mutex->depth;
386 /* Depth down to zero so we are no longer the owner. */
387 mutex->depth = 0;
388 mutex->owner = NULL;
390 /* Have the backend unlock the mutex. */
391 status = __gthread_objc_mutex_unlock (mutex);
393 /* Failed? */
394 if (status)
395 return status;
397 return 0;
400 /* Public condition mutex functions */
402 /* Allocate a condition. Return the condition pointer if successful
403 or NULL if the allocation failed for any reason. */
404 objc_condition_t
405 objc_condition_allocate (void)
407 objc_condition_t condition;
409 /* Allocate the condition mutex structure. */
410 if (! (condition =
411 (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
412 return NULL;
414 /* Call the backend to create the condition mutex. */
415 if (__gthread_objc_condition_allocate (condition))
417 /* Failed! */
418 objc_free (condition);
419 return NULL;
422 /* Success! */
423 return condition;
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. */
432 objc_condition_deallocate (objc_condition_t condition)
434 /* Broadcast the condition. */
435 if (objc_condition_broadcast (condition))
436 return -1;
438 /* Call the backend to destroy. */
439 if (__gthread_objc_condition_deallocate (condition))
440 return -1;
442 /* Free the condition mutex structure. */
443 objc_free (condition);
445 return 0;
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. */
456 objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
458 objc_thread_t thread_id;
460 /* Valid arguments? */
461 if (! mutex || ! condition)
462 return -1;
464 /* Make sure we are owner of mutex. */
465 thread_id = __gthread_objc_thread_id ();
466 if (mutex->owner != thread_id)
467 return -1;
469 /* Cannot be locked more than once. */
470 if (mutex->depth > 1)
471 return -1;
473 /* Virtually unlock the mutex. */
474 mutex->depth = 0;
475 mutex->owner = (objc_thread_t)NULL;
477 /* Call the backend to wait. */
478 __gthread_objc_condition_wait (condition, mutex);
480 /* Make ourselves owner of the mutex. */
481 mutex->owner = thread_id;
482 mutex->depth = 1;
484 return 0;
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. */
492 objc_condition_broadcast (objc_condition_t condition)
494 /* Valid condition mutex? */
495 if (! condition)
496 return -1;
498 return __gthread_objc_condition_broadcast (condition);
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. */
506 objc_condition_signal (objc_condition_t condition)
508 /* Valid condition mutex? */
509 if (! condition)
510 return -1;
512 return __gthread_objc_condition_signal (condition);
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
519 objc_thread_add () before an alien thread makes any calls to
520 Objective-C. Do not cause the _objc_became_multi_threaded hook to
521 be executed. */
522 void
523 objc_thread_add (void)
525 objc_mutex_lock (__objc_runtime_mutex);
526 __objc_is_multi_threaded = 1;
527 __objc_runtime_threads_alive++;
528 objc_mutex_unlock (__objc_runtime_mutex);
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
534 objc_thread_remove () when your alien thread is done with making
535 calls to Objective-C. */
536 void
537 objc_thread_remove (void)
539 objc_mutex_lock (__objc_runtime_mutex);
540 __objc_runtime_threads_alive--;
541 objc_mutex_unlock (__objc_runtime_mutex);