OSDN Git Service

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