1 // posix-threads.cc - interface between libjava and POSIX threads.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2004 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 // * Document signal handling limitations
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives. This is fairly gross.
20 #endif /* HAVE_BOEHM_GC */
28 #include <unistd.h> // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
33 #include <java/lang/Thread.h>
34 #include <java/lang/System.h>
35 #include <java/lang/Long.h>
36 #include <java/lang/OutOfMemoryError.h>
37 #include <java/lang/InternalError.h>
39 // This is used to implement thread startup.
42 _Jv_ThreadStartFunc
*method
;
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.
49 pthread_key_t _Jv_ThreadKey
;
51 // This is the key used to map from the POSIX thread value back to the
52 // _Jv_Thread_t* representing the thread.
53 pthread_key_t _Jv_ThreadDataKey
;
55 // We keep a count of all non-daemon threads which are running. When
56 // this reaches zero, _Jv_ThreadWait returns.
57 static pthread_mutex_t daemon_mutex
;
58 static pthread_cond_t daemon_cond
;
59 static int non_daemon_count
;
61 // The signal to use when interrupting a thread.
62 #if defined(LINUX_THREADS) || defined(FREEBSD_THREADS)
63 // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
64 // GC on FreeBSD uses both SIGUSR1 and SIGUSR2.
66 #else /* LINUX_THREADS */
68 #endif /* LINUX_THREADS */
71 // These are the flags that can appear in _Jv_Thread_t.
75 #define FLAG_START 0x01
77 #define FLAG_DAEMON 0x02
81 // Wait for the condition variable "CV" to be notified.
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.
87 _Jv_CondWait (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
,
88 jlong millis
, jint nanos
)
90 pthread_t self
= pthread_self();
91 if (mu
->owner
!= self
)
97 if (millis
> 0 || nanos
> 0)
99 startTime
= java::lang::System::currentTimeMillis();
100 m
= millis
+ startTime
;
101 ts
.tv_sec
= m
/ 1000;
102 ts
.tv_nsec
= ((m
% 1000) * 1000000) + nanos
;
105 _Jv_Thread_t
*current
= _Jv_ThreadCurrentData ();
106 java::lang::Thread
*current_obj
= _Jv_ThreadCurrent ();
108 pthread_mutex_lock (¤t
->wait_mutex
);
110 // Now that we hold the wait mutex, check if this thread has been
111 // interrupted already.
112 if (current_obj
->interrupt_flag
)
114 pthread_mutex_unlock (¤t
->wait_mutex
);
115 return _JV_INTERRUPTED
;
118 // Add this thread to the cv's wait set.
119 current
->next
= NULL
;
121 if (cv
->first
== NULL
)
124 for (_Jv_Thread_t
*t
= cv
->first
;; t
= t
->next
)
133 // Record the current lock depth, so it can be restored when we re-aquire it.
134 int count
= mu
->count
;
136 // Release the monitor mutex.
139 pthread_mutex_unlock (&mu
->mutex
);
142 bool done_sleeping
= false;
144 while (! done_sleeping
)
146 if (millis
== 0 && nanos
== 0)
147 r
= pthread_cond_wait (¤t
->wait_cond
, ¤t
->wait_mutex
);
149 r
= pthread_cond_timedwait (¤t
->wait_cond
, ¤t
->wait_mutex
,
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.
155 done_sleeping
= true;
158 // Check for an interrupt *before* releasing the wait mutex.
159 jboolean interrupted
= current_obj
->interrupt_flag
;
161 pthread_mutex_unlock (¤t
->wait_mutex
);
163 // Reaquire the monitor mutex, and restore the lock count.
164 pthread_mutex_lock (&mu
->mutex
);
168 // If we were interrupted, or if a timeout occurred, remove ourself from
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
)
173 _Jv_Thread_t
*prev
= NULL
;
174 for (_Jv_Thread_t
*t
= cv
->first
; t
!= NULL
; t
= t
->next
)
179 prev
->next
= t
->next
;
188 return _JV_INTERRUPTED
;
195 _Jv_CondNotify (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
197 if (_Jv_MutexCheckMonitor (mu
))
198 return _JV_NOT_OWNER
;
200 _Jv_Thread_t
*target
;
201 _Jv_Thread_t
*prev
= NULL
;
203 for (target
= cv
->first
; target
!= NULL
; target
= target
->next
)
205 pthread_mutex_lock (&target
->wait_mutex
);
207 if (target
->thread_obj
->interrupt_flag
)
209 // Don't notify a thread that has already been interrupted.
210 pthread_mutex_unlock (&target
->wait_mutex
);
215 pthread_cond_signal (&target
->wait_cond
);
216 pthread_mutex_unlock (&target
->wait_mutex
);
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.
221 cv
->first
= target
->next
;
223 prev
->next
= target
->next
;
234 _Jv_CondNotifyAll (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
236 if (_Jv_MutexCheckMonitor (mu
))
237 return _JV_NOT_OWNER
;
239 _Jv_Thread_t
*target
;
240 _Jv_Thread_t
*prev
= NULL
;
242 for (target
= cv
->first
; target
!= NULL
; target
= target
->next
)
244 pthread_mutex_lock (&target
->wait_mutex
);
245 pthread_cond_signal (&target
->wait_cond
);
246 pthread_mutex_unlock (&target
->wait_mutex
);
261 _Jv_ThreadInterrupt (_Jv_Thread_t
*data
)
263 pthread_mutex_lock (&data
->wait_mutex
);
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;
270 // Interrupt blocking system calls using a signal.
271 pthread_kill (data
->thread
, INTR
);
273 pthread_cond_signal (&data
->wait_cond
);
275 pthread_mutex_unlock (&data
->wait_mutex
);
285 _Jv_InitThreads (void)
287 pthread_key_create (&_Jv_ThreadKey
, NULL
);
288 pthread_key_create (&_Jv_ThreadDataKey
, NULL
);
289 pthread_mutex_init (&daemon_mutex
, NULL
);
290 pthread_cond_init (&daemon_cond
, 0);
291 non_daemon_count
= 0;
293 // Arrange for the interrupt signal to interrupt system calls.
294 struct sigaction act
;
295 act
.sa_handler
= handle_intr
;
296 sigemptyset (&act
.sa_mask
);
298 sigaction (INTR
, &act
, NULL
);
302 _Jv_ThreadInitData (java::lang::Thread
*obj
)
304 _Jv_Thread_t
*data
= (_Jv_Thread_t
*) _Jv_Malloc (sizeof (_Jv_Thread_t
));
306 data
->thread_obj
= obj
;
308 pthread_mutex_init (&data
->wait_mutex
, NULL
);
309 pthread_cond_init (&data
->wait_cond
, NULL
);
315 _Jv_ThreadDestroyData (_Jv_Thread_t
*data
)
317 pthread_mutex_destroy (&data
->wait_mutex
);
318 pthread_cond_destroy (&data
->wait_cond
);
319 _Jv_Free ((void *)data
);
323 _Jv_ThreadSetPriority (_Jv_Thread_t
*data
, jint prio
)
325 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
326 if (data
->flags
& FLAG_START
)
328 struct sched_param param
;
330 param
.sched_priority
= prio
;
331 pthread_setschedparam (data
->thread
, SCHED_RR
, ¶m
);
341 sigaddset (&mask
, SIGCHLD
);
342 int c
= pthread_sigmask (SIG_BLOCK
, &mask
, NULL
);
344 throw new java::lang::InternalError (JvNewStringUTF (strerror (c
)));
348 _Jv_ThreadRegister (_Jv_Thread_t
*data
)
350 pthread_setspecific (_Jv_ThreadKey
, data
->thread_obj
);
351 pthread_setspecific (_Jv_ThreadDataKey
, data
);
353 // glibc 2.1.3 doesn't set the value of `thread' until after start_routine
354 // is called. Since it may need to be accessed from the new thread, work
355 // around the potential race here by explicitly setting it again.
356 data
->thread
= pthread_self ();
358 # ifdef SLOW_PTHREAD_SELF
359 // Clear all self cache slots that might be needed by this thread.
361 int low_index
= SC_INDEX(&dummy
) + SC_CLEAR_MIN
;
362 int high_index
= SC_INDEX(&dummy
) + SC_CLEAR_MAX
;
363 for (int i
= low_index
; i
<= high_index
; ++i
)
365 int current_index
= i
;
366 if (current_index
< 0)
367 current_index
+= SELF_CACHE_SIZE
;
368 if (current_index
>= SELF_CACHE_SIZE
)
369 current_index
-= SELF_CACHE_SIZE
;
370 _Jv_self_cache
[current_index
].high_sp_bits
= BAD_HIGH_SP_VALUE
;
373 // Block SIGCHLD which is used in natPosixProcess.cc.
378 _Jv_ThreadUnRegister ()
380 pthread_setspecific (_Jv_ThreadKey
, NULL
);
381 pthread_setspecific (_Jv_ThreadDataKey
, NULL
);
384 // This function is called when a thread is started. We don't arrange
385 // to call the `run' method directly, because this function must
388 really_start (void *x
)
390 struct starter
*info
= (struct starter
*) x
;
392 _Jv_ThreadRegister (info
->data
);
394 info
->method (info
->data
->thread_obj
);
396 if (! (info
->data
->flags
& FLAG_DAEMON
))
398 pthread_mutex_lock (&daemon_mutex
);
400 if (! non_daemon_count
)
401 pthread_cond_signal (&daemon_cond
);
402 pthread_mutex_unlock (&daemon_mutex
);
409 _Jv_ThreadStart (java::lang::Thread
*thread
, _Jv_Thread_t
*data
,
410 _Jv_ThreadStartFunc
*meth
)
412 struct sched_param param
;
414 struct starter
*info
;
416 if (data
->flags
& FLAG_START
)
418 data
->flags
|= FLAG_START
;
420 // Block SIGCHLD which is used in natPosixProcess.cc.
421 // The current mask is inherited by the child thread.
424 param
.sched_priority
= thread
->getPriority();
426 pthread_attr_init (&attr
);
427 pthread_attr_setschedparam (&attr
, ¶m
);
428 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
430 info
= (struct starter
*) _Jv_AllocBytes (sizeof (struct starter
));
434 if (! thread
->isDaemon())
436 pthread_mutex_lock (&daemon_mutex
);
438 pthread_mutex_unlock (&daemon_mutex
);
441 data
->flags
|= FLAG_DAEMON
;
442 int r
= pthread_create (&data
->thread
, &attr
, really_start
, (void *) info
);
444 pthread_attr_destroy (&attr
);
448 const char* msg
= "Cannot create additional threads";
449 throw new java::lang::OutOfMemoryError (JvNewStringUTF (msg
));
454 _Jv_ThreadWait (void)
456 pthread_mutex_lock (&daemon_mutex
);
457 if (non_daemon_count
)
458 pthread_cond_wait (&daemon_cond
, &daemon_mutex
);
459 pthread_mutex_unlock (&daemon_mutex
);
462 #if defined(SLOW_PTHREAD_SELF)
464 #include "sysdep/locks.h"
466 // Support for pthread_self() lookup cache.
467 volatile self_cache_entry _Jv_self_cache
[SELF_CACHE_SIZE
];
470 _Jv_ThreadSelf_out_of_line(volatile self_cache_entry
*sce
, size_t high_sp_bits
)
472 pthread_t self
= pthread_self();
473 sce
-> high_sp_bits
= high_sp_bits
;
479 #endif /* SLOW_PTHREAD_SELF */