* libguile/threads.h (scm_i_thread): Add bool tracking whether the
thread needs to be unregistered from libgc.
* libguile/threads.c (guilify_self_1): Add needs_unregister arg.
(on_thread_exit): Only unregister thread if the thread needs it.
(scm_i_init_thread_for_guile): A thread needs unregistering if
GC_register_my_thread succeeded.
(scm_threads_prehistory): Don't unregister initial thread.
Fixes#19523. Thanks to Anthonin Bonnefoy for the report.
* libguile/threads.c (struct launch_data): Add prev/next pointers.
(protected_launch_data, protected_launch_data_lock): New static vars.
(protect_launch_data, unprotect_launch_data): New functions.
(really_launch, scm_sys_call_with_new_thread): Preserve launch data
from GC. Thanks to Linas Vepstas for the report!
* libguile/threads.c (really_launch): Start threads with asyncs
blocked.
* module/ice-9/threads.scm (call-with-new-thread): Unblock asyncs once
we have the bookkeeping sorted out. Don't use
with-continuation-barrier; it's not needed. Print nice thread
backtraces.
* libguile/async.h:
* libguile/async.c (struct scm_thread_wake_data): Include the cond to
signal. Be a union and include a tag.
(scm_i_prepare_to_wait): Rename from scm_i_setup_sleep and take wake
data directly. Also call scm_i_wait_finished as appropriate.
(scm_i_wait_finished): Rename from scm_i_reset_sleep.
(scm_i_prepare_to_wait_on_fd, scm_c_prepare_to_wait_on_fd):
(scm_i_prepare_to_wait_on_cond, scm_c_prepare_to_wait_on_cond): New
functions.
(scm_c_wait_finished): New function.
(scm_system_async_mark_for_thread): Adapt to wake data change.
* libguile/threads.c (block_self, scm_std_select): Adapt to async
interface changes.
* doc/ref/api-scheduling.texi (Asyncs): Doc new public interfaces.
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.
* libguile/threads.c (timed_wait): When looping to reacquire mutex,
check if mutex owner after dropping mutex to run asyncs when the
reacquire is interrupted.
Also for asyncs that interrupted the initial wait, just return #t
directly, and allow the caller to loop. Fixes a deadlock in which a
thread could have pending asyncs after dropping a mutex and which
prevent it from going to wait on a cond, but then the broadcast comes
while nobody is waiting and the mutex is dropped, then after
reacquiring the mutex when we go to wait again, we wait forever.
The correct thing to do is after reacquiring the mutex, to allow the
application to check if waiting is appropriate.
* libguile/root.h:
* libguile/root.c: Remove these files.
* libguile/deprecated.h:
* libguile/deprecated.c (scm_internal_cwdr, scm_call_with_dynamic_root)
(scm_dynamic_root, scm_apply_with_dynamic_root): Deprecate.
Remove all root.h usage, which was vestigial.
* module/ice-9/serialize.scm: Use (current-thread) instead
of (dynamic-root).
* module/ice-9/threads.scm (join-thread): Implement in Scheme.
(call-with-new-thread): Arrange to record values in a weak table and
signal the join cond.
(with-mutex): Move up definition; call-with-new-thread needs it. (How
was this working before?)
* libguile/threads.c (guilify_self_1, guilify_self_2, do_thread_exit):
Remove join queue management.
* libguile/threads.c (scm_join_thread, scm_join_thread_timed): Call out
to Scheme.
(scm_init_ice_9_threads): Capture join-thread var.
* libguile/throw.h (scm_i_make_catch_body_closure)
(scm_i_make_catch_handler_closure): Add scm_i_ prefix and make
available for internal use.
* libguile/throw.c: Adapt.
* libguile/threads.c (scm_spawn_thread): Rewrite in terms of
scm_call_with_new_thread.
* libguile/threads.c (lock_mutex, scm_timed_lock_mutex): As with
unlock_mutex before, optimize by specializing to the mutex kind.
Also, avoid lock thrashing after a return from block_self.
* libguile/threads.c (scm_timed_lock_mutex): Use "level" field only for
recursive mutexes.
(unlock_mutex, scm_unlock_mutex): Factor implementation back out of
scm_unlock_mutex (doh?), but in such a way that can specialize against
mutex type.
(scm_mutex_level): Only look at level for recursive mutexes.
(scm_mutex_locked_p): Look at owner field, not level field.
(timed_wait, scm_timed_wait_condition_variable): Factor implementation
out, as above with unlock-mutex. Specialize relocking of the Guile
mutex.
* libguile/threads.c (scm_unlock_mutex): Bind to unlock-mutex.
* libguile/threads.h: Remove scm_unlock_mutex_timed.
* libguile/deprecated.h: Add scm_unlock_mutex_timed.
* libguile/deprecated.c (scm_unlock_mutex_timed): Deprecate.
* test-suite/tests/threads.test: Update unlock-mutex tests to use
wait-condition-variable if they would wait on a cond.
* libguile/threads.c (enum fat_mutex_kind): New data type, replacing
separate flags.
(struct fat_mutex): Adapt.
(make_fat_mutex): Fat mutexes can only be one of three kinds, not one
of 4 kinds. (Recursive unowned mutexes are not a thing.)
(scm_make_mutex): Adapt.
(scm_make_mutex_with_kind): New function, replacing
scm_make_mutex_with_flags. Still bound to make-mutex.
(scm_make_recursive_mutex): Adapt.
(fat_mutex_lock, fat_mutex_unlock): Adapt.
* libguile/threads.h (scm_make_mutex_with_kind): New decl.
* libguile/deprecated.h:
* libguile/deprecated.c (scm_make_mutex_with_flags): Deprecate.
* libguile/threads.c (fat_mutex_lock): allow-external-unlock mutexes
can't be recursive, but a recursive lock attempt can be unblocked by
an external thread, so these mutexes shouldn't throw an error on
recursive lock attempts.
* test-suite/tests/srfi-18.test: Add tests.
* libguile/threads.h (scm_i_thread):
* libguile/threads.c (guilify_self_1, on_thread_exit)
(scm_pthread_cond_wait, scm_pthread_cond_timedwait): The thread-local
held_mutex field is no longer needed, now that we cancel threads via
interrupts instead of pthread_cancel.
* module/ice-9/threads.scm (cancel-tag): New variable.
(cancel-thread): New Scheme function.
(call-with-new-thread): Install a prompt around the thread.
* libguile/threads.h (scm_i_thread): Remove cancelled member.
* libguile/threads.c (scm_cancel_thread): Call out to Scheme. Always
available, and works on the current thread too.
(scm_set_thread_cleanup_x, scm_thread_cleanup): Adapt.
(scm_init_ice_9_threads): Capture cancel-thread var.
* doc/ref/api-scheduling.texi (Threads): Update.
* NEWS: Update.
* libguile/__scm.h (SCM_TICK): Always define as scm_async_tick().
* libguile/error.c (scm_syserror, scm_syserror_msg):
* libguile/fports.c (fport_read, fport_write):
* libguile/_scm.h (SCM_SYSCALL): Replace SCM_ASYNC_TICK with
scm_async_tick ().
(SCM_ASYNC_TICK, SCM_ASYNC_TICK_WITH_CODE)
(SCM_ASYNC_TICK_WITH_GUARD_CODE): Remove internal definitions. We
inline into vm-engine.c, the only place where it matters.
* libguile/async.h:
* libguile/async.c (scm_async_tick, scm_i_setup_sleep):
(scm_i_reset_sleep, scm_system_async_mark_for_thread):
* libguile/threads.h (struct scm_thread_wake_data):
* libguile/threads.h (scm_i_thread):
* libguile/threads.c (block_self, guilify_self_1, scm_std_select):
Rewrite to use sequentially-consistent atomic references.
* libguile/atomics-internal.h (scm_atomic_set_pointer):
(scm_atomic_ref_pointer): New definitions.
* libguile/finalizers.c (queue_finalizer_async): We can allocate, so
just use scm_system_async_mark_for_thread instead of the set-cdr!
shenanigans.
* libguile/scmsigs.c (take_signal):
* libguile/gc.c (queue_after_gc_hook): Adapt to new asyncs mechanism.
Can't allocate but we're just manipulating the current thread when no
other threads are running so we should be good.
* libguile/vm-engine.c (VM_HANDLE_INTERRUPTS): Inline the async_tick
business.
Reported by Sylvain Beucler <beuc@beuc.net>.
* configure.ac: Check for 'pthread_cancel'.
* libguile/threads.c (scm_cancel_thread): Conditionalize on
!SCM_USE_PTHREAD_THREADS || defined HAVE_PTHREAD_CANCEL.
* test-suite/tests/threads.test (require-cancel-thread): New procedure.
("timed joining fails if timeout exceeded", "join-thread returns
timeoutval on timeout", "cancel succeeds", "handler result passed to
join", "can cancel self"): Use it.
* libguile/threads.c (thread_mark): There is a window in which the
thread has a handle but doesn't yet have the set of pointerless
freelists, so don't unconditionally dereference
t->pointerless_freelists.
* libguile/iselect.h:
* libguile/threads.c:
* libguile/deprecated.h: Rely on Gnulib for sys/select.h.
* libguile/filesys.c: Rely on Gnulib for 'lstat' and 'mkstemp'.
* libguile/bdw-gc.h: Remove a needless compatibility hack.
* libguile/gc-inline.h: New file, implementing thread-local freelists
providing faster allocation if we already have a scm_i_thread*
pointer. Based on gc_inline.h from libgc.
* libguile/threads.h (scm_i_thread): Add freelists here.
* libguile/threads.c (guilify_self_1, guilify_self_2): Initialize
freelists.
* libguile/vm.c: Include gc-inline.h.
* libguile/vm-engine.c: Rename current_thread to thread. Use
scm_inline_cons instead of scm_cons, scm_inline_cell instead of
scm_cell, and scm_inline_words instead of words.