Merge remote-tracking branch 'origin/master'
[bpt/guile.git] / libguile / weak-table.c
index 49d5b6d..be73e1b 100644 (file)
@@ -484,7 +484,7 @@ resize_table (scm_t_weak_table *table)
       /* Allocating memory might cause finalizers to run, which could
          run anything, so drop our lock to avoid deadlocks.  */
       new_entries = allocate_entries (new_size, table->kind);
-      scm_i_pthread_mutex_unlock (&table->lock);
+      scm_i_pthread_mutex_lock (&table->lock);
     }
   while (!is_acceptable_size_index (table, new_size_index));
 
@@ -772,79 +772,10 @@ weak_table_remove_x (scm_t_weak_table *table, unsigned long hash,
 
 
 \f
-
-static void
-lock_weak_table (scm_t_weak_table *table)
-{
-  scm_i_pthread_mutex_lock (&table->lock);
-}
-
-static void
-unlock_weak_table (scm_t_weak_table *table)
-{
-  scm_i_pthread_mutex_unlock (&table->lock);
-}
-
-/* A weak table of weak tables, for use in the pthread_atfork handler. */
-static SCM all_weak_tables = SCM_BOOL_F;
-
-#if SCM_USE_PTHREAD_THREADS
-
-static void
-lock_all_weak_tables (void)
-{
-  scm_t_weak_table *s;
-  scm_t_weak_entry *entries;
-  unsigned long k, size;
-  scm_t_weak_entry copy;
-
-  s = SCM_WEAK_TABLE (all_weak_tables);
-  lock_weak_table (s);
-  size = s->size;
-  entries = s->entries;
-
-  for (k = 0; k < size; k++)
-    if (entries[k].hash)
-      {
-        copy_weak_entry (&entries[k], &copy);
-        if (copy.key)
-          lock_weak_table (SCM_WEAK_TABLE (SCM_PACK (copy.key)));
-      }
-}
-
-static void
-unlock_all_weak_tables (void)
-{
-  scm_t_weak_table *s;
-  scm_t_weak_entry *entries;
-  unsigned long k, size;
-  scm_t_weak_entry copy;
-
-  s = SCM_WEAK_TABLE (all_weak_tables);
-  size = s->size;
-  entries = s->entries;
-
-  for (k = 0; k < size; k++)
-    if (entries[k].hash)
-      {
-        copy_weak_entry (&entries[k], &copy);
-        if (copy.key)
-          unlock_weak_table (SCM_WEAK_TABLE (SCM_PACK (copy.key)));
-      }
-
-  unlock_weak_table (s);
-}
-
-#endif /* SCM_USE_PTHREAD_THREADS */
-
-
-\f
-
 static SCM
 make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
 {
   scm_t_weak_table *table;
-  SCM ret;
 
   int i = 0, n = k ? k : 31;
   while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
@@ -862,12 +793,7 @@ make_weak_table (unsigned long k, scm_t_weak_table_kind kind)
   table->min_size_index = i;
   scm_i_pthread_mutex_init (&table->lock, NULL);
 
-  ret = scm_cell (scm_tc7_weak_table, (scm_t_bits)table);
-
-  if (scm_is_true (all_weak_tables))
-    scm_weak_table_putq_x (all_weak_tables, ret, SCM_BOOL_T);
-  
-  return ret;
+  return scm_cell (scm_tc7_weak_table, (scm_t_bits)table);
 }
 
 void
@@ -891,7 +817,7 @@ do_vacuum_weak_table (SCM table)
   if (scm_i_pthread_mutex_trylock (&t->lock) == 0)
     {
       vacuum_weak_table (t);
-      unlock_weak_table (t);
+      scm_i_pthread_mutex_unlock (&t->lock);
     }
 
   return;
@@ -978,11 +904,11 @@ scm_c_weak_table_ref (SCM table, unsigned long raw_hash,
 
   t = SCM_WEAK_TABLE (table);
 
-  lock_weak_table (t);
+  scm_i_pthread_mutex_lock (&t->lock);
 
   ret = weak_table_ref (t, raw_hash, pred, closure, dflt);
 
-  unlock_weak_table (t);
+  scm_i_pthread_mutex_unlock (&t->lock);
 
   return ret;
 }
@@ -1000,11 +926,11 @@ scm_c_weak_table_put_x (SCM table, unsigned long raw_hash,
 
   t = SCM_WEAK_TABLE (table);
 
-  lock_weak_table (t);
+  scm_i_pthread_mutex_lock (&t->lock);
 
   weak_table_put_x (t, raw_hash, pred, closure, key, value);
 
-  unlock_weak_table (t);
+  scm_i_pthread_mutex_unlock (&t->lock);
 }
 #undef FUNC_NAME
 
@@ -1020,11 +946,11 @@ scm_c_weak_table_remove_x (SCM table, unsigned long raw_hash,
 
   t = SCM_WEAK_TABLE (table);
 
-  lock_weak_table (t);
+  scm_i_pthread_mutex_lock (&t->lock);
 
   weak_table_remove_x (t, raw_hash, pred, closure);
 
-  unlock_weak_table (t);
+  scm_i_pthread_mutex_unlock (&t->lock);
 }
 #undef FUNC_NAME
 
@@ -1045,24 +971,22 @@ scm_weak_table_refq (SCM table, SCM key, SCM dflt)
                                dflt);
 }
 
