1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-21 20:20:24 +02:00

convert internal weak hash table users to use the weak table api

The weak table API isn't public yet.  It could be after some review.
But we can go ahead and use it now internally.

* libguile/foreign.c:
* libguile/goops.c:
* libguile/objprop.c:
* libguile/procprop.c:
* libguile/smob.c:
* libguile/srcprop.c: Update weak table users to new API.  No locking
  needed!
This commit is contained in:
Andy Wingo 2011-10-23 23:38:51 +02:00
parent 54a9b981a4
commit 203a92b67b
6 changed files with 51 additions and 91 deletions

View file

@ -87,15 +87,12 @@ static SCM cif_to_procedure (SCM cif, SCM func_ptr);
static SCM pointer_weak_refs = SCM_BOOL_F; static SCM pointer_weak_refs = SCM_BOOL_F;
static scm_i_pthread_mutex_t weak_refs_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
static void static void
register_weak_reference (SCM from, SCM to) register_weak_reference (SCM from, SCM to)
{ {
scm_i_pthread_mutex_lock (&weak_refs_lock); scm_weak_table_putq_x (pointer_weak_refs, from, to);
scm_hashq_set_x (pointer_weak_refs, from, to);
scm_i_pthread_mutex_unlock (&weak_refs_lock);
} }
static void static void
@ -1272,7 +1269,7 @@ scm_register_foreign (void)
"scm_init_foreign", "scm_init_foreign",
(scm_t_extension_init_func)scm_init_foreign, (scm_t_extension_init_func)scm_init_foreign,
NULL); NULL);
pointer_weak_refs = scm_make_weak_key_hash_table (SCM_UNDEFINED); pointer_weak_refs = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
} }
/* /*

View file

@ -163,7 +163,6 @@ static SCM class_bytevector;
static SCM class_uvec; static SCM class_uvec;
static SCM vtable_class_map = SCM_BOOL_F; static SCM vtable_class_map = SCM_BOOL_F;
static scm_i_pthread_mutex_t vtable_class_map_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
/* Port classes. Allocate 3 times the maximum number of port types so that /* Port classes. Allocate 3 times the maximum number of port types so that
input ports, output ports, and in/out ports can be stored at different input ports, output ports, and in/out ports can be stored at different
@ -191,17 +190,15 @@ scm_i_define_class_for_vtable (SCM vtable)
{ {
SCM class; SCM class;
scm_i_pthread_mutex_lock (&vtable_class_map_lock); scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
if (scm_is_false (vtable_class_map)) if (scm_is_false (vtable_class_map))
vtable_class_map = scm_make_weak_key_hash_table (SCM_UNDEFINED); vtable_class_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
if (scm_is_false (scm_struct_vtable_p (vtable))) if (scm_is_false (scm_struct_vtable_p (vtable)))
abort (); abort ();
class = scm_hashq_ref (vtable_class_map, vtable, SCM_BOOL_F); class = scm_weak_table_refq (vtable_class_map, vtable, SCM_BOOL_F);
scm_i_pthread_mutex_unlock (&vtable_class_map_lock);
if (scm_is_false (class)) if (scm_is_false (class))
{ {
@ -220,9 +217,7 @@ scm_i_define_class_for_vtable (SCM vtable)
/* Don't worry about races. This only happens when creating a /* Don't worry about races. This only happens when creating a
vtable, which happens by definition in one thread. */ vtable, which happens by definition in one thread. */
scm_i_pthread_mutex_lock (&vtable_class_map_lock); scm_weak_table_putq_x (vtable_class_map, vtable, class);
scm_hashq_set_x (vtable_class_map, vtable, class);
scm_i_pthread_mutex_unlock (&vtable_class_map_lock);
} }
return class; return class;

View file

