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