guile/libguile/threads.h

213 lines
6.5 KiB
C
Raw Normal View History

#ifndef SCM_THREADS_H
#define SCM_THREADS_H
/* Copyright 1996-1998,2000-2004,2006-2009,2011-2014,2018
Free Software Foundation, Inc.
This file is part of Guile.
Guile is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Guile is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
Refactor handling of auxiliary stacks and setjmp/longjmp * libguile/__scm.h (scm_i_jmp_buf): Remove definition, which was a shim for ia64. Instead, always use setjmp/longjmp and jmp_buf. * libguile/_scm.h (SCM_I_SETJMP, SCM_I_LONGJMP): Remove; instead use setjmp and longjmp. * libguile/continuations.c (capture_auxiliary_stack): (restore_auxiliary_stack): New helpers. (scm_i_make_continuation): Use capture_auxiliary_stack. (copy_stack_and_call): Use restore_auxiliary_stack. No need to stash the aux stack on the thread, either. * libguile/continuations.h (scm_t_contregs): Use SCM_HAVE_AUXILIARY_STACK to flag when to have an auxiliary_stack member. * libguile/control.h: * libguile/control.c (reify_partial_continuation, scm_c_abort): (scm_suspendable_continuation_p): Adapt to use setjmp/longjmp directly. * libguile/deprecated.h: Add deprecated scm_i_jmp_buf define. * libguile/dynstack.h: * libguile/dynstack.c (PROMPT_JMPBUF): (scm_dynstack_push_prompt, scm_dynstack_find_prompt): (scm_dynstack_wind_prompt): Adapt to jmp_buf type. * libguile/eval.c (eval): Use jmp_buf and setjmp directly. * libguile/gc-malloc.c: No need for ia64-specific things. * libguile/gc.c: No need for ia64-specific things. * libguile/gc.h: No need to declare scm_ia64_ar_bsp. * libguile/init.c: Remove typedef of setjmp_type for Cray, unused. * libguile/threads.c (guilify_self_1): No more pending_rbs_continuation in scm_i_thread, and register_backing_store_base is handled by libgc. (scm_ia64_ar_bsp): Remove definitions; inlined into continuations.c's capture_auxiliary_stack. * libguile/threads.h (scm_i_thread): jmpbuf member is plain jmp_buf. * libguile/throw.c (catch): Just use jmp_buf and setjmp. * libguile/vm-engine.c (VM_NAME): Adapt prototype to take jmp_buf pointer. * libguile/vm.c (vm_abort): Adapt jmp_buf types. (scm_call_n): Use setjmp.
2018-06-17 13:03:39 +02:00
#include <setjmp.h>
#include "libguile/procs.h"
#include "libguile/throw.h"
the dynamic stack is really a stack now, instead of a list * 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.
2012-03-03 17:01:16 +01:00
#include "libguile/dynstack.h"
#include "libguile/iselect.h"
Devolve more validate macros * libguile.h: Add chooks.h. * libguile/Makefile.am: Add chooks.h and chooks.c. * libguile/chooks.c: * libguile/chooks.h: New files, for the C implementation of hooks. Broken out to avoid cycles in gc.h includes. * libguile/gc.h: * libguile/gc.c (scm_i_gc_admin_mutex, scm_i_sweep_mutex): Remove unused mutexes. Include chooks instead of hooks in the main header. * libguile/deprecated.h (scm_gc_running_p): Deprecate this macro always defined to 0. (SCM_VALIDATE_VECTOR_OR_DVECTOR): Deprecate this weird one too. * libguile/array-map.c: Add procs.h include. * libguile/threads.h: * libguile/vm.c: * libguile/r6rs-ports.c: Add smob.h include. * libguile/atomic.h (SCM_VALIDATE_ATOMIC_BOX): Devolve. * libguile/deprecation.c (scm_c_issue_deprecation_warning): Remove use of scm_gc_running_p. * libguile/error.c (scm_error_scm): Remove use of scm_gc_running_p. * libguile/filesys.h (SCM_VALIDATE_DIR) * libguile/fluids.h (SCM_VALIDATE_FLUID) * libguile/fports.h (SCM_VALIDATE_FPORT, SCM_VALIDATE_OPFPORT) * libguile/generalized-arrays.h (SCM_VALIDATE_ARRAY) * libguile/hooks.h (SCM_VALIDATE_HOOK) * libguile/keywords.h (SCM_VALIDATE_KEYWORD) * libguile/ports.h (SCM_VALIDATE_PORT, SCM_VALIDATE_INPUT_PORT) (SCM_VALIDATE_OUTPUT_PORT, SCM_VALIDATE_OPINPORT) (SCM_VALIDATE_OPENPORT, SCM_VALIDATE_OPPORT, SCM_VALIDATE_OPOUTPORT) * libguile/procs.h (SCM_VALIDATE_PROC) * libguile/random.h (SCM_VALIDATE_RSTATE) * libguile/regex-posix.h (SCM_VALIDATE_RGXP) * libguile/stacks.h (SCM_VALIDATE_STACK, SCM_VALIDATE_FRAME) * libguile/strports.h (SCM_VALIDATE_OPOUTSTRPORT) * libguile/struct.h (SCM_VALIDATE_STRUCT SCM_VALIDATE_VTABLE) * libguile/symbols.h (SCM_VALIDATE_SYMBOL) * libguile/variable.h (SCM_VALIDATE_VARIABLE) * libguile/vectors.h (SCM_VALIDATE_VECTOR SCM_VALIDATE_VECTOR_LEN): Devolve. * libguile/validate.h: It's empty now! hooks squish
2018-06-18 10:23:04 +02:00
#include "libguile/smob.h"
Fix continuation problems on IA64. * Specific problems in IA64 make check ** test-unwind Representation of the relevant dynamic context: non-rewindable catch frame make cont. o----o-----a----------b-------------c \ \ call cont. o-----o-----------d A continuation is captured at (c), with a non-rewindable frame in the dynamic context at (b). If a rewind through that frame was attempted, Guile would throw to the catch at (a). Then the context unwinds back past (a), then winds forwards again, and the captured continuation is called at (d). We should end up at the catch at (a). On ia64, we get an "illegal instruction". The problem is that Guile does not restore the ia64 register backing store (RBS) stack (which is saved off when the continuation is captured) until all the unwinding and rewinding is done. Therefore, when the rewind code (scm_i_dowinds) hits the non-rewindable frame at (b), the RBS stack hasn't yet been restored. The throw finds the jmp_buf (for the catch at (a)) correctly from the dynamic context, and jumps back to (a), but the RBS stack is invalid, hence the illegal instruction. This could be fixed by restoring the RBS stack earlier, at the same point (copy_stack) where the normal stack is restored. But that causes a problem in the next test... ** continuations.test The dynamic context diagram for this case is similar: non-rewindable catch frame make cont. a----x-----o----------b-------------c \ \ call cont. o-------d The only significant difference is that the catch point (a) is upstream of where the dynamic context forks. This means that the RBS stack at (d) already contains the correct RBS contents for throwing back to (a), so it doesn't matter whether the RBS stack that was saved off with the continuation gets restored. This test passes with the Guile 1.8.4 code, but fails (with an "illegal instruction") when the code is changed to restore the RBS stack earlier as described above. The problem now is that the RBS stack is being restored _too_ early; specifically when there is still stuff to do that relies on the old RBS contents. When a continuation is called, the sequence of relevant events is: (1) Grow the (normal) stack until it is bigger than the (normal) stack saved off in the continuation. (scm_dynthrow, grow_stack) (2) scm_i_dowinds calls itself recursively, such that (2.1) for each rewind (from (x) to (c)) that will be needed, another frame is added to the stack (both normal and RBS), with local variables specifying the required rewind; the rewinds don't actually happen yet, they will happen when the stack unwinds again through these frames (2.2) required unwinds - back from where the continuation was called (d) to the fork point (x) - are done immediately. (3) The normal (i.e. non-RBS) stack that was stored in the continuation is restored (i.e. copied on top of the actual stack). Note that this doesn't overwrite the frames that were added in (2.1), because the growth in (1) ensures that the added frames are beyond the end of the restored stack. (4) ? Restore the RBS stack here too ? (5) Return (from copy_stack) through the (2.1) frames, which means that the rewinds now happen. (6) setcontext (or longjmp) to the context (c) where the continuation was captured. The trouble is that step (1) does not create space in the RBS stack in the same kind of way that it does for the normal stack. Therefore, if the saved (in the continuation) RBS stack is big enough, it can overwrite the RBS of the (2.1) frames that still need to complete. This causes an illegal instruction when we return through those frames and try to perform the rewinds. * Fix The key to the fix is that the saved RBS stack only needs to be restored at some point before the next setcontext call, and that doing it as close to the setcontext call as possible will avoid bad interactions with the pre-setcontext stack. Therefore we do the restoration at the last possible point, immediately before the next setcontext call. The situation is complicated by there being two ways that the next setcontext call can happen. - If the unwinding and rewinding is all successful, the next setcontext will be the one from step (6) above. This is the "normal" continuation invocation case. - If one of the rewinds throws an error, the next setcontext will come from the throw implementation code. (And the one in step (6) will never happen.) This is the rewind error case. In the rewind error case, the code calling setcontext knows nothing about the continuation. So to cover both cases, we: - copy (in step (4) above) the address and length of the continuation's saved RBS stack to the current thread state (SCM_I_CURRENT_THREAD) - modify all setcontext callers so that they check the current thread state for a saved RBS stack, and restore it if so before calling setcontext. * Notes ** I think rewinders cannot rely on using any stack data Unless it can be guaranteed that the data won't go into a register. I'm not 100% sure about this, but I think it follows from the fact that the RBS stack is not restored until after the rewinds have happened. Note that this isn't a regression caused by the current fix. In Guile 1.8.4, the RBS stack was restored _after_ the rewinds, and this is still the case now. ** Most setcontext calls for `throw' don't need to change the RBS stack In the absence of continuation invocation, the setcontext call in the throw implementation code always sets context to a place higher up the same stack (both normal and RBS), hence no stack restoration is needed. * Other changes ** Using setcontext for all non-local jumps (for __ia64__) Along the way, I read a claim somewhere that setcontext was more reliable than longjmp, in cases where the stack has been manipulated. I don't now have any reason to believe this, but it seems reasonable anyway to leave the __ia64__ code using getcontext/setcontext, instead of setjmp/longjmp. (I think the only possible argument against this would be performance - if getcontext was significantly slower than setjmp. It that proves to be the case, we should revisit this.) ** Capping RBS base for non-main threads Somewhere else along the way, I hit a problem in GC, involving the RBS stack of a non-main thread. The problem was, in SCM_MARK_BACKING_STORE, that scm_ia64_register_backing_store_base was returning a value that was massively greater than the value of scm_ia64_ar_bsp, leading to a seg fault. This is because the implementation of scm_ia64_register_backing_store_base is only valid for the main thread. I couldn't find a neat way of getting the true RBS base of a non-main thread, but one idea is simply to call scm_ia64_ar_bsp when guilifying a thread, and use the value returned as an upper bound for that thread's RBS base. (Note that the RBS stack grows upwards.) (Were it not for scm_init_guile, we could be much more definitive about this. We could take the value of scm_ia64_ar_bsp as a definitive base address for the part of the RBS stack that Guile cares about. We could also then discard scm_ia64_register_backing_store_base.)
2008-05-08 00:29:53 +01:00
#include "libguile/continuations.h"
#include "libguile/vm.h"
2005-03-02 20:42:01 +00:00
#if SCM_USE_PTHREAD_THREADS
#include "libguile/pthread-threads.h"
#endif
#if SCM_USE_NULL_THREADS
#include "libguile/null-threads.h"
#endif
/* smob tags for the thread datatypes */
SCM_API scm_t_bits scm_tc16_thread;
SCM_API scm_t_bits scm_tc16_mutex;
SCM_API scm_t_bits scm_tc16_condvar;
struct scm_thread_wake_data;
2005-03-02 20:42:01 +00:00
typedef struct scm_i_thread {
struct scm_i_thread *next_thread;
SCM handle;
scm_i_pthread_t pthread;
2007-10-20 11:09:58 +00:00
2005-03-02 20:42:01 +00:00
SCM result;
int exited;
/* Boolean indicating whether the thread is in guile mode. */
int guile_mode;
/* Boolean indicating whether to call GC_unregister_my_thread () when
this thread exits. */
int needs_unregister;
struct scm_thread_wake_data *wake;
2005-03-02 20:42:01 +00:00
scm_i_pthread_cond_t sleep_cond;
int sleep_pipe[2];
2005-03-02 20:42:01 +00:00
/* Thread-local freelists; see gc-inline.h. */
void **freelists;
void **pointerless_freelists;
2005-03-02 20:42:01 +00:00
/* Other thread local things.
*/
Reimplement dynamic states 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.
2016-11-27 21:33:30 +01:00
scm_t_dynamic_state *dynamic_state;
the dynamic stack is really a stack now, instead of a list * 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.
2012-03-03 17:01:16 +01:00
/* The dynamic stack. */
scm_t_dynstack dynstack;
2005-03-02 20:42:01 +00:00
/* For system asyncs.
*/
SCM pending_asyncs; /* The thunks to be run at the next
safe point. Accessed atomically. */
2008-05-14 23:52:49 +01:00
unsigned int block_asyncs; /* Non-zero means that asyncs should
2005-03-02 20:42:01 +00:00
not be run. */
/* The current continuation root and the stack base for it.
The continuation root is an arbitrary but unique object that
identifies a dynamic extent. Continuations created during that
extent can also only be invoked during it.
We use pairs where the car is the thread handle and the cdr links
to the previous pair. This might be used for better error
messages but is not essential for identifying continuation roots.
The continuation base is the far end of the stack upto which it
needs to be copied.
*/
SCM continuation_root;
SCM_STACKITEM *continuation_base;
/* For keeping track of the stack and registers. */
struct scm_vm vm;
2005-03-02 20:42:01 +00:00
SCM_STACKITEM *base;
Refactor handling of auxiliary stacks and setjmp/longjmp * libguile/__scm.h (scm_i_jmp_buf): Remove definition, which was a shim for ia64. Instead, always use setjmp/longjmp and jmp_buf. * libguile/_scm.h (SCM_I_SETJMP, SCM_I_LONGJMP): Remove; instead use setjmp and longjmp. * libguile/continuations.c (capture_auxiliary_stack): (restore_auxiliary_stack): New helpers. (scm_i_make_continuation): Use capture_auxiliary_stack. (copy_stack_and_call): Use restore_auxiliary_stack. No need to stash the aux stack on the thread, either. * libguile/continuations.h (scm_t_contregs): Use SCM_HAVE_AUXILIARY_STACK to flag when to have an auxiliary_stack member. * libguile/control.h: * libguile/control.c (reify_partial_continuation, scm_c_abort): (scm_suspendable_continuation_p): Adapt to use setjmp/longjmp directly. * libguile/deprecated.h: Add deprecated scm_i_jmp_buf define. * libguile/dynstack.h: * libguile/dynstack.c (PROMPT_JMPBUF): (scm_dynstack_push_prompt, scm_dynstack_find_prompt): (scm_dynstack_wind_prompt): Adapt to jmp_buf type. * libguile/eval.c (eval): Use jmp_buf and setjmp directly. * libguile/gc-malloc.c: No need for ia64-specific things. * libguile/gc.c: No need for ia64-specific things. * libguile/gc.h: No need to declare scm_ia64_ar_bsp. * libguile/init.c: Remove typedef of setjmp_type for Cray, unused. * libguile/threads.c (guilify_self_1): No more pending_rbs_continuation in scm_i_thread, and register_backing_store_base is handled by libgc. (scm_ia64_ar_bsp): Remove definitions; inlined into continuations.c's capture_auxiliary_stack. * libguile/threads.h (scm_i_thread): jmpbuf member is plain jmp_buf. * libguile/throw.c (catch): Just use jmp_buf and setjmp. * libguile/vm-engine.c (VM_NAME): Adapt prototype to take jmp_buf pointer. * libguile/vm.c (vm_abort): Adapt jmp_buf types. (scm_call_n): Use setjmp.
2018-06-17 13:03:39 +02:00
jmp_buf regs;
2005-03-02 20:42:01 +00:00
} scm_i_thread;
#define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
#define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
#define SCM_VALIDATE_THREAD(pos, a) \
2005-03-02 20:42:01 +00:00
scm_assert_smob_type (scm_tc16_thread, (a))
#define SCM_VALIDATE_MUTEX(pos, a) \
2005-03-02 20:42:01 +00:00
scm_assert_smob_type (scm_tc16_mutex, (a))
#define SCM_VALIDATE_CONDVAR(pos, a) \
2005-03-02 20:42:01 +00:00
scm_assert_smob_type (scm_tc16_condvar, (a))
SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
scm_t_catch_handler handler, void *handler_data);
2005-03-02 20:42:01 +00:00
SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
SCM_INTERNAL void scm_threads_prehistory (void *);
2008-05-14 05:20:47 +02:00
SCM_INTERNAL void scm_init_threads (void);
SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
2005-03-02 20:42:01 +00:00
SCM_INTERNAL void scm_i_dynwind_pthread_mutex_lock_block_asyncs (scm_i_pthread_mutex_t *mutex);
SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
SCM_API SCM scm_yield (void);
2007-10-20 11:09:58 +00:00
SCM_API SCM scm_cancel_thread (SCM t);
SCM_API SCM scm_join_thread (SCM t);
SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
SCM_API SCM scm_thread_p (SCM t);
2005-03-02 20:42:01 +00:00
SCM_API SCM scm_make_mutex (void);
2005-03-02 20:42:01 +00:00
SCM_API SCM scm_make_recursive_mutex (void);
SCM_API SCM scm_make_mutex_with_kind (SCM kind);
SCM_API SCM scm_lock_mutex (SCM m);
SCM_API SCM scm_timed_lock_mutex (SCM m, SCM timeout);
SCM_API void scm_dynwind_lock_mutex (SCM mutex);
* threads.h: Include "coop-pthreads.h" when requested. (scm_threads_make_mutex, scm_threads_lock_mutex, scm_threads_unlock_mutex, scm_threads_monitor): Removed, they were not implemented anyway. (scm_init_thread_procs, scm_try_mutex, scm_timed_condition_variable_wait, scm_broadcast_condition_variable, scm_c_thread_exited_p, scm_thread_exited_p): New prototypes. (struct timespec): Define if not already defined. (scm_t_mutex, scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_t_cond, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Declarations moved here and deprecated. * threads.c: Include <errno.h>. Include "coop-pthreads.c" when requested. (scm_thread_exited_p): New. (scm_try_mutex, scm_broadcast_condition_variable): Newly registered procedures. (scm_wait_condition_variable, scm_timed_wait_condition_variable): Use the latter as the procedure for "wait-condition-variable", thus offering a optional timeout parameter to Scheme. (scm_wait_condition_variable): Implement in terms of scm_timed_wait_condition_variable. (scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Implement in terms of scm_make_mutex, etc, and deprecate. (scm_init_threads): Do not create smobs, leave this to scm_threads_init. Do not include "threads.x" file. (scm_init_thread_procs): New, include "threads.x" here. * null-threads.h (scm_null_mutex, scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed. (scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init, scm_cond_wait, scm_cond_signal, scm_cond_broadcast, scm_cond_destory): Do not define, they are now deprecated and handled by threads.{h,c}. * null-threads.c (scm_null_mutex, scm_null_cond): Define here. (scm_threads_init): Create smobs here, using the appropriate sizes. (block): Removed, now unused. (scm_c_thread_exited_p): New. (scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed and updated users to do their task directly. (scm_try_mutex, timeval_subtract, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed. * coop-threads.c (scm_threads_init): Create smobs here, using the appropriate sizes. (scm_c_thread_exited_p, scm_try_mutex, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed.
2002-10-27 20:12:37 +00:00
SCM_API SCM scm_try_mutex (SCM m);
SCM_API SCM scm_unlock_mutex (SCM m);
SCM_API SCM scm_mutex_p (SCM o);
SCM_API SCM scm_mutex_locked_p (SCM m);
SCM_API SCM scm_mutex_owner (SCM m);
SCM_API SCM scm_mutex_level (SCM m);
2005-03-02 20:42:01 +00:00
SCM_API SCM scm_make_condition_variable (void);
SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
* threads.h: Include "coop-pthreads.h" when requested. (scm_threads_make_mutex, scm_threads_lock_mutex, scm_threads_unlock_mutex, scm_threads_monitor): Removed, they were not implemented anyway. (scm_init_thread_procs, scm_try_mutex, scm_timed_condition_variable_wait, scm_broadcast_condition_variable, scm_c_thread_exited_p, scm_thread_exited_p): New prototypes. (struct timespec): Define if not already defined. (scm_t_mutex, scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_t_cond, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Declarations moved here and deprecated. * threads.c: Include <errno.h>. Include "coop-pthreads.c" when requested. (scm_thread_exited_p): New. (scm_try_mutex, scm_broadcast_condition_variable): Newly registered procedures. (scm_wait_condition_variable, scm_timed_wait_condition_variable): Use the latter as the procedure for "wait-condition-variable", thus offering a optional timeout parameter to Scheme. (scm_wait_condition_variable): Implement in terms of scm_timed_wait_condition_variable. (scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Implement in terms of scm_make_mutex, etc, and deprecate. (scm_init_threads): Do not create smobs, leave this to scm_threads_init. Do not include "threads.x" file. (scm_init_thread_procs): New, include "threads.x" here. * null-threads.h (scm_null_mutex, scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed. (scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init, scm_cond_wait, scm_cond_signal, scm_cond_broadcast, scm_cond_destory): Do not define, they are now deprecated and handled by threads.{h,c}. * null-threads.c (scm_null_mutex, scm_null_cond): Define here. (scm_threads_init): Create smobs here, using the appropriate sizes. (block): Removed, now unused. (scm_c_thread_exited_p): New. (scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed and updated users to do their task directly. (scm_try_mutex, timeval_subtract, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed. * coop-threads.c (scm_threads_init): Create smobs here, using the appropriate sizes. (scm_c_thread_exited_p, scm_try_mutex, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed.
2002-10-27 20:12:37 +00:00
SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
SCM abstime);
SCM_API SCM scm_signal_condition_variable (SCM cond);
* threads.h: Include "coop-pthreads.h" when requested. (scm_threads_make_mutex, scm_threads_lock_mutex, scm_threads_unlock_mutex, scm_threads_monitor): Removed, they were not implemented anyway. (scm_init_thread_procs, scm_try_mutex, scm_timed_condition_variable_wait, scm_broadcast_condition_variable, scm_c_thread_exited_p, scm_thread_exited_p): New prototypes. (struct timespec): Define if not already defined. (scm_t_mutex, scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_t_cond, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Declarations moved here and deprecated. * threads.c: Include <errno.h>. Include "coop-pthreads.c" when requested. (scm_thread_exited_p): New. (scm_try_mutex, scm_broadcast_condition_variable): Newly registered procedures. (scm_wait_condition_variable, scm_timed_wait_condition_variable): Use the latter as the procedure for "wait-condition-variable", thus offering a optional timeout parameter to Scheme. (scm_wait_condition_variable): Implement in terms of scm_timed_wait_condition_variable. (scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Implement in terms of scm_make_mutex, etc, and deprecate. (scm_init_threads): Do not create smobs, leave this to scm_threads_init. Do not include "threads.x" file. (scm_init_thread_procs): New, include "threads.x" here. * null-threads.h (scm_null_mutex, scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed. (scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init, scm_cond_wait, scm_cond_signal, scm_cond_broadcast, scm_cond_destory): Do not define, they are now deprecated and handled by threads.{h,c}. * null-threads.c (scm_null_mutex, scm_null_cond): Define here. (scm_threads_init): Create smobs here, using the appropriate sizes. (block): Removed, now unused. (scm_c_thread_exited_p): New. (scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed and updated users to do their task directly. (scm_try_mutex, timeval_subtract, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed. * coop-threads.c (scm_threads_init): Create smobs here, using the appropriate sizes. (scm_c_thread_exited_p, scm_try_mutex, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed.
2002-10-27 20:12:37 +00:00
SCM_API SCM scm_broadcast_condition_variable (SCM cond);
SCM_API SCM scm_condition_variable_p (SCM o);
SCM_API SCM scm_current_thread (void);
SCM_API SCM scm_all_threads (void);
* threads.h: Include "coop-pthreads.h" when requested. (scm_threads_make_mutex, scm_threads_lock_mutex, scm_threads_unlock_mutex, scm_threads_monitor): Removed, they were not implemented anyway. (scm_init_thread_procs, scm_try_mutex, scm_timed_condition_variable_wait, scm_broadcast_condition_variable, scm_c_thread_exited_p, scm_thread_exited_p): New prototypes. (struct timespec): Define if not already defined. (scm_t_mutex, scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_t_cond, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Declarations moved here and deprecated. * threads.c: Include <errno.h>. Include "coop-pthreads.c" when requested. (scm_thread_exited_p): New. (scm_try_mutex, scm_broadcast_condition_variable): Newly registered procedures. (scm_wait_condition_variable, scm_timed_wait_condition_variable): Use the latter as the procedure for "wait-condition-variable", thus offering a optional timeout parameter to Scheme. (scm_wait_condition_variable): Implement in terms of scm_timed_wait_condition_variable. (scm_mutex_init, scm_mutex_lock, scm_mutex_trylock, scm_mutex_unlock, scm_mutex_destroy, scm_cond_init, scm_cond_wait, scm_cond_timedwait, scm_cond_signal, scm_cond_broadcast, scm_cond_destroy): Implement in terms of scm_make_mutex, etc, and deprecate. (scm_init_threads): Do not create smobs, leave this to scm_threads_init. Do not include "threads.x" file. (scm_init_thread_procs): New, include "threads.x" here. * null-threads.h (scm_null_mutex, scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed. (scm_mutex_init, scm_mutex_lock, scm_mutex_unlock, scm_cond_init, scm_cond_wait, scm_cond_signal, scm_cond_broadcast, scm_cond_destory): Do not define, they are now deprecated and handled by threads.{h,c}. * null-threads.c (scm_null_mutex, scm_null_cond): Define here. (scm_threads_init): Create smobs here, using the appropriate sizes. (block): Removed, now unused. (scm_c_thread_exited_p): New. (scm_null_mutex_init, scm_null_mutex_lock, scm_null_mutex_unlock, scm_null_mutex_destroy, scm_null_condvar_init, scm_null_condvar_wait, scm_null_condvar_signal, scm_null_condvar_destroy): Removed and updated users to do their task directly. (scm_try_mutex, timeval_subtract, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed. * coop-threads.c (scm_threads_init): Create smobs here, using the appropriate sizes. (scm_c_thread_exited_p, scm_try_mutex, scm_timed_wait_condition_variable, scm_broadcast_condition_variable): New. (scm_wait_condition_variable): Removed.
2002-10-27 20:12:37 +00:00
SCM_API int scm_c_thread_exited_p (SCM thread);
SCM_API SCM scm_thread_exited_p (SCM thread);
#ifdef BUILDING_LIBGUILE
/* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
we do use it for cleanup purposes. */
SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key;
# ifdef SCM_HAVE_THREAD_STORAGE_CLASS
SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread;
# define SCM_I_CURRENT_THREAD (scm_i_current_thread)
# else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
# define SCM_I_CURRENT_THREAD \
((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
# endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
2005-03-02 20:42:01 +00:00
#endif /* BUILDING_LIBGUILE */
2008-05-14 05:20:47 +02:00
SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
* __scm.h (SCM_ALLOW_INTS_ONLY): Removed. (SCM_NONREC_CRITICAL_SECTION_START, SCM_NONREC_CRITICAL_SECTION_END, SCM_REC_CRITICAL_SECTION_START, SCM_REC_CRITICAL_SECTION_END): New macros. (SCM_CRITICAL_SECTION_START/END): Defined here. * eval.c: Insert SOURCE_SECTION_START / SOURCE_SECTION_END around the three calls to scm_m_expand_body. * gc.h: #include "libguile/pthread-threads.h"; (SCM_FREELIST_CREATE, SCM_FREELIST_LOC): New macros. * gc.c (scm_i_freelist, scm_i_freelist2): Defined to be of type scm_t_key; * gc.c, gc-freelist.c, inline.h: Use SCM_FREELIST_LOC for freelist access. * gc-freelist.c (scm_gc_init_freelist): Create freelist keys. * gc-freelist.c, threads.c (really_launch): Use SCM_FREELIST_CREATE. * gc-malloc.c (scm_realloc, scm_gc_register_collectable_memory): * gc.c (scm_i_expensive_validation_check, scm_gc, scm_gc_for_newcell): Put threads to sleep before doing GC-related heap administration so that those pieces of code are executed single-threaded. We might consider rewriting these code sections in terms of a "call_gc_code_singly_threaded" construct instead of calling the pair of scm_i_thread_put_to_sleep () and scm_i_thread_wake_up (). Also, we would want to have as many of these sections eleminated. * init.c (scm_init_guile_1): Call scm_threads_prehistory. * inline.h: #include "libguile/threads.h" * pthread-threads.h: Macros now conform more closely to the pthreads interface. Some of them now take a second argument. * threads.c, threads.h: Many changes. * configure.in: Temporarily replaced "copt" threads option with new option "pthreads". (USE_PTHREAD_THREADS): Define if pthreads configured.
2002-12-09 13:42:58 +00:00
2005-03-02 20:42:01 +00:00
/* Convenience functions for working with the pthread API in guile
mode.
*/
#if SCM_USE_PTHREAD_THREADS
SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
2005-03-02 20:42:01 +00:00
SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
pthread_mutex_t *mutex);
SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
pthread_mutex_t *mutex,
const scm_t_timespec *abstime);
2005-03-02 20:42:01 +00:00
#endif
/* More convenience functions.
*/
2005-03-02 20:42:01 +00:00
SCM_API unsigned int scm_std_sleep (unsigned int);
SCM_API unsigned long scm_std_usleep (unsigned long);
SCM_API SCM scm_total_processor_count (void);
SCM_API SCM scm_current_processor_count (void);
#endif /* SCM_THREADS_H */