@ -36,20 +36,13 @@
*/ */
static SCM object_whash; static SCM object_whash;
static scm_i_pthread_mutex_t whash_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
SCM_DEFINE (scm_object_properties, "object-properties", 1, 0, 0, SCM_DEFINE (scm_object_properties, "object-properties", 1, 0, 0,
(SCM obj), (SCM obj),
"Return @var{obj}'s property list.") "Return @var{obj}'s property list.")
#define FUNC_NAME s_scm_object_properties #define FUNC_NAME s_scm_object_properties
{ {
SCM ret; return scm_weak_table_refq (object_whash, obj, SCM_EOL);
scm_i_pthread_mutex_lock (&whash_mutex);
ret = scm_hashq_ref (object_whash, obj, SCM_EOL);
scm_i_pthread_mutex_unlock (&whash_mutex);
return ret;
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -59,9 +52,7 @@ SCM_DEFINE (scm_set_object_properties_x, "set-object-properties!", 2, 0, 0,
"Set @var{obj}'s property list to @var{alist}.") "Set @var{obj}'s property list to @var{alist}.")
#define FUNC_NAME s_scm_set_object_properties_x #define FUNC_NAME s_scm_set_object_properties_x
{ {
scm_i_pthread_mutex_lock (&whash_mutex); scm_weak_table_putq_x (object_whash, obj, alist);
scm_hashq_set_x (object_whash, obj, alist);
scm_i_pthread_mutex_unlock (&whash_mutex);
return alist; return alist;
} }
@ -87,14 +78,14 @@ SCM_DEFINE (scm_set_object_property_x, "set-object-property!", 3, 0, 0,
SCM alist; SCM alist;
SCM assoc; SCM assoc;
scm_i_pthread_mutex_lock (&whash_mutex); scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
alist = scm_hashq_ref (object_whash, obj, SCM_EOL); alist = scm_weak_table_refq (object_whash, obj, SCM_EOL);
assoc = scm_assq (key, alist); assoc = scm_assq (key, alist);
if (SCM_NIMP (assoc)) if (SCM_NIMP (assoc))
SCM_SETCDR (assoc, value); SCM_SETCDR (assoc, value);
else else
scm_hashq_set_x (object_whash, obj, scm_acons (key, value, alist)); scm_weak_table_putq_x (object_whash, obj, scm_acons (key, value, alist));
scm_i_pthread_mutex_unlock (&whash_mutex); scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
return value; return value;
} }
@ -104,7 +95,7 @@ SCM_DEFINE (scm_set_object_property_x, "set-object-property!", 3, 0, 0,
void void
scm_init_objprop () scm_init_objprop ()
{ {
object_whash = scm_make_weak_key_hash_table (SCM_UNDEFINED); object_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
#include "libguile/objprop.x" #include "libguile/objprop.x"
} }

View file

@ -31,7 +31,7 @@
#include "libguile/smob.h" #include "libguile/smob.h"
#include "libguile/root.h" #include "libguile/root.h"
#include "libguile/vectors.h" #include "libguile/vectors.h"
#include "libguile/hashtab.h" #include "libguile/weak-table.h"
#include "libguile/programs.h" #include "libguile/programs.h"
#include "libguile/validate.h" #include "libguile/validate.h"
@ -42,7 +42,6 @@ SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
SCM_GLOBAL_SYMBOL (scm_sym_name, "name"); SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
static SCM overrides; static SCM overrides;
static scm_i_pthread_mutex_t overrides_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
int int
scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest) scm_i_procedure_arity (SCM proc, int *req, int *opt, int *rest)
@ -104,9 +103,7 @@ SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
SCM_VALIDATE_PROC (1, proc); SCM_VALIDATE_PROC (1, proc);
scm_i_pthread_mutex_lock (&overrides_lock); ret = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
ret = scm_hashq_ref (overrides, proc, SCM_BOOL_F);
scm_i_pthread_mutex_unlock (&overrides_lock);
if (scm_is_false (ret)) if (scm_is_false (ret))
{ {
@ -127,9 +124,7 @@ SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0
{ {
SCM_VALIDATE_PROC (1, proc); SCM_VALIDATE_PROC (1, proc);
scm_i_pthread_mutex_lock (&overrides_lock); scm_weak_table_putq_x (overrides, proc, alist);
scm_hashq_set_x (overrides, proc, alist);
scm_i_pthread_mutex_unlock (&overrides_lock);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
@ -156,8 +151,8 @@ SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
SCM_VALIDATE_PROC (1, proc); SCM_VALIDATE_PROC (1, proc);
scm_i_pthread_mutex_lock (&overrides_lock); scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
props = scm_hashq_ref (overrides, proc, SCM_BOOL_F); props = scm_weak_table_refq (overrides, proc, SCM_BOOL_F);
if (scm_is_false (props)) if (scm_is_false (props))
{ {
if (SCM_PROGRAM_P (proc)) if (SCM_PROGRAM_P (proc))
@ -165,8 +160,8 @@ SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
else else
props = SCM_EOL; props = SCM_EOL;
} }
scm_hashq_set_x (overrides, proc, scm_assq_set_x (props, key, val)); scm_weak_table_putq_x (overrides, proc, scm_assq_set_x (props, key, val));
scm_i_pthread_mutex_unlock (&overrides_lock); scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
@ -178,7 +173,7 @@ SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
void void
scm_init_procprop () scm_init_procprop ()
{ {
overrides = scm_make_weak_key_hash_table (SCM_UNDEFINED); overrides = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
#include "libguile/procprop.x" #include "libguile/procprop.x"
} }

View file

@ -418,16 +418,13 @@ scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
} }
static SCM tramp_weak_map = SCM_BOOL_F; static SCM tramp_weak_map = SCM_BOOL_F;
static scm_i_pthread_mutex_t tramp_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
SCM SCM
scm_i_smob_apply_trampoline (SCM smob) scm_i_smob_apply_trampoline (SCM smob)
{ {
SCM tramp; SCM tramp;
scm_i_pthread_mutex_lock (&tramp_lock); tramp = scm_weak_table_refq (tramp_weak_map, smob, SCM_BOOL_F);
tramp = scm_hashq_ref (tramp_weak_map, smob, SCM_BOOL_F);
scm_i_pthread_mutex_unlock (&tramp_lock);
if (scm_is_true (tramp)) if (scm_is_true (tramp))
return tramp; return tramp;
@ -447,9 +444,7 @@ scm_i_smob_apply_trampoline (SCM smob)
/* Race conditions (between the ref and this set!) cannot cause /* Race conditions (between the ref and this set!) cannot cause
any harm here. */ any harm here. */
scm_i_pthread_mutex_lock (&tramp_lock); scm_weak_table_putq_x (tramp_weak_map, smob, tramp);
scm_hashq_set_x (tramp_weak_map, smob, tramp);
scm_i_pthread_mutex_unlock (&tramp_lock);
return tramp; return tramp;
} }
} }
@ -677,7 +672,7 @@ scm_smob_prehistory ()
scm_smobs[i].apply_trampoline_objcode = SCM_BOOL_F; scm_smobs[i].apply_trampoline_objcode = SCM_BOOL_F;
} }
tramp_weak_map = scm_make_weak_key_hash_table (SCM_UNDEFINED); tramp_weak_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
} }
/* /*

View file

@ -61,7 +61,6 @@ SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
SCM_GLOBAL_SYMBOL (scm_sym_column, "column"); SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
static SCM scm_source_whash; static SCM scm_source_whash;
static scm_i_pthread_mutex_t source_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
/* /*
@ -167,9 +166,7 @@ SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
SCM p; SCM p;
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
scm_i_pthread_mutex_unlock (&source_lock);
if (SRCPROPSP (p)) if (SRCPROPSP (p))
return scm_srcprops_to_alist (p); return scm_srcprops_to_alist (p);
@ -189,9 +186,7 @@ SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
{ {
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); scm_weak_table_putq_x (scm_source_whash, obj, alist);
scm_hashq_set_x (scm_source_whash, obj, alist);
scm_i_pthread_mutex_unlock (&source_lock);
return alist; return alist;
} }
@ -205,9 +200,7 @@ scm_i_has_source_properties (SCM obj)
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); ret = scm_is_true (scm_weak_table_refq (scm_source_whash, obj, SCM_BOOL_F));
ret = scm_is_true (scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F));
scm_i_pthread_mutex_unlock (&source_lock);
return ret; return ret;
} }
@ -220,14 +213,12 @@ scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
{ {
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); scm_weak_table_putq_x (scm_source_whash, obj,
scm_hashq_set_x (scm_source_whash, obj,
scm_make_srcprops (line, col, fname, scm_make_srcprops (line, col, fname,
SCM_COPY_SOURCE_P SCM_COPY_SOURCE_P
? scm_copy_tree (obj) ? scm_copy_tree (obj)
: SCM_UNDEFINED, : SCM_UNDEFINED,
SCM_EOL)); SCM_EOL));
scm_i_pthread_mutex_unlock (&source_lock);
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -240,9 +231,7 @@ SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
SCM p; SCM p;
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
scm_i_pthread_mutex_unlock (&source_lock);
if (!SRCPROPSP (p)) if (!SRCPROPSP (p))
goto alist; goto alist;
@ -272,15 +261,15 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
SCM p; SCM p;
SCM_VALIDATE_NIM (1, obj); SCM_VALIDATE_NIM (1, obj);
scm_i_pthread_mutex_lock (&source_lock); scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); p = scm_weak_table_refq (scm_source_whash, obj, SCM_EOL);
if (scm_is_eq (scm_sym_line, key)) if (scm_is_eq (scm_sym_line, key))
{ {
if (SRCPROPSP (p)) if (SRCPROPSP (p))
SETSRCPROPLINE (p, scm_to_int (datum)); SETSRCPROPLINE (p, scm_to_int (datum));
else else
scm_hashq_set_x (scm_source_whash, obj, scm_weak_table_putq_x (scm_source_whash, obj,
scm_make_srcprops (scm_to_int (datum), 0, scm_make_srcprops (scm_to_int (datum), 0,
SCM_UNDEFINED, SCM_UNDEFINED, p)); SCM_UNDEFINED, SCM_UNDEFINED, p));
} }
@ -289,7 +278,7 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
if (SRCPROPSP (p)) if (SRCPROPSP (p))
SETSRCPROPCOL (p, scm_to_int (datum)); SETSRCPROPCOL (p, scm_to_int (datum));
else else
scm_hashq_set_x (scm_source_whash, obj, scm_weak_table_putq_x (scm_source_whash, obj,
scm_make_srcprops (0, scm_to_int (datum), scm_make_srcprops (0, scm_to_int (datum),
SCM_UNDEFINED, SCM_UNDEFINED, p)); SCM_UNDEFINED, SCM_UNDEFINED, p));
} }
@ -298,7 +287,7 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
if (SRCPROPSP (p)) if (SRCPROPSP (p))
SETSRCPROPCOPY (p, datum); SETSRCPROPCOPY (p, datum);
else else
scm_hashq_set_x (scm_source_whash, obj, scm_weak_table_putq_x (scm_source_whash, obj,
scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
} }
else else
@ -306,10 +295,10 @@ SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
if (SRCPROPSP (p)) if (SRCPROPSP (p))
SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p))); SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
else else
scm_hashq_set_x (scm_source_whash, obj, scm_weak_table_putq_x (scm_source_whash, obj,
scm_acons (key, datum, p)); scm_acons (key, datum, p));
} }
scm_i_pthread_mutex_unlock (&source_lock); scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
@ -325,12 +314,10 @@ SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
{ {
SCM p, z; SCM p, z;
z = scm_cons (x, y); z = scm_cons (x, y);
scm_i_pthread_mutex_lock (&source_lock);
/* Copy source properties possibly associated with xorig. */ /* Copy source properties possibly associated with xorig. */
p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F); p = scm_weak_table_refq (scm_source_whash, xorig, SCM_BOOL_F);
if (scm_is_true (p)) if (scm_is_true (p))
scm_hashq_set_x (scm_source_whash, z, p); scm_weak_table_putq_x (scm_source_whash, z, p);
scm_i_pthread_mutex_unlock (&source_lock);
return z; return z;
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -342,7 +329,7 @@ scm_init_srcprop ()
scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
scm_set_smob_print (scm_tc16_srcprops, srcprops_print); scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047)); scm_source_whash = scm_c_make_weak_table (2047, SCM_WEAK_TABLE_KIND_KEY);
scm_c_define ("source-whash", scm_source_whash); scm_c_define ("source-whash", scm_source_whash);
scm_last_alist_filename = scm_cons (SCM_EOL, scm_last_alist_filename = scm_cons (SCM_EOL,