]> git.ipfire.org Git - thirdparty/gcc.git/blame - libjava/posix-threads.cc
Fix typo in docs.
[thirdparty/gcc.git] / libjava / posix-threads.cc
CommitLineData
ee9dd372
TT
1// posix-threads.cc - interface between libjava and POSIX threads.
2
c58f2900 3/* Copyright (C) 1998, 1999, 2000, 2001, 2004 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
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
ee9dd372 19#include <gc.h>
ee9dd372
TT
20#endif /* HAVE_BOEHM_GC */
21
22#include <stdlib.h>
23#include <time.h>
24#include <signal.h>
d55d01bd 25#include <errno.h>
43cbc943 26#include <limits.h>
66c4e258
JT
27#ifdef HAVE_UNISTD_H
28#include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
29#endif
ee9dd372 30
27e934d8 31#include <gcj/cni.h>
ee9dd372
TT
32#include <jvm.h>
33#include <java/lang/Thread.h>
34#include <java/lang/System.h>
43cbc943
BM
35#include <java/lang/Long.h>
36#include <java/lang/OutOfMemoryError.h>
c58f2900 37#include <java/lang/InternalError.h>
ee9dd372
TT
38
39// This is used to implement thread startup.
40struct starter
41{
42 _Jv_ThreadStartFunc *method;
ee9dd372
TT
43 _Jv_Thread_t *data;
44};
45
46// This is the key used to map from the POSIX thread value back to the
47// Java object representing the thread. The key is global to all
48// threads, so it is ok to make it a global here.
49pthread_key_t _Jv_ThreadKey;
50
fd59e3a0
TT
51// This is the key used to map from the POSIX thread value back to the
52// _Jv_Thread_t* representing the thread.
53pthread_key_t _Jv_ThreadDataKey;
54
ee9dd372
TT
55// We keep a count of all non-daemon threads which are running. When
56// this reaches zero, _Jv_ThreadWait returns.
57static pthread_mutex_t daemon_mutex;
58static pthread_cond_t daemon_cond;
59static int non_daemon_count;
60
61// The signal to use when interrupting a thread.
da979152 62#if defined(LINUX_THREADS) || defined(FREEBSD_THREADS)
43cbc943 63 // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
da979152 64 // GC on FreeBSD uses both SIGUSR1 and SIGUSR2.
ee9dd372
TT
65# define INTR SIGHUP
66#else /* LINUX_THREADS */
67# define INTR SIGUSR2
68#endif /* LINUX_THREADS */
69
70//
71// These are the flags that can appear in _Jv_Thread_t.
72//
73
74// Thread started.
75#define FLAG_START 0x01
76// Thread is daemon.
77#define FLAG_DAEMON 0x02
78
79\f
80
b834f1fa
BM
81// Wait for the condition variable "CV" to be notified.
82// Return values:
83// 0: the condition was notified, or the timeout expired.
84// _JV_NOT_OWNER: the thread does not own the mutex "MU".
85// _JV_INTERRUPTED: the thread was interrupted. Its interrupted flag is set.
ee9dd372
TT
86int
87_Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
88 jlong millis, jint nanos)
89{
b834f1fa
BM
90 pthread_t self = pthread_self();
91 if (mu->owner != self)
92 return _JV_NOT_OWNER;
6e87747b 93
43cbc943 94 struct timespec ts;
b834f1fa 95 jlong m, startTime;
6e87747b 96
b834f1fa 97 if (millis > 0 || nanos > 0)
ee9dd372 98 {
fd59e3a0
TT
99 startTime = java::lang::System::currentTimeMillis();
100 m = millis + startTime;
43cbc943
BM
101 ts.tv_sec = m / 1000;
102 ts.tv_nsec = ((m % 1000) * 1000000) + nanos;
103 }
fd59e3a0 104
b834f1fa
BM
105 _Jv_Thread_t *current = _Jv_ThreadCurrentData ();
106 java::lang::Thread *current_obj = _Jv_ThreadCurrent ();
107
304daac5
TT
108 pthread_mutex_lock (&current->wait_mutex);
109
110 // Now that we hold the wait mutex, check if this thread has been
111 // interrupted already.
112 if (current_obj->interrupt_flag)
113 {
114 pthread_mutex_unlock (&current->wait_mutex);
115 return _JV_INTERRUPTED;
116 }
117
b834f1fa
BM
118 // Add this thread to the cv's wait set.
119 current->next = NULL;
fd59e3a0 120
b834f1fa
BM
121 if (cv->first == NULL)
122 cv->first = current;
123 else
124 for (_Jv_Thread_t *t = cv->first;; t = t->next)
125 {
126 if (t->next == NULL)
127 {
128 t->next = current;
129 break;
130 }
131 }
132
b834f1fa
BM
133 // Record the current lock depth, so it can be restored when we re-aquire it.
134 int count = mu->count;
135
136 // Release the monitor mutex.
137 mu->count = 0;
138 mu->owner = 0;
139 pthread_mutex_unlock (&mu->mutex);
140
141 int r = 0;
142 bool done_sleeping = false;
143
144 while (! done_sleeping)
145 {
146 if (millis == 0 && nanos == 0)
147 r = pthread_cond_wait (&current->wait_cond, &current->wait_mutex);
148 else
149 r = pthread_cond_timedwait (&current->wait_cond, &current->wait_mutex,
150 &ts);
e301621d 151
b834f1fa
BM
152 // In older glibc's (prior to 2.1.3), the cond_wait functions may
153 // spuriously wake up on a signal. Catch that here.
154 if (r != EINTR)
155 done_sleeping = true;
156 }
157
304daac5 158 // Check for an interrupt *before* releasing the wait mutex.
b834f1fa
BM
159 jboolean interrupted = current_obj->interrupt_flag;
160
161 pthread_mutex_unlock (&current->wait_mutex);
162
163 // Reaquire the monitor mutex, and restore the lock count.
164 pthread_mutex_lock (&mu->mutex);
165 mu->owner = self;
166 mu->count = count;
167
18e1f2bd 168 // If we were interrupted, or if a timeout occurred, remove ourself from
b834f1fa
BM
169 // the cv wait list now. (If we were notified normally, notify() will have
170 // already taken care of this)
171 if (r == ETIMEDOUT || interrupted)
172 {
173 _Jv_Thread_t *prev = NULL;
174 for (_Jv_Thread_t *t = cv->first; t != NULL; t = t->next)
43cbc943 175 {
b834f1fa 176 if (t == current)
fd59e3a0 177 {
b834f1fa
BM
178 if (prev != NULL)
179 prev->next = t->next;
180 else
181 cv->first = t->next;
182 t->next = NULL;
183 break;
fd59e3a0 184 }
b834f1fa 185 prev = t;
fd59e3a0 186 }
b834f1fa
BM
187 if (interrupted)
188 return _JV_INTERRUPTED;
ee9dd372 189 }
b834f1fa
BM
190
191 return 0;
ee9dd372
TT
192}
193
b834f1fa
BM
194int
195_Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
ee9dd372 196{
16a10fb6 197 if (_Jv_MutexCheckMonitor (mu))
b834f1fa 198 return _JV_NOT_OWNER;
ee9dd372 199
b834f1fa
BM
200 _Jv_Thread_t *target;
201 _Jv_Thread_t *prev = NULL;
ee9dd372 202
b834f1fa
BM
203 for (target = cv->first; target != NULL; target = target->next)
204 {
205 pthread_mutex_lock (&target->wait_mutex);
ee9dd372 206
b834f1fa
BM
207 if (target->thread_obj->interrupt_flag)
208 {
209 // Don't notify a thread that has already been interrupted.
210 pthread_mutex_unlock (&target->wait_mutex);
211 prev = target;
212 continue;
213 }
ee9dd372 214
b834f1fa
BM
215 pthread_cond_signal (&target->wait_cond);
216 pthread_mutex_unlock (&target->wait_mutex);
ee9dd372 217
f52c7239
BM
218 // Two concurrent notify() calls must not be delivered to the same
219 // thread, so remove the target thread from the cv wait list now.
b834f1fa
BM
220 if (prev == NULL)
221 cv->first = target->next;
222 else
223 prev->next = target->next;
224
225 target->next = NULL;
226
227 break;
228 }
ee9dd372 229
b834f1fa 230 return 0;
ee9dd372
TT
231}
232
233int
b834f1fa 234_Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
ee9dd372 235{
16a10fb6 236 if (_Jv_MutexCheckMonitor (mu))
b834f1fa
BM
237 return _JV_NOT_OWNER;
238
239 _Jv_Thread_t *target;
240 _Jv_Thread_t *prev = NULL;
241
242 for (target = cv->first; target != NULL; target = target->next)
ee9dd372 243 {
b834f1fa
BM
244 pthread_mutex_lock (&target->wait_mutex);
245 pthread_cond_signal (&target->wait_cond);
246 pthread_mutex_unlock (&target->wait_mutex);
247
248 if (prev != NULL)
249 prev->next = NULL;
250 prev = target;
ee9dd372 251 }
b834f1fa
BM
252 if (prev != NULL)
253 prev->next = NULL;
254
255 cv->first = NULL;
256
ee9dd372
TT
257 return 0;
258}
259
b834f1fa
BM
260void
261_Jv_ThreadInterrupt (_Jv_Thread_t *data)
ee9dd372 262{
b834f1fa 263 pthread_mutex_lock (&data->wait_mutex);
ee9dd372 264
b834f1fa
BM
265 // Set the thread's interrupted flag *after* aquiring its wait_mutex. This
266 // ensures that there are no races with the interrupt flag being set after
267 // the waiting thread checks it and before pthread_cond_wait is entered.
268 data->thread_obj->interrupt_flag = true;
269
270 // Interrupt blocking system calls using a signal.
f449e885 271 pthread_kill (data->thread, INTR);
b834f1fa
BM
272
273 pthread_cond_signal (&data->wait_cond);
274
275 pthread_mutex_unlock (&data->wait_mutex);
276}
ee9dd372
TT
277
278static void
279handle_intr (int)
280{
281 // Do nothing.
282}
283
7e9534bc
DD
284static void
285block_sigchld()
286{
287 sigset_t mask;
288 sigemptyset (&mask);
289 sigaddset (&mask, SIGCHLD);
290 int c = pthread_sigmask (SIG_BLOCK, &mask, NULL);
291 if (c != 0)
292 JvFail (strerror (c));
293}
294
ee9dd372
TT
295void
296_Jv_InitThreads (void)
297{
298 pthread_key_create (&_Jv_ThreadKey, NULL);
fd59e3a0 299 pthread_key_create (&_Jv_ThreadDataKey, NULL);
ee9dd372
TT
300 pthread_mutex_init (&daemon_mutex, NULL);
301 pthread_cond_init (&daemon_cond, 0);
302 non_daemon_count = 0;
303
304 // Arrange for the interrupt signal to interrupt system calls.
305 struct sigaction act;
306 act.sa_handler = handle_intr;
307 sigemptyset (&act.sa_mask);
308 act.sa_flags = 0;
309 sigaction (INTR, &act, NULL);
7e9534bc
DD
310
311 // Block SIGCHLD here to ensure that any non-Java threads inherit the new
312 // signal mask.
313 block_sigchld();
ee9dd372
TT
314}
315
e301621d
BM
316_Jv_Thread_t *
317_Jv_ThreadInitData (java::lang::Thread *obj)
ee9dd372 318{
0539f1f7 319 _Jv_Thread_t *data = (_Jv_Thread_t *) _Jv_Malloc (sizeof (_Jv_Thread_t));
e301621d
BM
320 data->flags = 0;
321 data->thread_obj = obj;
b834f1fa 322
e301621d
BM
323 pthread_mutex_init (&data->wait_mutex, NULL);
324 pthread_cond_init (&data->wait_cond, NULL);
ee9dd372 325
e301621d
BM
326 return data;
327}
ee9dd372 328
e301621d
BM
329void
330_Jv_ThreadDestroyData (_Jv_Thread_t *data)
331{
332 pthread_mutex_destroy (&data->wait_mutex);
333 pthread_cond_destroy (&data->wait_cond);
0539f1f7 334 _Jv_Free ((void *)data);
ee9dd372
TT
335}
336
337void
338_Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
339{
66c4e258 340#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
ee9dd372
TT
341 if (data->flags & FLAG_START)
342 {
343 struct sched_param param;
344
345 param.sched_priority = prio;
5e180a3f 346 pthread_setschedparam (data->thread, SCHED_OTHER, &param);
ee9dd372 347 }
66c4e258 348#endif
ee9dd372
TT
349}
350
c93d7fae
PB
351void
352_Jv_ThreadRegister (_Jv_Thread_t *data)
353{
354 pthread_setspecific (_Jv_ThreadKey, data->thread_obj);
355 pthread_setspecific (_Jv_ThreadDataKey, data);
356
357 // glibc 2.1.3 doesn't set the value of `thread' until after start_routine
358 // is called. Since it may need to be accessed from the new thread, work
359 // around the potential race here by explicitly setting it again.
360 data->thread = pthread_self ();
3610e0d5
TT
361
362# ifdef SLOW_PTHREAD_SELF
363 // Clear all self cache slots that might be needed by this thread.
364 int dummy;
365 int low_index = SC_INDEX(&dummy) + SC_CLEAR_MIN;
366 int high_index = SC_INDEX(&dummy) + SC_CLEAR_MAX;
367 for (int i = low_index; i <= high_index; ++i)
368 {
369 int current_index = i;
370 if (current_index < 0)
371 current_index += SELF_CACHE_SIZE;
372 if (current_index >= SELF_CACHE_SIZE)
373 current_index -= SELF_CACHE_SIZE;
374 _Jv_self_cache[current_index].high_sp_bits = BAD_HIGH_SP_VALUE;
375 }
376# endif
c58f2900
DD
377 // Block SIGCHLD which is used in natPosixProcess.cc.
378 block_sigchld();
c93d7fae
PB
379}
380
381void
382_Jv_ThreadUnRegister ()
383{
384 pthread_setspecific (_Jv_ThreadKey, NULL);
385 pthread_setspecific (_Jv_ThreadDataKey, NULL);
386}
387
ee9dd372
TT
388// This function is called when a thread is started. We don't arrange
389// to call the `run' method directly, because this function must
390// return a value.
391static void *
392really_start (void *x)
393{
394 struct starter *info = (struct starter *) x;
395
c93d7fae 396 _Jv_ThreadRegister (info->data);
b834f1fa
BM
397
398 info->method (info->data->thread_obj);
3610e0d5 399
ee9dd372
TT
400 if (! (info->data->flags & FLAG_DAEMON))
401 {
402 pthread_mutex_lock (&daemon_mutex);
403 --non_daemon_count;
404 if (! non_daemon_count)
405 pthread_cond_signal (&daemon_cond);
406 pthread_mutex_unlock (&daemon_mutex);
407 }
3610e0d5 408
ee9dd372
TT
409 return NULL;
410}
411
412void
413_Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
414 _Jv_ThreadStartFunc *meth)
415{
416 struct sched_param param;
417 pthread_attr_t attr;
418 struct starter *info;
419
420 if (data->flags & FLAG_START)
421 return;
422 data->flags |= FLAG_START;
423
c58f2900
DD
424 // Block SIGCHLD which is used in natPosixProcess.cc.
425 // The current mask is inherited by the child thread.
426 block_sigchld();
427
ee9dd372
TT
428 param.sched_priority = thread->getPriority();
429
430 pthread_attr_init (&attr);
431 pthread_attr_setschedparam (&attr, &param);
e301621d 432 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
ee9dd372 433
ee9dd372
TT
434 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
435 info->method = meth;
ee9dd372
TT
436 info->data = data;
437
438 if (! thread->isDaemon())
439 {
440 pthread_mutex_lock (&daemon_mutex);
441 ++non_daemon_count;
442 pthread_mutex_unlock (&daemon_mutex);
443 }
444 else
445 data->flags |= FLAG_DAEMON;
43cbc943
BM
446 int r = pthread_create (&data->thread, &attr, really_start, (void *) info);
447
ee9dd372 448 pthread_attr_destroy (&attr);
43cbc943
BM
449
450 if (r)
451 {
452 const char* msg = "Cannot create additional threads";
b3208f56 453 throw new java::lang::OutOfMemoryError (JvNewStringUTF (msg));
43cbc943 454 }
ee9dd372
TT
455}
456
457void
458_Jv_ThreadWait (void)
459{
ee9dd372
TT
460 pthread_mutex_lock (&daemon_mutex);
461 if (non_daemon_count)
462 pthread_cond_wait (&daemon_cond, &daemon_mutex);
463 pthread_mutex_unlock (&daemon_mutex);
464}
3610e0d5
TT
465
466#if defined(SLOW_PTHREAD_SELF)
467
45597167 468#include "sysdep/locks.h"
3610e0d5 469
45597167 470// Support for pthread_self() lookup cache.
3610e0d5
TT
471volatile self_cache_entry _Jv_self_cache[SELF_CACHE_SIZE];
472
3610e0d5
TT
473_Jv_ThreadId_t
474_Jv_ThreadSelf_out_of_line(volatile self_cache_entry *sce, size_t high_sp_bits)
475{
476 pthread_t self = pthread_self();
3610e0d5 477 sce -> high_sp_bits = high_sp_bits;
1de21d0e
BM
478 write_barrier();
479 sce -> self = self;
3610e0d5
TT
480 return self;
481}
482
483#endif /* SLOW_PTHREAD_SELF */