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.
* libguile/threads.c (lock_mutex_return_void, unlock_mutex_return_void):
New static functions that simply call scm_lock_mutex and
scm_unlock_mutex, respectively, but return void instead of SCM.
(scm_dynwind_lock_mutex): Pass unlock_mutex_return_void to
scm_dynwind_unwind_handler_with_scm, and lock_mutex_return_void to
scm_dynwind_rewind_handler_with_scm. Previously, we passed
scm_unlock_mutex and scm_lock_mutex (which return SCM), but the
scm_dynwind_* functions expect pointers to functions which return
void. When SCM is of type union, this changes the calling conventions
of the functions on some platforms (e.g. GCC 4.5.2 and 4.5.3 on x86).
* libguile/threads.c (lock_mutex_return_void, unlock_mutex_return_void):
New static functions that simply call scm_lock_mutex and
scm_unlock_mutex, respectively, but return void instead of SCM.
(scm_dynwind_lock_mutex): Pass unlock_mutex_return_void to
scm_dynwind_unwind_handler_with_scm, and lock_mutex_return_void to
scm_dynwind_rewind_handler_with_scm. Previously, we passed
scm_unlock_mutex and scm_lock_mutex (which return SCM), but the
scm_dynwind_* functions expect pointers to functions which return
void. When SCM is of type union, this changes the calling conventions
of the functions on some platforms (e.g. GCC 4.5.2 and 4.5.3 on x86).
* libguile/_scm.h (SCM_ASYNC_TICK, SCM_ASYNC_TICK_WITH_CODE): Make these
definitions private. Call the tick() function instead of click().
* libguile/__scm.h:
* libguile/async.h (scm_async_tick): Move declaration here. Remove
scm_async_click declaration. Tick is the new (and old!) click, you
see.
(SCM_CRITICAL_SECTION_END): Call tick() instead of click().
* libguile/async.c (scm_async_tick): Remove old definition, and rename
scm_async_click to scm_async_tick.
(decrease_block): Adapt to click() -> tick() name change.
* libguile/deprecated.h (scm_async_click, SCM_ASYNC_TICK): Define some
GONE macros.
* libguile/threads.c (fat_mutex_unlock): Tick() instead of click().
* libguile/threads.c (do_thread_exit_trampoline, on_thread_exit):
(scm_i_init_thread_for_guile): Only register and unregister threads
with bdw-gc when we are building with threads support. Thanks to
Marijn for the report.
GC_unregister_my_thread is only supposed to be called from
a thread other than the main thread, so, it should never be
called when the system is compiled with null threads.
* libguile/threads.c (on_thread_exit)[SCM_USE_NULL_THREADS]: don't
call GC_unregister_my_thread
This is not present in earlier versions of BDW-GC
* configure.ac: check for GC_allow_register_threads
* libguile/threads.c (scm_i_init_thread_for_guile): Only call
GC_allow_register_threads if it is present.
* libguile/threads.c (guilify_self_1): Prevent finalizers from running
before SCM_I_CURRENT_THREAD is set.
(do_thread_exit_trampoline): Leave the thread in the registered state.
(on_thread_exit): Always unregister the thread here.
* libguile/init.h:
* libguile/init.c (scm_i_init_guile): Change arg to this internal
function from SCM_STACKITEM* to void*. Actually it's a
struct GC_stack_base*.
* libguile/bdw-gc.h: Don't do pthread redirects, because we don't want
to affect applications' pthread_* bindings.
* libguile/pthread-threads.h (scm_i_pthread_create)
(scm_i_pthread_detach, scm_i_pthread_exit, scm_i_pthread_cancel)
(scm_i_pthread_sigmask): Do pthread redirects here, in this internal
header.
* libguile/threads.h: Remove declaration of internal
scm_i_with_guile_and_parent. Remove declaration of undefined
scm_threads_init_first_thread. Make declaration of internal
scm_threads_prehistory actually internal, and take a void* (actually a
struct GC_stack_base*).
* libguile/threads.c (GC_get_stack_base): Implement a shim if this
function is unavailable, and fold in the implementations of
get_thread_stack_base.
(GC_call_with_stack_base): Actually implement.
(guilify_self_1): Take a GC_stack_base* as an arg.
(scm_i_init_thread_for_guile): Likewise, and set up libgc for
registration of other threads.
(scm_init_guile): Use GC_get_stack_base instead of our own guesswork.
(with_guile_and_parent, scm_i_with_guile_and_parent): Rework to
trampoline through a GC_call_with_stack_base.
(scm_threads_prehistory): Pass the "base" arg on to guilify_self_1.
* libguile/threads.h: Always declare a scm_i_thread_key, for cleanup
purposes, in the BUILDING_LIBGUILE case.
* libguile/threads.c (scm_i_thread_key): Init with a cleanup handler, so
any guile-specific info for a thread can be cleaned up reliably.
(guilify_self_1): Always set the thread key.
(do_thread_exit_trampoline, on_thread_exit): Enter guile-mode for the
guile-mode cleanup handler, and trampoline through a
gc_call_with_stack_base for reasons explained in the code.
(init_thread_key, scm_i_init_thread_for_guile): Always init the key.
(scm_i_with_guile_and_parent): No need for pthread_cancel cleanup
handlers, as the pthread key destructor will take care of that for
us.
(really_launch): Remove needless pthread_exit call with incorrect
comment.
* configure.ac: Check for GC_call_with_gc_active.
* libguile/threads.h (scm_i_thread): Remove "top", as it's not used.
* libguile/threads.c (with_gc_inactive, with_gc_active): Define shims to
GC_do_blocking and GC_call_with_gc_active.
(scm_i_init_thread_for_guile): Don't do thread base adjustment here,
do it in scm_i_with_guile_and_parent. The previous logic would never
be run.
(scm_i_with_guile_and_parent): If we enter Guile mode, leave it too.
Take care of adjusting the thread stack base here too. Also, call
with_gc_active.
(scm_without_guile): Refactor.
* libguile/threads.c:
* libguile/posix.c: Change a couple #if HAVE_FOO to #ifdef HAVE_FOO to
pacify -Wundef. Some vars are defined or not, whereas some are always
defined to 0 or 1. The inconsistency is unfortunate.
The memory leak is trivially reproducible with:
(define m (make-mutex))
(let loop () (lock-mutex m) (unlock-mutex m) (loop))
or similarly with:
(define p (delay (+ 1 2)))
(let loop () (force p) (loop))
since `force' acquires P's mutex.
It could also lead to premature release of a thread waiting in
`fat_mutex_lock' when a former owner's `do_thread_exit' is run.
* libguile/threads.c (fat_mutex_unlock): When `m->level' becomes 0,
remove MUTEX from `t->mutexes'.
(fat_mutex_lock): Update comment above the `t->mutexes' assignment.
(do_thread_exit): Add an assertion making sure that each mutex in
`t->mutexes' is owned by T.
* libguile/debug.h (scm_t_debug_frame): Remove this type, as it was
internal to the old evaluator.
(SCM_EVALFRAME, SCM_APPLYFRAME, SCM_VOIDFRAME, SCM_MACROEXPF)
(SCM_TAILREC, SCM_TRACED_FRAME, SCM_ARGS_READY, SCM_DOVERFLOW)
(SCM_MAX_FRAME_SIZE, SCM_FRAMETYPE)
(SCM_EVALFRAMEP, SCM_APPLYFRAMEP, SCM_VOIDFRAMEP, SCM_MACROEXPFP)
(SCM_TAILRECP, SCM_TRACED_FRAME_P, SCM_ARGS_READY_P, SCM_OVERFLOWP)
(SCM_SET_MACROEXP, SCM_SET_TAILREC, SCM_SET_TRACED_FRAME)
(SCM_SET_ARGSREADY, SCM_SET_OVERFLOW)
(SCM_CLEAR_MACROEXP, SCM_CLEAR_TRACED_FRAME, SCM_CLEAR_ARGSREADY):
Remove macro accessors to scm_t_debug_frame.
(SCM_DEBUGOBJP, SCM_DEBUGOBJ_FRAME, SCM_SET_DEBUGOBJ_FRAME):
(scm_debug_object_p, scm_make_debugobj): Remove debugobj accessors.
(scm_i_unmemoize_expr): Remove unused declaration.
* libguile/debug.c (scm_debug_options): No more max limit on frame
sizes.
(scm_start_stack): Just call out to scm_vm_call_with_new_stack.
(scm_debug_object_p, scm_make_debugobj, scm_init_debug): No more
debugobj smob type.
* libguile/deprecated.h:
* libguile/deprecated.c (scm_i_deprecated_last_debug_frame)
(scm_last_debug_frame): Remove deprecated debug-frame bits.
* libguile/stacks.c (scm_make_stack): Rework this function and its
dependents to only walk VM frames.
(scm_stack_id): Call out to the holder of the VM frame in question,
which should be a VM or a VM continuation, for the stack ID. Currently
this bit is stubbed out.
(scm_last_stack_frame): Removed. It seems this is mainly useful for a
debugger, and we need to rewrite the debugger to work on the Scheme
level.
* test-suite/tests/continuations.test ("continuations"): Remove test for
last-stack-frame.
* libguile/continuations.h (struct scm_t_contregs):
* libguile/continuations.c (scm_make_continuation):
(copy_stack_and_call, scm_i_with_continuation_barrier): No need to
save and restore debug frames.
* libguile/threads.h (scm_i_thread): Don't track debug frames.
(scm_i_last_debug_frame, scm_i_set_last_debug_frame): Remove macro
accessors.
* libguile/threads.c (guilify_self_1): Don't track debug frames.
* libguile/throw.c: No need to track debug frames in a jmpbuf.
* libguile/vm-engine.c (vm_engine, VM_PUSH_DEBUG_FRAMES): Don't push
debug frames.
* libguile/vm.h:
* libguile/vm.c (scm_vm_call_with_new_stack): New function. Currently
stubbed out though.