From c0937f0988d202d1062fea6e5b61d68d387d1542 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Tue, 24 May 2011 21:09:24 +0200 Subject: [PATCH] safely access the trampoline weak map * libguile/smob.c (scm_i_smob_apply_trampoline): Protect the trampoline weak map with a mutex. --- libguile/smob.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libguile/smob.c b/libguile/smob.c index adb34ba0b..c414913fc 100644 --- a/libguile/smob.c +++ b/libguile/smob.c @@ -418,12 +418,16 @@ scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (), } static SCM tramp_weak_map = SCM_BOOL_F; +static scm_i_pthread_mutex_t tramp_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; + SCM scm_i_smob_apply_trampoline (SCM smob) { - /* could use hashq-create-handle!, but i don't know what to do if it returns a - weak pair */ - SCM tramp = scm_hashq_ref (tramp_weak_map, smob, SCM_BOOL_F); + SCM tramp; + + scm_i_pthread_mutex_lock (&tramp_lock); + tramp = scm_hashq_ref (tramp_weak_map, smob, SCM_BOOL_F); + scm_i_pthread_mutex_unlock (&tramp_lock); if (scm_is_true (tramp)) return tramp; @@ -440,7 +444,12 @@ scm_i_smob_apply_trampoline (SCM smob) SCM_SIMPLE_VECTOR_SET (objtable, 1, scm_from_locale_symbol (name)); tramp = scm_make_program (SCM_SMOB_DESCRIPTOR (smob).apply_trampoline_objcode, objtable, SCM_BOOL_F); + + /* Race conditions (between the ref and this set!) cannot cause + any harm here. */ + scm_i_pthread_mutex_lock (&tramp_lock); scm_hashq_set_x (tramp_weak_map, smob, tramp); + scm_i_pthread_mutex_unlock (&tramp_lock); return tramp; } }