1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-14 15:40:19 +02:00

Reimplement dynamic states

There are two goals: one, to use less memory per dynamic state in order
to allow millions of dynamic states to be allocated in light-weight
threading scenarios.  The second goal is to prevent dynamic states from
being actively mutated in two threads at once.  This second goal does
mean that dynamic states object that escape into scheme are now copies
that won't receive further updates; an incompatible change, but one
which we hope doesn't affect anyone.

* libguile/cache-internal.h: New file.
* libguile/fluids.c (is_dynamic_state, get_dynamic_state)
  (save_dynamic_state, restore_dynamic_state, add_entry)
  (copy_value_table): New functions.
  (scm_i_fluid_print, scm_i_dynamic_state_print): Move up.
  (new_fluid): No need for a number.
  (scm_fluid_p: scm_is_fluid): Inline IS_FLUID uses.
  (fluid_set_x, fluid_ref): Adapt to dynamic state changes.
  (scm_fluid_set_x, scm_fluid_unset_x): Call fluid_set_x.
  (scm_swap_fluid): Rewrite in terms of fluid_ref and fluid_set.
  (swap_fluid): Use internal fluid_set_x.
  (scm_i_make_initial_dynamic_state): Adapt to dynamic state
  representation change.
  (scm_dynamic_state_p, scm_is_dynamic_state): Use new accessors.
  (scm_current_dynamic_state): Use make_dynamic_state.
  (scm_dynwind_current_dynamic_state): Use new accessor.
* libguile/fluids.h: Remove internal definitions.  Add new struct
  definition.
* libguile/threads.h (scm_i_thread): Use scm_t_dynamic_state for dynamic
  state.
* libguile/threads.c (guilify_self_1, guilify_self_2):
  (scm_i_init_thread_for_guile, scm_init_guile):
  (scm_call_with_new_thread):
  (scm_init_threads, scm_init_threads_default_dynamic_state): Adapt to
  scm_i_thread change.
  (scm_i_with_guile, with_guile): Remove "and parent" suffix.
  (scm_i_reset_fluid): Remove unneeded function.
* doc/ref/api-scheduling.texi (Fluids and Dynamic States): Remove
  scm_make_dynamic_state docs.  Update current-dynamic-state docs.
* libguile/vm-engine.c (vm_engine): Update fluid-ref and fluid-set!
  inlined fast paths for dynamic state changes.
* libguile/vm.c (vm_error_unbound_fluid): Remove now-unused function.
* NEWS: Update.
* module/ice-9/deprecated.scm (make-dynamic-state): New definition.
* libguile/deprecated.h:
* libguile/deprecated.c (scm_make_dynamic_state): Move here.
* libguile/__scm.h (scm_t_dynamic_state): New typedef.
* libguile/dynstack.h:
* libguile/dynstack.c (scm_dynstack_push_fluid):
  (scm_dynstack_unwind_fluid): Take raw dynstate in these internal
  functions.
* libguile/throw.c (catch): Adapt to dynstack changes.
This commit is contained in:
Andy Wingo 2016-11-27 21:33:30 +01:00
parent a9dc553893
commit aa84489d18
17 changed files with 463 additions and 310 deletions

View file

@ -403,7 +403,7 @@
#define VM_VALIDATE_BYTEVECTOR(x, proc) \
VM_VALIDATE (x, SCM_BYTEVECTOR_P, proc, bytevector)
#define VM_VALIDATE_CHAR(x, proc) \
VM_VALIDATE (x, SCM_CHARP, proc, char);
VM_VALIDATE (x, SCM_CHARP, proc, char)
#define VM_VALIDATE_PAIR(x, proc) \
VM_VALIDATE (x, scm_is_pair, proc, pair)
#define VM_VALIDATE_STRING(obj, proc) \
@ -2166,30 +2166,26 @@ VM_NAME (scm_i_thread *thread, struct scm_vm *vp,
VM_DEFINE_OP (74, fluid_ref, "fluid-ref", OP1 (X8_S12_S12) | OP_DST)
{
scm_t_uint16 dst, src;
size_t num;
SCM fluid, fluids;
SCM fluid;
struct scm_cache_entry *entry;
UNPACK_12_12 (op, dst, src);
fluid = SP_REF (src);
fluids = SCM_I_DYNAMIC_STATE_FLUIDS (thread->dynamic_state);
if (SCM_UNLIKELY (!SCM_FLUID_P (fluid))
|| ((num = SCM_I_FLUID_NUM (fluid)) >= SCM_SIMPLE_VECTOR_LENGTH (fluids)))
/* If we find FLUID in the cache, then it is indeed a fluid. */
entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)
&& !SCM_UNBNDP (SCM_PACK (entry->value))))
{
/* Punt dynstate expansion and error handling to the C proc. */
SYNC_IP ();
SP_SET (dst, scm_fluid_ref (fluid));
SP_SET (dst, SCM_PACK (entry->value));
NEXT (1);
}
else
{
SCM val = SCM_SIMPLE_VECTOR_REF (fluids, num);
if (scm_is_eq (val, SCM_UNDEFINED))
val = SCM_I_FLUID_DEFAULT (fluid);
VM_ASSERT (!scm_is_eq (val, SCM_UNDEFINED),
vm_error_unbound_fluid (fluid));
SP_SET (dst, val);
SYNC_IP ();
SP_SET (dst, scm_fluid_ref (fluid));
NEXT (1);
}
NEXT (1);
}
/* fluid-set fluid:12 val:12
@ -2199,23 +2195,26 @@ VM_NAME (scm_i_thread *thread, struct scm_vm *vp,
VM_DEFINE_OP (75, fluid_set, "fluid-set!", OP1 (X8_S12_S12))
{
scm_t_uint16 a, b;
size_t num;
SCM fluid, fluids;
SCM fluid, value;
struct scm_cache_entry *entry;
UNPACK_12_12 (op, a, b);
fluid = SP_REF (a);
fluids = SCM_I_DYNAMIC_STATE_FLUIDS (thread->dynamic_state);
if (SCM_UNLIKELY (!SCM_FLUID_P (fluid))
|| ((num = SCM_I_FLUID_NUM (fluid)) >= SCM_SIMPLE_VECTOR_LENGTH (fluids)))
value = SP_REF (b);
/* If we find FLUID in the cache, then it is indeed a fluid. */
entry = scm_cache_lookup (&thread->dynamic_state->cache, fluid);
if (SCM_LIKELY (scm_is_eq (SCM_PACK (entry->key), fluid)))
{
/* Punt dynstate expansion and error handling to the C proc. */
SYNC_IP ();
scm_fluid_set_x (fluid, SP_REF (b));
entry->value = SCM_UNPACK (value);
NEXT (1);
}
else
SCM_SIMPLE_VECTOR_SET (fluids, num, SP_REF (b));
NEXT (1);
{
SYNC_IP ();
scm_fluid_set_x (fluid, value);
NEXT (1);
}
}