1 /* GNU Objective C Runtime Thread Interface - SGI IRIX Implementation
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
5 This file is part of GNU CC.
7 GNU CC 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 2, or (at your option) any later version.
11 GNU CC 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 You should have received a copy of the GNU General Public License along with
17 GNU CC; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* As a special exception, if you link this library with files compiled with
22 GCC to produce an executable, this does not cause the resulting executable
23 to be covered by the GNU General Public License. This exception does not
24 however invalidate any other reasons why the executable file might be
25 covered by the GNU General Public License. */
28 #include <sys/types.h>
29 #include <sys/sysmp.h>
30 #include <sys/prctl.h>
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 ulock_t lock; /* Irix lock. */
48 /*****************************************************************************
51 static void * __objc_shared_arena_handle = NULL; /* Storage arena locks. */
54 * Initialize the threads subsystem. Returns 0 if successful, or -1 if no
55 * thread support is available.
58 __objc_init_thread_system(void)
60 char arena_name[64]; /* Name of IRIX arena. */
62 DEBUG_PRINTF("__objc_init_thread_system\n");
63 sprintf(arena_name, "/usr/tmp/objc_%05u", (unsigned)getpid());
64 usconfig(CONF_INITUSERS, 256); /* Up to 256 threads. */
65 usconfig(CONF_ARENATYPE, US_SHAREDONLY); /* Arena only for threads. */
66 if (!(__objc_shared_arena_handle = usinit(arena_name))) /* Init Failed? */
67 return -1; /* Yes, return error code. */
73 __objc_fini_thread_system(void)
79 * Create a new thread of execution and return its id. Return NULL if fails.
80 * The new thread starts in "func" with the given argument.
83 objc_thread_create(void (*func)(void *arg), void *arg)
85 objc_thread_t thread_id = NULL;
88 objc_mutex_lock(__objc_runtime_mutex);
89 if ((sys_id = sproc((void *)func, PR_SALL, arg)) >= 0) {
90 thread_id = (objc_thread_t)sys_id;
91 __objc_runtime_threads_alive++;
93 objc_mutex_unlock(__objc_runtime_mutex);
99 * Set the current thread's priority.
102 objc_thread_set_priority(int priority)
104 int sys_priority = 0;
107 case OBJC_THREAD_INTERACTIVE_PRIORITY:
110 case OBJC_THREAD_BACKGROUND_PRIORITY:
112 case OBJC_THREAD_LOW_PRIORITY:
115 return -1; /* Failed. */
119 * Return the current thread's priority.
122 objc_thread_get_priority(void)
124 return -1; /* Couldn't get priority. */
128 * Yield our process time to another thread. Any BUSY waiting that is done
129 * by a thread should use this function to make sure that other threads can
130 * make progress even on a lazy uniprocessor system.
133 objc_thread_yield(void)
135 sginap(0); /* Yield to equal process. */
139 * Terminate the current tread. Doesn't return anything. Doesn't return.
140 * Actually, if it failed returns -1.
143 objc_thread_exit(void)
145 objc_mutex_lock(__objc_runtime_mutex);
146 __objc_runtime_threads_alive--;
147 objc_mutex_unlock(__objc_runtime_mutex);
149 exit(__objc_thread_exit_status); /* IRIX only has exit. */
154 * Returns an integer value which uniquely describes a thread. Must not be
155 * NULL which is reserved as a marker for "no thread".
160 return (objc_thread_t)get_pid(); /* Threads are processes. */
164 * Sets the thread's local storage pointer. Returns 0 if successful or -1
168 objc_thread_set_data(void *value)
170 *((void **)&PRDA->usr_prda) = value; /* Set thread data ptr. */
175 * Returns the thread's local storage pointer. Returns NULL on failure.
178 objc_thread_get_data(void)
180 return *((void **)&PRDA->usr_prda); /* Return thread data ptr. */
185 * Return the mutex pointer if successful or NULL if the allocation failed
189 objc_mutex_allocate(void)
194 if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
195 return NULL; /* Abort if malloc failed. */
197 if (!(mutex->lock = usnewlock(__objc_shared_arena_handle)))
200 if (err != 0) { /* System init failed? */
201 objc_free(mutex); /* Yes, free local memory. */
202 return NULL; /* Abort. */
204 mutex->owner = NULL; /* No owner. */
205 mutex->depth = 0; /* No locks. */
206 return mutex; /* Return mutex handle. */
210 * Deallocate a mutex. Note that this includes an implicit mutex_lock to
211 * insure that no one else is using the lock. It is legal to deallocate
212 * a lock if we have a lock on it, but illegal to deallotcate a lock held
214 * Returns the number of locks on the thread. (1 for deallocate).
217 objc_mutex_deallocate(objc_mutex_t mutex)
219 int depth; /* # of locks on mutex. */
221 if (!mutex) /* Is argument bad? */
222 return -1; /* Yes, abort. */
223 depth = objc_mutex_lock(mutex); /* Must have lock. */
225 usfreelock(mutex->lock, __objc_shared_arena_handle); /* Free IRIX lock. */
227 objc_free(mutex); /* Free memory. */
228 return depth; /* Return last depth. */
232 * Grab a lock on a mutex. If this thread already has a lock on this mutex
233 * then we increment the lock count. If another thread has a lock on the
234 * mutex we block and wait for the thread to release the lock.
235 * Returns the lock count on the mutex held by this thread.
238 objc_mutex_lock(objc_mutex_t mutex)
240 objc_thread_t thread_id; /* Cache our thread id. */
242 if (!mutex) /* Is argument bad? */
243 return -1; /* Yes, abort. */
244 thread_id = objc_thread_id(); /* Get this thread's id. */
245 if (mutex->owner == thread_id) { /* Already own lock? */
246 DEBUG_PRINTF("lock owned by: %d:%d\n", mutex->owner, mutex->depth);
247 return ++mutex->depth; /* Yes, increment depth. */
250 DEBUG_PRINTF("lock owned by: %d:%d (attempt by %d)\n",
251 mutex->owner, mutex->depth, thread_id);
253 if (ussetlock(mutex->lock) == 0) /* Did lock acquire fail? */
254 return -1; /* Yes, abort. */
256 mutex->owner = thread_id; /* Mark thread as owner. */
257 return mutex->depth = 1; /* Increment depth to end. */
261 * Try to grab a lock on a mutex. If this thread already has a lock on
262 * this mutex then we increment the lock count and return it. If another
263 * thread has a lock on the mutex returns -1.
266 objc_mutex_trylock(objc_mutex_t mutex)
268 objc_thread_t thread_id; /* Cache our thread id. */
270 if (!mutex) /* Is argument bad? */
271 return -1; /* Yes, abort. */
272 thread_id = objc_thread_id(); /* Get this thread's id. */
273 if (mutex->owner == thread_id) /* Already own lock? */
274 return ++mutex->depth; /* Yes, increment depth. */
276 if (ustestlock(mutex->lock) == 0) /* Did lock acquire fail? */
277 return -1; /* Yes, abort. */
279 mutex->owner = thread_id; /* Mark thread as owner. */
280 return mutex->depth = 1; /* Increment depth to end. */
284 * Decrements the lock count on this mutex by one. If the lock count reaches
285 * zero, release the lock on the mutex. Returns the lock count on the mutex.
286 * It is an error to attempt to unlock a mutex which this thread doesn't hold
287 * in which case return -1 and the mutex is unaffected.
288 * Will also return -1 if the mutex free fails.
292 objc_mutex_unlock(objc_mutex_t mutex)
294 objc_thread_t thread_id; /* Cache our thread id. */
296 if (!mutex) /* Is argument bad? */
297 return -1; /* Yes, abort. */
298 thread_id = objc_thread_id(); /* Get this thread's id. */
299 if (mutex->owner != thread_id) /* Does some else own lock? */
300 return -1; /* Yes, abort. */
302 DEBUG_PRINTF("unlock by: %d:%d\n", mutex->owner, mutex->depth - 1);
304 if (mutex->depth > 1) /* Released last lock? */
305 return --mutex->depth; /* No, Decrement depth, end.*/
306 mutex->depth = 0; /* Yes, reset depth to 0. */
307 mutex->owner = NULL; /* Set owner to "no thread".*/
309 usunsetlock(mutex->lock); /* Free lock. */
311 return 0; /* No, return success. */