OSDN Git Service

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