diff -ruN klic-3.003-2002-01-06c/runtime/kmain.c klic-3.003-2002-01-07/runtime/kmain.c
--- klic-3.003-2002-01-06c/runtime/kmain.c	Sun Jan  6 15:18:16 2002
+++ klic-3.003-2002-01-07/runtime/kmain.c	Mon Jan  7 11:27:02 2002
@@ -4,10 +4,8 @@
 %   (C)1996, 1997, 1998, 1999 Japan Information Processing Development Center
 %       (Read COPYRIGHT-JIPDEC for detailed information.)
 ----------------------------------------------------------- */
+
 #define MAIN
-#ifdef DIST
-/*#define exit(X) myexit(__FILE__, __LINE__, X)*/
-#endif
 
 /* klic_interrupt, initalloc, register_gc_hook, register_after_gc_hook,
  * enqueue_goal
@@ -43,282 +41,288 @@
 
 #define IDLE_ON()  (perfmon_state.idle_on = 1)
 #define IDLE_OFF() (perfmon_state.idle_on = 0)
-
-int receive_message();
-q* gc_exp_table();
-q* gc_decode_stack();
-q* scan_imp_table();
-#endif  /* DIST */
+#endif /* DIST */
 
 #include "options.h"
 
+#ifdef DEBUGLIB
+#include "trace.h"  /* trace_goal */
+#endif
+
+#define klic_local_fprintf fprintf
+
+extern char *runtime_version, *runtime_date;
+extern const struct predicate predicate_main_xmain_0;
+extern const struct predicate topsucceed_pred;
+extern q* initiate_prioq();
+extern struct goalrec* get_top_priority_queue();
+
+#ifdef DIST
+extern int receive_message();
+extern q* gc_exp_table();
+extern q* gc_decode_stack();
+extern q* scan_imp_table();
+extern void send_return_wtc();
+#endif
+
+#ifdef USESIG
+extern void init_klic_signal_handling();
+#endif
+
+#ifdef USETIMER
+extern void init_virtualized_timer();
+extern void init_klic_timer_handling();
+#endif
+
+#if !defined(DIST) && defined(USESIG) && defined(USETIMER)
+extern void call_after_specified_interval();
+#endif
+
+jmp_buf klic_topmost;
+
+#ifdef DEBUGLIB
+int count_suspension = 0;
+#endif
+
 static long num_pes;
 static double max_a_ratio;
 static unsigned long h_size;
 static unsigned long max_h_size;
 static unsigned long inc_size;
 static int verbose;
-extern int start_tracing;
-extern int count_suspension;
+static int start_tracing = 0;
 
 #ifdef USETIMER
 static unsigned long dl_det_interval;
