1 /* GNU Objective C Runtime Thread Interface
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4 Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
12 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 You should have received a copy of the GNU General Public License along with
18 GNU CC; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* As a special exception, if you link this library with files compiled with
23 GCC to produce an executable, this does not cause the resulting executable
24 to be covered by the GNU General Public License. This exception does not
25 however invalidate any other reasons why the executable file might be
26 covered by the GNU General Public License. */
36 * This structure represents a single mutual exclusion lock. Lock semantics
37 * are detailed with the subsequent functions. We use whatever lock is
38 * provided by the system. We augment it with depth and current owner id
39 * fields to implement and re-entrant lock.
43 volatile objc_thread_t owner; /* Id of thread that owns. */
44 volatile int depth; /* # of acquires. */
45 mutex_t lock; /* System mutex. */
50 cond_t condition; /* solaris condition */
53 /*****************************************************************************
56 static thread_key_t __objc_thread_data_key; /* Data key for thread data.*/
59 * Initialize the threads subsystem. Returns 0 if successful, or -1 if no
60 * thread support is available.
63 __objc_init_thread_system(void)
65 DEBUG_PRINTF("__objc_init_thread_system\n");
67 if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
68 return 0; /* Yes, return success. */
70 return -1; /* Failed. */
74 __objc_fini_thread_system(void)
80 * Create a new thread of execution and return its id. Return -1 if fails.
81 * The new thread starts in "func" with the given argument.
84 objc_thread_create(void (*func)(void *arg), void *arg)
86 objc_thread_t thread_id = NULL; /* Detached thread id. */
87 thread_t new_thread_id = 0; /* Solaris thread id type. */
90 objc_mutex_lock(__objc_runtime_mutex);
92 if (thr_create(NULL, 0, (void *)func, arg,
93 THR_DETACHED | THR_NEW_LWP,
94 &new_thread_id) == 0) { /* Created new thread? */
95 thread_id = (objc_thread_t)new_thread_id; /* Yes, remember its id. */
96 __objc_runtime_threads_alive++;
99 objc_mutex_unlock(__objc_runtime_mutex);
105 * Set the current thread's priority.
108 objc_thread_set_priority(int priority)
110 int sys_priority = 0;
113 case OBJC_THREAD_INTERACTIVE_PRIORITY:
117 case OBJC_THREAD_BACKGROUND_PRIORITY:
120 case OBJC_THREAD_LOW_PRIORITY:
125 if (thr_setprio(thr_self(), sys_priority) == 0)
126 return 0; /* Changed priority. End. */
128 return -1; /* Failed. */
132 * Return the current thread's priority.
135 objc_thread_get_priority(void)
137 int sys_priority; /* Solaris thread priority. */
139 if (thr_getprio(thr_self(), &sys_priority) == 0) {
140 if (sys_priority >= 250)
141 return OBJC_THREAD_INTERACTIVE_PRIORITY;
142 else if (sys_priority >= 150)
143 return OBJC_THREAD_BACKGROUND_PRIORITY;
144 return OBJC_THREAD_LOW_PRIORITY;
147 return -1; /* Couldn't get priority. */
151 * Yield our process time to another thread. Any BUSY waiting that is done
152 * by a thread should use this function to make sure that other threads can
153 * make progress even on a lazy uniprocessor system.
156 objc_thread_yield(void)
158 thr_yield(); /* Yield to equal thread. */
162 * Terminate the current tread. Doesn't return anything. Doesn't return.
163 * Actually, if it failed returns -1.
166 objc_thread_exit(void)
168 objc_mutex_lock(__objc_runtime_mutex);
169 __objc_runtime_threads_alive++;
170 objc_mutex_unlock(__objc_runtime_mutex);
172 thr_exit(&__objc_thread_exit_status); /* Terminate thread. */
177 * Returns an integer value which uniquely describes a thread. Must not be
178 * NULL which is reserved as a marker for "no thread".
183 return (objc_thread_t)thr_self();
187 * Sets the thread's local storage pointer. Returns 0 if successful or -1
191 objc_thread_set_data(void *value)
193 if (thr_setspecific(__objc_thread_data_key, value) == 0)
199 * Returns the thread's local storage pointer. Returns NULL on failure.
202 objc_thread_get_data(void)
206 if (thr_getspecific(__objc_thread_data_key, &value) == 0)
207 return value; /* Return thread data. */
213 * Allocate a mutex. Return the mutex pointer if successful or NULL if
214 * the allocation fails for any reason.
217 objc_mutex_allocate(void)
219 struct objc_mutex *mutex;
222 if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
223 return NULL; /* Abort if malloc failed. */
225 err = mutex_init(&mutex->lock, USYNC_THREAD, 0);
227 if (err != 0) { /* System init failed? */
228 objc_free(mutex); /* Yes, free local memory. */
229 return NULL; /* Abort. */
231 mutex->owner = NULL; /* No owner. */
232 mutex->depth = 0; /* No locks. */
233 return mutex; /* Return mutex handle. */
237 * Deallocate a mutex. Note that this includes an implicit mutex_lock to
238 * insure that no one else is using the lock. It is legal to deallocate
239 * a lock if we have a lock on it, but illegal to deallotcate a lock held
241 * Returns the number of locks on the thread. (1 for deallocate).
244 objc_mutex_deallocate(objc_mutex_t mutex)
246 int depth; /* # of locks on mutex. */
248 if (!mutex) /* Is argument bad? */
249 return -1; /* Yes, abort. */
250 depth = objc_mutex_lock(mutex); /* Must have lock. */
252 mutex_destroy(&mutex->lock); /* System deallocate. */
254 objc_free(mutex); /* Free memory. */
255 return depth; /* Return last depth. */
259 * Grab a lock on a mutex. If this thread already has a lock on this mutex
260 * then we increment the lock count. If another thread has a lock on the
261 * mutex we block and wait for the thread to release the lock.
262 * Returns the lock count on the mutex held by this thread.
265 objc_mutex_lock(objc_mutex_t mutex)
267 objc_thread_t thread_id; /* Cache our thread id. */
269 if (!mutex) /* Is argument bad? */
270 return -1; /* Yes, abort. */
271 thread_id = objc_thread_id(); /* Get this thread's id. */
272 if (mutex->owner == thread_id) /* Already own lock? */
273 return ++mutex->depth; /* Yes, increment depth. */
275 if (mutex_lock(&mutex->lock) != 0) /* Did lock acquire fail? */
276 return -1; /* Yes, abort. */
278 mutex->owner = thread_id; /* Mark thread as owner. */
279 return mutex->depth = 1; /* Increment depth to end. */
283 * Try to grab a lock on a mutex. If this thread already has a lock on
284 * this mutex then we increment the lock count and return it. If another
285 * thread has a lock on the mutex returns -1.
288 objc_mutex_trylock(objc_mutex_t mutex)
290 objc_thread_t thread_id; /* Cache our thread id. */
292 if (!mutex) /* Is argument bad? */
293 return -1; /* Yes, abort. */
294 thread_id = objc_thread_id(); /* Get this thread's id. */
295 if (mutex->owner == thread_id) /* Already own lock? */
296 return ++mutex->depth; /* Yes, increment depth. */
298 if (mutex_trylock(&mutex->lock) != 0) /* Did lock acquire fail? */
299 return -1; /* Yes, abort. */
301 mutex->owner = thread_id; /* Mark thread as owner. */
302 return mutex->depth = 1; /* Increment depth to end. */
306 * Decrements the lock count on this mutex by one. If the lock count reaches
307 * zero, release the lock on the mutex. Returns the lock count on the mutex.
308 * It is an error to attempt to unlock a mutex which this thread doesn't hold
309 * in which case return -1 and the mutex is unaffected.
310 * Will also return -1 if the mutex free fails.
313 objc_mutex_unlock(objc_mutex_t mutex)
315 objc_thread_t thread_id; /* Cache our thread id. */
317 if (!mutex) /* Is argument bad? */
318 return -1; /* Yes, abort. */
319 thread_id = objc_thread_id(); /* Get this thread's id. */
320 if (mutex->owner != thread_id) /* Does some else own lock? */
321 return -1; /* Yes, abort. */
322 if (mutex->depth > 1) /* Released last lock? */
323 return --mutex->depth; /* No, Decrement depth, end.*/
324 mutex->depth = 0; /* Yes, reset depth to 0. */
325 mutex->owner = NULL; /* Set owner to "no thread".*/
327 if (mutex_unlock(&mutex->lock) != 0) /* Did lock release fail? */
328 return -1; /* Yes, return error value. */
330 return 0; /* No, return success. */
334 * Allocate a condition. Return the condition pointer if successful or NULL
335 * if the allocation failed for any reason.
338 objc_condition_allocate(void)
340 objc_condition_t condition;
342 if (!(condition = (objc_condition_t)objc_malloc(
343 sizeof(struct objc_condition))))
344 return NULL; /* Abort if malloc failed. */
346 cond_init(&(condition->condition), USYNC_THREAD, NULL);
348 return condition; /* Return new condition */
352 * Deallocate a condition. Note that this includes an implicit
353 * condition_broadcast to insure that waiting threads have the opportunity
354 * to wake. It is legal to dealloc a condition only if no other
355 * thread is/will be using it. Here we do NOT check for other threads
356 * waiting but just wake them up.
359 objc_condition_deallocate(objc_condition_t condition)
361 cond_broadcast(&(condition->condition)); /* Wakeup waiting threads */
362 cond_destroy(&(condition->condition)); /* Kill condition */
363 objc_free(condition); /* Release struct memory */
368 * Wait on the condition unlocking the mutex until objc_condition_signal()
369 * or objc_condition_broadcast() are called for the same condition. The
370 * given mutex *must* have the depth set to 1 so that it can be unlocked
371 * here, so that someone else can lock it and signal/broadcast the condition.
372 * The mutex is used to lock access to the shared data that make up the
373 * "condition" predicate.
376 objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
378 objc_thread_t thread_id; /* Cache our thread id. */
380 if (!mutex || !condition) /* Is argument bad? */
381 return -1; /* Yes, abort. */
383 thread_id = objc_thread_id(); /* Get this thread's id. */
384 if (mutex->owner != thread_id) /* Does some else own lock? */
385 return -1; /* Yes, abort. */
386 if (mutex->depth > 1) /* Locked more than once ? */
387 return -1; /* YES, return error */
388 /* mutex will be unlocked */
389 mutex->depth = 0; /* Yes, reset depth to 0. */
390 mutex->owner = (objc_thread_t) -1; /* Set owner to "no thread".*/
392 cond_wait(&(condition->condition),
393 &(mutex->lock)); /* unlock, wait ..., lock */
395 mutex->owner = thread_id; /* Mark thread as owner. */
396 mutex->depth = 1; /* Must be here ! */
398 return 0; /* Return success. */
402 * Wake up all threads waiting on this condition. It is recommended that
403 * the called would lock the same mutex as the threads in objc_condition_wait
404 * before changing the "condition predicate" and make this call and unlock it
405 * right away after this call.
408 objc_condition_broadcast(objc_condition_t condition)
412 cond_broadcast(&(condition->condition));
417 * Wake up one thread waiting on this condition. It is recommended that
418 * the called would lock the same mutex as the threads in objc_condition_wait
419 * before changing the "condition predicate" and make this call and unlock it
420 * right away after this call.
423 objc_condition_signal(objc_condition_t condition)
427 cond_signal(&(condition->condition));