OSDN Git Service

Changes to support ObjC as a front-end language.
[pf3gnuchains/gcc-fork.git] / gcc / objc / thr-decosf1.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
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 <pthread.h>
28 #include <objc/thr.h>
29 #include "runtime.h"
30
31 /********
32  *  This structure represents a single mutual exclusion lock.  Lock semantics
33  *  are detailed with the subsequent functions.  We use whatever lock is
34  *  provided by the system.  We augment it with depth and current owner id
35  *  fields to implement and re-entrant lock.
36  */
37 struct objc_mutex 
38 {
39     volatile objc_thread_t      owner;          /* Id of thread that owns.  */
40     volatile int                depth;          /* # of acquires.           */
41     pthread_mutex_t             lock;           /* pthread mutex.           */
42 };
43
44 /*****************************************************************************
45  *  Static variables.
46  */
47 static pthread_key_t    __objc_thread_data_key; /* Data key for thread data.*/
48
49
50 /********
51  *  Initialize the threads subsystem.  Returns 0 if successful, or -1 if no
52  *  thread support is available.
53  */
54 int
55 __objc_init_thread_system(void)
56 {
57     printf("__objc_init_thread_system\n");
58     
59     if (pthread_keycreate(&__objc_thread_data_key, NULL) == 0)
60         return 0;                               /* Yes, return success.     */
61     
62     return -1;                                  /* Failed.                  */
63 }
64
65 int
66 __objc_fini_thread_system(void)
67 {
68   return 0;
69 }
70
71 /********
72  *  Create a new thread of execution and return its id.  Return NULL if fails.
73  *  The new thread starts in "func" with the given argument.
74  */
75 objc_thread_t
76 objc_thread_create(void (*func)(void *arg), void *arg)
77 {
78     objc_thread_t      thread_id = NULL;       /* Detached thread id.      */
79     pthread_t           new_thread_handle;      /* DCE thread handle.       */
80
81     objc_mutex_lock(__objc_runtime_mutex);
82     
83     if (pthread_create(&new_thread_handle, pthread_attr_default,
84                        (void *)func, arg) == 0) {
85       /* ??? May not work! (64bit)*/
86         thread_id = *(objc_thread_t *)&new_thread_handle; 
87         pthread_detach(&new_thread_handle);     /* Fully detach thread.     */
88         __objc_runtime_threads_alive++;
89     }
90     
91     objc_mutex_unlock(__objc_runtime_mutex);
92     return thread_id;
93 }
94
95 /********
96  *  Set the current thread's priority.
97  */
98 int
99 objc_thread_set_priority(int priority)
100 {
101     int         sys_priority = 0;
102
103     switch (priority) {
104     case OBJC_THREAD_INTERACTIVE_PRIORITY:
105         sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
106         break;
107     default:
108     case OBJC_THREAD_BACKGROUND_PRIORITY:
109         sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
110         break;
111     case OBJC_THREAD_LOW_PRIORITY:
112         sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
113         break;
114     }
115     
116     if (pthread_setprio(pthread_self(), sys_priority) >= 0)
117         return 0;                               /* Changed priority. End.   */
118     
119     return -1;                                  /* Failed.                  */
120 }
121
122 /********
123  *  Return the current thread's priority.
124  */
125 int
126 objc_thread_get_priority(void)
127 {
128     int         sys_priority;                   /* DCE thread priority.     */
129     
130     if ((sys_priority = pthread_getprio(pthread_self())) >= 0) {
131         if (sys_priority >= PRI_FG_MIN_NP && sys_priority <= PRI_FG_MAX_NP)
132             return OBJC_THREAD_INTERACTIVE_PRIORITY;
133         if (sys_priority >= PRI_BG_MIN_NP && sys_priority <= PRI_BG_MAX_NP)
134             return OBJC_THREAD_BACKGROUND_PRIORITY;
135         return OBJC_THREAD_LOW_PRIORITY;
136     }
137     return -1;                                  /* Couldn't get priority.   */
138 }
139
140 /********
141  *  Yield our process time to another thread.  Any BUSY waiting that is done
142  *  by a thread should use this function to make sure that other threads can
143  *  make progress even on a lazy uniprocessor system.
144  */
145 void
146 objc_thread_yield(void)
147 {
148     pthread_yield();                            /* Yield to equal thread.   */
149 }
150
151 /********
152  *  Terminate the current tread.  Doesn't return anything.  Doesn't return.
153  *  Actually, if it failed returns -1.
154  */
155 int
156 objc_thread_exit(void)
157 {
158   objc_mutex_lock(__objc_runtime_mutex);
159   __objc_runtime_threads_alive--;
160   objc_mutex_unlock(__objc_runtime_mutex);
161       
162   pthread_exit(&__objc_thread_exit_status);     /* Terminate thread.        */
163   return -1;
164 }
165
166 /********
167  *  Returns an integer value which uniquely describes a thread.  Must not be
168  *  -1 which is reserved as a marker for "no thread".
169  */
170 objc_thread_t
171 objc_thread_id(void)
172 {
173   pthread_t self = pthread_self();
174
175   return (objc_thread_t) pthread_getuniqe_np (&self);
176 }
177
178 /********
179  *  Sets the thread's local storage pointer.  Returns 0 if successful or -1
180  *  if failed.
181  */
182 int
183 objc_thread_set_data(void *value)
184 {
185     if (pthread_setspecific(__objc_thread_data_key, (void *)value) == 0)
186         return 0;                               /* Return thread data.      */
187     return -1;
188 }
189
190 /********
191  *  Returns the thread's local storage pointer.  Returns NULL on failure.
192  */
193 void *
194 objc_thread_get_data(void)
195 {
196     void *      value = NULL;
197     
198     if (pthread_getspecific(__objc_thread_data_key, (void *)&value) == 0)
199         return value;                           /* Return thread data.      */
200     
201     return NULL;
202 }
203
204 /********
205  *  Allocate a mutex.  Return the mutex pointer if successful or NULL if
206  *  the allocation fails for any reason.
207  */
208 objc_mutex_t
209 objc_mutex_allocate(void)
210 {
211     objc_mutex_t mutex;
212     int         err = 0;
213     
214     if (!(mutex = (objc_mutex_t)objc_malloc(sizeof(struct objc_mutex))))
215         return NULL;                            /* Abort if malloc failed.  */
216     
217     err = pthread_mutex_init(&mutex->lock, pthread_mutexattr_default);
218     
219     if (err != 0) {                             /* System init failed?      */
220         objc_free(mutex);                       /* Yes, free local memory.  */
221         return NULL;                            /* Abort.                   */
222     }
223     mutex->owner = (objc_thread_t) -1;         /* No owner.                */
224     mutex->depth = 0;                           /* No locks.                */
225     return mutex;                               /* Return mutex handle.     */
226 }
227
228 /********
229  *  Deallocate a mutex.  Note that this includes an implicit mutex_lock to
230  *  insure that no one else is using the lock.  It is legal to deallocate
231  *  a lock if we have a lock on it, but illegal to deallotcate a lock held
232  *  by anyone else.
233  *  Returns the number of locks on the thread.  (1 for deallocate).
234  */
235 int
236 objc_mutex_deallocate(objc_mutex_t mutex)
237 {
238     int         depth;                          /* # of locks on mutex.     */
239
240     if (!mutex)                                 /* Is argument bad?         */
241         return -1;                              /* Yes, abort.              */
242     depth = objc_mutex_lock(mutex);             /* Must have lock.          */
243     
244     pthread_mutex_unlock(&mutex->lock);         /* Must unlock system mutex.*/
245     pthread_mutex_destroy(&mutex->lock);        /* Free system mutex.       */
246     
247     objc_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 (pthread_mutex_lock(&mutex->lock) != 0)  /* Lock DCE system mutex.   */
269         return -1;                              /* Failed, 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 (pthread_mutex_trylock(&mutex->lock) != 1) /* Lock DCE system mutex. */
292         return -1;                              /* Failed, 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 = (objc_thread_t) -1;         /* Set owner to "no thread".*/
319     
320     if (pthread_mutex_unlock(&mutex->lock) != 0)  /* Unlock system mutex.   */
321         return -1;                              /* Failed, abort.           */
322     
323     return 0;                                   /* No, return success.      */
324 }
325
326 /* End of File */