*** empty log message ***
[bpt/guile.git] / libguile / gc.c
index 9d4d321..2eebbb5 100644 (file)
@@ -1,47 +1,26 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003 Free Software Foundation, Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this software; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- *
- * As a special exception, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
- *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
 
 
 /* #define DEBUGINFO */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
 
 #include <stdio.h>
 #include <errno.h>
@@ -90,6 +69,10 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 
 unsigned int scm_gc_running_p = 0;
 
+/* Lock this mutex before doing lazy sweeping.
+ */
+scm_t_rec_mutex scm_i_sweep_mutex;
+
 /* Set this to != 0 if every cell that is accessed shall be checked:
  */
 int scm_debug_cell_accesses_p = 0;
@@ -144,9 +127,7 @@ scm_i_expensive_validation_check (SCM cell)
       else
        {
          counter = scm_debug_cells_gc_interval;
-         scm_i_thread_put_to_sleep ();
          scm_igc ("scm_assert_cell_valid");
-         scm_i_thread_wake_up ();
        }
     }
 }
@@ -226,26 +207,7 @@ SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
-#else
 
-/*
-  Provide a stub, so people can use their Scheme code on non-debug
-  versions of GUILE as well.
- */
-SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
-           (SCM flag),
-           "This function is used to turn on checking for a debug version of GUILE. This version does not support this functionality\n")
-#define FUNC_NAME s_scm_set_debug_cell_accesses_x
-{
-  
-  /*
-    do nothing
-   */
-  fprintf (stderr, "\nWARNING: GUILE was not compiled with SCM_DEBUG_CELL_ACCESSES");
-  scm_remember_upto_here (flag);
-  return SCM_UNSPECIFIED;
-}
-#undef FUNC_NAME
 
 #endif  /* SCM_DEBUG_CELL_ACCESSES == 1 */
 
@@ -294,6 +256,7 @@ double scm_gc_cells_marked_acc = 0.;
 double scm_gc_cells_swept_acc = 0.;
 int scm_gc_cell_yield_percentage =0;
 int scm_gc_malloc_yield_percentage = 0;
+unsigned long protected_obj_count = 0;
 
 
 SCM_SYMBOL (sym_cells_allocated, "cells-allocated");
@@ -308,6 +271,7 @@ SCM_SYMBOL (sym_cells_marked, "cells-marked");
 SCM_SYMBOL (sym_cells_swept, "cells-swept");
 SCM_SYMBOL (sym_malloc_yield, "malloc-yield");
 SCM_SYMBOL (sym_cell_yield, "cell-yield");
+SCM_SYMBOL (sym_protected_objects, "protected-objects");
 
 
 
@@ -337,6 +301,7 @@ SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
   unsigned long int local_scm_gc_time_taken;
   unsigned long int local_scm_gc_times;
   unsigned long int local_scm_gc_mark_time_taken;
+  unsigned long int local_protected_obj_count;
   double local_scm_gc_cells_swept;
   double local_scm_gc_cells_marked;
   SCM answer;
@@ -372,7 +337,7 @@ SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
   local_scm_gc_times = scm_gc_times;
   local_scm_gc_malloc_yield_percentage = scm_gc_malloc_yield_percentage;
   local_scm_gc_cell_yield_percentage=  scm_gc_cell_yield_percentage;
-  
+  local_protected_obj_count = protected_obj_count;
   local_scm_gc_cells_swept =
     (double) scm_gc_cells_swept_acc
     + (double) scm_gc_cells_swept;
@@ -397,7 +362,8 @@ SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
                       scm_cons (sym_cells_marked, scm_i_dbl2big (local_scm_gc_cells_marked)),
                       scm_cons (sym_cells_swept, scm_i_dbl2big (local_scm_gc_cells_swept)),
                       scm_cons (sym_malloc_yield, scm_long2num (local_scm_gc_malloc_yield_percentage)),
-                      scm_cons (sym_cell_yield, scm_long2num (local_scm_gc_cell_yield_percentage)),                   
+                      scm_cons (sym_cell_yield, scm_long2num (local_scm_gc_cell_yield_percentage)),
+                      scm_cons (sym_protected_objects, scm_ulong2num (local_protected_obj_count)),
                       scm_cons (sym_heap_segments, heap_segs),
                       SCM_UNDEFINED);
   SCM_ALLOW_INTS;
