OSDN Git Service

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