boehm-gc and sigwait() ?

Bryce McKinlay bryce@albatross.co.nz
Tue Nov 23 21:09:00 GMT 1999


"Boehm, Hans" wrote:
> 
> What does sigwait do if another signal arrives?  Does it allow the handler
> to run?

Thanks - it looks like that was the problem, even though the linux-threads
documentation seems to imply otherwise.

Luckily, it turns out that I can implement the same functionality with a sigint
handler and sigsuspend(). It seems to co-operate nicely with the gc now.

Tom, what do you think?

regards

  [ bryce ]

Index: posix-threads.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/posix-threads.cc,v
retrieving revision 1.13
diff -u -r1.13 posix-threads.cc
--- posix-threads.cc	1999/11/04 16:45:11	1.13
+++ posix-threads.cc	1999/11/24 04:53:18
@@ -32,6 +32,7 @@
 #include <jvm.h>
 #include <java/lang/Thread.h>
 #include <java/lang/System.h>
+#include <java/lang/Runtime.h>
 
 // This is used to implement thread startup.
 struct starter
@@ -53,7 +54,6 @@
 // We keep a count of all non-daemon threads which are running.  When
 // this reaches zero, _Jv_ThreadWait returns.
 static pthread_mutex_t daemon_mutex;
-static pthread_cond_t daemon_cond;
 static int non_daemon_count;
 
 // The signal to use when interrupting a thread.
@@ -254,13 +254,20 @@
   // Do nothing.
 }
 
+static int got_sigint = 0;
+
+static void
+handle_sigint (int)
+{
+  got_sigint = 1;
+}
+
 void
 _Jv_InitThreads (void)
 {
   pthread_key_create (&_Jv_ThreadKey, NULL);
   pthread_key_create (&_Jv_ThreadDataKey, NULL);
   pthread_mutex_init (&daemon_mutex, NULL);
-  pthread_cond_init (&daemon_cond, 0);
   non_daemon_count = 0;
 
   // Arrange for the interrupt signal to interrupt system calls.
@@ -320,7 +327,7 @@
       pthread_mutex_lock (&daemon_mutex);
       --non_daemon_count;
       if (! non_daemon_count)
-	pthread_cond_signal (&daemon_cond);
+	java::lang::Runtime::getRuntime ()->exit (0);        
       pthread_mutex_unlock (&daemon_mutex);
     }
 
@@ -367,15 +374,21 @@
 _Jv_ThreadWait (void)
 {
   // Arrange for SIGINT to be delivered to the master thread.
+  struct sigaction act;
+  act.sa_flags = 0;
+  act.sa_handler = handle_sigint;
+  sigemptyset (&act.sa_mask);
+  sigaddset (&act.sa_mask, SIGINT);
+  sigaction (SIGINT, &act, NULL);
+
   sigset_t mask;
   sigemptyset (&mask);
-  sigaddset (&mask, SIGINT);
-  pthread_sigmask (SIG_UNBLOCK, &mask, NULL);
 
-  pthread_mutex_lock (&daemon_mutex);
-  if (non_daemon_count)
-    pthread_cond_wait (&daemon_cond, &daemon_mutex);
-  pthread_mutex_unlock (&daemon_mutex);
+  // sigsuspend() will return whenever a signal handler has run. The sigint
+  // handler will set got_sigint, and _Jv_ThreadWait will return, resulting
+  // in a clean shutdown.
+  while (!got_sigint)
+    sigsuspend(&mask);
 }
 
 void
Index: prims.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/prims.cc,v
retrieving revision 1.13
diff -u -r1.13 prims.cc
--- prims.cc	1999/11/05 17:34:32	1.13
+++ prims.cc	1999/11/24 04:53:18
@@ -793,7 +793,8 @@
   main_thread->start();
   _Jv_ThreadWait ();
 
-  java::lang::Runtime::getRuntime ()->exit (0);
+  // _Jv_ThreadWait returns only on SIGINT (Ctrl-C).
+  ::exit(1);
 }
 
 void


More information about the Java mailing list