OSDN Git Service

* boehm.cc: Undefine TRUE and FALSE.
[pf3gnuchains/gcc-fork.git] / libjava / posix-threads.cc
1 // posix-threads.cc - interface between libjava and POSIX threads.
2
3 /* Copyright (C) 1998, 1999  Cygnus Solutions
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 // TO DO:
12 // * Document signal handling limitations
13
14 #include <config.h>
15
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives.  This is fairly gross.
18 #ifdef HAVE_BOEHM_GC
19 extern "C"
20 {
21 #include <boehm-config.h>
22 #include <gc.h>
23 };
24 #endif /* HAVE_BOEHM_GC */
25
26 #include <stdlib.h>
27 #include <time.h>
28 #include <signal.h>
29
30 #include <cni.h>
31 #include <jvm.h>
32 #include <java/lang/Thread.h>
33 #include <java/lang/System.h>
34
35 // This is used to implement thread startup.
36 struct starter
37 {
38   _Jv_ThreadStartFunc *method;
39   java::lang::Thread *object;
40   _Jv_Thread_t *data;
41 };
42
43 // This is the key used to map from the POSIX thread value back to the
44 // Java object representing the thread.  The key is global to all
45 // threads, so it is ok to make it a global here.
46 pthread_key_t _Jv_ThreadKey;
47
48 // We keep a count of all non-daemon threads which are running.  When
49 // this reaches zero, _Jv_ThreadWait returns.
50 static pthread_mutex_t daemon_mutex;
51 static pthread_cond_t daemon_cond;
52 static int non_daemon_count;
53
54 // The signal to use when interrupting a thread.
55 #ifdef LINUX_THREADS
56   // LinuxThreads usurps both SIGUSR1 and SIGUSR2.
57 #  define INTR SIGHUP
58 #else /* LINUX_THREADS */
59 #  define INTR SIGUSR2
60 #endif /* LINUX_THREADS */
61
62 //
63 // These are the flags that can appear in _Jv_Thread_t.
64 //
65
66 // Thread started.
67 #define FLAG_START   0x01
68 // Thread is daemon.
69 #define FLAG_DAEMON  0x02
70
71 \f
72
73 int
74 _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
75               jlong millis, jint nanos)
76 {
77   int r;
78   pthread_mutex_t *pmu;
79 #ifdef HAVE_RECURSIVE_MUTEX
80   pmu = mu;
81 #else
82   pmu = &mu->mutex2;
83 #endif
84   if (millis == 0 && nanos == 0)
85     r = pthread_cond_wait (cv, pmu);
86   else
87     {
88       struct timespec ts; 
89       jlong m = millis + java::lang::System::currentTimeMillis (); 
90       ts.tv_sec = m / 1000; 
91       ts.tv_nsec = ((m % 1000) * 1000000) + nanos; 
92              
93       r = pthread_cond_timedwait (cv, pmu, &ts);
94       /* A timeout is a normal result.  */
95       if (r && errno == ETIMEDOUT)
96         r = 0;
97     }
98   return r;
99 }
100
101 #ifndef RECURSIVE_MUTEX_IS_DEFAULT
102
103 void
104 _Jv_MutexInit (_Jv_Mutex_t *mu)
105 {
106 #ifdef HAVE_RECURSIVE_MUTEX
107   pthread_mutexattr_t *val = NULL;
108
109 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
110   pthread_mutexattr_t attr;
111
112   // If this is slow, then allocate it statically and only initialize
113   // it once.
114   pthread_mutexattr_init (&attr);
115   pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
116   val = &attr;
117 #elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
118   pthread_mutexattr_t attr;
119   pthread_mutexattr_init (&attr);
120   pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
121   val = &attr;
122 #endif
123
124   pthread_mutex_init (mu, val);
125
126 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
127   pthread_mutexattr_destroy (&attr);
128 #endif
129
130 #else /* HAVE_RECURSIVE_MUTEX */
131
132   // No recursive mutex, so simulate one.
133   pthread_mutex_init (&mu->mutex, NULL);
134   pthread_mutex_init (&mu->mutex2, NULL);
135   pthread_cond_init (&mu->cond, 0);
136   mu->count = 0;
137
138 #endif /* HAVE_RECURSIVE_MUTEX */
139 }
140
141 #endif /* not RECURSIVE_MUTEX_IS_DEFAULT */
142
143 #if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)
144
145 void
146 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
147 {
148   pthread_mutex_destroy (&mu->mutex);
149   pthread_mutex_destroy (&mu->mutex2);
150   pthread_cond_destroy (&mu->cond);
151 }
152
153 int
154 _Jv_MutexLock (_Jv_Mutex_t *mu)
155 {
156   if (pthread_mutex_lock (&mu->mutex))
157     return -1;
158   while (1)
159     {
160       if (mu->count == 0)
161         {
162           // Grab the lock.
163           mu->thread = pthread_self ();
164           mu->count = 1;
165           pthread_mutex_lock (&mu->mutex2);
166           break;
167         }
168       else if (pthread_self () == mu->thread)
169         {
170           // Already have the lock.
171           mu->count += 1;
172           break;
173         }
174       else
175         {
176           // Try to acquire the lock.
177           pthread_cond_wait (&mu->cond, &mu->mutex);
178         }
179     }
180   pthread_mutex_unlock (&mu->mutex);
181   return 0;
182 }
183
184 int
185 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
186 {
187   if (pthread_mutex_lock (&mu->mutex))
188     return -1;
189   int r = 0;
190   if (mu->count == 0 || pthread_self () != mu->thread)
191     r = -1;
192   else
193     {
194       mu->count -= 1;
195       if (! mu->count)
196         {
197           pthread_mutex_unlock (&mu->mutex2);
198           pthread_cond_signal (&mu->cond);
199         }
200     }
201   pthread_mutex_unlock (&mu->mutex);
202   return r;
203 }
204
205 #endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */
206
207 static void
208 handle_intr (int)
209 {
210   // Do nothing.
211 }
212
213 void
214 _Jv_InitThreads (void)
215 {
216   pthread_key_create (&_Jv_ThreadKey, NULL);
217   pthread_mutex_init (&daemon_mutex, NULL);
218   pthread_cond_init (&daemon_cond, 0);
219   non_daemon_count = 0;
220
221   // Arrange for the interrupt signal to interrupt system calls.
222   struct sigaction act;
223   act.sa_handler = handle_intr;
224   sigemptyset (&act.sa_mask);
225   act.sa_flags = 0;
226   sigaction (INTR, &act, NULL);
227
228   // Arrange for SIGINT to be blocked to all threads.  It is only
229   // deliverable to the master thread.
230   sigset_t mask;
231   sigemptyset (&mask);
232   sigaddset (&mask, SIGINT);
233   pthread_sigmask (SIG_BLOCK, &mask, NULL);
234 }
235
236 void
237 _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
238 {
239   _Jv_Thread_t *info = new _Jv_Thread_t;
240
241   info->flags = 0;
242   info->exception = NULL;
243
244   // FIXME register a finalizer for INFO here.
245   // FIXME also must mark INFO somehow.
246
247   *data = info;
248 }
249
250 void
251 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
252 {
253   if (data->flags & FLAG_START)
254     {
255       struct sched_param param;
256
257       param.sched_priority = prio;
258       pthread_setschedparam (data->thread, SCHED_RR, &param);
259     }
260 }
261
262
263 // This is called as a cleanup handler when a thread is exiting.  We
264 // use it to throw the requested exception.  It's entirely possible
265 // that this approach is doomed to failure, in which case we'll need
266 // to adopt some alternate.  For instance, use a signal to implement
267 // _Jv_ThreadCancel.
268 static void
269 throw_cleanup (void *data)
270 {
271   _Jv_Thread_t *td = (_Jv_Thread_t *) data;
272   _Jv_Throw ((java::lang::Throwable *) td->exception);
273 }
274
275 void
276 _Jv_ThreadCancel (_Jv_Thread_t *data, void *error)
277 {
278   data->exception = error;
279   pthread_cancel (data->thread);
280 }
281
282 // This function is called when a thread is started.  We don't arrange
283 // to call the `run' method directly, because this function must
284 // return a value.
285 static void *
286 really_start (void *x)
287 {
288   struct starter *info = (struct starter *) x;
289
290   pthread_cleanup_push (throw_cleanup, info->data);
291   pthread_setspecific (_Jv_ThreadKey, info->object);
292   info->method (info->object);
293   pthread_cleanup_pop (0);
294
295   if (! (info->data->flags & FLAG_DAEMON))
296     {
297       pthread_mutex_lock (&daemon_mutex);
298       --non_daemon_count;
299       if (! non_daemon_count)
300         pthread_cond_signal (&daemon_cond);
301       pthread_mutex_unlock (&daemon_mutex);
302     }
303
304   return NULL;
305 }
306
307 void
308 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
309                  _Jv_ThreadStartFunc *meth)
310 {
311   struct sched_param param;
312   pthread_attr_t attr;
313   struct starter *info;
314
315   if (data->flags & FLAG_START)
316     return;
317   data->flags |= FLAG_START;
318
319   param.sched_priority = thread->getPriority();
320
321   pthread_attr_init (&attr);
322   pthread_attr_setschedparam (&attr, &param);
323
324   // FIXME: handle marking the info object for GC.
325   info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
326   info->method = meth;
327   info->object = thread;
328   info->data = data;
329
330   if (! thread->isDaemon())
331     {
332       pthread_mutex_lock (&daemon_mutex);
333       ++non_daemon_count;
334       pthread_mutex_unlock (&daemon_mutex);
335     }
336   else
337     data->flags |= FLAG_DAEMON;
338   pthread_create (&data->thread, &attr, really_start, (void *) info);
339
340   pthread_attr_destroy (&attr);
341 }
342
343 void
344 _Jv_ThreadWait (void)
345 {
346   // Arrange for SIGINT to be delivered to the master thread.
347   sigset_t mask;
348   sigemptyset (&mask);
349   sigaddset (&mask, SIGINT);
350   pthread_sigmask (SIG_UNBLOCK, &mask, NULL);
351
352   pthread_mutex_lock (&daemon_mutex);
353   if (non_daemon_count)
354     pthread_cond_wait (&daemon_cond, &daemon_mutex);
355   pthread_mutex_unlock (&daemon_mutex);
356 }
357
358 void
359 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
360 {
361   pthread_kill (data->thread, INTR);
362 }