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

Use Whippet API to boot threads

* libguile/scm.h (scm_tc7_thread): Give threads their own tc7.
* libguile/threads.h (struct scm_thread): Add a tag, so that struct
thread can be a SCM directly.  Add a struct gc_mutator* member.
(scm_thread_handle): New inline function.
(SCM_I_IS_THREAD, SCM_I_THREAD_DATA, SCM_VALIDATE_THREAD): Update to use
tc7 instead of SMOB tags.

* libguile/continuations.c (scm_i_with_continuation_barrier)
* libguile/finalizers.c (queue_finalizer_async)
* libguile/jit.c (compile_current_thread)
* libguile/threads.c (block_self, guilify_self_2)
(lock_mutex, unlock_mutex, timed_wait scm_current_thread)
(scm_all_threads)
* libguile/vm-engine.c (current-thread): Use scm_thread_handle instead
of thread->handle.

* libguile/evalext.c (scm_self_evaluating_p):
* libguile/goops.c (class_thread, scm_class_of, scm_sys_goops_early_init)
* libguile/print.c (iprin1)
* module/language/cps/compile-bytecode.scm (compile-function)
* module/oop/goops.scm (<thread>)
* module/system/base/types.scm (cell->object)
* module/system/base/types/internal.scm (heap-tags)
* module/system/vm/assembler.scm: (emit-thread?): Adapt to
scm_tc7_thread.

* libguile/gc-internal.h: Move init functions that take "struct
gc_stack_addr" here, so that internal Whippet uses don't cause Whippet
to be added to public headers.
* libguile/gc.c (scm_storage_prehistory): Take struct gc_stack_addr as
arg, and pass to gc_init.  Return a mutator pointer.
* libguile/init.c (scm_i_init_guile): Pass mutator and stack base to GC
and thread init routines.
* libguile/threads.c (scm_trace_dynstack, scm_trace_thread)
(scm_trace_thread_mutator_roots): New infra for marking threads in terms
of Whippet API.
* libguile/threads.c (guilify_self_1): Since we don't use a separate GC
kind for threads any more, and thread marking is keyed off
gc_mutator_set_roots, we can avoid some of the gnarly synchronization.
(on_thread_exit): Arrange to gc_finish_for_thread.
(scm_i_init_thread_for_guile): Use gc_init_for_thread.
(init_main_thread, with_guile, scm_i_with_guile): Use Whippet API.
(scm_threads_prehistory): Take main-thread mutator and the stack base as
arguments.
* libguile/vm.c (scm_trace_vm): Rework in terms of Whippet API.
* libguile/whippet-embedder.h (gc_trace_mutator_roots): Arrange to trace
the current mutator's SCM thread object.
* libguile/trace.h: New file, to declare implementations of trace
routines.
* libguile/Makefile.am (noinst_HEADERS): Add trace.h.
This commit is contained in:
Andy Wingo 2025-04-22 10:21:20 +02:00
parent 55e9d0672b
commit 27f0490801
25 changed files with 299 additions and 217 deletions

View file

@ -1,4 +1,4 @@
/* Copyright 2001,2009-2015,2017-2020,2022-2023
/* Copyright 2001,2009-2015,2017-2020,2022-2023,2025
Free Software Foundation, Inc.
This file is part of Guile.
@ -67,6 +67,7 @@
#include "smob.h"
#include "stackchk.h"
#include "symbols.h"
#include "trace.h"
#include "values.h"
#include "vectors.h"
#include "version.h"
@ -708,10 +709,12 @@ enum slot_desc
SLOT_DESC_UNUSED = 3
};
/* Mark the active VM stack region. */
struct GC_ms_entry *
scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
struct GC_ms_entry *mark_stack_limit)
void
scm_trace_vm (struct scm_vm *vp,
void (*trace_edge) (struct gc_edge edge,
struct gc_heap *heap,
void *trace_data),
struct gc_heap *heap, void *trace_data)
{
union scm_vm_stack_element *sp, *fp;
/* The first frame will be marked conservatively (without a slot map).
@ -720,8 +723,6 @@ scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
providing slot maps for all points in a program would take a
prohibitive amount of space. */
const uint8_t *slot_map = NULL;
void *upper = (void *) GC_greatest_plausible_heap_addr;
void *lower = (void *) GC_least_plausible_heap_addr;
struct slot_map_cache cache;
memset (&cache, 0, sizeof (cache));
@ -745,12 +746,8 @@ scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
break;
case SLOT_DESC_UNUSED:
case SLOT_DESC_LIVE_GC:
if (SCM_NIMP (sp->as_scm) &&
sp->as_ptr >= lower && sp->as_ptr <= upper)
mark_stack_ptr = GC_mark_and_push (sp->as_ptr,
mark_stack_ptr,
mark_stack_limit,
NULL);
if (SCM_NIMP (sp->as_scm))
trace_edge (gc_edge (sp), heap, trace_data);
break;
case SLOT_DESC_DEAD:
/* This value may become dead as a result of GC,
@ -768,8 +765,6 @@ scm_i_vm_mark_stack (struct scm_vm *vp, struct GC_ms_entry *mark_stack_ptr,
}
return_unused_stack_to_os (vp);
return mark_stack_ptr;
}
/* Free the VM stack, as this thread is exiting. */
@ -1401,7 +1396,7 @@ scm_i_vm_emergency_abort (SCM *tag_and_argv, size_t n)
fp = vp->stack_top - fp_offset;
sp = vp->stack_top - sp_offset;
/* Restore FP first so that a concurrent 'scm_i_vm_mark_stack' does
/* Restore FP first so that a concurrent 'scm_trace_vm' does
not overwrite the 'abort' arguments assigned below (see
<https://bugs.gnu.org/28211>). */
vp->fp = fp;
@ -1476,7 +1471,7 @@ abort_to_prompt (scm_thread *thread, uint8_t *saved_mra)
/* Continuation gets nargs+1 values: the one more is for the cont. */
sp = sp - nargs - 1;
/* Restore FP first so that a concurrent 'scm_i_vm_mark_stack' does
/* Restore FP first so that a concurrent 'scm_trace_vm' does
not overwrite the 'abort' arguments assigned below (see
<https://bugs.gnu.org/28211>). */
vp->fp = fp;