1 /* GNU Objective C Runtime Thread Interface - SGI IRIX Implementation
2 Copyright (C) 1996 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>
34 * This structure represents a single mutual exclusion lock. Lock semantics
35 * are detailed with the subsequent functions. We use whatever lock is
36 * provided by the system. We augment it with depth and current owner id
37 * fields to implement and re-entrant lock.
41 volatile _objc_thread_t owner; /* Id of thread that owns. */
42 volatile int depth; /* # of acquires. */
43 ulock_t lock; /* Irix lock. */
46 /*****************************************************************************
49 static void * __objc_shared_arena_handle = NULL; /* Storage arena locks. */
52 * Initialize the threads subsystem. Returns 0 if successful, or -1 if no
53 * thread support is available.
56 __objc_init_thread_system(void)
58 char arena_name[64]; /* Name of IRIX arena. */
60 DEBUG_PRINTF("__objc_init_thread_system\n");
61 sprintf(arena_name, "/usr/tmp/objc_%05u", (unsigned)getpid());
62 usconfig(CONF_INITUSERS, 256); /* Up to 256 threads. */
63 usconfig(CONF_ARENATYPE, US_SHAREDONLY); /* Arena only for threads. */
64 if (!(__objc_shared_arena_handle = usinit(arena_name))) /* Init Failed? */
65 return -1; /* Yes, return error code. */
71 __objc_fini_thread_system(void)
77 * Create a new thread of execution and return its id. Return NULL if fails.
78 * The new thread starts in "func" with the given argument.
81 objc_thread_create(void (*func)(void *arg), void *arg)
83 _objc_thread_t thread_id = NULL;
86 objc_mutex_lock(__objc_runtime_mutex);
87 if ((sys_id = sproc((void *)func, PR_SALL, arg)) >= 0) {
88 thread_id = (_objc_thread_t)sys_id;
89 __objc_runtime_threads_alive++;
91 objc_mutex_unlock(__objc_runtime_mutex);
97 * Set the current thread's priority.
100 objc_thread_set_priority(int priority)
102 int sys_priority = 0;
105 case OBJC_THREAD_INTERACTIVE_PRIORITY:
108 case OBJC_THREAD_BACKGROUND_PRIORITY:
110 case OBJC_THREAD_LOW_PRIORITY:
113 return -1; /* Failed. */
117 * Return the current thread's priority.
120 objc_thread_get_priority(void)
122 return -1; /* Couldn't get priority. */
126 * Yield our process time to another thread. Any BUSY waiting that is done
127 * by a thread should use this function to make sure that other threads can
128 * make progress even on a lazy uniprocessor system.
131 objc_thread_yield(void)
133 sginap(0); /* Yield to equal process. */
137 * Terminate the current tread. Doesn't return anything. Doesn't return.
138 * Actually, if it failed returns -1.
141 objc_thread_exit(void)
143 objc_mutex_lock(__objc_runtime_mutex);
144 __objc_runtime_threads_alive--;
145 objc_mutex_unlock(__objc_runtime_mutex);
147 exit(__objc_thread_exit_status); /* IRIX only has exit. */
152 * Returns an integer value which uniquely describes a thread. Must not be
153 * NULL which is reserved as a marker for "no thread".
158 return (_objc_thread_t)get_pid(); /* Threads are processes. */
162 * Sets the thread's local storage pointer. Returns 0 if successful or -1
166 objc_thread_set_data(void *value)
168 *((void **)&PRDA->usr_prda) = value; /* Set thread data ptr. */
173 * Returns the thread's local storage pointer. Returns NULL on failure.
176 objc_thread_get_data(void)
178 return *((void **)&PRDA->usr_prda); /* Return thread data ptr. */
183 * Return the mutex pointer if successful or NULL if the allocation failed
187 objc_mutex_allocate(void)
192 if (!(mutex = (_objc_mutex_t)__objc_xmalloc(sizeof(struct _objc_mutex))))
193 return NULL; /* Abort if malloc failed. */
195 if (!(mutex->lock = usnewlock(__objc_shared_arena_handle)))
198 if (err != 0) { /* System init failed? */
199 free(mutex); /* Yes, free local memory. */
200 return NULL; /* Abort. */
202 mutex->owner = NULL; /* No owner. */
203 mutex->depth = 0; /* No locks. */
204 return mutex; /* Return mutex handle. */
208 * Deallocate a mutex. Note that this includes an implicit mutex_lock to
209 * insure that no one else is using the lock. It is legal to deallocate
210 * a lock if we have a lock on it, but illegal to deallotcate a lock held
212 * Returns the number of locks on the thread. (1 for deallocate).
215 objc_mutex_deallocate(_objc_mutex_t mutex)
217 int depth; /* # of locks on mutex. */
219 if (!mutex) /* Is argument bad? */
220 return -1; /* Yes, abort. */
221 depth = objc_mutex_lock(mutex); /* Must have lock. */
223 usfreelock(mutex->lock, __objc_shared_arena_handle); /* Free IRIX lock. */
225 free(mutex); /* Free memory. */
226 return depth; /* Return last depth. */
230 * Grab a lock on a mutex. If this thread already has a lock on this mutex
231 * then we increment the lock count. If another thread has a lock on the
232 * mutex we block and wait for the thread to release the lock.
233 * Returns the lock count on the mutex held by this thread.
236 objc_mutex_lock(_objc_mutex_t mutex)
238 _objc_thread_t thread_id; /* Cache our thread id. */
240 if (!mutex) /* Is argument bad? */
241 return -1; /* Yes, abort. */
242 thread_id = objc_thread_id(); /* Get this thread's id. */
243 if (mutex->owner == thread_id) { /* Already own lock? */
244 DEBUG_PRINTF("lock owned by: %d:%d\n", mutex->owner, mutex->depth);
245 return ++mutex->depth; /* Yes, increment depth. */
248 DEBUG_PRINTF("lock owned by: %d:%d (attempt by %d)\n",
249 mutex->owner, mutex->depth, thread_id);
251 if (ussetlock(mutex->lock) == 0) /* Did lock acquire fail? */
252 return -1; /* Yes, abort. */
254 mutex->owner = thread_id; /* Mark thread as owner. */
255 return mutex->depth = 1; /* Increment depth to end. */
259 * Try to grab a lock on a mutex. If this thread already has a lock on
260 * this mutex then we increment the lock count and return it. If another
261 * thread has a lock on the mutex returns -1.
264 objc_mutex_trylock(_objc_mutex_t mutex)
266 _objc_thread_t thread_id; /* Cache our thread id. */
268 if (!mutex) /* Is argument bad? */
269 return -1; /* Yes, abort. */
270 thread_id = objc_thread_id(); /* Get this thread's id. */
271 if (mutex->owner == thread_id) /* Already own lock? */
272 return ++mutex->depth; /* Yes, increment depth. */
274 if (ustestlock(mutex->lock) == 0) /* Did lock acquire fail? */
275 return -1; /* Yes, abort. */
277 mutex->owner = thread_id; /* Mark thread as owner. */
278 return mutex->depth = 1; /* Increment depth to end. */
282 * Decrements the lock count on this mutex by one. If the lock count reaches
283 * zero, release the lock on the mutex. Returns the lock count on the mutex.
284 * It is an error to attempt to unlock a mutex which this thread doesn't hold
285 * in which case return -1 and the mutex is unaffected.
286 * Will also return -1 if the mutex free fails.
290 objc_mutex_unlock(_objc_mutex_t mutex)
292 _objc_thread_t thread_id; /* Cache our thread id. */
294 if (!mutex) /* Is argument bad? */
295 return -1; /* Yes, abort. */
296 thread_id = objc_thread_id(); /* Get this thread's id. */
297 if (mutex->owner != thread_id) /* Does some else own lock? */
298 return -1; /* Yes, abort. */
300 DEBUG_PRINTF("unlock by: %d:%d\n", mutex->owner, mutex->depth - 1);
302 if (mutex->depth > 1) /* Released last lock? */
303 return --mutex->depth; /* No, Decrement depth, end.*/
304 mutex->depth = 0; /* Yes, reset depth to 0. */
305 mutex->owner = NULL; /* Set owner to "no thread".*/
307 usunsetlock(mutex->lock); /* Free lock. */
309 return 0; /* No, return success. */