@@ -458,11 +424,7 @@ SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
            "no longer accessible.")
 #define FUNC_NAME s_scm_gc
 {
-  SCM_DEFER_INTS;
-  scm_i_thread_put_to_sleep ();
   scm_igc ("call");
-  scm_i_thread_wake_up ();
-  SCM_ALLOW_INTS;
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -479,7 +441,7 @@ scm_gc_for_newcell (scm_t_cell_type_statistics *freelist, SCM *free_cells)
 {
   SCM cell;
  
-  scm_i_thread_put_to_sleep ();
+  scm_rec_mutex_lock (&scm_i_sweep_mutex);
 
   *free_cells = scm_i_sweep_some_segments (freelist);
   if (*free_cells == SCM_EOL && scm_i_gc_grow_heap_p (freelist))
@@ -521,7 +483,7 @@ scm_gc_for_newcell (scm_t_cell_type_statistics *freelist, SCM *free_cells)
 
   *free_cells = SCM_FREE_CELL_CDR (cell);
 
-  scm_i_thread_wake_up ();
+  scm_rec_mutex_unlock (&scm_i_sweep_mutex);
 
   return cell;
 }
@@ -536,6 +498,7 @@ scm_t_c_hook scm_after_gc_c_hook;
 void
 scm_igc (const char *what)
 {
+  scm_rec_mutex_lock (&scm_i_sweep_mutex);
   ++scm_gc_running_p;
   scm_c_hook_run (&scm_before_gc_c_hook, 0);
 
@@ -549,11 +512,7 @@ scm_igc (const char *what)
 #endif
 
   /* During the critical section, only the current thread may run. */
-#if 0 /* MDJ 021207 <djurfeldt@nada.kth.se>
-       Currently, a much larger piece of the GC is single threaded.
-       Can we shrink it again? */
-  SCM_CRITICAL_SECTION_START;
-#endif
+  scm_i_thread_put_to_sleep ();
 
   if (!scm_root || !scm_stack_base || scm_block_gc)
     {
@@ -613,19 +572,37 @@ scm_igc (const char *what)
   /*
     TODO: this hook should probably be moved to just before the mark,
     since that's where the  sweep is finished in lazy sweeping.
+
+    MDJ 030219 <djurfeldt@nada.kth.se>: No, probably not.  The
+    original meaning implied at least two things: that it would be
+    called when
+
+      1. the freelist is re-initialized (no evaluation possible, though)
+      
+    and
+    
+      2. the heap is "fresh"
+         (it is well-defined what data is used and what is not)
+
+    Neither of these conditions would hold just before the mark phase.
+    
+    Of course, the lazy sweeping has muddled the distinction between
+    scm_before_sweep_c_hook and scm_after_sweep_c_hook, but even if
+    there were no difference, it would still be useful to have two
+    distinct classes of hook functions since this can prevent some
+    bad interference when several modules adds gc hooks.
    */
   scm_c_hook_run (&scm_after_sweep_c_hook, 0);
   gc_end_stats ();
 
-#if 0 /* MDJ 021207 <djurfeldt@nada.kth.se> */
-  SCM_CRITICAL_SECTION_END;
-#endif
+  scm_i_thread_wake_up ();
 
   /*
     See above.
    */
   scm_c_hook_run (&scm_after_gc_c_hook, 0);
   --scm_gc_running_p;
+  scm_rec_mutex_unlock (&scm_i_sweep_mutex);
 
   /*
     For debugging purposes, you could do
@@ -664,6 +641,12 @@ scm_igc (const char *what)
  *   scm_remember_upto_here_1 (str);  // str will be alive up to this point.
  */
 
+/* Remove any macro versions of these while defining the functions.
+   Functions are always included in the library, for upward binary
+   compatibility and in case combinations of GCC and non-GCC are used.  */
+#undef scm_remember_upto_here_1
+#undef scm_remember_upto_here_2
+
 void
 scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
 {
@@ -727,6 +710,8 @@ scm_permanent_object (SCM obj)
    scm_gc_protect_object(X) increments and scm_gc_unprotect_object(X) decrements.
 */
 
+
+
 SCM
 scm_gc_protect_object (SCM obj)
 {
@@ -738,6 +723,8 @@ scm_gc_protect_object (SCM obj)
   handle = scm_hashq_create_handle_x (scm_protects, obj, SCM_MAKINUM (0));
   SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), SCM_MAKINUM (1)));
 
+  protected_obj_count ++;
+  
   SCM_REALLOW_INTS;
 
   return obj;
@@ -771,6 +758,7 @@ scm_gc_unprotect_object (SCM obj)
       else
        SCM_SETCDR (handle, count);
     }
+  protected_obj_count --;
 
   SCM_REALLOW_INTS;
 
@@ -879,12 +867,24 @@ scm_getenv_int (const char *var, int def)
   return res;
 }
 
+void
+scm_storage_prehistory ()
+{
+  scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
+  scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
+  scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
+  scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
+  scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
+}
 
 int
 scm_init_storage ()
 {
   size_t j;
 
+  /* Fixme: Should use mutexattr from the low-level API. */
+  scm_rec_mutex_init (&scm_i_sweep_mutex, &scm_i_plugin_rec_mutex);
+  
   j = SCM_NUM_PROTECTS;
   while (j)
     scm_sys_protects[--j] = SCM_BOOL_F;
@@ -896,13 +896,6 @@ scm_init_storage ()
   j = SCM_HEAP_SEG_SIZE;
 
   
-
-  scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
-  scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
-  scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
-  scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
-  scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
-
   /* Initialise the list of ports.  */
   scm_i_port_table = (scm_t_port **)
     malloc (sizeof (scm_t_port *) * scm_i_port_table_room);
@@ -1022,6 +1015,9 @@ scm_gc_sweep (void)
      be GC-protected instead. */
   *SCM_FREELIST_LOC (scm_i_freelist) = SCM_EOL;
   *SCM_FREELIST_LOC (scm_i_freelist2) = SCM_EOL;
+
+  /* Invalidate the freelists of other threads. */
+  scm_i_thread_invalidate_freelists ();
 }
 
 #undef FUNC_NAME