-#endif /*USETIMER*/
+#endif /* USETIMER */
 
 const struct opttable opttable[] = {
 #ifdef USER_OPTIONS
-    USER_OPTIONS
+  USER_OPTIONS
 #endif
 #ifdef POLLING_OPTIONS
-    POLLING_OPTIONS
+  POLLING_OPTIONS
 #endif
 #ifdef PARALLEL
-    "p",        (union all_type*) &num_pes, ARG_SIZE,
-    "-p <# of workers>: number of worker processes ",
-#endif
+  "p",        (union all_type*) &num_pes, ARG_SIZE,
+  "-p <# of workers>: number of worker processes ",
+#endif /* PARALLEL */
 #ifdef DIST_OPTIONS
-    DIST_OPTIONS
-#endif
+  DIST_OPTIONS
+#endif /* DIST_OPTIONS */
 #if defined(DIST) && defined(SHM_DIST)
-    "B",	(union all_type*) &shared_buffer_size, ARG_SIZE,
-    "-B <communication buffer size>",
-#endif
+  "B",	(union all_type*) &shared_buffer_size, ARG_SIZE,
+  "-B <communication buffer size>",
+#endif /* SHM_DIST */
 #ifdef SHM
-    "S",        (union all_type*) &SHM_SIZE, ARG_SIZE,
-    "-S <shared-memory heap size>: shared-memory heap size, never expand",
+  "S",        (union all_type*) &SHM_SIZE, ARG_SIZE,
+  "-S <shared-memory heap size>: shared-memory heap size, never expand",
 
-    "D",        (union all_type*) &Disp_child, ARG_SET,
-    "-D: display child process number on UNIX",
-#endif
-    "a",	(union all_type*) &max_a_ratio, ARG_DOUBLE,
-    "-a <ratio>: active cell ratio to triger heap extension\n" \
-    "\t  <ratio> should be a floating point number >0 and <1",
-
-    "h",	(union all_type*) &h_size, ARG_SIZE,
-    "-h <initial heap size>: initial size of heap",
-
-    "H",	(union all_type*) &max_h_size, ARG_SIZE,
-    "-H <max heap size>: maximum possible heap size",
-
-    "i",	(union all_type*) &inc_size, ARG_SIZE,
-    "-i <increment>: gap between heap top and bottom\n" \
-    "\t  for -h, -H and -i, 32k means 32KW and 2m means 2MW",
+  "D",        (union all_type*) &Disp_child, ARG_SET,
+  "-D: display child process number on UNIX",
+#endif /* SHM */
+  "a",	(union all_type*) &max_a_ratio, ARG_DOUBLE,
+  "-a <ratio>: active cell ratio to triger heap extension\n" \
+  "\t  <ratio> should be a floating point number >0 and <1",
+
+  "h",	(union all_type*) &h_size, ARG_SIZE,
+  "-h <initial heap size>: initial size of heap",
+
+  "H",	(union all_type*) &max_h_size, ARG_SIZE,
+  "-H <max heap size>: maximum possible heap size",
+
+  "i",	(union all_type*) &inc_size, ARG_SIZE,
+  "-i <increment>: gap between heap top and bottom\n" \
+  "\t  for -h, -H and -i, 32k means 32KW and 2m means 2MW",
 
-    "g",	(union all_type*) &measure_gc, ARG_SET,
-    "-g: set garbage collection measurement on",
+  "g",	(union all_type*) &measure_gc, ARG_SET,
+  "-g: set garbage collection measurement on",
 
-    "v",	(union all_type*) &verbose, ARG_SET,
-    "-v: display KLIC runtime version",
+  "v",	(union all_type*) &verbose, ARG_SET,
+  "-v: display KLIC runtime version",
 #ifdef DEBUGLIB
-    "t",	(union all_type*) &start_tracing, ARG_SET,
-    "-t: start execution with tracing",
+  "t",	(union all_type*) &start_tracing, ARG_SET,
+  "-t: start execution with tracing",
 
-    "s",	(union all_type*) &count_suspension, ARG_SET,
-    "-s: count suspension",
+  "s",	(union all_type*) &count_suspension, ARG_SET,
+  "-s: count suspension",
 #endif /* DEBUGLIB */
 #ifdef USETIMER
-    "deadlock-detection-interval",
-		(union all_type*) &dl_det_interval, ARG_SIZE,
-    "-deadlock-detection-interval <interval>:\n" \
-    "\t  specify deadlock detect interval in micro sec.\n" \
-    "\t  Set <interval> to 0 to supress early deadlock detection completely.",
-#endif /*USETIMER*/
-    0, 0 };
+  "deadlock-detection-interval",
+	(union all_type*) &dl_det_interval, ARG_SIZE,
+  "-deadlock-detection-interval <interval>:\n" \
+  "\t  specify deadlock detect interval in micro sec.\n" \
+  "\t  Set <interval> to 0 to supress early deadlock detection completely.",
+#endif /* USETIMER */
+  NULL, NULL };
 
-#ifdef DEBUGLIB
-#include "trace.h"  /* trace_goal */
-#endif
-
-#define klic_local_fprintf fprintf
-
-
-extern char *runtime_version, *runtime_date;
-
-jmp_buf klic_topmost;
-int start_tracing = 0;
-#ifdef DEBUGLIB
-int count_suspension = 0;
-#endif
 #ifdef USETIMER
 static volatile int dummy_alarm_set;
-#endif /*USETIMER*/
-module module_main();
-
-extern const struct predicate predicate_main_xmain_0;
-#ifdef DIST
-extern const struct predicate topsucceed_pred;
-#endif
-
-char *optarg;
+#endif /* USETIMER */
 
 #ifdef DIST
 static int
-kill_tasks (allocp, sig)
-q *allocp;
-int sig;
+kill_tasks(allocp, sig)
+  q* allocp;
+  int sig;
 {
-    klic_fprintf(stdout, "kill tasks\n");
-    kill_child();
-    exit(1);
+  klic_fprintf(stdout, "kill tasks\n");
+  kill_child();
+  exit(1);
 }
 #endif
 