-SCM
+void
 scm_weak_table_putq_x (SCM table, SCM key, SCM value)
 {
   scm_c_weak_table_put_x (table, scm_ihashq (key, -1),
                           assq_predicate, SCM_UNPACK_POINTER (key),
                           key, value);
-  return SCM_UNSPECIFIED;
 }
 
-SCM
+void
 scm_weak_table_remq_x (SCM table, SCM key)
 {
   scm_c_weak_table_remove_x (table, scm_ihashq (key, -1),
                              assq_predicate, SCM_UNPACK_POINTER (key));
-  return SCM_UNSPECIFIED;
 }
 
-SCM
+void
 scm_weak_table_clear_x (SCM table)
 #define FUNC_NAME "weak-table-clear!"
 {
@@ -1072,14 +996,12 @@ scm_weak_table_clear_x (SCM table)
 
   t = SCM_WEAK_TABLE (table);
 
-  lock_weak_table (t);
+  scm_i_pthread_mutex_lock (&t->lock);
 
   memset (t->entries, 0, sizeof (scm_t_weak_entry) * t->size);
   t->n_items = 0;
 
-  unlock_weak_table (t);
-
-  return SCM_UNSPECIFIED;
+  scm_i_pthread_mutex_unlock (&t->lock);
 }
 #undef FUNC_NAME
 
@@ -1093,7 +1015,7 @@ scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
 
   t = SCM_WEAK_TABLE (table);
 
-  lock_weak_table (t);
+  scm_i_pthread_mutex_lock (&t->lock);
 
   size = t->size;
   entries = t->entries;
@@ -1109,16 +1031,16 @@ scm_c_weak_table_fold (scm_t_table_fold_fn proc, void *closure,
           if (copy.key && copy.value)
             {
               /* Release table lock while we call the function.  */
-              unlock_weak_table (t);
+              scm_i_pthread_mutex_unlock (&t->lock);
               init = proc (closure,
                            SCM_PACK (copy.key), SCM_PACK (copy.value),
                            init);
-              lock_weak_table (t);
+              scm_i_pthread_mutex_lock (&t->lock);
             }
         }
     }
   
-  unlock_weak_table (t);
+  scm_i_pthread_mutex_unlock (&t->lock);
   
   return init;
 }
@@ -1147,7 +1069,7 @@ for_each_trampoline (void *closure, SCM k, SCM v, SCM seed)
   return seed;
 }
 
-SCM
+void
 scm_weak_table_for_each (SCM proc, SCM table)
 #define FUNC_NAME "weak-table-for-each"
 {
@@ -1155,8 +1077,6 @@ scm_weak_table_for_each (SCM proc, SCM table)
   SCM_VALIDATE_PROC (1, proc);
 
   scm_c_weak_table_fold (for_each_trampoline, SCM_UNPACK_POINTER (proc), SCM_BOOL_F, table);
-
-  return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
@@ -1273,12 +1193,6 @@ scm_weak_table_prehistory (void)
     GC_new_kind (GC_new_free_list (),
                 GC_MAKE_PROC (GC_new_proc (mark_weak_value_table), 0),
                 0, 0);
-
-#if SCM_USE_PTHREAD_THREADS
-  all_weak_tables = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
-  pthread_atfork (lock_all_weak_tables, unlock_all_weak_tables,
-                  unlock_all_weak_tables);
-#endif
 }
 
 void