OSDN Git Service

* de.po: Update.
[pf3gnuchains/gcc-fork.git] / libjava / posix-threads.cc
index 286bf83..91da25c 100644 (file)
@@ -1,6 +1,6 @@
 // posix-threads.cc - interface between libjava and POSIX threads.
 
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2004  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -24,6 +24,9 @@ details.  */
 #include <signal.h>
 #include <errno.h>
 #include <limits.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>    // To test for _POSIX_THREAD_PRIORITY_SCHEDULING
+#endif
 
 #include <gcj/cni.h>
 #include <jvm.h>
@@ -31,6 +34,7 @@ details.  */
 #include <java/lang/System.h>
 #include <java/lang/Long.h>
 #include <java/lang/OutOfMemoryError.h>
+#include <java/lang/InternalError.h>
 
 // This is used to implement thread startup.
 struct starter
@@ -55,8 +59,9 @@ static pthread_cond_t daemon_cond;
 static int non_daemon_count;
 
 // The signal to use when interrupting a thread.
-#ifdef LINUX_THREADS
+#if defined(LINUX_THREADS) || defined(FREEBSD_THREADS)
   // LinuxThreads (prior to glibc 2.1) usurps both SIGUSR1 and SIGUSR2.
+  // GC on FreeBSD uses both SIGUSR1 and SIGUSR2.
 #  define INTR SIGHUP
 #else /* LINUX_THREADS */
 #  define INTR SIGUSR2
@@ -160,7 +165,7 @@ _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
   mu->owner = self;
   mu->count = count;
 
-  // If we were interrupted, or if a timeout occured, remove ourself from
+  // If we were interrupted, or if a timeout occurred, remove ourself from
   // the cv wait list now. (If we were notified normally, notify() will have
   // already taken care of this)
   if (r == ETIMEDOUT || interrupted)
@@ -189,7 +194,7 @@ _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
 int
 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
 {
-  if (_Jv_PthreadCheckMonitor (mu))
+  if (_Jv_MutexCheckMonitor (mu))
     return _JV_NOT_OWNER;
 
   _Jv_Thread_t *target;
@@ -228,7 +233,7 @@ _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
 int
 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
 {
-  if (_Jv_PthreadCheckMonitor (mu))
+  if (_Jv_MutexCheckMonitor (mu))
     return _JV_NOT_OWNER;
 
   _Jv_Thread_t *target;
@@ -263,7 +268,7 @@ _Jv_ThreadInterrupt (_Jv_Thread_t *data)
   data->thread_obj->interrupt_flag = true;
 
   // Interrupt blocking system calls using a signal.
-//  pthread_kill (data->thread, INTR);
+  pthread_kill (data->thread, INTR);
   
   pthread_cond_signal (&data->wait_cond);
   
@@ -276,6 +281,17 @@ handle_intr (int)
   // Do nothing.
 }
 
+static void
+block_sigchld()
+{
+  sigset_t mask;
+  sigemptyset (&mask);
+  sigaddset (&mask, SIGCHLD);
+  int c = pthread_sigmask (SIG_BLOCK, &mask, NULL);
+  if (c != 0)
+    JvFail (strerror (c));
+}
+
 void
 _Jv_InitThreads (void)
 {
@@ -291,6 +307,10 @@ _Jv_InitThreads (void)
   sigemptyset (&act.sa_mask);
   act.sa_flags = 0;
   sigaction (INTR, &act, NULL);
+
+  // Block SIGCHLD here to ensure that any non-Java threads inherit the new 
+  // signal mask.
+  block_sigchld();
 }
 
 _Jv_Thread_t *
@@ -317,6 +337,7 @@ _Jv_ThreadDestroyData (_Jv_Thread_t *data)
 void
 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
 {
+#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
   if (data->flags & FLAG_START)
     {
       struct sched_param param;
@@ -324,6 +345,7 @@ _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
       param.sched_priority = prio;
       pthread_setschedparam (data->thread, SCHED_RR, &param);
     }
+#endif
 }
 
 void
@@ -336,6 +358,24 @@ _Jv_ThreadRegister (_Jv_Thread_t *data)
   // is called. Since it may need to be accessed from the new thread, work 
   // around the potential race here by explicitly setting it again.
   data->thread = pthread_self ();
+
+# ifdef SLOW_PTHREAD_SELF
+    // Clear all self cache slots that might be needed by this thread.
+    int dummy;
+    int low_index = SC_INDEX(&dummy) + SC_CLEAR_MIN;
+    int high_index = SC_INDEX(&dummy) + SC_CLEAR_MAX;
+    for (int i = low_index; i <= high_index; ++i) 
+      {
+        int current_index = i;
+       if (current_index < 0)
+         current_index += SELF_CACHE_SIZE;
+       if (current_index >= SELF_CACHE_SIZE)
+         current_index -= SELF_CACHE_SIZE;
+       _Jv_self_cache[current_index].high_sp_bits = BAD_HIGH_SP_VALUE;
+      }
+# endif
+  // Block SIGCHLD which is used in natPosixProcess.cc.
+  block_sigchld();
 }
 
 void
@@ -356,7 +396,7 @@ really_start (void *x)
   _Jv_ThreadRegister (info->data);
 
   info->method (info->data->thread_obj);
-  
+
   if (! (info->data->flags & FLAG_DAEMON))
     {
       pthread_mutex_lock (&daemon_mutex);
@@ -365,7 +405,7 @@ really_start (void *x)
        pthread_cond_signal (&daemon_cond);
       pthread_mutex_unlock (&daemon_mutex);
     }
-  
+
   return NULL;
 }
 
@@ -381,6 +421,10 @@ _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
     return;
   data->flags |= FLAG_START;
 
+  // Block SIGCHLD which is used in natPosixProcess.cc.
+  // The current mask is inherited by the child thread.
+  block_sigchld();
+
   param.sched_priority = thread->getPriority();
 
   pthread_attr_init (&attr);
@@ -418,3 +462,22 @@ _Jv_ThreadWait (void)
     pthread_cond_wait (&daemon_cond, &daemon_mutex);
   pthread_mutex_unlock (&daemon_mutex);
 }
+
+#if defined(SLOW_PTHREAD_SELF)
+
+#include "sysdep/locks.h"
+
+// Support for pthread_self() lookup cache.
+volatile self_cache_entry _Jv_self_cache[SELF_CACHE_SIZE];
+
+_Jv_ThreadId_t
+_Jv_ThreadSelf_out_of_line(volatile self_cache_entry *sce, size_t high_sp_bits)
+{
+  pthread_t self = pthread_self();
+  sce -> high_sp_bits = high_sp_bits;
+  write_barrier();
+  sce -> self = self;
+  return self;
+}
+
+#endif /* SLOW_PTHREAD_SELF */