OSDN Git Service

(objc_skip_typespec): Don't abort for _C_UNDEF.
[pf3gnuchains/gcc-fork.git] / gcc / objc / thr-single.c
1 /* GNU Objective C Runtime Thread 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 /********
28  *  This structure represents a single mutual exclusion lock.  Lock semantics
29  *  are detailed with the subsequent functions.  We use whatever lock is
30  *  provided by the system.  We augment it with depth and current owner id
31  *  fields to implement and re-entrant lock.
32  */
33 struct _objc_mutex 
34 {
35     volatile _objc_thread_t     owner;          /* Id of thread that owns.  */
36     volatile int                depth;          /* # of acquires.           */
37 };
38
39 /********
40  *  Initialize the threads subsystem.  Returns 0 if successful, or -1 if no
41  *  thread support is available.
42  */
43 int
44 __objc_init_thread_system(void)
45 {
46   DEBUG_PRINTF("__objc_init_thread_system\n");
47   return -1;                                    /* Failed.                  */
48 }
49
50 /********
51  *  Create a new thread of execution and return its id.  Return NULL if fails.
52  *  The new thread starts in "func" with the given argument.
53  */
54 _objc_thread_t
55 objc_thread_create(void (*func)(void *arg), void *arg)
56 {
57   return NULL;                                  /* We can't start threads.  */
58 }
59
60 /********
61  *  Set the current thread's priority.
62  */
63 int
64 objc_thread_set_priority(int priority)
65 {
66   return -1;                                    /* Failed.                  */
67 }
68
69 /********
70  *  Return the current thread's priority.
71  */
72 int
73 objc_thread_get_priority(void)
74 {
75   return OBJC_THREAD_INTERACTIVE_PRIORITY;      /* Highest priority.        */
76 }
77
78 /********
79  *  Yield our process time to another thread.  Any BUSY waiting that is done
80  *  by a thread should use this function to make sure that other threads can
81  *  make progress even on a lazy uniprocessor system.
82  */
83 void
84 objc_thread_yield(void)
85 {
86   return;
87 }
88
89 /********
90  *  Terminate the current tread.  Doesn't return anything.  Doesn't return.
91  *  Actually, if it failed returns -1.
92  */
93 int
94 objc_thread_exit(void)
95 {
96   exit(__objc_thread_exit_status);
97   return -1;
98 }
99
100 /********
101  *  Returns an integer value which uniquely describes a thread.  Must not be
102  *  NULL which is reserved as a marker for "no thread".
103  */
104 _objc_thread_t
105 objc_thread_id(void)
106 {
107   return (_objc_thread_t)1;                     /* No thread support, use 1.*/
108 }
109
110 /********
111  *  Sets the thread's local storage pointer.  Returns 0 if successful or -1
112  *  if failed.
113  */
114
115 static void *thread_local_storage = NULL;
116
117 int
118 objc_thread_set_data(void *value)
119 {
120   thread_local_storage = value;
121   return 0;
122 }
123
124 /********
125  *  Returns the thread's local storage pointer.  Returns NULL on failure.
126  */
127 void *
128 objc_thread_get_data(void)
129 {
130   return thread_local_storage;
131 }
132
133 /********
134  *  Allocate a mutex.  Return the mutex pointer if successful or NULL if the
135  *  allocation failed for any reason.
136  */
137 _objc_mutex_t
138 objc_mutex_allocate(void)
139 {
140     _objc_mutex_t mutex;
141     
142     if (!(mutex = (_objc_mutex_t)__objc_xmalloc(sizeof(struct _objc_mutex))))
143         return NULL;                            /* Abort if malloc failed.  */
144     
145     mutex->owner = NULL;                        /* No owner.                */
146     mutex->depth = 0;                           /* No locks.                */
147     return mutex;                               /* Return mutex handle.     */
148 }
149
150 /********
151  *  Deallocate a mutex.  Note that this includes an implicit mutex_lock to
152  *  insure that no one else is using the lock.  It is legal to deallocate
153  *  a lock if we have a lock on it, but illegal to deallocate a lock held
154  *  by anyone else.
155  *  Returns the number of locks on the thread.  (1 for deallocate).
156  */
157 int
158 objc_mutex_deallocate(_objc_mutex_t mutex)
159 {
160     int         depth;                          /* # of locks on mutex.     */
161
162     if (!mutex)                                 /* Is argument bad?         */
163         return -1;                              /* Yes, abort.              */
164     depth = objc_mutex_lock(mutex);             /* Must have lock.          */
165     
166     free(mutex);                                /* Free memory.             */
167     return depth;                               /* Return last depth.       */
168 }
169
170 /********
171  *  Grab a lock on a mutex.  If this thread already has a lock on this mutex
172  *  then we increment the lock count.  If another thread has a lock on the 
173  *  mutex we block and wait for the thread to release the lock.
174  *  Returns the lock count on the mutex held by this thread.
175  */
176 int
177 objc_mutex_lock(_objc_mutex_t mutex)
178 {
179     _objc_thread_t      thread_id;              /* Cache our thread id.     */
180
181     if (!mutex)                                 /* Is argument bad?         */
182         return -1;                              /* Yes, abort.              */
183     thread_id = objc_thread_id();               /* Get this thread's id.    */
184     if (mutex->owner == thread_id)              /* Already own lock?        */
185         return ++mutex->depth;                  /* Yes, increment depth.    */
186
187     mutex->owner = thread_id;                   /* Mark thread as owner.    */
188
189     return mutex->depth = 1;                    /* Increment depth to end.  */
190 }
191
192 /********
193  *  Try to grab a lock on a mutex.  If this thread already has a lock on
194  *  this mutex then we increment the lock count and return it.  If another
195  *  thread has a lock on the mutex returns -1.
196  */
197 int
198 objc_mutex_trylock(_objc_mutex_t mutex)
199 {
200     _objc_thread_t      thread_id;              /* Cache our thread id.     */
201
202     if (!mutex)                                 /* Is argument bad?         */
203         return -1;                              /* Yes, abort.              */
204     thread_id = objc_thread_id();               /* Get this thread's id.    */
205     if (mutex->owner == thread_id)              /* Already own lock?        */
206         return ++mutex->depth;                  /* Yes, increment depth.    */
207     
208     mutex->owner = thread_id;                   /* Mark thread as owner.    */
209     return mutex->depth = 1;                    /* Increment depth to end.  */
210 }
211
212 /********
213  *  Decrements the lock count on this mutex by one.  If the lock count reaches
214  *  zero, release the lock on the mutex.  Returns the lock count on the mutex.
215  *  It is an error to attempt to unlock a mutex which this thread doesn't hold
216  *  in which case return -1 and the mutex is unaffected.
217  *  Will also return -1 if the mutex free fails.
218  */
219 int
220 objc_mutex_unlock(_objc_mutex_t mutex)
221 {
222     _objc_thread_t  thread_id;                  /* Cache our thread id.     */
223     
224     if (!mutex)                                 /* Is argument bad?         */
225         return -1;                              /* Yes, abort.              */
226     thread_id = objc_thread_id();               /* Get this thread's id.    */
227     if (mutex->owner != thread_id)              /* Does some else own lock? */
228         return -1;                              /* Yes, abort.              */
229     if (mutex->depth > 1)                       /* Released last lock?      */
230         return --mutex->depth;                  /* No, Decrement depth, end.*/
231     mutex->depth = 0;                           /* Yes, reset depth to 0.   */
232     mutex->owner = NULL;                        /* Set owner to "no thread".*/
233     
234     return 0;                                   /* No, return success.      */
235 }
236
237 /* End of File */