OSDN Git Service

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