* libguile/threads.c (thread_mark): Mark the VM stack, if we have one.
(on_thread_exit): Free the VM stack here.
* libguile/vm.c (make_vm): Allocate the VM stack using mmap, and arrange
for it to be marked by the thread marker.
(scm_i_vm_mark_stack, scm_i_vm_free_stack): New internal interfaces.
(allocate_stack, free_stack): New helpers.
* libguile/threads.c (thread_mark): A mark procedure for threads.
Eventually will mark the stack.
(guilify_self_1): Move initialization of VP earlier. Allocate thread
using thread_gc_kind.
(scm_threads_prehistory): Initialize thread_gc_kind.
Reported and debugged by Andrew Gaylard <ag@computer.org>.
* libguile/threads.c (remqueue): When removing the last element from a
queue with more than one element, set (car q) to the previous element.
* THANKS: Thank Andrew Gaylard.
Reported by Chaos Eternal <chaoseternal@shlug.org>
Based on a patch by Nala Ginrut <nalaginrut@gmail.com>
* libguile/threads.c (scm_unlock_mutex_timed): If 'timeout' argument
is false, interpret that as no timeout.
* doc/ref/api-scheduling.texi (Mutexes and Condition Variables):
Update documentation.
* libguile/iselect.h: If we do not have sys/select.h, don't provide
scm_std_select, SELECT_TYPE, FD_SET, FD_ZERO, FD_ISSET, or FD_CLR.
Guile should not be setting these macros in public API. This is an
incompatible change on mingw, but oh well.
* libguile/threads.c: Rely on gnulib's select, and use that to implement
scm_std_select.
* libguile/deprecated.h:
* libguile/deprecated.c: Only provide scm_internal_select if we have
sys/select.h.
* libguile/threads.c (do_thread_exit): Don't assert m->owner == t->handle
since that is not the case when MUTEX was abandoned by T. Reported by Mark
Weaver and others.
* libguile/finalizers.c: New excitement! We'll be running finalizers
asynchronously, from asyncs. This will make it safer to allocate
while holding a mutex.
(GC_set_finalizer_notifier): Add back-compat shim.
* libguile/init.c (scm_i_init_guile): Init the async finalizer mechanism
during boot.
* libguile/gc.c (scm_storage_prehistory): Tell libgc we'll be finalizing
on demand.
(scm_gc): Explicitly run finalizers here.
* libguile/threads.c (guilify_self_2): Run finalizers here if
queue_finalizer_async happened to run during guilify_self_1.
* configure.ac: Add check for GC_set_finalizer_notifier.
* libguile/dynstack.h:
* libguile/dynstack.c: New files, implementing the dynamic stack as a
true stack instead of a linked list. This lowers the cost of
dynwinds: frames, winders, prompts, with-fluids, and dynamic-wind.
For the most part, we allocate these items directly on the stack.
* libguile/dynwinds.h:
* libguile/dynwinds.c: Adapt all manipulators of the wind stack to use
interfaces from dynstack.c. Remove heap-allocated winder and frame
object types.
(scm_dowinds, scm_i_dowinds): Remove these. The first was exported,
but it was not a public interface.
* libguile/continuations.c:
* libguile/continuations.h (scm_t_contregs): Continuation objects
reference scm_t_dynstack* values now. Adapt to the new interfaces.
* libguile/control.c:
* libguile/control.h: There is no longer a scm_tc7_prompt kind of object
that can be allocated on the heap. Instead, the prompt flags, key,
and registers are pushed on the dynwind stack. (The registers are
still on the heap.) Also, since the vm_cont will reference the
dynwinds, make the partial continuation stub take just one extra arg,
instead of storing the intwinds separately in the object table.
* libguile/fluids.c:
* libguile/fluids.h: No more with-fluids objects; instead, the fluids go
on the dynstack. The values still have to be on the heap, though.
(scm_prepare_fluids, scm_swap_fluids): New internal functions,
replacing scm_i_make_with_fluids and scm_i_swap_with_fluids.
* libguile/print.c: Remove prompt and with-fluids printers.
* libguile/tags.h: Revert prompt and with-fluids tc7 values to what they
were before they were allocated.
* libguile/vm-i-system.c (partial_cont_call): Just pop the vmcont, the
intwinds will not be passed as a second arg. Rewind the dynamic stack
from within the VM, so that any rewinder sees valid prompt entries.
(call_cc, tail_call_cc): Adapt to pass the dynstack to
scm_i_vm_capture_stack.
(prompt, wind, unwind, wind_fluids, unwind_fluids): Adapt to the new
interfaces.
* libguile/vm.h (scm_i_capture_current_stack): Rename from
scm_i_vm_capture_continuation.
(scm_i_vm_capture_stack): Take a dynstack as an argument.
* libguile/vm.c (vm_reinstate_partial_continuation): Don't wind here, as
that could result in winders seeing invalid prompts.
* libguile/eval.c:
* libguile/root.c:
* libguile/stacks.c:
* libguile/threads.c:
* libguile/threads.h:
* libguile/throw.c: Adapt other users of dynwinds to use the dynstack.
* libguile/async.c:
* libguile/deprecation.c:
* libguile/fluids.c:
* libguile/gc.c:
* libguile/instructions.c:
* libguile/ports.c:
* libguile/posix.c:
* libguile/strings.c:
* libguile/threads.c: Use the SCM_PTHREAD_ATFORK_LOCK_STATIC_MUTEX
mechanism to lock a number of static mutexen.
This reverts commit ad432bc831.
Not all gensyms need to be universally-unique: most of them just need to
be unique within some portion of a Guile session. We'll take a
different tack on this problem in the next commit.
* libguile/symbols.c (scm_gensym): Make the gensym counter a 128-bit
thread-local, initialized to a random number upon the first call to
`gensym' within a given thread. This counter is rendered as a 22 byte
suffix of mostly base64 digits.
* libguile/threads.h (scm_i_thread): Add a thread-local gensym_counter.
* libguile/threads.c (guilify_self_1): Initialize gensym_counter to NULL.
* libguile/threads.c (scm_ia64_register_backing_store_base)
(scm_ia64_ar_bsp): Provide implementation of these itanium helpers on
freebsd. Thanks to Jim Pryor.
* configure.ac: Check for pthread_np.h and pthread_attr_get_np. Patch
by Jim Pryor.
* libguile/threads.c (get_thread_stack_base): Provide an implementation
for FreeBSD.
* libguile/threads.c (scm_call_with_new_thread): Collect a little before
making a new thread. Our adjust_gc_frequency hack doesn't work well
if the main allocation load is thread creation, as in
https://savannah.gnu.org/bugs/?34140. This does not appreciably
affect the speed of the test in that bug, but does effectively limit
the image size.
* libguile/fluids.c (grow_dynamic_state, new_fluid): Arrange for the
default value in the dynamic-state vector to be SCM_UNDEFINED instead
of SCM_BOOL_F. If the value in the dynamic-state is #f, default to a
value attached to the fluid instead. This allows useful default
values.
(scm_make_fluid_with_default): New function, allows the user to
specify a default value for the fluid. Defaults to #f. Bound to
`make-fluid' on the Scheme side.
(scm_make_unbound_fluid): Use SCM_UNDEFINED as the default in all
threads.
(scm_fluid_unset_x): Also unset the default value. Not sure if this
is the right thing.
(fluid_ref): Update to the new default-value strategy.
* libguile/threads.c (scm_i_reset_fluid): Reset to SCM_UNDEFINED.
* libguile/threads.h: Remove extra arg to scm_i_reset_fluid.
* libguile/vm-i-system.c (fluid-ref): Update to new default-value
strategy.
* module/ice-9/vlist.scm (block-growth-factor): Default to 2 in all
threads. Fixes http://debbugs.gnu.org/10093.
* libguile/ports.c (scm_putc, scm_puts):
* libguile/ports.h (scm_putc_unlocked, scm_puts_unlocked): Separate into
_unlocked and locked variants. Change all callers to use the
_unlocked versions.
* libguile/weak-vector.c:
* libguile/weak-vector.h: Renamed from weaks.[ch]. Remove weak pairs.
They were not safe to access with `car' and `cdr'. Remove weak alist
vectors, as we have weak tables and sets. Reimplement weak vectors,
moving the implementation here.
* libguile/vectors.c:
* libguile/vectors.h: Remove the extra header word. Use
scm_c_weak_vector_ref / scm_c_weak_vector_set_x to access weak
vectors.
* libguile/snarf.h: Remove the extra header word in vectors.
* libguile/threads.c (do_thread_exit, fat_mutex_lock, fat_mutex_unlock):
Instead of weak pairs, store thread-owned mutexes in a list of
one-element weak vectors.
* libguile/guardians.c (finalize_guarded): Similarly, store object
guardians in a list of one-element weak vectors.
* libguile/modules.c (scm_module_reverse_lookup): We no longer need to
handle the case of weak references.
* libguile/print.c (iprin1): Use the standard vector accessor to print
vectors.
* libguile.h:
* libguile/Makefile.am:
* libguile/gc-malloc.c:
* libguile/gc.c:
* libguile/goops.c:
* libguile/init.c:
* libguile/objprop.c:
* libguile/struct.c: Update includes.
* module/ice-9/weak-vector.scm: Load weak vector definitions using an
extension instead of %init-weaks-builtins.
* test-suite/tests/weaks.test: Use the make-...-hash-table names instead
of the old alist vector names.
This reverts commit ccb80964cd, which
introduced a race condition, with a small window during which a mutex
could be held by a thread without being part of its `mutexes' list,
thereby violating the invariant tested at line 667.
* libguile/threads.c (on_thread_exit): Clear `t->guile_mode' upon
entry. This fixes a bug whereby `t->base' would be incorrect for
canceled threads, leading to a misdiagnosed VM stack overflow.
See <http://lists.gnu.org/archive/html/bug-guile/2011-06/msg00068.html>
for details.
(scm_leave_guile_cleanup): Remove because it's unused.
Original Helgrind report:
==14160== Thread #57: lock order "0x47F6B90 before 0x7C25A28" violated
==14160== at 0x4C27730: pthread_mutex_lock (in /.../valgrind-3.6.0/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==14160== by 0x4EFF87E: do_thread_exit (threads.c:664)
==14160== by 0x4E87B89: c_body (continuations.c:512)
==14160== by 0x4F16C21: vm_regular_engine (vm-i-system.c:960)
==14160== by 0x4E90F92: scm_call_4 (eval.c:506)
==14160== by 0x4E88372: scm_i_with_continuation_barrier (continuations.c:450)
==14160== by 0x4E88424: scm_c_with_continuation_barrier (continuations.c:546)
==14160== by 0x51CA3AF: GC_call_with_gc_active (pthread_support.c:1128)
==14160== by 0x4EFF2E0: with_guile_and_parent (threads.c:206)
==14160== by 0x51C46B4: GC_call_with_stack_base (misc.c:1505)
==14160== by 0x4EFF447: scm_with_guile (threads.c:917)
==14160== by 0x51C46B4: GC_call_with_stack_base (misc.c:1505)
==14160== Required order was established by acquisition of lock at 0x47F6B90
==14160== at 0x4C27730: pthread_mutex_lock (in /.../valgrind-3.6.0/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==14160== by 0x4F00262: fat_mutex_lock (threads.c:1362)
==14160== by 0x4F004DA: scm_lock_mutex_timed (threads.c:1462)
==14160== by 0x4F16C09: vm_regular_engine (vm-i-system.c:898)
==14160== by 0x4E90F4D: scm_call_3 (eval.c:499)
==14160== by 0x4EFFA4C: really_launch (threads.c:975)
==14160== by 0x4E87B89: c_body (continuations.c:512)
==14160== by 0x4F16C21: vm_regular_engine (vm-i-system.c:960)
==14160== by 0x4E90F92: scm_call_4 (eval.c:506)
==14160== by 0x4E88372: scm_i_with_continuation_barrier (continuations.c:450)
==14160== by 0x4E88424: scm_c_with_continuation_barrier (continuations.c:546)
==14160== by 0x4EFF289: with_guile_and_parent (threads.c:874)
==14160== followed by a later acquisition of lock at 0x7C25A28
==14160== at 0x4C27730: pthread_mutex_lock (in /.../valgrind-3.6.0/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==14160== by 0x4F00352: fat_mutex_lock (threads.c:1374)
==14160== by 0x4F004DA: scm_lock_mutex_timed (threads.c:1462)
==14160== by 0x4F16C09: vm_regular_engine (vm-i-system.c:898)
==14160== by 0x4E90F4D: scm_call_3 (eval.c:499)
==14160== by 0x4EFFA4C: really_launch (threads.c:975)
==14160== by 0x4E87B89: c_body (continuations.c:512)
==14160== by 0x4F16C21: vm_regular_engine (vm-i-system.c:960)
==14160== by 0x4E90F92: scm_call_4 (eval.c:506)
==14160== by 0x4E88372: scm_i_with_continuation_barrier (continuations.c:450)
==14160== by 0x4E88424: scm_c_with_continuation_barrier (continuations.c:546)
==14160== by 0x4EFF289: with_guile_and_parent (threads.c:874)
* libguile/threads.c (fat_mutex_lock): In the `m->level == 0' case,
release M's lock before taking T's `admin_mutex'.
* libguile/threads.h:
* libguile/threads.c (scm_i_reset_fluid): New internal function, resets
the binding of a fluid for all threads. Needed for fluid GC.
* libguile/fluids.c (new_fluid): Call scm_i_reset_fluid here.