* 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.
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 (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.
The intent is to allow compilation with `-Wundef', which in turn should
make it easier to catch erroneous uses of nonexistent macros.
* libguile/__scm.h: Don't assume `BUILDING_LIBGUILE' is defined.
* libguile/conv-uinteger.i.c (SCM_TO_TYPE_PROTO): Remove unneeded CPP
conditional on `TYPE_MIN == 0'.
* libguile/fports.c: Check for the definition of `HAVE_CHSIZE' and
`HAVE_FTRUNCATE', not for their value.
* libguile/ports.c: Likewise.
* libguile/numbers.c (guile_ieee_init): Likewise with `HAVE_DINFINITY'
and `HAVE_DQNAN'.
* test-suite/standalone/test-conversion.c (ieee_init): Likewise.
* libguile/strings.c: Likewise with `SCM_STRING_LENGTH_HISTOGRAM'.
* libguile/strings.h: Likewise.
* libguile/tags.h: Likewise with `HAVE_INTTYPES_H' and `HAVE_STDINT_H'.
* libguile/threads.c: Likewise with `HAVE_PTHREAD_GET_STACKADDR_NP'.
* libguile/vm-engine.c (VM_NAME): Likewise with `VM_CHECK_IP'.
* libguile/gen-scmconfig.c (main): Use "#ifdef HAVE_", not "#if HAVE_".
* libguile/socket.c (scm_setsockopt): Likewise.
In libgc CVS the `GC_do_blocking ()' declaration is now public but it
uses a slightly different signature for its first argument.
* configure.ac: Check for `GC_fn_type'.
* libguile/threads.c (GC_fn_type)[HAVE_GC_DO_BLOCKING &&
!HAVE_GC_FN_TYPE]: New typedef.
(scm_without_guile): Explicitly cast `without_guile_trampoline' to
`GC_fn_type'. This is because the upstream definition currently
looks like `typedef void * (extern *GC_fn_type)(void *);', which
isn't compatible.
The crux of this problem was that the thread doing a throw, and so
checking scm_i_critical_section_level, was different from the thread
that was in a critical section.
* libguile/async.h (scm_i_critical_section_level): Removed, replaced
by per-thread critical_section_level.
(SCM_CRITICAL_SECTION_START, SCM_CRITICAL_SECTION_END): Use
per-thread critical_section_level.
* libguile/continuations.c (scm_dynthrow): Check per-thread
critical_section_level.
* libguile/threads.c (guilify_self_1): Init per-thread
critical_section_level.
(scm_i_critical_section_level): Removed.
* libguile/threads.h (scm_i_thread): New critical_section_level field.
* libguile/throw.c (scm_ithrow): Check per-thread critical_section_level.
* libguile/threads.c (do_thread_exit): Deal with `t->mutexes' being a
weak list.
(fat_mutex_lock): Use weak-car pairs when building up `t->mutexes'.
* test-suite/tests/threads.test ("mutex-ownership")["mutex with owner
not retained (bug #27450)"]: New test.
* libguile/threads.c (scm_t_guile_ticket): Remove type.
(resume, scm_enter_guile, suspend, scm_leave_guile): Remove.
(scm_i_init_thread_for_guile): Set `t->top' to NULL, which has the
same effect as calling `scm_enter_guile ()'.
(scm_leave_guile_cleanup, scm_i_with_guile_and_parent): Remove
`scm_leave_guile ()' call.
On Aug 5, 2009, at 10:06, Ken Raeburn wrote:
> (1) In scm_pthread_mutex_lock, we leave and re-enter guile mode so
> that we don't block the thread while in guile mode. But we could
> use pthread_mutex_trylock first, and avoid the costs scm_leave_guile
> seems to incur on the Mac. If we can't acquire the lock, it should
> return immediately, and then we can do the expensive, blocking
> version. A quick, hack version of this changed my run time for
> A(3,8) from 17.5s to 14.5s, saving about 17%; sigaltstack and
> sigprocmask are still in the picture, because they're called from
> scm_catch_with_pre_unwind_handler. I'll work up a nicer patch
> later.
Ah, we already had scm_i_pthread_mutex_trylock lying around; that made
things easy.
A second timing test with A(3,9) and this version of the patch (based
on 1.9.1) shows the same improvement.
* libguile/threads.c (scm_pthread_mutex_lock): Try the mutex before
leaving and reentering guile mode.
Problem was that if an application includes both libguile.h and the
system's setjmp.h, and is compiled on IA64, it gets compile errors
because of jmp_buf, setjmp and longjmp being multiply defined.
* libguile/__scm.h (__ia64__): Define scm_i_jmp_buf, SCM_I_SETJMP and
SCM_I_LONGJMP instead of jmp_buf, setjmp and longjmp.
(all other platforms): Map scm_i_jmp_buf, SCM_I_SETJMP and
SCM_I_LONGJMP to jmp_buf, setjmp and longjmp.
* libguile/continuations.c (scm_make_continuation): Use `SCM_I_SETJMP'
instead of `setjmp'.
(copy_stack_and_call): Use `SCM_I_LONJMP' instead of `longjmp'.
(scm_ia64_longjmp): Use type `scm_i_jmp_buf' instead of `jmp_buf'.
* libguile/continuations.h (scm_t_contregs): Use type `scm_i_jmp_buf'
instead of `jmp_buf'.
* libguile/threads.c (suspend): Use `SCM_I_SETJMP' instead of
`setjmp'.
* libguile/threads.h (scm_i_thread): Use type `scm_i_jmp_buf' instead
of `jmp_buf'.
* libguile/throw.c (JBJMPBUF, make_jmpbuf, jmp_buf_and_retval): Use
type `scm_i_jmp_buf' instead of `jmp_buf'.
(scm_c_catch): Use `SCM_I_SETJMP' instead of `setjmp'.
(scm_ithrow): Use `SCM_I_LONGJMP' instead of `longjmp'.
* libguile/threads.c (scm_join_thread_timed): Recheck t->exited before
looping round to call block_self again, in case thread t has now
exited.
* test-suite/tests/threads.test ("don't hang when joined thread
terminates in SCM_TICK"): New test.
* libguile/threads.c (fat_mutex_unlock): Unblock asyncs when breaking
out of loop.
* test-suite/tests/threads.test (asyncs-still-working?): New function,
to test if asyncs are working (i.e. unblocked). Use this throughout
threads.test, in particular before and after the "timed locking
succeeds if mutex unlocked within timeout" test.