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

Move to store thread join cond/lock/results directly

* libguile/threads.h: Add join data directly on the thread instead of
using a Scheme-side weak table.  It's less complicated and it will let
the weak table implementation use locks in Scheme; otherwise you would
have threads depending on weak tables and vice versa.
* libguile/threads.c (scm_trace_thread, guilify_self_1): Init and mark
the new members.
(thread_join_cond, thread_join_lock, thread_join_results)
(thread_init_joinable_x, thread_set_join_results_x): New accessors.
* module/ice-9/threads.scm (call-with-new-thread, join-thread): Use the
new accessors.
This commit is contained in:
Andy Wingo 2025-05-07 09:56:10 +02:00
parent 852c0b05c7
commit 1f96d1bf4b
3 changed files with 82 additions and 39 deletions

View file

@ -108,6 +108,10 @@ scm_trace_thread (struct scm_thread *thread,
scm_trace_dynstack (&thread->dynstack, trace_edge, heap, trace_data);
trace_edge (gc_edge (&thread->continuation_root), heap, trace_data);
trace_edge (gc_edge (&thread->join_cond), heap, trace_data);
trace_edge (gc_edge (&thread->join_lock), heap, trace_data);
trace_edge (gc_edge (&thread->join_results), heap, trace_data);
}
/* Guile-level thread objects are themselves GC-allocated. A thread
@ -413,6 +417,8 @@ guilify_self_1 (struct gc_mutator *mut, struct gc_stack_addr base,
t->base = (SCM_STACKITEM *) gc_stack_addr_as_pointer (base);
t->continuation_root = SCM_EOL;
t->continuation_base = t->base;
t->join_cond = t->join_lock = t->join_results = SCM_BOOL_F;
scm_i_pthread_cond_init (&t->sleep_cond, NULL);
scm_i_vm_prepare_stack (&t->vm);
@ -840,6 +846,56 @@ scm_cancel_thread (SCM thread)
static SCM join_thread_var;
SCM_DEFINE_STATIC (thread_join_cond, "%thread-join-cond", 1, 0, 0,
(SCM thread), "")
#define FUNC_NAME s_thread_join_cond
{
SCM_VALIDATE_THREAD (1, thread);
return SCM_I_THREAD_DATA (thread)->join_cond;
}
#undef FUNC_NAME
SCM_DEFINE_STATIC (thread_join_lock, "%thread-join-lock", 1, 0, 0,
(SCM thread), "")
#define FUNC_NAME s_thread_join_lock
{
SCM_VALIDATE_THREAD (1, thread);
return SCM_I_THREAD_DATA (thread)->join_lock;
}
#undef FUNC_NAME
SCM_DEFINE_STATIC (thread_join_results, "%thread-join-results", 1, 0, 0,
(SCM thread), "")
#define FUNC_NAME s_thread_join_results
{
SCM_VALIDATE_THREAD (1, thread);
return SCM_I_THREAD_DATA (thread)->join_results;
}
#undef FUNC_NAME
SCM_DEFINE_STATIC (thread_init_joinable_x, "%thread-init-joinable!", 3, 0, 0,
(SCM thread, SCM cond, SCM lock), "")
#define FUNC_NAME s_thread_init_joinable_x
{
SCM_VALIDATE_THREAD (1, thread);
SCM_VALIDATE_CONDVAR (2, cond);
SCM_VALIDATE_MUTEX (3, lock);
SCM_I_THREAD_DATA (thread)->join_cond = cond;
SCM_I_THREAD_DATA (thread)->join_lock = lock;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE_STATIC (thread_set_join_results_x, "%thread-set-join-results!",
2, 0, 0, (SCM thread, SCM results), "")
#define FUNC_NAME s_thread_set_join_results_x
{
SCM_VALIDATE_THREAD (1, thread);
SCM_I_THREAD_DATA (thread)->join_results = results;
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM
scm_join_thread (SCM thread)
{