/* GNU Objective C Runtime Thread Interface
- Copyright (C) 1996 Free Software Foundation, Inc.
+ Copyright (C) 1996, 1997 Free Software Foundation, Inc.
Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
-
+ Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
+
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
* provided by the system. We augment it with depth and current owner id
* fields to implement and re-entrant lock.
*/
-struct _objc_mutex
+struct objc_mutex
{
- volatile _objc_thread_t owner; /* Id of thread that owns. */
+ volatile objc_thread_t owner; /* Id of thread that owns. */
volatile int depth; /* # of acquires. */
mutex_t lock; /* System mutex. */
};
+struct objc_condition
+{
+ cond_t condition; /* solaris condition */
+};
+
/*****************************************************************************
* Static variables.
*/
* Create a new thread of execution and return its id. Return -1 if fails.
* The new thread starts in "func" with the given argument.
*/
-_objc_thread_t
+objc_thread_t
objc_thread_create(void (*func)(void *arg), void *arg)
{
- _objc_thread_t thread_id = NULL; /* Detached thread id. */
+ objc_thread_t thread_id = NULL; /* Detached thread id. */
thread_t new_thread_id = 0; /* Solaris thread id type. */
int errn;
if (thr_create(NULL, 0, (void *)func, arg,
THR_DETACHED | THR_NEW_LWP,
&new_thread_id) == 0) { /* Created new thread? */
- thread_id = (_objc_thread_t)new_thread_id; /* Yes, remember its id. */
+ thread_id = (objc_thread_t)new_thread_id; /* Yes, remember its id. */
__objc_runtime_threads_alive++;
}
* 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
+objc_thread_t
objc_thread_id(void)
{
- return (_objc_thread_t)thr_self();
+ return (objc_thread_t)thr_self();
}
/********
* Allocate a mutex. Return the mutex pointer if successful or NULL if
* the allocation fails for any reason.
*/
-_objc_mutex_t
+objc_mutex_t
objc_mutex_allocate(void)
{
- struct _objc_mutex *mutex;
+ struct objc_mutex *mutex;
int err = 0;
- if (!(mutex = (_objc_mutex_t) objc_malloc(sizeof(struct _objc_mutex))))
+ if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
return NULL; /* Abort if malloc failed. */
err = mutex_init(&mutex->lock, USYNC_THREAD, 0);
* Returns the number of locks on the thread. (1 for deallocate).
*/
int
-objc_mutex_deallocate(_objc_mutex_t mutex)
+objc_mutex_deallocate(objc_mutex_t mutex)
{
int depth; /* # of locks on mutex. */
* Returns the lock count on the mutex held by this thread.
*/
int
-objc_mutex_lock(_objc_mutex_t mutex)
+objc_mutex_lock(objc_mutex_t mutex)
{
- _objc_thread_t thread_id; /* Cache our thread id. */
+ objc_thread_t thread_id; /* Cache our thread id. */
if (!mutex) /* Is argument bad? */
return -1; /* Yes, abort. */
* thread has a lock on the mutex returns -1.
*/
int
-objc_mutex_trylock(_objc_mutex_t mutex)
+objc_mutex_trylock(objc_mutex_t mutex)
{
- _objc_thread_t thread_id; /* Cache our thread id. */
+ objc_thread_t thread_id; /* Cache our thread id. */
if (!mutex) /* Is argument bad? */
return -1; /* Yes, abort. */
* Will also return -1 if the mutex free fails.
*/
int
-objc_mutex_unlock(_objc_mutex_t mutex)
+objc_mutex_unlock(objc_mutex_t mutex)
{
- _objc_thread_t thread_id; /* Cache our thread id. */
+ objc_thread_t thread_id; /* Cache our thread id. */
if (!mutex) /* Is argument bad? */
return -1; /* Yes, abort. */
return 0; /* No, return success. */
}
+/********
+ * Allocate a condition. Return the condition pointer if successful or NULL
+ * if the allocation failed for any reason.
+ */
+objc_condition_t
+objc_condition_allocate(void)
+{
+ objc_condition_t condition;
+
+ if (!(condition = (objc_condition_t)objc_malloc(
+ sizeof(struct objc_condition))))
+ return NULL; /* Abort if malloc failed. */
+
+ cond_init(&(condition->condition), USYNC_THREAD, NULL);
+
+ return condition; /* Return new 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
+objc_condition_deallocate(objc_condition_t condition)
+{
+ cond_broadcast(&(condition->condition)); /* Wakeup waiting threads */
+ cond_destroy(&(condition->condition)); /* Kill condition */
+ objc_free(condition); /* Release struct memory */
+ return 0;
+}
+
+/********
+ * Wait on the condition unlocking the mutex until objc_condition_signal()
+ * or objc_condition_broadcast() are called for the same condition. The
+ * 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
+objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
+{
+ objc_thread_t thread_id; /* Cache our thread id. */
+
+ if (!mutex || !condition) /* Is argument bad? */
+ return -1; /* Yes, abort. */
+
+ thread_id = objc_thread_id(); /* Get this thread's id. */
+ if (mutex->owner != thread_id) /* Does some else own lock? */
+ return -1; /* Yes, abort. */
+ if (mutex->depth > 1) /* Locked more than once ? */
+ return -1; /* YES, return error */
+ /* mutex will be unlocked */
+ mutex->depth = 0; /* Yes, reset depth to 0. */
+ mutex->owner = (objc_thread_t) -1; /* Set owner to "no thread".*/
+
+ cond_wait(&(condition->condition),
+ &(mutex->lock)); /* unlock, wait ..., lock */
+
+ mutex->owner = thread_id; /* Mark thread as owner. */
+ mutex->depth = 1; /* Must be here ! */
+
+ return 0; /* Return success. */
+}
+
+/********
+ * 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
+objc_condition_broadcast(objc_condition_t condition)
+{
+ if (!condition)
+ return -1;
+ cond_broadcast(&(condition->condition));
+ return 0;
+}
+
+/********
+ * 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
+objc_condition_signal(objc_condition_t condition)
+{
+ if (!condition)
+ return -1;
+ cond_signal(&(condition->condition));
+ return 0;
+}
+
/* End of File */