[FIXME: This is pasted in from Tom Lord's original guile.texi chapter plus the Cygnus programmer's manual; it should be *very* carefully reviewed and largely reorganized.]
Arbiters are synchronization objects. They are created with
make-arbiter. Two or more threads can synchronize on an arbiter
by trying to lock it using try-arbiter. This call will succeed
if no other thread has called try-arbiter on the arbiter yet,
otherwise it will fail and return #f. Once an arbiter is
successfully locked, it cannot be locked by another thread until the
thread holding the arbiter calls release-arbiter to unlock it.
#t and lock the arbiter arb if the arbiter
was unlocked. Otherwise, return #f.
#t and unlock the arbiter arb if the
arbiter was locked. Otherwise, return #f.
An async is a pair of one thunk (a parameterless procedure) and a mark. Setting the mark on an async guarantees that the thunk will be executed somewhen in the future (asynchronously). Setting the mark more than once is satisfied by one execution of the thunk.
Guile supports two types of asyncs: Normal asyncs and system asyncs. They differ in that marked system asyncs are executed implicitly as soon as possible, whereas normal asyncs have to be invoked explicitly. System asyncs are held in an internal data structure and are maintained by Guile.
Normal asyncs are created with async, system asyncs with
system-async. They are marked with async-mark or
system-async-mark, respectively.
As already mentioned above, system asyncs are executed automatically.
Normal asyncs have to be explicitly invoked by storing one or more of
them into a list and passing them to run-asyncs.
Automatic invocation of system asyncs can be temporarily disabled by
calling mask-signals and unmask-signals. Setting the mark
while async execution is disabled will nevertheless cause the async to
run once execution is enabled again. Please note that calls to these
procedures should always be paired, and they must not be nested, e.g. no
mask-signals is allowed if another one is still active.
#f,
otherwise return the first argument.
A dynamic root is a root frame of Scheme evaluation. The top-level repl, for example, is an instance of a dynamic root.
Each dynamic root has its own chain of dynamic-wind information. Each has its own set of continuations, jump-buffers, and pending CATCH statements which are inaccessible from the dynamic scope of any other dynamic root.
In a thread-based system, each thread has its own dynamic root. Therefore, continuations created by one thread may not be invoked by another.
Even in a single-threaded system, it is sometimes useful to create a new dynamic root. For example, if you want to apply a procedure, but to not allow that procedure to capture the current continuation, calling the procedure under a new dynamic root will do the job.
(thunk) in a new dynamic context, returning its value.
If an error occurs during evaluation, apply handler to the
arguments to the throw, just as throw would. If this happens,
handler is called outside the scope of the new root -- it is
called in the same dynamic context in which
call-with-dynamic-root was evaluated.
If thunk captures a continuation, the continuation is rooted at
the call to thunk. In particular, the call to
call-with-dynamic-root is not captured. Therefore,
call-with-dynamic-root always returns at most one time.
Before calling thunk, the dynamic-wind chain is un-wound back to the root and a new chain started for thunk. Therefore, this call may not do what you expect:
;; Almost certainly a bug:
(with-output-to-port
some-port
(lambda ()
(call-with-dynamic-root
(lambda ()
(display 'fnord)
(newline))
(lambda (errcode) errcode))))
The problem is, on what port will `fnord' be displayed? You
might expect that because of the with-output-to-port that
it will be displayed on the port bound to some-port. But it
probably won't -- before evaluating the thunk, dynamic winds are
unwound, including those created by with-output-to-port.
So, the standard output port will have been re-set to its default value
before display is evaluated.
(This function was added to Guile mostly to help calls to functions in C libraries that can not tolerate non-local exits or calls that return multiple times. If such functions call back to the interpreter, it should be under a new dynamic root.)
These objects are only useful for comparison using eq?.
They are currently represented as numbers, but your code should
in no way depend on this.
If integer exit_val is specified and if Guile is being used stand-alone and if quit is called from the initial dynamic-root, exit_val becomes the exit status of the Guile process and the process exits.
When Guile is run interactively, errors are caught from within the
read-eval-print loop. An error message will be printed and abort
called. A default set of signal handlers is installed, e.g., to allow
user interrupt of the interpreter.
It is possible to switch to a "batch mode", in which the interpreter will terminate after an error and in which all signals cause their default actions. Switching to batch mode causes any handlers installed from Scheme code to be removed. An example of where this is useful is after forking a new process intended to run non-interactively.
#f case has not been implemented.
[NOTE: this chapter was written for Cygnus Guile and has not yet been updated for the Guile 1.x release.]
Here is a the reference for Guile's threads. In this chapter I simply quote verbatim Tom Lord's description of the low-level primitives written in C (basically an interface to the POSIX threads library) and Anthony Green's description of the higher-level thread procedures written in scheme.
When using Guile threads, keep in mind that each guile thread is executed in a new dynamic root.
(thunk) in a new thread, and new dynamic context,
returning a new thread object representing the thread.
If an error occurs during evaluation, call error-handler, passing it an error code describing the condition. [Error codes are currently meaningless integers. In the future, real values will be specified.] If this happens, the error-handler is called outside the scope of the new root -- it is called in the same dynamic context in which with-new-thread was evaluated, but not in the caller's thread.
All the evaluation rules for dynamic roots apply to threads.
Higher level thread procedures are available by loading the
(ice-9 threads) module. These provide standardized
thread creation and mutex interaction.
This procedure is specified as the standard error-handler for
make-thread and begin-thread. If the number of args
is three or more, use display-error, otherwise display a message
"uncaught throw to tag". All output is sent to the port specified
by current-error-port.
Before display, global var the-last-stack is set to #f
and signals are unmasked with unmask-signals.
[FIXME: Why distinguish based on number of args?! Cue voodoo music here.]
call-with-new-thread using %thread-handler as the error
handler.
call-with-new-thread using %thread-handler as the error
handler.
dynamic-wind.
with-mutex.
[FIXME: Is there any way to access the mutex?]
Fluids are objects to store values in. They have a few properties which make them useful in certain situations: Fluids can have one value per dynamic root (see section Dynamic Roots), so that changes to the value in a fluid are only visible in the same dynamic root. Since threads are executed in separate dynamic roots, fluids can be used for thread local storage (see section Threads).
Fluids can be used to simulate dynamically scoped variables. These are
used in several (especially in older) dialects of lisp, such as in Emacs
Lisp, and they work a bit like global variables in that they can be
modified by the caller of a procedure, and the called procedure will see
the changes. With lexically scoped variables--which are normally used
in Scheme--this cannot happen. See the description of
with-fluids* below for details.
New fluids are created with make-fluid and fluid? is used
for testing whether an object is actually a fluid.
#t iff obj is a fluid; otherwise, return
#f.
The values stored in a fluid can be accessed with fluid-ref and
fluid-set!.
#f.
with-fluids* temporarily changes the values of one or more fluids,
so that the given procedure and each procedure called by it access the
given values. After the procedure returns, the old values are restored.
Go to the first, previous, next, last section, table of contents.