boehm-gc: revert all CACAO-specific modifications; this is now an exact copy of the...
[cacao.git] / src / mm / boehm-gc / pthread_stop_world.c
index 546dc0cc15f9580fb04ee4969a59d532f9efd5a2..ae1d225a94c02d298487653e60d3a34b131d8a7a 100644 (file)
@@ -1,5 +1,3 @@
-#include "config.h"
-
 #include "private/pthread_support.h"
 
 #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && \
@@ -25,7 +23,7 @@
 # endif
 #endif
 
-void GC_print_sig_mask()
+void GC_print_sig_mask(void)
 {
     sigset_t blocked;
     int i;
@@ -43,7 +41,7 @@ void GC_print_sig_mask()
 
 /* Remove the signals that we want to allow in thread stopping         */
 /* handler from a set.                                         */
-void GC_remove_allowed_signals(sigset_t *set)
+STATIC void GC_remove_allowed_signals(sigset_t *set)
 {
     if (sigdelset(set, SIGINT) != 0
          || sigdelset(set, SIGQUIT) != 0
@@ -77,9 +75,9 @@ volatile AO_t GC_world_is_stopped = FALSE;
                        /* stopped).                                         */
 
 #ifdef GC_OSF1_THREADS
-  GC_bool GC_retry_signals = TRUE;
+  STATIC GC_bool GC_retry_signals = TRUE;
 #else
-  GC_bool GC_retry_signals = FALSE;
+  STATIC GC_bool GC_retry_signals = FALSE;
 #endif
 
 /*
@@ -106,20 +104,24 @@ volatile AO_t GC_world_is_stopped = FALSE;
 #  endif
 #endif
 
-sem_t GC_suspend_ack_sem;
+STATIC sem_t GC_suspend_ack_sem;
 
 #ifdef GC_NETBSD_THREADS
 # define GC_NETBSD_THREADS_WORKAROUND
   /* It seems to be necessary to wait until threads have restarted.    */
   /* But it is unclear why that is the case.                           */
-  sem_t GC_restart_ack_sem;
+  STATIC sem_t GC_restart_ack_sem;
 #endif
 
-void GC_suspend_handler_inner(ptr_t sig_arg, void *context);
-/* int cacao_suspendhandler(void *); */
+STATIC void GC_suspend_handler_inner(ptr_t sig_arg, void *context);
 
 #if defined(IA64) || defined(HP_PA) || defined(M68K)
-void GC_suspend_handler(int sig, siginfo_t *info, void *context)
+#ifdef SA_SIGINFO
+/*ARGSUSED*/
+STATIC void GC_suspend_handler(int sig, siginfo_t *info, void *context)
+#else
+STATIC void GC_suspend_handler(int sig)
+#endif
 {
   int old_errno = errno;
   GC_with_callee_saves_pushed(GC_suspend_handler_inner, (ptr_t)(word)sig);
@@ -128,26 +130,29 @@ void GC_suspend_handler(int sig, siginfo_t *info, void *context)
 #else
 /* We believe that in all other cases the full context is already      */
 /* in the signal handler frame.                                                */
-void GC_suspend_handler(int sig, siginfo_t *info, void *context)
+#ifdef SA_SIGINFO
+STATIC void GC_suspend_handler(int sig, siginfo_t *info, void *context)
+#else
+STATIC void GC_suspend_handler(int sig)
+#endif
 {
   int old_errno = errno;
+# ifndef SA_SIGINFO
+    void *context = 0;
+# endif
   GC_suspend_handler_inner((ptr_t)(word)sig, context);
   errno = old_errno;
 }
 #endif
 
-void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
+/*ARGSUSED*/
+STATIC void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
 {
     int sig = (int)(word)sig_arg;
     int dummy;
     pthread_t my_thread = pthread_self();
     GC_thread me;
-#   ifdef PARALLEL_MARK
-       word my_mark_no = GC_mark_no;
-       /* Marker can't proceed until we acknowledge.  Thus this is     */
-       /* guaranteed to be the mark_no correspending to our            */
-       /* suspension, i.e. the marker can't have incremented it yet.   */
-#   endif
+
     AO_t my_stop_count = AO_load(&GC_stop_count);
 
     if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
@@ -164,8 +169,8 @@ void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
     if (me -> stop_info.last_stop_count == my_stop_count) {
        /* Duplicate signal.  OK if we are retrying.    */
        if (!GC_retry_signals) {
-           WARN("Duplicate suspend signal in thread %lx\n",
-                pthread_self());
+           WARN("Duplicate suspend signal in thread %p\n",
+                (word)pthread_self());
        }
        return;
     }
@@ -211,11 +216,8 @@ void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
 #   endif
 }
 
-void GC_restart_handler(int sig)
+STATIC void GC_restart_handler(int sig)
 {
-    pthread_t my_thread = pthread_self();
-    GC_thread me;
-
     if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
 
 #   ifdef GC_NETBSD_THREADS_WORKAROUND
@@ -235,6 +237,8 @@ void GC_restart_handler(int sig)
 #   endif
 }
 
+void GC_thr_init(void);
+
 # ifdef IA64
 #   define IF_IA64(x) x
 # else
@@ -242,7 +246,7 @@ void GC_restart_handler(int sig)
 # endif
 /* We hold allocation lock.  Should do exactly the right thing if the  */
 /* world is stopped.  Should not fail if it isn't.                     */
-void GC_push_all_stacks()
+void GC_push_all_stacks(void)
 {
     GC_bool found_me = FALSE;
     size_t nthreads = 0;
@@ -262,6 +266,7 @@ void GC_push_all_stacks()
         if (p -> flags & FINISHED) continue;
        ++nthreads;
         if (THREAD_EQUAL(p -> id, me)) {
+           GC_ASSERT(!p->thread_blocked);
 #          ifdef SPARC
                lo = (ptr_t)GC_save_regs_in_stack();
 #          else
@@ -294,7 +299,7 @@ void GC_push_all_stacks()
 #      endif
 #      ifdef IA64
 #         if DEBUG_THREADS
-            GC_printf("Reg stack for thread 0x%x = [%lx,%lx)\n",
+            GC_printf("Reg stack for thread 0x%x = [%p,%p)\n",
                      (unsigned)p -> id, bs_lo, bs_hi);
 #        endif
           if (THREAD_EQUAL(p -> id, me)) {
@@ -308,7 +313,7 @@ void GC_push_all_stacks()
       }
     }
     if (GC_print_stats == VERBOSE) {
-       GC_log_printf("Pushed %d thread stacks\n", nthreads);
+       GC_log_printf("Pushed %d thread stacks\n", (int)nthreads);
     }
     if (!found_me && !GC_in_thread_creation)
       ABORT("Collecting from unknown thread.");
@@ -316,13 +321,15 @@ void GC_push_all_stacks()
 
 /* There seems to be a very rare thread stopping problem.  To help us  */
 /* debug that, we save the ids of the stopping thread. */
+#if DEBUG_THREADS
 pthread_t GC_stopping_thread;
 int GC_stopping_pid;
+#endif
 
 /* We hold the allocation lock.  Suspend all threads that might        */
 /* still be running.  Return the number of suspend signals that        */
 /* were sent. */
-int GC_suspend_all()
+STATIC int GC_suspend_all(void)
 {
     int n_live_threads = 0;
     int i;
@@ -330,8 +337,10 @@ int GC_suspend_all()
     int result;
     pthread_t my_thread = pthread_self();
     
-    GC_stopping_thread = my_thread;    /* debugging only.      */
-    GC_stopping_pid = getpid();                /* debugging only.      */
+#   if DEBUG_THREADS
+      GC_stopping_thread = my_thread;
+      GC_stopping_pid = getpid();
+#   endif
     for (i = 0; i < THREAD_TABLE_SZ; i++) {
       for (p = GC_threads[i]; p != 0; p = p -> next) {
         if (!THREAD_EQUAL(p -> id, my_thread)) {
@@ -361,10 +370,7 @@ int GC_suspend_all()
     return n_live_threads;
 }
 
-void lock_stopworld(int);
-void unlock_stopworld();
-
-void GC_stop_world()
+void GC_stop_world(void)
 {
     int i;
     int n_live_threads;
@@ -380,9 +386,11 @@ void GC_stop_world()
     /* required to acquire and release the GC lock before it starts,   */
     /* and we have the lock.                                           */
 #   ifdef PARALLEL_MARK
-      GC_acquire_mark_lock();
-      GC_ASSERT(GC_fl_builder_count == 0);
-      /* We should have previously waited for it to become zero. */
+      if (GC_parallel) {
+       GC_acquire_mark_lock();
+       GC_ASSERT(GC_fl_builder_count == 0);
+       /* We should have previously waited for it to become zero. */
+      }
 #   endif /* PARALLEL_MARK */
     AO_store(&GC_stop_count, GC_stop_count+1);
        /* Only concurrent reads are possible. */
@@ -429,17 +437,18 @@ void GC_stop_world()
          }
     }
 #   ifdef PARALLEL_MARK
-      GC_release_mark_lock();
+      if (GC_parallel)
+       GC_release_mark_lock();
 #   endif
-    #if DEBUG_THREADS
+#   if DEBUG_THREADS
       GC_printf("World stopped from 0x%x\n", (unsigned)pthread_self());
-    #endif
-    GC_stopping_thread = 0;  /* debugging only */
+      GC_stopping_thread = 0;
+#   endif
 }
 
 /* Caller holds allocation lock, and has held it continuously since    */
 /* the world stopped.                                                  */
-void GC_start_world()
+void GC_start_world(void)
 {
     pthread_t my_thread = pthread_self();
     register int i;
@@ -461,10 +470,10 @@ void GC_start_world()
             if (p -> flags & FINISHED) continue;
            if (p -> thread_blocked) continue;
             n_live_threads++;
-           #if DEBUG_THREADS
+#          if DEBUG_THREADS
              GC_printf("Sending restart signal to 0x%x\n",
                        (unsigned)(p -> id));
-           #endif
+#          endif
         
             result = pthread_kill(p -> id, SIG_THR_RESTART);
            switch(result) {
@@ -484,8 +493,8 @@ void GC_start_world()
       for (i = 0; i < n_live_threads; i++)
        while (0 != (code = sem_wait(&GC_restart_ack_sem)))
            if (errno != EINTR) {
-               GC_err_printf1("sem_wait() returned %ld\n",
-                              (unsigned long)code);
+               GC_err_printf("sem_wait() returned %d\n",
+                              code);
                ABORT("sem_wait() for restart handler failed");
            }
 #    endif
@@ -494,7 +503,7 @@ void GC_start_world()
 #    endif
 }
 
-void GC_stop_init() {
+void GC_stop_init(void) {
     struct sigaction act;
     
     if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
@@ -504,25 +513,35 @@ void GC_stop_init() {
        ABORT("sem_init failed");
 #   endif
 
-    act.sa_flags = SA_RESTART | SA_SIGINFO;
+    act.sa_flags = SA_RESTART
+#   ifdef SA_SIGINFO
+       | SA_SIGINFO
+#   endif
+       ;
     if (sigfillset(&act.sa_mask) != 0) {
        ABORT("sigfillset() failed");
     }
     GC_remove_allowed_signals(&act.sa_mask);
     /* SIG_THR_RESTART is set in the resulting mask.           */
     /* It is unmasked by the handler when necessary.           */
+#   ifdef SA_SIGINFO
     act.sa_sigaction = GC_suspend_handler;
+#   else
+    act.sa_handler = GC_suspend_handler;
+#   endif
     if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
        ABORT("Cannot set SIG_SUSPEND handler");
     }
 
+#   ifdef SA_SIGINFO
     act.sa_flags &= ~ SA_SIGINFO;
+#   endif
     act.sa_handler = GC_restart_handler;
     if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
        ABORT("Cannot set SIG_THR_RESTART handler");
     }
 
-    /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
+    /* Initialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
       if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
       GC_remove_allowed_signals(&suspend_handler_mask);
       if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
@@ -540,16 +559,4 @@ void GC_stop_init() {
       }
 }
 
-/* Added for cacao */
-int GC_signum1()
-{
-    return SIG_SUSPEND;
-}
-
-int GC_signum2()
-{
-    return SIG_THR_RESTART;
-}
-/* cacao END */
-
 #endif