2000-03-27 03:00:05 +00:00
|
|
|
/* GNU Objective C Runtime Thread Interface
|
|
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
|
|
Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
|
|
|
|
|
2004-07-28 03:12:12 +00:00
|
|
|
This file is part of GCC.
|
2000-03-27 03:00:05 +00:00
|
|
|
|
2004-07-28 03:12:12 +00:00
|
|
|
GCC is free software; you can redistribute it and/or modify it under the
|
2000-03-27 03:00:05 +00:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2, or (at your option) any later version.
|
|
|
|
|
2004-07-28 03:12:12 +00:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
2000-03-27 03:00:05 +00:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2004-07-28 03:12:12 +00:00
|
|
|
GCC; see the file COPYING. If not, write to the Free Software
|
2000-03-27 03:00:05 +00:00
|
|
|
Foundation, 59 Temple Place - Suite 330,
|
|
|
|
Boston, MA 02111-1307, USA. */
|
|
|
|
|
|
|
|
/* As a special exception, if you link this library with files compiled with
|
|
|
|
GCC to produce an executable, this does not cause the resulting executable
|
|
|
|
to be covered by the GNU General Public License. This exception does not
|
|
|
|
however invalidate any other reasons why the executable file might be
|
|
|
|
covered by the GNU General Public License. */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
/* Global exit status. */
|
|
|
|
int __objc_thread_exit_status = 0;
|
|
|
|
|
|
|
|
/* Flag which lets us know if we ever became multi threaded */
|
|
|
|
int __objc_is_multi_threaded = 0;
|
|
|
|
|
|
|
|
/* The hook function called when the runtime becomes multi threaded */
|
|
|
|
objc_thread_callback _objc_became_multi_threaded = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Use this to set the hook function that will be called when the
|
|
|
|
runtime initially becomes multi threaded.
|
|
|
|
The hook function is only called once, meaning only when the
|
|
|
|
2nd thread is spawned, not for each and every thread.
|
|
|
|
|
|
|
|
It returns the previous hook function or NULL if there is none.
|
|
|
|
|
|
|
|
A program outside of the runtime could set this to some function so
|
|
|
|
it can be informed; for example, the GNUstep Base Library sets it
|
|
|
|
so it can implement the NSBecomingMultiThreaded notification.
|
|
|
|
*/
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_callback objc_set_thread_callback (objc_thread_callback func)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_thread_callback temp = _objc_became_multi_threaded;
|
|
|
|
_objc_became_multi_threaded = func;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Private functions
|
|
|
|
|
|
|
|
These functions are utilized by the frontend, but they are not
|
|
|
|
considered part of the public interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
First function called in a thread, starts everything else.
|
|
|
|
|
|
|
|
This function is passed to the backend by objc_thread_detach
|
|
|
|
as the starting function for a new thread.
|
|
|
|
*/
|
|
|
|
struct __objc_thread_start_state
|
|
|
|
{
|
|
|
|
SEL selector;
|
|
|
|
id object;
|
|
|
|
id argument;
|
|
|
|
};
|
|
|
|
|
|
|
|
static volatile void
|
2003-07-11 03:42:15 +00:00
|
|
|
__objc_thread_detach_function (struct __objc_thread_start_state *istate)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Valid state? */
|
|
|
|
if (istate) {
|
2003-07-11 03:42:15 +00:00
|
|
|
id (*imp) (id, SEL, id);
|
2000-03-27 03:00:05 +00:00
|
|
|
SEL selector = istate->selector;
|
|
|
|
id object = istate->object;
|
|
|
|
id argument = istate->argument;
|
|
|
|
|
|
|
|
/* Don't need anymore so free it */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_free (istate);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Clear out the thread local storage */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_set_data (NULL);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Check to see if we just became multi threaded */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! __objc_is_multi_threaded)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
__objc_is_multi_threaded = 1;
|
|
|
|
|
|
|
|
/* Call the hook function */
|
|
|
|
if (_objc_became_multi_threaded != NULL)
|
2003-07-11 03:42:15 +00:00
|
|
|
(*_objc_became_multi_threaded) ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the method */
|
2003-07-11 03:42:15 +00:00
|
|
|
if ((imp = (id (*) (id, SEL, id))objc_msg_lookup (object, selector)))
|
|
|
|
(*imp) (object, selector, argument);
|
2000-03-27 03:00:05 +00:00
|
|
|
else
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_error (object, OBJC_ERR_UNIMPLEMENTED,
|
|
|
|
"objc_thread_detach called with bad selector.\n");
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
else
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_error (nil, OBJC_ERR_BAD_STATE,
|
|
|
|
"objc_thread_detach called with NULL state.\n");
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Exit the thread */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_exit ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Frontend functions
|
|
|
|
|
|
|
|
These functions constitute the public interface to the Objective-C thread
|
|
|
|
and mutex functionality.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Frontend thread functions */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Detach a new thread of execution and return its id. Returns NULL if fails.
|
|
|
|
Thread is started by sending message with selector to object. Message
|
|
|
|
takes a single argument.
|
|
|
|
*/
|
|
|
|
objc_thread_t
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_detach (SEL selector, id object, id argument)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
struct __objc_thread_start_state *istate;
|
|
|
|
objc_thread_t thread_id = NULL;
|
|
|
|
|
|
|
|
/* Allocate the state structure */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! (istate = (struct __objc_thread_start_state *)
|
|
|
|
objc_malloc (sizeof (*istate))))
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Initialize the state structure */
|
|
|
|
istate->selector = selector;
|
|
|
|
istate->object = object;
|
|
|
|
istate->argument = argument;
|
|
|
|
|
|
|
|
/* lock access */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Call the backend to spawn the thread */
|
2003-07-11 03:42:15 +00:00
|
|
|
if ((thread_id = __objc_thread_detach ((void *)__objc_thread_detach_function,
|
|
|
|
istate)) == NULL)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* failed! */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
|
|
|
objc_free (istate);
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment our thread counter */
|
|
|
|
__objc_runtime_threads_alive++;
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
return thread_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the current thread's priority. */
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_set_priority (int priority)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_set_priority (priority);
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the current thread's priority. */
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_get_priority (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_get_priority ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Yield our process time to another thread. Any BUSY waiting that is done
|
|
|
|
by a thread should use this function to make sure that other threads can
|
|
|
|
make progress even on a lazy uniprocessor system.
|
|
|
|
*/
|
|
|
|
void
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_yield (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
__objc_thread_yield ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Terminate the current tread. Doesn't return.
|
|
|
|
Actually, if it failed returns -1.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_exit (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Decrement our counter of the number of threads alive */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
__objc_runtime_threads_alive--;
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Call the backend to terminate the thread */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_exit ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns an integer value which uniquely describes a thread. Must not be
|
|
|
|
NULL which is reserved as a marker for "no thread".
|
|
|
|
*/
|
|
|
|
objc_thread_t
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_id (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_id ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Sets the thread's local storage pointer.
|
|
|
|
Returns 0 if successful or -1 if failed.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_set_data (void *value)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_set_data (value);
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns the thread's local storage pointer. Returns NULL on failure.
|
|
|
|
*/
|
|
|
|
void *
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_get_data (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Call the backend */
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_thread_get_data ();
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Frontend mutex functions */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Allocate a mutex. Return the mutex pointer if successful or NULL if the
|
|
|
|
allocation failed for any reason.
|
|
|
|
*/
|
|
|
|
objc_mutex_t
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_allocate (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_mutex_t mutex;
|
|
|
|
|
|
|
|
/* Allocate the mutex structure */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! (mutex = (objc_mutex_t)objc_malloc (sizeof (struct objc_mutex))))
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Call backend to create the mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (__objc_mutex_allocate (mutex))
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* failed! */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_free (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize mutex */
|
|
|
|
mutex->owner = NULL;
|
|
|
|
mutex->depth = 0;
|
|
|
|
return mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Deallocate a mutex. Note that this includes an implicit mutex_lock to
|
|
|
|
insure that no one else is using the lock. It is legal to deallocate
|
|
|
|
a lock if we have a lock on it, but illegal to deallocate a lock held
|
|
|
|
by anyone else.
|
|
|
|
Returns the number of locks on the thread. (1 for deallocate).
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_deallocate (objc_mutex_t mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
int depth;
|
|
|
|
|
|
|
|
/* Valid mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Acquire lock on mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
depth = objc_mutex_lock (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Call backend to destroy mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (__objc_mutex_deallocate (mutex))
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Free the mutex structure */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_free (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Return last depth */
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Grab a lock on a mutex. If this thread already has a lock on this mutex
|
|
|
|
then we increment the lock count. If another thread has a lock on the
|
|
|
|
mutex we block and wait for the thread to release the lock.
|
|
|
|
Returns the lock count on the mutex held by this thread.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_lock (objc_mutex_t mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_thread_t thread_id;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Valid mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* If we already own the lock then increment depth */
|
2003-07-11 03:42:15 +00:00
|
|
|
thread_id = __objc_thread_id ();
|
2000-03-27 03:00:05 +00:00
|
|
|
if (mutex->owner == thread_id)
|
|
|
|
return ++mutex->depth;
|
|
|
|
|
|
|
|
/* Call the backend to lock the mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
status = __objc_mutex_lock (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Failed? */
|
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/* Successfully locked the thread */
|
|
|
|
mutex->owner = thread_id;
|
|
|
|
return mutex->depth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Try to grab a lock on a mutex. If this thread already has a lock on
|
|
|
|
this mutex then we increment the lock count and return it. If another
|
|
|
|
thread has a lock on the mutex returns -1.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_trylock (objc_mutex_t mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_thread_t thread_id;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Valid mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* If we already own the lock then increment depth */
|
2003-07-11 03:42:15 +00:00
|
|
|
thread_id = __objc_thread_id ();
|
2000-03-27 03:00:05 +00:00
|
|
|
if (mutex->owner == thread_id)
|
|
|
|
return ++mutex->depth;
|
|
|
|
|
|
|
|
/* Call the backend to try to lock the mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
status = __objc_mutex_trylock (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Failed? */
|
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/* Successfully locked the thread */
|
|
|
|
mutex->owner = thread_id;
|
|
|
|
return mutex->depth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Unlocks the mutex by one level.
|
|
|
|
Decrements the lock count on this mutex by one.
|
|
|
|
If the lock count reaches zero, release the lock on the mutex.
|
|
|
|
Returns the lock count on the mutex.
|
|
|
|
It is an error to attempt to unlock a mutex which this thread
|
|
|
|
doesn't hold in which case return -1 and the mutex is unaffected.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (objc_mutex_t mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_thread_t thread_id;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Valid mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* If another thread owns the lock then abort */
|
2003-07-11 03:42:15 +00:00
|
|
|
thread_id = __objc_thread_id ();
|
2000-03-27 03:00:05 +00:00
|
|
|
if (mutex->owner != thread_id)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Decrement depth and return */
|
|
|
|
if (mutex->depth > 1)
|
|
|
|
return --mutex->depth;
|
|
|
|
|
|
|
|
/* Depth down to zero so we are no longer the owner */
|
|
|
|
mutex->depth = 0;
|
|
|
|
mutex->owner = NULL;
|
|
|
|
|
|
|
|
/* Have the backend unlock the mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
status = __objc_mutex_unlock (mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Failed? */
|
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Frontend condition mutex functions */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Allocate a condition. Return the condition pointer if successful or NULL
|
|
|
|
if the allocation failed for any reason.
|
|
|
|
*/
|
|
|
|
objc_condition_t
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_condition_allocate (void)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_condition_t condition;
|
|
|
|
|
|
|
|
/* Allocate the condition mutex structure */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! (condition =
|
|
|
|
(objc_condition_t) objc_malloc (sizeof (struct objc_condition))))
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Call the backend to create the condition mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (__objc_condition_allocate (condition))
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* failed! */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_free (condition);
|
2000-03-27 03:00:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Deallocate a condition. Note that this includes an implicit
|
|
|
|
condition_broadcast to insure that waiting threads have the opportunity
|
|
|
|
to wake. It is legal to dealloc a condition only if no other
|
|
|
|
thread is/will be using it. Here we do NOT check for other threads
|
|
|
|
waiting but just wake them up.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_condition_deallocate (objc_condition_t condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Broadcast the condition */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (objc_condition_broadcast (condition))
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Call the backend to destroy */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (__objc_condition_deallocate (condition))
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Free the condition mutex structure */
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_free (condition);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-07-11 03:42:15 +00:00
|
|
|
Wait on the condition unlocking the mutex until objc_condition_signal ()
|
|
|
|
or objc_condition_broadcast () are called for the same condition. The
|
2000-03-27 03:00:05 +00:00
|
|
|
given mutex *must* have the depth set to 1 so that it can be unlocked
|
|
|
|
here, so that someone else can lock it and signal/broadcast the condition.
|
|
|
|
The mutex is used to lock access to the shared data that make up the
|
|
|
|
"condition" predicate.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
objc_thread_t thread_id;
|
|
|
|
|
|
|
|
/* Valid arguments? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! mutex || ! condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Make sure we are owner of mutex */
|
2003-07-11 03:42:15 +00:00
|
|
|
thread_id = __objc_thread_id ();
|
2000-03-27 03:00:05 +00:00
|
|
|
if (mutex->owner != thread_id)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Cannot be locked more than once */
|
|
|
|
if (mutex->depth > 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Virtually unlock the mutex */
|
|
|
|
mutex->depth = 0;
|
|
|
|
mutex->owner = (objc_thread_t)NULL;
|
|
|
|
|
|
|
|
/* Call the backend to wait */
|
2003-07-11 03:42:15 +00:00
|
|
|
__objc_condition_wait (condition, mutex);
|
2000-03-27 03:00:05 +00:00
|
|
|
|
|
|
|
/* Make ourselves owner of the mutex */
|
|
|
|
mutex->owner = thread_id;
|
|
|
|
mutex->depth = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wake up all threads waiting on this condition. It is recommended that
|
|
|
|
the called would lock the same mutex as the threads in objc_condition_wait
|
|
|
|
before changing the "condition predicate" and make this call and unlock it
|
|
|
|
right away after this call.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_condition_broadcast (objc_condition_t condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Valid condition mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_condition_broadcast (condition);
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Wake up one thread waiting on this condition. It is recommended that
|
|
|
|
the called would lock the same mutex as the threads in objc_condition_wait
|
|
|
|
before changing the "condition predicate" and make this call and unlock it
|
|
|
|
right away after this call.
|
|
|
|
*/
|
|
|
|
int
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_condition_signal (objc_condition_t condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
{
|
|
|
|
/* Valid condition mutex? */
|
2003-07-11 03:42:15 +00:00
|
|
|
if (! condition)
|
2000-03-27 03:00:05 +00:00
|
|
|
return -1;
|
|
|
|
|
2003-07-11 03:42:15 +00:00
|
|
|
return __objc_condition_signal (condition);
|
2000-03-27 03:00:05 +00:00
|
|
|
}
|
|
|
|
|
2002-05-09 22:50:04 +00:00
|
|
|
/* Make the objc thread system aware that a thread which is managed
|
|
|
|
(started, stopped) by external code could access objc facilities
|
|
|
|
from now on. This is used when you are interfacing with some
|
|
|
|
external non-objc-based environment/system - you must call
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_add () before an alien thread makes any calls to
|
2002-05-09 22:50:04 +00:00
|
|
|
Objective-C. Do not cause the _objc_became_multi_threaded hook to
|
|
|
|
be executed. */
|
|
|
|
void
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_add (void)
|
2002-05-09 22:50:04 +00:00
|
|
|
{
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2002-05-09 22:50:04 +00:00
|
|
|
__objc_is_multi_threaded = 1;
|
|
|
|
__objc_runtime_threads_alive++;
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
2002-05-09 22:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the objc thread system aware that a thread managed (started,
|
|
|
|
stopped) by some external code will no longer access objc and thus
|
|
|
|
can be forgotten by the objc thread system. Call
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_remove () when your alien thread is done with making
|
2002-05-09 22:50:04 +00:00
|
|
|
calls to Objective-C. */
|
|
|
|
void
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_thread_remove (void)
|
2002-05-09 22:50:04 +00:00
|
|
|
{
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_lock (__objc_runtime_mutex);
|
2002-05-09 22:50:04 +00:00
|
|
|
__objc_runtime_threads_alive--;
|
2003-07-11 03:42:15 +00:00
|
|
|
objc_mutex_unlock (__objc_runtime_mutex);
|
2002-05-09 22:50:04 +00:00
|
|
|
}
|
|
|
|
|
2000-03-27 03:00:05 +00:00
|
|
|
/* End of File */
|