OSDN Git Service

(OBJC_VERSION): Increment version as recent changes have made old
[pf3gnuchains/gcc-fork.git] / gcc / objc / thr-solaris.c
1 /* GNU Objective C Runtime Thread Interface
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3    Contributed by Galen C. Hunt (gchunt@cs.rochester.edu)
4    Conditions added by Mircea Oancea (mircea@first.elcom.pub.ro)
5       
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
11
12 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 details.
16
17 You should have received a copy of the GNU General Public License along with
18 GNU CC; see the file COPYING.  If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with files compiled with
23    GCC to produce an executable, this does not cause the resulting executable
24    to be covered by the GNU General Public License. This exception does not
25    however invalidate any other reasons why the executable file might be
26    covered by the GNU General Public License.  */
27
28 #include <objc/thr.h>
29 #include "runtime.h"
30
31 #include <thread.h>
32 #include <synch.h>
33 #include <errno.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     mutex_t                     lock;           /* System mutex.            */
46 };
47
48 struct objc_condition
49 {
50     cond_t                     condition;      /* solaris condition        */
51 };
52
53 /*****************************************************************************
54  *  Static variables.
55  */
56 static thread_key_t     __objc_thread_data_key; /* Data key for thread data.*/
57
58 /********
59  *  Initialize the threads subsystem.  Returns 0 if successful, or -1 if no
60  *  thread support is available.
61  */
62 int
63 __objc_init_thread_system(void)
64 {
65     DEBUG_PRINTF("__objc_init_thread_system\n");
66
67     if (thr_keycreate(&__objc_thread_data_key, NULL) == 0)
68         return 0;                               /* Yes, return success.     */
69     
70     return -1;                                  /* Failed.                  */
71 }
72
73 int
74 __objc_fini_thread_system(void)
75 {
76   return 0;
77 }
78
79 /********
80  *  Create a new thread of execution and return its id.  Return -1 if fails.
81  *  The new thread starts in "func" with the given argument.
82  */
83 objc_thread_t
84 objc_thread_create(void (*func)(void *arg), void *arg)
85 {
86   objc_thread_t        thread_id = NULL;       /* Detached thread id.      */
87   thread_t              new_thread_id = 0;      /* Solaris thread id type.  */
88   int                   errn;
89
90   objc_mutex_lock(__objc_runtime_mutex);
91
92   if (thr_create(NULL, 0, (void *)func, arg,
93                  THR_DETACHED | THR_NEW_LWP,
94                  &new_thread_id) == 0) {        /* Created new thread?      */
95     thread_id = (objc_thread_t)new_thread_id;  /* Yes, remember its id.    */
96     __objc_runtime_threads_alive++;
97   }
98   
99   objc_mutex_unlock(__objc_runtime_mutex);
100     
101   return thread_id;
102 }
103
104 /********
105  *  Set the current thread's priority.
106  */
107 int
108 objc_thread_set_priority(int priority)
109 {
110     int         sys_priority = 0;
111
112     switch (priority) {
113     case OBJC_THREAD_INTERACTIVE_PRIORITY:
114         sys_priority = 300;
115         break;
116     default:
117     case OBJC_THREAD_BACKGROUND_PRIORITY:
118         sys_priority = 200;
119         break;
120     case OBJC_THREAD_LOW_PRIORITY:
121         sys_priority = 1000;
122         break;
123     }
124     
125     if (thr_setprio(thr_self(), sys_priority) == 0)
126         return 0;                               /* Changed priority. End.   */
127     
128     return -1;                                  /* Failed.                  */
129 }
130
131 /********
132  *  Return the current thread's priority.
133  */
134 int
135 objc_thread_get_priority(void)
136 {
137     int         sys_priority;                   /* Solaris thread priority. */
138                                                    
139     if (thr_getprio(thr_self(), &sys_priority) == 0) {
140         if (sys_priority >= 250)
141             return OBJC_THREAD_INTERACTIVE_PRIORITY;
142         else if (sys_priority >= 150)
143             return OBJC_THREAD_BACKGROUND_PRIORITY;
144         return OBJC_THREAD_LOW_PRIORITY;
145     }
146     
147     return -1;                                  /* Couldn't get priority.   */
148 }
149
150 /********
151  *  Yield our process time to another thread.  Any BUSY waiting that is done
152  *  by a thread should use this function to make sure that other threads can
153  *  make progress even on a lazy uniprocessor system.
154  */
155 void
156 objc_thread_yield(void)
157 {
158     thr_yield();                                /* Yield to equal thread.   */
159 }
160
161 /********
162  *  Terminate the current tread.  Doesn't return anything.  Doesn't return.
163  *  Actually, if it failed returns -1.
164  */
165 int
166 objc_thread_exit(void)
167 {
168   objc_mutex_lock(__objc_runtime_mutex);
169   __objc_runtime_threads_alive++;
170   objc_mutex_unlock(__objc_runtime_mutex);
171   
172   thr_exit(&__objc_thread_exit_status);         /* Terminate thread.        */
173   return -1;
174 }
175
176 /********
177  *  Returns an integer value which uniquely describes a thread.  Must not be
178  *  NULL which is reserved as a marker for "no thread".
179  */
180 objc_thread_t
181 objc_thread_id(void)
182 {
183     return (objc_thread_t)thr_self();
184 }
185
186 /********
187  *  Sets the thread's local storage pointer.  Returns 0 if successful or -1
188  *  if failed.
189  */
190 int
191 objc_thread_set_data(void *value)
192 {
193     if (thr_setspecific(__objc_thread_data_key, value) == 0)
194         return 0;
195     return -1;
196 }
197
198 /********
199  *  Returns the thread's local storage pointer.  Returns NULL on failure.
200  */
201 void *
202 objc_thread_get_data(void)
203 {
204     void *      value = NULL;
205     
206     if (thr_getspecific(__objc_thread_data_key, &value) == 0)
207         return value;                           /* Return thread data.      */
208     
209     return NULL;
210 }
211
212 /********
213  *  Allocate a mutex.  Return the mutex pointer if successful or NULL if
214  *  the allocation fails for any reason.
215  */
216 objc_mutex_t
217 objc_mutex_allocate(void)
218 {
219     struct objc_mutex *mutex;
220     int         err = 0;
221     
222     if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
223         return NULL;                            /* Abort if malloc failed.  */
224     
225     err = mutex_init(&mutex->lock, USYNC_THREAD, 0);
226     
227     if (err != 0) {                             /* System init failed?      */
228         objc_free(mutex);                       /* Yes, free local memory.  */
229         return NULL;                            /* Abort.                   */
230     }
231     mutex->owner = NULL;                        /* No owner.                */
232     mutex->depth = 0;                           /* No locks.                */
233     return mutex;                               /* Return mutex handle.     */
234 }
235
236 /********
237  *  Deallocate a mutex.  Note that this includes an implicit mutex_lock to
238  *  insure that no one else is using the lock.  It is legal to deallocate
239  *  a lock if we have a lock on it, but illegal to deallotcate a lock held
240  *  by anyone else.
241  *  Returns the number of locks on the thread.  (1 for deallocate).
242  */
243 int
244 objc_mutex_deallocate(objc_mutex_t mutex)
245 {
246     int         depth;                          /* # of locks on mutex.     */
247
248     if (!mutex)                                 /* Is argument bad?         */
249         return -1;                              /* Yes, abort.              */
250     depth = objc_mutex_lock(mutex);             /* Must have lock.          */
251     
252     mutex_destroy(&mutex->lock);                /* System deallocate.       */
253     
254     objc_free(mutex);                           /* Free memory.             */
255     return depth;                               /* Return last depth.       */
256 }
257
258 /********
259  *  Grab a lock on a mutex.  If this thread already has a lock on this mutex
260  *  then we increment the lock count.  If another thread has a lock on the 
261  *  mutex we block and wait for the thread to release the lock.
262  *  Returns the lock count on the mutex held by this thread.
263  */
264 int
265 objc_mutex_lock(objc_mutex_t mutex)
266 {
267     objc_thread_t      thread_id;              /* Cache our thread id.     */
268
269     if (!mutex)                                 /* Is argument bad?         */
270         return -1;                              /* Yes, abort.              */
271     thread_id = objc_thread_id();               /* Get this thread's id.    */
272     if (mutex->owner == thread_id)              /* Already own lock?        */
273         return ++mutex->depth;                  /* Yes, increment depth.    */
274
275     if (mutex_lock(&mutex->lock) != 0)          /* Did lock acquire fail?   */
276         return -1;                              /* Yes, abort.              */
277     
278     mutex->owner = thread_id;                   /* Mark thread as owner.    */
279     return mutex->depth = 1;                    /* Increment depth to end.  */
280 }
281
282 /********
283  *  Try to grab a lock on a mutex.  If this thread already has a lock on
284  *  this mutex then we increment the lock count and return it.  If another
285  *  thread has a lock on the mutex returns -1.
286  */
287 int
288 objc_mutex_trylock(objc_mutex_t mutex)
289 {
290     objc_thread_t      thread_id;              /* Cache our thread id.     */
291
292     if (!mutex)                                 /* Is argument bad?         */
293         return -1;                              /* Yes, abort.              */
294     thread_id = objc_thread_id();               /* Get this thread's id.    */
295     if (mutex->owner == thread_id)              /* Already own lock?        */
296         return ++mutex->depth;                  /* Yes, increment depth.    */
297     
298     if (mutex_trylock(&mutex->lock) != 0)       /* Did lock acquire fail?   */
299         return -1;                              /* Yes, abort.              */
300     
301     mutex->owner = thread_id;                   /* Mark thread as owner.    */
302     return mutex->depth = 1;                    /* Increment depth to end.  */
303 }
304
305 /********
306  *  Decrements the lock count on this mutex by one.  If the lock count reaches
307  *  zero, release the lock on the mutex.  Returns the lock count on the mutex.
308  *  It is an error to attempt to unlock a mutex which this thread doesn't hold
309  *  in which case return -1 and the mutex is unaffected.
310  *  Will also return -1 if the mutex free fails.
311  */
312 int
313 objc_mutex_unlock(objc_mutex_t mutex)
314 {
315     objc_thread_t      thread_id;              /* Cache our thread id.     */
316     
317     if (!mutex)                                 /* Is argument bad?         */
318         return -1;                              /* Yes, abort.              */
319     thread_id = objc_thread_id();               /* Get this thread's id.    */
320     if (mutex->owner != thread_id)              /* Does some else own lock? */
321         return -1;                              /* Yes, abort.              */
322     if (mutex->depth > 1)                       /* Released last lock?      */
323         return --mutex->depth;                  /* No, Decrement depth, end.*/
324     mutex->depth = 0;                           /* Yes, reset depth to 0.   */
325     mutex->owner = NULL;                        /* Set owner to "no thread".*/
326     
327     if (mutex_unlock(&mutex->lock) != 0)        /* Did lock release fail?   */
328         return -1;                              /* Yes, return error value. */
329     
330     return 0;                                   /* No, return success.      */
331 }
332
333 /********
334  *  Allocate a condition.  Return the condition pointer if successful or NULL
335  * if the allocation failed for any reason.
336  */
337 objc_condition_t
338 objc_condition_allocate(void)
339 {
340     objc_condition_t condition;
341
342     if (!(condition = (objc_condition_t)objc_malloc(
343                         sizeof(struct objc_condition))))
344         return NULL;                            /* Abort if malloc failed.  */
345
346     cond_init(&(condition->condition), USYNC_THREAD, NULL);
347
348     return condition;                           /* Return new condition     */
349 }
350
351 /********
352  *  Deallocate a condition. Note that this includes an implicit
353  *  condition_broadcast to insure that waiting threads have the opportunity
354  *  to wake.  It is legal to dealloc a condition only if no other
355  *  thread is/will be using it. Here we do NOT check for other threads
356  *  waiting but just wake them up.
357  */
358 int
359 objc_condition_deallocate(objc_condition_t condition)
360 {
361     cond_broadcast(&(condition->condition));    /* Wakeup waiting threads   */
362     cond_destroy(&(condition->condition));      /* Kill condition           */
363     objc_free(condition);                       /* Release struct memory    */
364     return 0;
365 }
366
367 /********
368  *  Wait on the condition unlocking the mutex until objc_condition_signal()
369  *  or objc_condition_broadcast() are called for the same condition. The
370  *  given mutex *must* have the depth set to 1 so that it can be unlocked
371  *  here, so that someone else can lock it and signal/broadcast the condition.
372  *  The mutex is used to lock access to the shared data that make up the
373  *  "condition" predicate.
374  */
375 int
376 objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
377 {
378     objc_thread_t    thread_id;                /* Cache our thread id.     */
379
380     if (!mutex || !condition)                   /* Is argument bad?         */
381         return -1;                              /* Yes, abort.              */
382
383     thread_id = objc_thread_id();               /* Get this thread's id.    */
384     if (mutex->owner != thread_id)              /* Does some else own lock? */
385         return -1;                              /* Yes, abort.              */
386     if (mutex->depth > 1)                       /* Locked more than once ?  */
387         return -1;                              /* YES, return error        */
388                                                 /* mutex will be unlocked   */
389     mutex->depth = 0;                           /* Yes, reset depth to 0.   */
390     mutex->owner = (objc_thread_t) -1;         /* Set owner to "no thread".*/
391
392     cond_wait(&(condition->condition),
393         &(mutex->lock));                        /* unlock, wait ..., lock   */
394
395     mutex->owner = thread_id;                   /* Mark thread as owner.    */
396     mutex->depth = 1;                           /* Must be here !           */
397
398     return 0;                                   /* Return success.          */
399 }
400
401 /********
402  *  Wake up all threads waiting on this condition. It is recommended that
403  *  the called would lock the same mutex as the threads in objc_condition_wait
404  *  before changing the "condition predicate" and make this call and unlock it
405  *  right away after this call.
406  */
407 int
408 objc_condition_broadcast(objc_condition_t condition)
409 {
410     if (!condition)
411         return -1;
412     cond_broadcast(&(condition->condition));
413     return 0;
414 }
415
416 /********
417  *  Wake up one thread waiting on this condition. It is recommended that
418  *  the called would lock the same mutex as the threads in objc_condition_wait
419  *  before changing the "condition predicate" and make this call and unlock it
420  *  right away after this call.
421  */
422 int
423 objc_condition_signal(objc_condition_t condition)
424 {
425     if (!condition)
426         return -1;
427     cond_signal(&(condition->condition));
428     return 0;
429 }
430
431 /* End of File */