-static void toploop()
+static void
+toploop()
 {
   declare_globals;
-  struct goalrec *qp = current_queue;
+  struct goalrec* qp = current_queue;
   const struct predicate* toppred = qp->pred;
-  module func = (module)toppred->func;
-  while (1) {
-    func = ((module (*)())func)(glbl, qp, heapp, toppred);
+  module func = (module) toppred->func;
+  for(;;){
+    func = ((module (*)()) func)(glbl, qp, heapp, toppred);
     qp = current_queue;
     toppred = qp->pred;
   }
 }
 
+
+extern int
 klic_main(argc, argv)
-     int argc;
-     char **argv;
+  int argc;
+  char** argv;
 {
   declare_globals;
-  q *initiate_prioq();
-#ifdef USESIG
-  void init_klic_signal_handling();
-#endif
-#ifdef USETIMER
-  void init_virtualized_timer();
-  void init_klic_timer_handling();
-#endif
+
 #ifdef SHM
   struct timeval time_before;
   struct timezone tzp;
   gettimeofday(&time_before, NULL);
-#endif /*SHM*/
+#endif /* SHM */
+
   program_name = argv[0];
   total_node = num_pes = 1;
   my_node = 0;
   h_size = HEAPSIZE;
-  max_h_size = (unsigned long)(-1); /* largest possible */
+  max_h_size = (unsigned long) -1;  /* largest possible */
   max_a_ratio = 0.5;
   inc_size = INCREMENTSIZE;
+
 #ifdef USETIMER
-  dl_det_interval = 500000;	/* default deadlock detect interval is 500ms */
+  dl_det_interval = 500000uL;	/* default deadlock detect interval is 500ms */
 #endif
+
   {
-      int targc = argc;
-      char **targv = argv;
-      char *errp = parse_opts(opttable, &targc, &targv);
-      if (errp) {
-	  int i;
-	  klic_local_fprintf(stderr,
+    int targc = argc;
+    char** targv = argv;
+    char* errp = parse_opts(opttable, &targc, &targv);
+    if( errp != NULL ){
+      int i;
+      klic_local_fprintf(stderr,
 		       "Invalid or missusage option or value '%s'\n", errp);
-	  klic_local_fprintf(stderr,
+      klic_local_fprintf(stderr,
 		     "Usage: %s [options, ...] [program arguments, ...]\n",
 		       program_name);
-	  for (i = 0; opttable[i].opt_str; ++i) {
-	      if (opttable[i].opt_usage)
-		klic_local_fprintf(stderr, "\t%s\n", opttable[i].opt_usage);
-	  }
-	  exit(1);
+      for( i = 0; opttable[i].opt_str; ++i ){
+	if( opttable[i].opt_usage != NULL )
+	  klic_local_fprintf(stderr, "\t%s\n", opttable[i].opt_usage);
       }
-      command_argc = targc;
-      command_argv = targv;
-      if (verbose) 
-	klic_local_fprintf(stderr, "KLIC runtime version %s (%s)\n",
-			   runtime_version, runtime_date);
-      
+      exit(1);
+    }
+    command_argc = targc;
+    command_argv = targv;
+    if( verbose )
+      klic_local_fprintf(stderr, "KLIC runtime version %s (%s)\n",
+			 runtime_version, runtime_date);
   }
   total_node = num_pes;
+
 #ifdef DIST
 #ifdef USE_REDUCTION_COUNT
   reduction_count = 0;
 #endif
-  if (total_node == 0)
+  if( total_node == 0 )
     total_node = 1;
 
-  if (eager_transfer_all_flag) {
-      eager_transfer_level = -1;
-  } else if (!eager_transfer_level) {
-      eager_transfer_level = 1;
+  if( eager_transfer_all_flag ){
+    eager_transfer_level = -1;
+  }else if( eager_transfer_level == 0 ){
+    eager_transfer_level = 1;
   }
   {
-      int child_id;
-      /* Spawn total_node+1 nodes.
-	 1 node for SHOEN node, 'total_node' for reduction nodes.
-	 the spawn_children() should return:
-	 parent process:	0
-	 child processes:	1 <= nodeid <=  total_node+1 */
-      child_id = spawn_children(total_node + 1, opttable,
-				    program_name, command_argc, command_argv);
-      if (child_id == 0) {
-	  io_node_flag = 1;  /* for klic_fprintf */
-	  io_init_comm();
-	  io_server(command_argc, command_argv);
-	  /* never return from io_server() */
-	  exit(0);
-      }
-      my_node = child_id - 1;
-      total_node = num_pes;
-      /* Master node:		my_node == 0		IS_MASTER_NODE()
-	 SHOEN node:		my_node == total_node	IS_SHOEN_NODE()
-	 Reduction nodes:	0 <= my_node < total_node
-	 */
-      init_dist();
+    int child_id;
+    /* Spawn total_node+1 nodes.
+     * 1 node for SHOEN node, 'total_node' for reduction nodes.
+     * the spawn_children() should return:
+     * parent process:	0
+     * child processes:	1 <= nodeid <=  total_node+1 */
+    child_id = spawn_children(total_node + 1, opttable,
+			      program_name, command_argc, command_argv);
+    if( child_id == 0 ){
+      io_node_flag = 1;  /* for klic_fprintf */
+      io_init_comm();
+      io_server(command_argc, command_argv);
+      /* never return from io_server() */
+      exit(0);
+    }
+    my_node = child_id - 1;
+    total_node = num_pes;
+    /* Master node:		my_node == 0		IS_MASTER_NODE()
+     * SHOEN node:		my_node == total_node	IS_SHOEN_NODE()
+     * Reduction nodes:	0 <= my_node < total_node
+     */
+    init_dist();
   }
 #endif /* DIST */
+
   /* All processing depending on the argument variables must be done
-     after spawn_children() or it fails to read the arguments. */
+   * after spawn_children() or it fails to read the arguments. */
   maxactiveratio = max_a_ratio;
   heapsize = h_size;
   maxheapsize = max_h_size;
   incrementsize = inc_size;
-  if (heapsize > maxheapsize)
+  if( heapsize > maxheapsize )
     maxheapsize = heapsize;
   initalloc();
 
 #ifdef SHM
   My_glbl = glbl = shm_init(glbl);
-#if 0 /* for Debugging */
-  getchar()
-#endif
 #endif
 
   {
-    extern struct goalrec *get_top_priority_queue();
-    q *allocp;
-    struct goalrec *qp;
+    q* allocp;
+    struct goalrec* qp;
 
     allocp = initiate_prioq(heapp);
-    qp = (struct goalrec *) allocp;
+    qp = (struct goalrec*) allocp;
 
 #ifdef USESIG
     init_klic_signal_handling();
-#endif	/*USESIG */
+#endif /* USESIG */
 #ifdef USETIMER
     init_virtualized_timer();
     init_klic_timer_handling();
-#endif
+#endif /* USETIMER */
 #ifdef DIST
     register_gc_hook(gc_exp_table);
     register_gc_hook(gc_decode_stack);
     register_after_gc_hook(scan_imp_table);
 
-    if (IS_MASTER_NODE(my_node)) {	/* master node */
-	qp->pred = &predicate_main_xmain_0;
-	heapp = allocp + 2;
-	node_wtc = SUPPLY_WTC_UNIT;
-    } else if (IS_SHOEN_NODE(my_node)) {	/* shoen node */
-	add_signal_handler(SIGTERM, kill_tasks);
-	shoen_wtc = -SUPPLY_WTC_UNIT;
-	{
-	    struct timeval tp;
-	    if(!gettimeofday(&tp, NULL)){
-		netstat.start_sec = tp.tv_sec;
-		netstat.start_usec= tp.tv_usec;
-	    }
-	}
+    if( IS_MASTER_NODE(my_node) ){	/* master node */
+      qp->pred = &predicate_main_xmain_0;
+      heapp = allocp + 2;
+      node_wtc = SUPPLY_WTC_UNIT;
+    }else if( IS_SHOEN_NODE(my_node) ){	/* shoen node */
+      struct timeval tp;
+
+      add_signal_handler(SIGTERM, kill_tasks);
+      shoen_wtc = -SUPPLY_WTC_UNIT;
+      if( !gettimeofday(&tp, NULL) ){
+	netstat.start_sec = tp.tv_sec;
+	netstat.start_usec= tp.tv_usec;
+      }
     }
-    init_rusage(); /***** for inter NODE *****/
+    init_rusage();  /***** for inter NODE *****/
 #else /* !DIST */
     qp->pred = &predicate_main_xmain_0;
     heapp = allocp + 2;
@@ -329,7 +333,8 @@
     suspensions = 0;
     resumes = 0;
 #endif /* DEBUGLIB */
-    while (1) {
+
+    for(;;){
 #ifdef DEBUGLIB
       cum_susps += suspensions;
       suspensions = 0;
@@ -337,23 +342,24 @@
       resumes = 0;
       current_prio = -1;
       suspended_goal_list = 0;
-      if (start_tracing) {
-	  initiate_trace();
+      if( start_tracing ){
+	initiate_trace();
 #ifdef DIST
-	  if (IS_MASTER_NODE(my_node))
+	if( IS_MASTER_NODE(my_node) )
 #endif
-	    qp = trace_goal(qp, 1, NULL);
-	  start_tracing = 0;
+	  qp = trace_goal(qp, 1, NULL);
+	start_tracing = 0;
       }
       initialize_suspension_count_table();
 #endif	/*DEBUGLIB*/
+
 #ifdef DIST
-      if (IS_MASTER_NODE(my_node))
+      if( IS_MASTER_NODE(my_node) )
 #endif
 #ifdef SHM
-      if ( my_node == MASTER )
+      if( my_node == MASTER )
 #endif
-	(void) enqueue_goal(NULL, HIGHESTPRIO-1, qp, glbl);
+	enqueue_goal(NULL, HIGHESTPRIO-1, qp, glbl);
 
       current_queue = get_top_priority_queue();
       resumed_goals = 0;
@@ -366,66 +372,69 @@
       postmortem_args = 0;
 #endif /* DIST */
 
-      if (setjmp(klic_topmost) == 0) toploop();
+      if( setjmp(klic_topmost) == 0 ) toploop();
 
 #ifdef DIST
       DetachIO();
       CloseIO();
       Close_net();
       break;
-#else /*!DIST*/
-      if (postmortem_pred == 0 || postmortem_args == 0) break;
+#else /* !DIST */
+      if( postmortem_pred == 0 || postmortem_args == 0 ) break;
       klic_fprintf(stderr, "Starting postmortem processing...\n");
-      allocp = initiate_prioq(heapp+incrementsize/2); /* dirty patch */
-      qp = (struct goalrec *) allocp;
+      allocp = initiate_prioq(heapp+incrementsize/2);  /* dirty patch */
+      qp = (struct goalrec*) allocp;
       qp->pred = postmortem_pred;
       allocp += 2;
-      if (isfunctor(postmortem_args)) {
+      if( isfunctor(postmortem_args) ){
 	int arity = arityof(functor_of(postmortem_args));
 	int k;
-	for (k=0; k<arity; k++) {
-	  *allocp++ = arg(postmortem_args, k);
+	for( k=0; k<arity; k++ ){
+	  *allocp = arg(postmortem_args, k);
+	  allocp++;
 	}
       }
       heapp = allocp;
-#endif /*DIST*/
+#endif /* DIST */
     }
   }
+
 #ifdef SHM
-  if ( my_node == MASTER ) {
+  if( my_node == MASTER ){
+    struct timeval time_after;
+    struct timezone tzp;
+
     wait(0);
-    {
-      struct timeval time_after;
-      struct timezone tzp;
-      if (!gettimeofday(&time_after, NULL)) {
-	fprintf(stderr,
+    if( !gettimeofday(&time_after, NULL) ){
+      fprintf(stderr,
 		"Response time is %d msec\n",
 		(time_after.tv_sec - time_before.tv_sec)*1000 +
 		(time_after.tv_usec - time_before.tv_usec)/1000);
-      }
     }
   }
-#endif /*SHM*/
+#endif /* SHM */
+
   return 0;
 }
 
+
 #ifdef USETIMER
-static void dummy_alarm_handler()
+static void
+dummy_alarm_handler()
 {
-    dummy_alarm_set = 0;
+  dummy_alarm_set = 0;
 }
-#endif
+#endif /* USETIMER */
+
 
-module topsucceed(glbl, qp, allocp, toppred)
-     struct global_variables *glbl;
-     struct goalrec *qp;
-     q *allocp;
-     const struct predicate* toppred;
+extern module
+topsucceed(glbl, qp, allocp, toppred)
+  struct global_variables* glbl;
+  struct goalrec* qp;
+  q* allocp;
+  const struct predicate* toppred;
 #ifdef DIST
 {
-  extern void send_return_wtc();
-  extern const struct predicate topsucceed_pred;
-
 #ifdef USE_REDUCTION_COUNT
   fd_set rfds;
   struct timeval tv;
@@ -435,96 +444,96 @@
 #endif
 
  idle_loop:
-      
-  if (IS_SHOEN_NODE(my_node)) {	/* if shoen node */
-      if (shoen_wtc > 0) { 
-	  ioeprintf("invalid wtc in shoen node\n");
-	  fatal("invalid WTC");
-      } else if(shoen_wtc == 0){
-	  struct timeval tp;
-	  if(!gettimeofday(&tp, NULL)){
-	      iosprintf("Response time is %d msec\n",
-		      (tp.tv_sec - netstat.start_sec)*1000 +
-		      (tp.tv_usec - netstat.start_usec)/1000);
-	  }
-	  terminate_all_node();
-	  CloseIO();
-	  Close_net();
-	  exit(0);
-      }
-  } else {			/* if normal node */
-      if ( suspensions == resumes && !susp_msg_list) {
-	  if (node_wtc)
-	    send_return_wtc(node_wtc);
+
+  if( IS_SHOEN_NODE(my_node) ){	/* if shoen node */
+    if( shoen_wtc > 0 ){ 
+      ioeprintf("invalid wtc in shoen node\n");
+      fatal("invalid WTC");
+    }else if( shoen_wtc == 0 ){
+      struct timeval tp;
+      if( !gettimeofday(&tp, NULL) ){
+	iosprintf("Response time is %d msec\n",
+		  (tp.tv_sec - netstat.start_sec)*1000 +
+		  (tp.tv_usec - netstat.start_usec)/1000 );
       }
+      terminate_all_node();
+      CloseIO();
+      Close_net();
+      exit(0);
+    }
+  }else{			/* if normal node */
+    if( suspensions == resumes && !susp_msg_list ){
+      if( node_wtc )
+	send_return_wtc(node_wtc);
+    }
   }
 
   IDLE_ON();
 #ifdef SHM_DIST
-  if (!(IS_SHOEN_NODE(my_node))) {
-    if (interrupt_off) while ( (allocp <= heaplimit) && (signal_done));
-    if (!signal_done)  {
+  if( !IS_SHOEN_NODE(my_node) ){
+    if( interrupt_off )
+      while( (allocp <= heaplimit) && (signal_done) )
+	;
+    if( !signal_done ){
       heaplimit = 0;
       interrupt_off = 0;
     }
-  } else 
-#endif
-    {
+  }else
+#endif /* SHM_DIST */
+  {
 #ifdef USE_REDUCTION_COUNT
-	/* Wait up to five seconds. */
-	tv.tv_sec = 0;
-  if (!(IS_SHOEN_NODE(my_node))) {
-	tv.tv_usec = polling_interval;
-  } else {
-	tv.tv_usec = SHOEN_POLLING_INTERVAL;
+    /* Wait up to five seconds. */
+    tv.tv_sec = 0;
+    if( !IS_SHOEN_NODE(my_node) ){
+      tv.tv_usec = polling_interval;
+    }else{
+      tv.tv_usec = SHOEN_POLLING_INTERVAL;
+    }
+    select(1, &rfds, NULL, NULL, &tv);
+#else /* not USE_REDUCTION_COUNT */
+    sigset_t new_mask;
+    sigset_t prev_mask;
+    if( sigfillset(&new_mask) < 0 )
+      fatalp("sigfillset", "");
+    if( sigprocmask(SIG_BLOCK, &new_mask, &prev_mask) < 0 )
+      fatalp("sigprocmask", "mask set error");
+    if( interrupt_off && sigsuspend(&prev_mask) < 0 && errno != EINTR )
+      fatalp("sigsuspend", "");
+    if( sigprocmask(SIG_SETMASK, &prev_mask, (sigset_t*) 0) < 0 )
+      fatalp("sigprocmask", "mask reset error");
+#endif /* not USE_REDUCTION_COUNT */
   }
-	(void) select(1, &rfds, NULL, NULL, &tv);
-#else
-	sigset_t new_mask;
-	sigset_t prev_mask;
-	if (sigfillset(&new_mask) < 0)
-	  fatalp("sigfillset", "");
-	if (sigprocmask(SIG_BLOCK, &new_mask, &prev_mask) < 0)
-	  fatalp("sigprocmask", "mask set error");
-	if (interrupt_off)
-	  if (sigsuspend(&prev_mask) < 0 && errno != EINTR)
-	    fatalp("sigsuspend", "");
-	if (sigprocmask(SIG_SETMASK, &prev_mask, (sigset_t *)0) < 0)
-	  fatalp("sigprocmask", "mask reset error");
-#endif
-      }
 
-  allocp[0] = (q)qp->next;
-  allocp[1] = (q)&topsucceed_pred;
-  qp = (struct goalrec *)allocp;
+  allocp[0] = (q) qp->next;
+  allocp[1] = (q) &topsucceed_pred;
+  qp = (struct goalrec*) allocp;
   allocp += 2;
 
   IDLE_OFF();
   heapp = klic_interrupt(allocp, qp);
   qp = current_queue;
 
-  if ((qp->pred == &topsucceed_pred) && (heapp == allocp)) {
-      allocp -= 2;
-      goto idle_loop;
+  if( (qp->pred == &topsucceed_pred) && (heapp == allocp) ){
+    allocp -= 2;
+    goto idle_loop;
   }
-  
+
   return (module) current_queue->pred->func;
 }
 
-#else /* #ifndef DIST */
+#else /* not DIST */
 {
-  extern const struct predicate topsucceed_pred;
-  if (suspensions == resumes) {
+  if( suspensions == resumes ){
 #ifdef DEBUGLIB
-    if(count_suspension) {
-      if(suspensions) {
+    if( count_suspension ){
+      if( suspensions ){
 	klic_fprintf(stderr, "Suspension counts:\n");
 	scan_suspended_pred();
-      } else {
+      }else{
 	klic_fprintf(stderr, "No suspension in this run.\n");
       }
     }
-#endif
+#endif /* DEBUGLIB */
     longjmp(klic_topmost, 1);
   }
 
@@ -532,70 +541,66 @@
   /* perpetual suspension suspected, but let's wait a while for interrupts */
 
   /* enqueue topsucceed goal again */
-  allocp[0] = (q)qp;
-  allocp[1] = (q)&topsucceed_pred;
-  qp = (struct goalrec *)allocp;
+  allocp[0] = (q) qp;
+  allocp[1] = (q) &topsucceed_pred;
+  qp = (struct goalrec*) allocp;
   allocp += 2;
 
   /* Let's not hastily check perpetual suspension */
 #ifdef USETIMER
-  if (dl_det_interval) {
-    extern void call_after_specified_interval();
+  if( dl_det_interval != 0uL ){
     sigset_t new_mask;
     sigset_t prev_mask;
     sigfillset(&new_mask);
     sigprocmask(SIG_BLOCK, &new_mask, &prev_mask);
-    while (interrupt_off && dummy_alarm_set)
+    while( interrupt_off && dummy_alarm_set )
       sigsuspend(&prev_mask);
-    if (interrupt_off) {
-	call_after_specified_interval(dl_det_interval/(unsigned long)1000000,
-				      dl_det_interval%(unsigned long)1000000,
-				      dummy_alarm_handler);
-	dummy_alarm_set = 1;
-	/* Use while-do rather than do-while to avoid suspected OS bugs */
-	while (interrupt_off && dummy_alarm_set)
-	    sigsuspend(&prev_mask);
+    if( interrupt_off ){
+      call_after_specified_interval(dl_det_interval/1000000uL,
+				    dl_det_interval%1000000uL,
+				    dummy_alarm_handler);
+      dummy_alarm_set = 1;
+      /* Use while-do rather than do-while to avoid suspected OS bugs */
+      while( interrupt_off && dummy_alarm_set )
+	sigsuspend(&prev_mask);
     }
     sigprocmask(SIG_SETMASK, &prev_mask, 0);
   }
-#else
+#else /* not USETIMER */
 #ifdef USEUSLEEP
   usleep(1000);
 #else
-  (void) sleep(1);
-#endif /*USEUSLEEP*/
-#endif
+  sleep(1);
+#endif /* USEUSLEEP */
+#endif /* not USETIMER */
 
   /* If no interrupts during the pause, run GC */
-  if (interrupt_off)
+  if( interrupt_off )
 #ifdef USETIMER
-    if (dl_det_interval != 0)
+    if( dl_det_interval != 0uL )
 #endif
       allocp = real_heaplimit;
   allocp = klic_interrupt(allocp, qp);
   qp = current_queue;
 
-  if (qp->pred == &topsucceed_pred) {
+  if( qp->pred == &topsucceed_pred ){
     /* no interrupt after topsucceed was called */
-      sigset_t new_mask;
-      sigset_t prev_mask;
-      if (sigfillset(&new_mask) < 0)
-	fatalp("sigfillset", "");
-      if (sigprocmask(SIG_BLOCK, &new_mask, &prev_mask) < 0)
-	fatalp("sigprocmask", "mask set error");
-      if (interrupt_off)
-	if (sigsuspend(&prev_mask) < 0 && errno != EINTR)
-	  fatalp("sigsuspend", "");
-      if (sigprocmask(SIG_SETMASK, &prev_mask, (sigset_t *)0) < 0)
-	fatalp("sigprocmask", "mask reset error");
-
+    sigset_t new_mask;
+    sigset_t prev_mask;
+    if( sigfillset(&new_mask) < 0 )
+      fatalp("sigfillset", "");
+    if( sigprocmask(SIG_BLOCK, &new_mask, &prev_mask) < 0 )
+      fatalp("sigprocmask", "mask set error");
+    if( interrupt_off && sigsuspend(&prev_mask) < 0 && errno != EINTR )
+      fatalp("sigsuspend", "");
+    if( sigprocmask(SIG_SETMASK, &prev_mask, (sigset_t*) 0) < 0 )
+      fatalp("sigprocmask", "mask reset error");
   }
   /* check resumpsions */
   heapp = klic_interrupt(allocp, qp);
   return (module) current_queue->pred->func;
-#else /*!USESIG*/
-  fatalf("%d perpetually suspending goals found",
-	 suspensions-resumes);
-#endif /*USESIG*/
+#else /* not USESIG */
+  fatalf("%d perpetually suspending goals found", suspensions-resumes);
+#endif /* not USESIG */
 }
-#endif /*DIST*/
+#endif /* not DIST */
diff -ruN klic-3.003-2002-01-06c/runtime/ktimer.c klic-3.003-2002-01-07/runtime/ktimer.c
--- klic-3.003-2002-01-06c/runtime/ktimer.c	Sat Dec 29 12:46:20 2001
+++ klic-3.003-2002-01-07/runtime/ktimer.c	Mon Jan  7 09:51:56 2002
@@ -5,15 +5,30 @@
 %       (Read COPYRIGHT-JIPDEC for detailed information.)
 ----------------------------------------------------------- */
 
-#include <klic/basic.h>
 #ifdef USETIMER
+#include <klic/basic.h>
 #include <sys/time.h>
 #include <klic/alloc.h>  /* malloc_check, register_gc_hook */
 #include <klic/struct.h>
 #include "timer.h"
 #include <klic/sighndl.h>
 
-void klic_timer_interrupt_handler()
+extern q* do_unify_value();
+extern q* copy_one_term();
+
+struct timer_reservation_rec {
+  struct timeval at;
+  struct timeval interval;
+  struct timer_reservation_rec* next;
+  int (*handler)();
+  q data;
+};
+
+static struct timer_reservation_rec* timer_reservations;
+static struct timer_reservation_rec* timer_rsv_free;
+
+static void
+klic_timer_interrupt_handler()
 {
   declare_globals;
   interrupt_off = 0;
@@ -22,70 +37,60 @@
   signal_flags[SIGALRM] = 1;
 }
 
-struct timer_reservation_rec {
-  struct timeval at;
-  struct timeval interval;
-  struct timer_reservation_rec *next;
-  int (*handler)();
+static int
+process_timed_instantiation(allocp, data)
+  q* allocp;
   q data;
-};
-static struct timer_reservation_rec *timer_reservations;
-static struct timer_reservation_rec *timer_rsv_free;
-
-static int process_timed_instantiation(allocp, data)
-     q *allocp;
-     q data;
 {
   declare_globals;
-  extern q *do_unify_value();
 
   heapp = do_unify_value(allocp, data, NILATOM);
   return 0;
 }
 
-static int process_simple_timer_handler(allocp, data)
-     q *allocp;
-     q data;
+static int
+process_simple_timer_handler(allocp, data)
+  q* allocp;
+  q data;
 {
   declare_globals;
-
-  return ((int (*)())((unsigned long)data-ATOMIC))(allocp, SIGALRM);
+  return ((int (*)()) ((unsigned long)data-ATOMIC))(allocp, SIGALRM);
 }
 
-int
+static int
 process_timer_interrupt(allocp, sig)
-     q* allocp;
-     long sig;
+  q* allocp;
+  long sig;
 {
   declare_globals;
   struct timeval now;
 
   klic_gettod(&now);
-  while (timer_reservations != 0 &&
-	 NotLaterThan(timer_reservations->at, now)) {
+  while( timer_reservations != NULL &&
+	 NotLaterThan(timer_reservations->at, now) ){
     int result =
       timer_reservations-> handler(allocp, timer_reservations->data);
-    if (result != 0) return result;
+    if( result != 0 ) return result;
     allocp = heapp;
-    if (TimeIsZero(timer_reservations->interval)) {
-      struct timer_reservation_rec *tmp = timer_reservations->next;
+    if( TimeIsZero(timer_reservations->interval) ){
+      struct timer_reservation_rec* tmp = timer_reservations->next;
       timer_reservations->next = timer_rsv_free;
       timer_rsv_free = timer_reservations;
       timer_reservations = tmp;
-    } else {
-      struct timer_reservation_rec **tmp, *trec;
-      trec = timer_reservations;
+    }else{
+      struct timer_reservation_rec** tmp;
+      struct timer_reservation_rec* trec = timer_reservations;
       TimeAdd(trec->at, trec->interval, trec->at);
       timer_reservations = timer_reservations->next;
-      for (tmp = &timer_reservations;
-	   *tmp != 0 && NotLaterThan((*tmp)->at, trec->at);
-	   tmp = &(*tmp)->next)
+      for( tmp = &timer_reservations;
+	   *tmp != NULL && NotLaterThan((*tmp)->at, trec->at);
+	   tmp = &(*tmp)->next )
 	;
       trec->next = *tmp;
       *tmp = trec;
     }
   }
-  if (timer_reservations != 0) {
+  if( timer_reservations != NULL ){
     call_at_specified_time(timer_reservations->at.tv_sec,
 			   timer_reservations->at.tv_usec,
 			   klic_timer_interrupt_handler);
@@ -95,47 +100,49 @@
 }
 
 
-static q* gc_timer_data(allocp, ntop, otop, nsize, osize)
-     q *allocp, *ntop, *otop;
-     unsigned long nsize, osize;
-{
-  struct timer_reservation_rec *trec;
-  extern q *copy_one_term();
-  for (trec = timer_reservations;
-       trec != 0;
-       trec = trec->next) {
+static q*
+gc_timer_data(allocp, ntop, otop, nsize, osize)
+  q *allocp, *ntop, *otop;
+  unsigned long nsize, osize;
+{
+  struct timer_reservation_rec* trec;
+  for( trec = timer_reservations;
+       trec != NULL;
+       trec = trec->next ){
     allocp =
       copy_one_term(&trec->data, allocp, ntop, otop, nsize, osize);
   }
   return allocp;
 }
 
-void init_klic_timer_handling()
+extern void
+init_klic_timer_handling()
 {
-  timer_reservations = 0;
-  timer_rsv_free = 0;
+  timer_reservations = NULL;
+  timer_rsv_free = NULL;
   register_gc_hook(gc_timer_data);
   add_slit_check_handler(SIGALRM, process_timer_interrupt);
 }
 
-static struct timer_reservation_rec *
+static struct timer_reservation_rec*
 alloc_timer_reservation_rec()
 {
-  struct timer_reservation_rec *newrec;
-  if (timer_rsv_free == 0) {
-    newrec = (struct timer_reservation_rec *)
+  struct timer_reservation_rec* newrec;
+  if( timer_rsv_free == 0 ){
+    newrec = (struct timer_reservation_rec*)
       malloc_check(sizeof(struct timer_reservation_rec));
-  } else {
+  }else{
     newrec = timer_rsv_free;
     timer_rsv_free = timer_rsv_free->next;
   }
   return newrec;
 }
 
-q *set_simple_interval_timer_handler(allocp, sec, usec, func)
-     q *allocp;
-     long sec, usec;
-     int (*func)();
+extern q*
+set_simple_interval_timer_handler(allocp, sec, usec, func)
+  q* allocp;
+  long sec, usec;
+  int (*func)();
 {
   declare_globals;
   struct timeval now;
@@ -148,9 +155,9 @@
   TimeAdd(newrec->at, newrec->interval, newrec->at);
   newrec->handler = process_simple_timer_handler;
   newrec->data = makeatomic(func);
-  for (tmp = &timer_reservations;
-       *tmp != 0 && NotLaterThan((*tmp)->at, newrec->at);
-       tmp = &(*tmp)->next)
+  for( tmp = &timer_reservations;
+       *tmp != NULL && NotLaterThan((*tmp)->at, newrec->at);
+       tmp = &(*tmp)->next )
     ;
   newrec->next = *tmp;
   *tmp = newrec;
@@ -158,10 +165,11 @@
   return heapp;
 }  
 
-q *reserve_klic_timer_interrupt(allocp, sec, usec, vrbl)
-     q *allocp;
-     long sec, usec;
-     q vrbl;
+extern q*
+reserve_klic_timer_interrupt(allocp, sec, usec, vrbl)
+  q* allocp;
+  long sec, usec;
+  q vrbl;
 {
   declare_globals;
   struct timer_reservation_rec *newrec, **tmp;
@@ -171,13 +179,13 @@
   TimeSet(newrec->interval, 0, 0);
   newrec->handler = process_timed_instantiation;
   newrec->data = vrbl;
-  for (tmp = &timer_reservations;
-       *tmp != 0 && NotLaterThan((*tmp)->at, newrec->at);
-       tmp = &(*tmp)->next)
+  for( tmp = &timer_reservations;
+       *tmp != NULL && NotLaterThan((*tmp)->at, newrec->at);
+       tmp = &(*tmp)->next )
     ;
   newrec->next = *tmp;
   *tmp = newrec;
   process_timer_interrupt(allocp, SIGALRM);
   return heapp;
 }
-#endif
+#endif  /* USETIMER */
