1 // posix-threads.cc - interface between libjava and POSIX threads.
3 /* Copyright (C) 1998, 1999, 2000, 2001 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>
38 // This is used to implement thread startup.
41 _Jv_ThreadStartFunc
*method
;
45 // This is the key used to map from the POSIX thread value back to the
46 // Java object representing the thread. The key is global to all
47 // threads, so it is ok to make it a global here.
48 pthread_key_t _Jv_ThreadKey
;
50 // This is the key used to map from the POSIX thread value back to the
51 // _Jv_Thread_t* representing the thread.
52 pthread_key_t _Jv_ThreadDataKey
;
54 // We keep a count of all non-daemon threads which are running. When
55 // this reaches zero, _Jv_ThreadWait returns.
56 static pthread_mutex_t daemon_mutex
;
57 static pthread_cond_t daemon_cond
;
58 static int non_daemon_count
;
60 // The signal to use when interrupting a thread.
61 #if defined(LINUX_THREADS) || defined(FREEBSD_THREADS)
62 // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
63 // GC on FreeBSD uses both SIGUSR1 and SIGUSR2.
65 #else /* LINUX_THREADS */
67 #endif /* LINUX_THREADS */
70 // These are the flags that can appear in _Jv_Thread_t.
74 #define FLAG_START 0x01
76 #define FLAG_DAEMON 0x02
80 // Wait for the condition variable "CV" to be notified.
82 // 0: the condition was notified, or the timeout expired.
83 // _JV_NOT_OWNER: the thread does not own the mutex "MU".
84 // _JV_INTERRUPTED: the thread was interrupted. Its interrupted flag is set.
86 _Jv_CondWait (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
,
87 jlong millis
, jint nanos
)
89 pthread_t self
= pthread_self();
90 if (mu
->owner
!= self
)
96 if (millis
> 0 || nanos
> 0)
98 startTime
= java::lang::System::currentTimeMillis();
99 m
= millis
+ startTime
;
100 ts
.tv_sec
= m
/ 1000;
101 ts
.tv_nsec
= ((m
% 1000) * 1000000) + nanos
;
104 _Jv_Thread_t
*current
= _Jv_ThreadCurrentData ();
105 java::lang::Thread
*current_obj
= _Jv_ThreadCurrent ();
107 pthread_mutex_lock (¤t
->wait_mutex
);
109 // Now that we hold the wait mutex, check if this thread has been
110 // interrupted already.
111 if (current_obj
->interrupt_flag
)
113 pthread_mutex_unlock (¤t
->wait_mutex
);
114 return _JV_INTERRUPTED
;
117 // Add this thread to the cv's wait set.
118 current
->next
= NULL
;
120 if (cv
->first
== NULL
)
123 for (_Jv_Thread_t
*t
= cv
->first
;; t
= t
->next
)
132 // Record the current lock depth, so it can be restored when we re-aquire it.
133 int count
= mu
->count
;
135 // Release the monitor mutex.
138 pthread_mutex_unlock (&mu
->mutex
);
141 bool done_sleeping
= false;
143 while (! done_sleeping
)
145 if (millis
== 0 && nanos
== 0)
146 r
= pthread_cond_wait (¤t
->wait_cond
, ¤t
->wait_mutex
);
148 r
= pthread_cond_timedwait (¤t
->wait_cond
, ¤t
->wait_mutex
,
151 // In older glibc's (prior to 2.1.3), the cond_wait functions may
152 // spuriously wake up on a signal. Catch that here.
154 done_sleeping
= true;
157 // Check for an interrupt *before* releasing the wait mutex.
158 jboolean interrupted
= current_obj
->interrupt_flag
;
160 pthread_mutex_unlock (¤t
->wait_mutex
);
162 // Reaquire the monitor mutex, and restore the lock count.
163 pthread_mutex_lock (&mu
->mutex
);
167 // If we were interrupted, or if a timeout occurred, remove ourself from
168 // the cv wait list now. (If we were notified normally, notify() will have
169 // already taken care of this)
170 if (r
== ETIMEDOUT
|| interrupted
)
172 _Jv_Thread_t
*prev
= NULL
;
173 for (_Jv_Thread_t
*t
= cv
->first
; t
!= NULL
; t
= t
->next
)
178 prev
->next
= t
->next
;
187 return _JV_INTERRUPTED
;
194 _Jv_CondNotify (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
196 if (_Jv_PthreadCheckMonitor (mu
))
197 return _JV_NOT_OWNER
;
199 _Jv_Thread_t
*target
;
200 _Jv_Thread_t
*prev
= NULL
;
202 for (target
= cv
->first
; target
!= NULL
; target
= target
->next
)
204 pthread_mutex_lock (&target
->wait_mutex
);
206 if (target
->thread_obj
->interrupt_flag
)
208 // Don't notify a thread that has already been interrupted.
209 pthread_mutex_unlock (&target
->wait_mutex
);
214 pthread_cond_signal (&target
->wait_cond
);
215 pthread_mutex_unlock (&target
->wait_mutex
);
217 // Two concurrent notify() calls must not be delivered to the same
218 // thread, so remove the target thread from the cv wait list now.
220 cv
->first
= target
->next
;
222 prev
->next
= target
->next
;
233 _Jv_CondNotifyAll (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
)
235 if (_Jv_PthreadCheckMonitor (mu
))
236 return _JV_NOT_OWNER
;
238 _Jv_Thread_t
*target
;
239 _Jv_Thread_t
*prev
= NULL
;
241 for (target
= cv
->first
; target
!= NULL
; target
= target
->next
)
243 pthread_mutex_lock (&target
->wait_mutex
);
244 pthread_cond_signal (&target
->wait_cond
);
245 pthread_mutex_unlock (&target
->wait_mutex
);
260 _Jv_ThreadInterrupt (_Jv_Thread_t
*data
)
262 pthread_mutex_lock (&data
->wait_mutex
);
264 // Set the thread's interrupted flag *after* aquiring its wait_mutex. This
265 // ensures that there are no races with the interrupt flag being set after
266 // the waiting thread checks it and before pthread_cond_wait is entered.
267 data
->thread_obj
->interrupt_flag
= true;
269 // Interrupt blocking system calls using a signal.
270 pthread_kill (data
->thread
, INTR
);
272 pthread_cond_signal (&data
->wait_cond
);
274 pthread_mutex_unlock (&data
->wait_mutex
);
284 _Jv_InitThreads (void)
286 pthread_key_create (&_Jv_ThreadKey
, NULL
);
287 pthread_key_create (&_Jv_ThreadDataKey
, NULL
);
288 pthread_mutex_init (&daemon_mutex
, NULL
);
289 pthread_cond_init (&daemon_cond
, 0);
290 non_daemon_count
= 0;
292 // Arrange for the interrupt signal to interrupt system calls.
293 struct sigaction act
;
294 act
.sa_handler
= handle_intr
;
295 sigemptyset (&act
.sa_mask
);
297 sigaction (INTR
, &act
, NULL
);
301 _Jv_ThreadInitData (java::lang::Thread
*obj
)
303 _Jv_Thread_t
*data
= (_Jv_Thread_t
*) _Jv_Malloc (sizeof (_Jv_Thread_t
));
305 data
->thread_obj
= obj
;
307 pthread_mutex_init (&data
->wait_mutex
, NULL
);
308 pthread_cond_init (&data
->wait_cond
, NULL
);
314 _Jv_ThreadDestroyData (_Jv_Thread_t
*data
)
316 pthread_mutex_destroy (&data
->wait_mutex
);
317 pthread_cond_destroy (&data
->wait_cond
);
318 _Jv_Free ((void *)data
);
322 _Jv_ThreadSetPriority (_Jv_Thread_t
*data
, jint prio
)
324 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
325 if (data
->flags
& FLAG_START
)
327 struct sched_param param
;
329 param
.sched_priority
= prio
;
330 pthread_setschedparam (data
->thread
, SCHED_RR
, ¶m
);
336 _Jv_ThreadRegister (_Jv_Thread_t
*data
)
338 pthread_setspecific (_Jv_ThreadKey
, data
->thread_obj
);
339 pthread_setspecific (_Jv_ThreadDataKey
, data
);
341 // glibc 2.1.3 doesn't set the value of `thread' until after start_routine
342 // is called. Since it may need to be accessed from the new thread, work
343 // around the potential race here by explicitly setting it again.
344 data
->thread
= pthread_self ();
346 # ifdef SLOW_PTHREAD_SELF
347 // Clear all self cache slots that might be needed by this thread.
349 int low_index
= SC_INDEX(&dummy
) + SC_CLEAR_MIN
;
350 int high_index
= SC_INDEX(&dummy
) + SC_CLEAR_MAX
;
351 for (int i
= low_index
; i
<= high_index
; ++i
)
353 int current_index
= i
;
354 if (current_index
< 0)
355 current_index
+= SELF_CACHE_SIZE
;
356 if (current_index
>= SELF_CACHE_SIZE
)
357 current_index
-= SELF_CACHE_SIZE
;
358 _Jv_self_cache
[current_index
].high_sp_bits
= BAD_HIGH_SP_VALUE
;
364 _Jv_ThreadUnRegister ()
366 pthread_setspecific (_Jv_ThreadKey
, NULL
);
367 pthread_setspecific (_Jv_ThreadDataKey
, NULL
);
370 // This function is called when a thread is started. We don't arrange
371 // to call the `run' method directly, because this function must
374 really_start (void *x
)
376 struct starter
*info
= (struct starter
*) x
;
378 _Jv_ThreadRegister (info
->data
);
380 info
->method (info
->data
->thread_obj
);
382 if (! (info
->data
->flags
& FLAG_DAEMON
))
384 pthread_mutex_lock (&daemon_mutex
);
386 if (! non_daemon_count
)
387 pthread_cond_signal (&daemon_cond
);
388 pthread_mutex_unlock (&daemon_mutex
);
395 _Jv_ThreadStart (java::lang::Thread
*thread
, _Jv_Thread_t
*data
,
396 _Jv_ThreadStartFunc
*meth
)
398 struct sched_param param
;
400 struct starter
*info
;
402 if (data
->flags
& FLAG_START
)
404 data
->flags
|= FLAG_START
;
406 param
.sched_priority
= thread
->getPriority();
408 pthread_attr_init (&attr
);
409 pthread_attr_setschedparam (&attr
, ¶m
);
410 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
412 info
= (struct starter
*) _Jv_AllocBytes (sizeof (struct starter
));
416 if (! thread
->isDaemon())
418 pthread_mutex_lock (&daemon_mutex
);
420 pthread_mutex_unlock (&daemon_mutex
);
423 data
->flags
|= FLAG_DAEMON
;
424 int r
= pthread_create (&data
->thread
, &attr
, really_start
, (void *) info
);
426 pthread_attr_destroy (&attr
);
430 const char* msg
= "Cannot create additional threads";
431 throw new java::lang::OutOfMemoryError (JvNewStringUTF (msg
));
436 _Jv_ThreadWait (void)
438 pthread_mutex_lock (&daemon_mutex
);
439 if (non_daemon_count
)
440 pthread_cond_wait (&daemon_cond
, &daemon_mutex
);
441 pthread_mutex_unlock (&daemon_mutex
);
444 #if defined(SLOW_PTHREAD_SELF)
446 #include "sysdep/locks.h"
448 // Support for pthread_self() lookup cache.
449 volatile self_cache_entry _Jv_self_cache
[SELF_CACHE_SIZE
];
452 _Jv_ThreadSelf_out_of_line(volatile self_cache_entry
*sce
, size_t high_sp_bits
)
454 pthread_t self
= pthread_self();
455 sce
-> high_sp_bits
= high_sp_bits
;
461 #endif /* SLOW_PTHREAD_SELF */