1 /* GNU Objective C Runtime Thread Interface
2 Copyright (C) 1996, 1997, 2009, 2010 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
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/>. */
26 /* The line below is needed for declarations of functions such as
27 pthread_mutexattr_settype, without which gthr-posix.h may fail to
28 compile within libobjc. Unfortunately, this breaks compilation on
29 Tru64 UNIX V4.0F, so disable it there. */
31 #define _XOPEN_SOURCE 500
35 #include "coretypes.h"
39 #include "objc/objc.h"
40 #include "objc/objc-api.h"
41 #include "objc-private/runtime.h"
46 /* Global exit status. */
47 int __objc_thread_exit_status = 0;
49 /* Flag which lets us know if we ever became multi threaded */
50 int __objc_is_multi_threaded = 0;
52 /* The hook function called when the runtime becomes multi threaded */
53 objc_thread_callback _objc_became_multi_threaded = NULL;
56 Use this to set the hook function that will be called when the
57 runtime initially becomes multi threaded.
58 The hook function is only called once, meaning only when the
59 2nd thread is spawned, not for each and every thread.
61 It returns the previous hook function or NULL if there is none.
63 A program outside of the runtime could set this to some function so
64 it can be informed; for example, the GNUstep Base Library sets it
65 so it can implement the NSBecomingMultiThreaded notification.
67 objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
69 objc_thread_callback temp = _objc_became_multi_threaded;
70 _objc_became_multi_threaded = func;
77 These functions are utilized by the frontend, but they are not
78 considered part of the public interface.
81 /* Initialize the threads subsystem. */
83 __objc_init_thread_system(void)
85 return __gthread_objc_init_thread_system ();
89 First function called in a thread, starts everything else.
91 This function is passed to the backend by objc_thread_detach
92 as the starting function for a new thread.
94 struct __objc_thread_start_state
101 static void __attribute__((noreturn))
102 __objc_thread_detach_function (struct __objc_thread_start_state *istate)
106 id (*imp) (id, SEL, id);
107 SEL selector = istate->selector;
108 id object = istate->object;
109 id argument = istate->argument;
111 /* Don't need anymore so free it */
114 /* Clear out the thread local storage */
115 objc_thread_set_data (NULL);
117 /* Check to see if we just became multi threaded */
118 if (! __objc_is_multi_threaded)
120 __objc_is_multi_threaded = 1;
122 /* Call the hook function */
123 if (_objc_became_multi_threaded != NULL)
124 (*_objc_became_multi_threaded) ();
127 /* Call the method */
128 if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
129 (*imp) (object, selector, argument);
131 objc_error (object, OBJC_ERR_UNIMPLEMENTED,
132 "objc_thread_detach called with bad selector.\n");
135 objc_error (nil, OBJC_ERR_BAD_STATE,
136 "objc_thread_detach called with NULL state.\n");
138 /* Exit the thread */
141 /* Make sure compiler detects no return. */
148 These functions constitute the public interface to the Objective-C thread
149 and mutex functionality.
152 /* Frontend thread functions */
155 Detach a new thread of execution and return its id. Returns NULL if fails.
156 Thread is started by sending message with selector to object. Message
157 takes a single argument.
160 objc_thread_detach (SEL selector, id object, id argument)
162 struct __objc_thread_start_state *istate;
163 objc_thread_t thread_id = NULL;
165 /* Allocate the state structure */
166 if (! (istate = (struct __objc_thread_start_state *)
167 objc_malloc (sizeof (*istate))))
170 /* Initialize the state structure */
171 istate->selector = selector;
172 istate->object = object;
173 istate->argument = argument;
176 objc_mutex_lock (__objc_runtime_mutex);
178 /* Call the backend to spawn the thread */
179 if ((thread_id = __gthread_objc_thread_detach ((void *)__objc_thread_detach_function,
183 objc_mutex_unlock (__objc_runtime_mutex);
188 /* Increment our thread counter */
189 __objc_runtime_threads_alive++;
190 objc_mutex_unlock (__objc_runtime_mutex);
195 /* Set the current thread's priority. */
197 objc_thread_set_priority (int priority)
199 /* Call the backend */
200 return __gthread_objc_thread_set_priority (priority);
203 /* Return the current thread's priority. */
205 objc_thread_get_priority (void)
207 /* Call the backend */
208 return __gthread_objc_thread_get_priority ();
212 Yield our process time to another thread. Any BUSY waiting that is done
213 by a thread should use this function to make sure that other threads can
214 make progress even on a lazy uniprocessor system.
217 objc_thread_yield (void)
219 /* Call the backend */
220 __gthread_objc_thread_yield ();
224 Terminate the current tread. Doesn't return.
225 Actually, if it failed returns -1.
228 objc_thread_exit (void)
230 /* Decrement our counter of the number of threads alive */
231 objc_mutex_lock (__objc_runtime_mutex);
232 __objc_runtime_threads_alive--;
233 objc_mutex_unlock (__objc_runtime_mutex);
235 /* Call the backend to terminate the thread */
236 return __gthread_objc_thread_exit ();
240 Returns an integer value which uniquely describes a thread. Must not be
241 NULL which is reserved as a marker for "no thread".
244 objc_thread_id (void)
246 /* Call the backend */
247 return __gthread_objc_thread_id ();
251 Sets the thread's local storage pointer.
252 Returns 0 if successful or -1 if failed.
255 objc_thread_set_data (void *value)
257 /* Call the backend */
258 return __gthread_objc_thread_set_data (value);
262 Returns the thread's local storage pointer. Returns NULL on failure.
265 objc_thread_get_data (void)
267 /* Call the backend */
268 return __gthread_objc_thread_get_data ();
271 /* Frontend mutex functions */
274 Allocate a mutex. Return the mutex pointer if successful or NULL if the
275 allocation failed for any reason.
278 objc_mutex_allocate (void)
282 /* Allocate the mutex structure */
283 if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
286 /* Call backend to create the mutex */
287 if (__gthread_objc_mutex_allocate (mutex))
294 /* Initialize mutex */
301 Deallocate a mutex. Note that this includes an implicit mutex_lock to
302 insure that no one else is using the lock. It is legal to deallocate
303 a lock if we have a lock on it, but illegal to deallocate a lock held
305 Returns the number of locks on the thread. (1 for deallocate).
308 objc_mutex_deallocate (objc_mutex_t mutex)
316 /* Acquire lock on mutex */
317 depth = objc_mutex_lock (mutex);
319 /* Call backend to destroy mutex */
320 if (__gthread_objc_mutex_deallocate (mutex))
323 /* Free the mutex structure */
326 /* Return last depth */
331 Grab a lock on a mutex. If this thread already has a lock on this mutex
332 then we increment the lock count. If another thread has a lock on the
333 mutex we block and wait for the thread to release the lock.
334 Returns the lock count on the mutex held by this thread.
337 objc_mutex_lock (objc_mutex_t mutex)
339 objc_thread_t thread_id;
346 /* If we already own the lock then increment depth */
347 thread_id = __gthread_objc_thread_id ();
348 if (mutex->owner == thread_id)
349 return ++mutex->depth;
351 /* Call the backend to lock the mutex */
352 status = __gthread_objc_mutex_lock (mutex);
358 /* Successfully locked the thread */
359 mutex->owner = thread_id;
360 return mutex->depth = 1;
364 Try to grab a lock on a mutex. If this thread already has a lock on
365 this mutex then we increment the lock count and return it. If another
366 thread has a lock on the mutex returns -1.
369 objc_mutex_trylock (objc_mutex_t mutex)
371 objc_thread_t thread_id;
378 /* If we already own the lock then increment depth */
379 thread_id = __gthread_objc_thread_id ();
380 if (mutex->owner == thread_id)
381 return ++mutex->depth;
383 /* Call the backend to try to lock the mutex */
384 status = __gthread_objc_mutex_trylock (mutex);
390 /* Successfully locked the thread */
391 mutex->owner = thread_id;
392 return mutex->depth = 1;
396 Unlocks the mutex by one level.
397 Decrements the lock count on this mutex by one.
398 If the lock count reaches zero, release the lock on the mutex.
399 Returns the lock count on the mutex.
400 It is an error to attempt to unlock a mutex which this thread
401 doesn't hold in which case return -1 and the mutex is unaffected.
404 objc_mutex_unlock (objc_mutex_t mutex)
406 objc_thread_t thread_id;
413 /* If another thread owns the lock then abort */
414 thread_id = __gthread_objc_thread_id ();
415 if (mutex->owner != thread_id)
418 /* Decrement depth and return */
419 if (mutex->depth > 1)
420 return --mutex->depth;
422 /* Depth down to zero so we are no longer the owner */
426 /* Have the backend unlock the mutex */
427 status = __gthread_objc_mutex_unlock (mutex);
436 /* Frontend condition mutex functions */
439 Allocate a condition. Return the condition pointer if successful or NULL
440 if the allocation failed for any reason.
443 objc_condition_allocate (void)
445 objc_condition_t condition;
447 /* Allocate the condition mutex structure */
449 (objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
452 /* Call the backend to create the condition mutex */
453 if (__gthread_objc_condition_allocate (condition))
456 objc_free (condition);
465 Deallocate a condition. Note that this includes an implicit
466 condition_broadcast to insure that waiting threads have the opportunity
467 to wake. It is legal to dealloc a condition only if no other
468 thread is/will be using it. Here we do NOT check for other threads
469 waiting but just wake them up.
472 objc_condition_deallocate (objc_condition_t condition)
474 /* Broadcast the condition */
475 if (objc_condition_broadcast (condition))
478 /* Call the backend to destroy */
479 if (__gthread_objc_condition_deallocate (condition))
482 /* Free the condition mutex structure */
483 objc_free (condition);
489 Wait on the condition unlocking the mutex until objc_condition_signal ()
490 or objc_condition_broadcast () are called for the same condition. The
491 given mutex *must* have the depth set to 1 so that it can be unlocked
492 here, so that someone else can lock it and signal/broadcast the condition.
493 The mutex is used to lock access to the shared data that make up the
494 "condition" predicate.
497 objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
499 objc_thread_t thread_id;
501 /* Valid arguments? */
502 if (! mutex || ! condition)
505 /* Make sure we are owner of mutex */
506 thread_id = __gthread_objc_thread_id ();
507 if (mutex->owner != thread_id)
510 /* Cannot be locked more than once */
511 if (mutex->depth > 1)
514 /* Virtually unlock the mutex */
516 mutex->owner = (objc_thread_t)NULL;
518 /* Call the backend to wait */
519 __gthread_objc_condition_wait (condition, mutex);
521 /* Make ourselves owner of the mutex */
522 mutex->owner = thread_id;
529 Wake up all threads waiting on this condition. It is recommended that
530 the called would lock the same mutex as the threads in objc_condition_wait
531 before changing the "condition predicate" and make this call and unlock it
532 right away after this call.
535 objc_condition_broadcast (objc_condition_t condition)
537 /* Valid condition mutex? */
541 return __gthread_objc_condition_broadcast (condition);
545 Wake up one thread waiting on this condition. It is recommended that
546 the called would lock the same mutex as the threads in objc_condition_wait
547 before changing the "condition predicate" and make this call and unlock it
548 right away after this call.
551 objc_condition_signal (objc_condition_t condition)
553 /* Valid condition mutex? */
557 return __gthread_objc_condition_signal (condition);
560 /* Make the objc thread system aware that a thread which is managed
561 (started, stopped) by external code could access objc facilities
562 from now on. This is used when you are interfacing with some
563 external non-objc-based environment/system - you must call
564 objc_thread_add () before an alien thread makes any calls to
565 Objective-C. Do not cause the _objc_became_multi_threaded hook to
568 objc_thread_add (void)
570 objc_mutex_lock (__objc_runtime_mutex);
571 __objc_is_multi_threaded = 1;
572 __objc_runtime_threads_alive++;
573 objc_mutex_unlock (__objc_runtime_mutex);
576 /* Make the objc thread system aware that a thread managed (started,
577 stopped) by some external code will no longer access objc and thus
578 can be forgotten by the objc thread system. Call
579 objc_thread_remove () when your alien thread is done with making
580 calls to Objective-C. */
582 objc_thread_remove (void)
584 objc_mutex_lock (__objc_runtime_mutex);
585 __objc_runtime_threads_alive--;
586 objc_mutex_unlock (__objc_runtime_mutex);