OSDN Git Service

(OBJC_VERSION): Increment version as recent changes have made old
[pf3gnuchains/gcc-fork.git] / gcc / objc / thr-irix.c
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)
4
5 This file is part of GNU CC.
6
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.
10
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
14 details.
15
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.  */
20
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.  */
26
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/sysmp.h>
30 #include <sys/prctl.h>
31 #include <ulocks.h>
32 #include <objc/thr.h>
33 #include "runtime.h"
34
35 /********
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.
40  */
41 struct objc_mutex 
42 {
43     volatile objc_thread_t     owner;          /* Id of thread that owns.  */
44     volatile int                depth;          /* # of acquires.           */
45     ulock_t                     lock;           /* Irix lock.               */
46 };
47
48 /*****************************************************************************
49  *  Static variables.
50  */
51 static void *  __objc_shared_arena_handle = NULL; /* Storage arena locks.   */
52
53 /********
54  *  Initialize the threads subsystem.  Returns 0 if successful, or -1 if no
55  *  thread support is available.
56  */
57 int
58 __objc_init_thread_system(void)
59 {
60     char        arena_name[64];                 /* Name of IRIX arena.      */
61
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.  */
68     
69     return 0;
70 }
71
72 int
73 __objc_fini_thread_system(void)
74 {
75   return 0;
76 }
77
78 /********
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.
81  */
82 objc_thread_t
83 objc_thread_create(void (*func)(void *arg), void *arg)
84 {
85     objc_thread_t      thread_id = NULL;
86     int                 sys_id;
87     
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++;
92     }
93     objc_mutex_unlock(__objc_runtime_mutex);
94
95     return thread_id;
96 }
97
98 /********
99  *  Set the current thread's priority.
100  */
101 int
102 objc_thread_set_priority(int priority)
103 {
104     int         sys_priority = 0;
105
106     switch (priority) {
107     case OBJC_THREAD_INTERACTIVE_PRIORITY:
108         break;
109     default:
110     case OBJC_THREAD_BACKGROUND_PRIORITY:
111         break;
112     case OBJC_THREAD_LOW_PRIORITY:
113         break;
114     }
115     return -1;                                  /* Failed.                  */
116 }
117
118 /********
119  *  Return the current thread's priority.
120  */
121 int
122 objc_thread_get_priority(void)
123 {
124     return -1;                                  /* Couldn't get priority.   */
125 }
126
127 /********
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.
131  */
132 void
133 objc_thread_yield(void)
134 {
135     sginap(0);                                  /* Yield to equal process.  */
136 }
137
138 /********
139  *  Terminate the current tread.  Doesn't return anything.  Doesn't return.
140  *  Actually, if it failed returns -1.
141  */
142 int
143 objc_thread_exit(void)
144 {
145     objc_mutex_lock(__objc_runtime_mutex);
146     __objc_runtime_threads_alive--;
147     objc_mutex_unlock(__objc_runtime_mutex);
148
149     exit(__objc_thread_exit_status);            /* IRIX only has exit.      */
150     return -1;
151 }
152
153 /********
154  *  Returns an integer value which uniquely describes a thread.  Must not be
155  *  NULL which is reserved as a marker for "no thread".
156  */
157 objc_thread_t
158 objc_thread_id(void)
159 {
160     return (objc_thread_t)get_pid();           /* Threads are processes.   */
161 }
162
163 /********
164  *  Sets the thread's local storage pointer.  Returns 0 if successful or -1
165  *  if failed.
166  */
167 int
168 objc_thread_set_data(void *value)
169 {
170     *((void **)&PRDA->usr_prda) = value;        /* Set thread data ptr.     */
171     return 0;
172 }
173
174 /********
175  *  Returns the thread's local storage pointer.  Returns NULL on failure.
176  */
177 void *
178 objc_thread_get_data(void)
179 {
180     return *((void **)&PRDA->usr_prda);         /* Return thread data ptr.  */
181 }
182
183 /********
184  *  Allocate a mutex.
185  *  Return the mutex pointer if successful or NULL if the allocation failed
186  *  for any reason.
187  */
188 objc_mutex_t
189 objc_mutex_allocate(void)
190 {
191     objc_mutex_t       mutex;
192     int                 err = 0;
193     
194     if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
195         return NULL;                            /* Abort if malloc failed.  */
196     
197     if (!(mutex->lock = usnewlock(__objc_shared_arena_handle)))
198         err = -1;
199     
200     if (err != 0) {                             /* System init failed?      */
201         objc_free(mutex);                       /* Yes, free local memory.  */
202         return NULL;                            /* Abort.                   */
203     }
204     mutex->owner = NULL;                        /* No owner.                */
205     mutex->depth = 0;                           /* No locks.                */
206     return mutex;                               /* Return mutex handle.     */
207 }
208
209 /********
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
213  *  by anyone else.
214  *  Returns the number of locks on the thread.  (1 for deallocate).
215  */
216 int
217 objc_mutex_deallocate(objc_mutex_t mutex)
218 {
219     int         depth;                          /* # of locks on mutex.     */
220
221     if (!mutex)                                 /* Is argument bad?         */
222         return -1;                              /* Yes, abort.              */
223     depth = objc_mutex_lock(mutex);             /* Must have lock.          */
224     
225     usfreelock(mutex->lock, __objc_shared_arena_handle); /* Free IRIX lock. */
226     
227     objc_free(mutex);                           /* Free memory.             */
228     return depth;                               /* Return last depth.       */
229 }
230
231 /********
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.
236  */
237 int
238 objc_mutex_lock(objc_mutex_t mutex)
239 {
240     objc_thread_t      thread_id;              /* Cache our thread id.     */
241
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.    */
248     }
249     
250     DEBUG_PRINTF("lock owned by: %d:%d (attempt by %d)\n",
251                  mutex->owner, mutex->depth, thread_id);
252
253     if (ussetlock(mutex->lock) == 0)            /* Did lock acquire fail?   */
254         return -1;                              /* Yes, abort.              */
255     
256     mutex->owner = thread_id;                   /* Mark thread as owner.    */
257     return mutex->depth = 1;                    /* Increment depth to end.  */
258 }
259
260 /********
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.
264  */
265 int
266 objc_mutex_trylock(objc_mutex_t mutex)
267 {
268     objc_thread_t      thread_id;              /* Cache our thread id.     */
269
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.    */
275     
276     if (ustestlock(mutex->lock) == 0)           /* Did lock acquire fail?   */
277         return -1;                              /* Yes, abort.              */
278     
279     mutex->owner = thread_id;                   /* Mark thread as owner.    */
280     return mutex->depth = 1;                    /* Increment depth to end.  */
281 }
282
283 /********
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.
289  */
290
291 int
292 objc_mutex_unlock(objc_mutex_t mutex)
293 {
294     objc_thread_t     thread_id;               /* Cache our thread id.     */
295     
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.              */
301
302     DEBUG_PRINTF("unlock by: %d:%d\n", mutex->owner, mutex->depth - 1);
303     
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".*/
308     
309     usunsetlock(mutex->lock);                   /* Free lock.               */
310     
311     return 0;                                   /* No, return success.      */
312 }
313
314 /* End of File */