OSDN Git Service

(objc_skip_typespec): Don't abort for _C_UNDEF.
[pf3gnuchains/gcc-fork.git] / gcc / objc / thr-solaris.c
1 /* GNU Objective C Runtime Thread Interface
2    Copyright (C) 1996 Free Software Foundation, Inc.
3    Cobnrtibuted 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 "runtime.h"
28
29 #include <thread.h>
30 #include <synch.h>
31 #include <errno.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     mutex_t                     lock;           /* System mutex.            */
44 };
45
46 /*****************************************************************************
47  *  Static variables.
48  */
49 static thread_key_t     __objc_thread_data_key; /* Data key for thread data.*/
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     DEBUG_PRINTF("__objc_init_thread_system\n");
59
60     if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
61         return 0;                               /* Yes, return success.     */
62     
63     return -1;                                  /* Failed.                  */
64 }
65
66 int
67 __objc_fini_thread_system(void)
68 {
69   return 0;
70 }
71
72 /********
73  *  Create a new thread of execution and return its id.  Return -1 if fails.
74  *  The new thread starts in "func" with the given argument.
75  */
76 _objc_thread_t
77 objc_thread_create(void (*func)(void *arg), void *arg)
78 {
79   _objc_thread_t        thread_id = NULL;       /* Detached thread id.      */
80   thread_t              new_thread_id = 0;      /* Solaris thread id type.  */
81   int                   errn;
82
83   objc_mutex_lock(__objc_runtime_mutex);
84
85   if (thr_create(NULL, 0, (void *)func, arg,
86                  THR_DETACHED | THR_NEW_LWP,
87                  &new_thread_id) == 0) {        /* Created new thread?      */
88     thread_id = (_objc_thread_t)new_thread_id;  /* Yes, remember its id.    */
89     __objc_runtime_threads_alive++;
90   }
91   
92   objc_mutex_unlock(__objc_runtime_mutex);
93     
94   return thread_id;
95 }
96
97 /********
98  *  Set the current thread's priority.
99  */
100 int
101 objc_thread_set_priority(int priority)
102 {
103     int         sys_priority = 0;
104
105     switch (priority) {
106     case OBJC_THREAD_INTERACTIVE_PRIORITY:
107         sys_priority = 300;
108         break;
109     default:
110     case OBJC_THREAD_BACKGROUND_PRIORITY:
111         sys_priority = 200;
112         break;
113     case OBJC_THREAD_LOW_PRIORITY:
114         sys_priority = 1000;
115         break;
116     }
117     
118     if (thr_setprio(thr_self(), sys_priority) == 0)
119         return 0;                               /* Changed priority. End.   */
120     
121     return -1;                                  /* Failed.                  */
122 }
123
124 /********
125  *  Return the current thread's priority.
126  */
127 int
128 objc_thread_get_priority(void)
129 {
130     int         sys_priority;                   /* Solaris thread priority. */
131                                                    
132     if (thr_getprio(thr_self(), &sys_priority) == 0) {
133         if (sys_priority >= 250)
134             return OBJC_THREAD_INTERACTIVE_PRIORITY;
135         else if (sys_priority >= 150)
136             return OBJC_THREAD_BACKGROUND_PRIORITY;
137         return OBJC_THREAD_LOW_PRIORITY;
138     }
139     
140     return -1;                                  /* Couldn't get priority.   */
141 }
142
143 /********
144  *  Yield our process time to another thread.  Any BUSY waiting that is done
145  *  by a thread should use this function to make sure that other threads can
146  *  make progress even on a lazy uniprocessor system.
147  */
148 void
149 objc_thread_yield(void)
150 {
151     thr_yield();                                /* Yield to equal thread.   */
152 }
153
154 /********
155  *  Terminate the current tread.  Doesn't return anything.  Doesn't return.
156  *  Actually, if it failed returns -1.
157  */
158 int
159 objc_thread_exit(void)
160 {
161   objc_mutex_lock(__objc_runtime_mutex);
162   __objc_runtime_threads_alive++;
163   objc_mutex_unlock(__objc_runtime_mutex);
164   
165   thr_exit(&__objc_thread_exit_status);         /* Terminate thread.        */
166   return -1;
167 }
168
169 /********
170  *  Returns an integer value which uniquely describes a thread.  Must not be
171  *  NULL which is reserved as a marker for "no thread".
172  */
173 _objc_thread_t
174 objc_thread_id(void)
175 {
176     return (_objc_thread_t)thr_self();
177 }
178
179 /********
180  *  Sets the thread's local storage pointer.  Returns 0 if successful or -1
181  *  if failed.
182  */
183 int
184 objc_thread_set_data(void *value)
185 {
186     if (thr_setspecific(__objc_thread_data_key, value) == 0)
187         return 0;
188     return -1;
189 }
190
191 /********
192  *  Returns the thread's local storage pointer.  Returns NULL on failure.
193  */
194 void *
195 objc_thread_get_data(void)
196 {
197     void *      value = NULL;
198     
199     if (thr_getspecific(__objc_thread_data_key, &value) == 0)
200         return value;                           /* Return thread data.      */
201     
202     return NULL;
203 }
204
205 /********
206  *  Allocate a mutex.  Return the mutex pointer if successful or NULL if
207  *  the allocation fails for any reason.
208  */
209 _objc_mutex_t
210 objc_mutex_allocate(void)
211 {
212     struct _objc_mutex *mutex;
213     int         err = 0;
214     
215     if (!(mutex = (_objc_mutex_t)__objc_xmalloc(sizeof(struct _objc_mutex))))
216         return NULL;                            /* Abort if malloc failed.  */
217     
218     err = mutex_init(&mutex->lock, USYNC_THREAD, 0);
219     
220     if (err != 0) {                             /* System init failed?      */
221         free(mutex);                            /* Yes, free local memory.  */
222         return NULL;                            /* Abort.                   */
223     }
224     mutex->owner = NULL;                        /* No owner.                */
225     mutex->depth = 0;                           /* No locks.                */
226     return mutex;                               /* Return mutex handle.     */
227 }
228
229 /********
230  *  Deallocate a mutex.  Note that this includes an implicit mutex_lock to
231  *  insure that no one else is using the lock.  It is legal to deallocate
232  *  a lock if we have a lock on it, but illegal to deallotcate a lock held
233  *  by anyone else.
234  *  Returns the number of locks on the thread.  (1 for deallocate).
235  */
236 int
237 objc_mutex_deallocate(_objc_mutex_t mutex)
238 {
239     int         depth;                          /* # of locks on mutex.     */
240
241     if (!mutex)                                 /* Is argument bad?         */
242         return -1;                              /* Yes, abort.              */
243     depth = objc_mutex_lock(mutex);             /* Must have lock.          */
244     
245     mutex_destroy(&mutex->lock);                /* System deallocate.       */
246     
247     free(mutex);                                /* Free memory.             */
248     return depth;                               /* Return last depth.       */
249 }
250
251 /********
252  *  Grab a lock on a mutex.  If this thread already has a lock on this mutex
253  *  then we increment the lock count.  If another thread has a lock on the 
254  *  mutex we block and wait for the thread to release the lock.
255  *  Returns the lock count on the mutex held by this thread.
256  */
257 int
258 objc_mutex_lock(_objc_mutex_t mutex)
259 {
260     _objc_thread_t      thread_id;              /* Cache our thread id.     */
261
262     if (!mutex)                                 /* Is argument bad?         */
263         return -1;                              /* Yes, abort.              */
264     thread_id = objc_thread_id();               /* Get this thread's id.    */
265     if (mutex->owner == thread_id)              /* Already own lock?        */
266         return ++mutex->depth;                  /* Yes, increment depth.    */
267
268     if (mutex_lock(&mutex->lock) != 0)          /* Did lock acquire fail?   */
269         return -1;                              /* Yes, abort.              */
270     
271     mutex->owner = thread_id;                   /* Mark thread as owner.    */
272     return mutex->depth = 1;                    /* Increment depth to end.  */
273 }
274
275 /********
276  *  Try to grab a lock on a mutex.  If this thread already has a lock on
277  *  this mutex then we increment the lock count and return it.  If another
278  *  thread has a lock on the mutex returns -1.
279  */
280 int
281 objc_mutex_trylock(_objc_mutex_t mutex)
282 {
283     _objc_thread_t      thread_id;              /* Cache our thread id.     */
284
285     if (!mutex)                                 /* Is argument bad?         */
286         return -1;                              /* Yes, abort.              */
287     thread_id = objc_thread_id();               /* Get this thread's id.    */
288     if (mutex->owner == thread_id)              /* Already own lock?        */
289         return ++mutex->depth;                  /* Yes, increment depth.    */
290     
291     if (mutex_trylock(&mutex->lock) != 0)       /* Did lock acquire fail?   */
292         return -1;                              /* Yes, abort.              */
293     
294     mutex->owner = thread_id;                   /* Mark thread as owner.    */
295     return mutex->depth = 1;                    /* Increment depth to end.  */
296 }
297
298 /********
299  *  Decrements the lock count on this mutex by one.  If the lock count reaches
300  *  zero, release the lock on the mutex.  Returns the lock count on the mutex.
301  *  It is an error to attempt to unlock a mutex which this thread doesn't hold
302  *  in which case return -1 and the mutex is unaffected.
303  *  Will also return -1 if the mutex free fails.
304  */
305 int
306 objc_mutex_unlock(_objc_mutex_t mutex)
307 {
308     _objc_thread_t      thread_id;              /* Cache our thread id.     */
309     
310     if (!mutex)                                 /* Is argument bad?         */
311         return -1;                              /* Yes, abort.              */
312     thread_id = objc_thread_id();               /* Get this thread's id.    */
313     if (mutex->owner != thread_id)              /* Does some else own lock? */
314         return -1;                              /* Yes, abort.              */
315     if (mutex->depth > 1)                       /* Released last lock?      */
316         return --mutex->depth;                  /* No, Decrement depth, end.*/
317     mutex->depth = 0;                           /* Yes, reset depth to 0.   */
318     mutex->owner = NULL;                        /* Set owner to "no thread".*/
319     
320     if (mutex_unlock(&mutex->lock) != 0)        /* Did lock release fail?   */
321         return -1;                              /* Yes, return error value. */
322     
323     return 0;                                   /* No, return success.      */
324 }
325
326 /* End of File */