Threads, Mutexes, Asyncs and Dynamic Roots
******************************************

[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
========

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.

 -- Scheme Procedure: make-arbiter name
 -- C Function: scm_make_arbiter (name)
     Return an object of type arbiter and name NAME. Its state is initially unlocked.  Arbiters are a way to achieve process synchronization.

 -- Scheme Procedure: try-arbiter arb
 -- C Function: scm_try_arbiter (arb)
     Return #t and lock the arbiter ARB if the arbiter was unlocked. Otherwise, return #f.

 -- Scheme Procedure: release-arbiter arb
 -- C Function: scm_release_arbiter (arb)
     Return #t and unlock the arbiter ARB if the arbiter was locked. Otherwise, return #f.

Asyncs
======

Asyncs are a means of deferring the excution of Scheme code until it is safe to do so.

Guile provides two kinds of asyncs that share the basic concept but are otherwise quite different: system asyncs and user asyncs.  System asyncs are integrated into the core of Guile and are executed automatically when the system is in a state to allow the execution of Scheme code. For example, it is not possible to execute Scheme code in a POSIX signal handler, but such a signal handler can queue a system async to be executed in the near future, when it is safe to do so.

System asyncs can also be queued for threads other than the current one. This way, you can cause threads to asynchronously execute arbitrary code.

User asyncs offer a convenient means of queueing procedures for future execution and triggering this execution.  They will not be executed automatically.

System asyncs
-------------

To cause the future asynchronous execution of a procedure in a given thread, use system-async-mark.

Automatic invocation of system asyncs can be temporarily disabled by calling call-with-blocked-asyncs.  This function works by temporarily increasing the async blocking level of the current thread while a given procedure is running.  The blocking level starts out at zero, and whenever a safe point is reached, a blocking level greater than zero will prevent the execution of queued asyncs.

Analogously, the procedure call-with-unblocked-asyncs will temporarily decrease the blocking level of the current thread.  You can use it when you want to disable asyncs by default and only allow them temporarily.

 -- Scheme Procedure: system-async-mark proc [thread]
 -- C Function: scm_system_async_mark (proc)
 -- C Function: scm_system_async_mark_for_thread (proc, thread)
     Mark PROC (a procedure with zero arguments) for future execution in THREAD.  When PROC has already been marked for THREAD but has not been executed yet, this call has no effect. When THREAD is omitted, the thread that called system-async-mark is used.

     This procedure is not safe to be called from signal handlers.  Use scm_sigaction or scm_sigaction_for_thread to install signal handlers.

 -- Scheme Procedure: call-with-blocked-asyncs proc
 -- C Function: scm_call_with_blocked_asyncs (proc)
 -- C Function: void *scm_c_call_with_blocked_asyncs (void * (*proc) (void *data), void *data)
     Call PROC and block the execution of system asyncs by one level for the current thread while it is running.  Return the value returned by PROC.  For the first two variants, call PROC with no arguments; for the third, call it with DATA.

 -- Scheme Procedure: call-with-unblocked-asyncs proc
 -- C Function: scm_call_with_unblocked_asyncs (proc)
 -- C Function: void *scm_c_call_with_unblocked_asyncs (void *(*p) (void *d), void *d)
     Call PROC and unblock the execution of system asyncs by one level for the current thread while it is running.  Return the value returned by PROC.  For the first two variants, call PROC with no arguments; for the third, call it with DATA.

User asyncs
-----------

A user async is a pair of a thunk (a parameterless procedure) and a mark.  Setting the mark on a user async will cause the thunk to be executed when the user async is passed to run-asyncs.  Setting the mark more than once is satisfied by one execution of the thunk.

User asyncs are created with async.  They are marked with async-mark.

 -- Scheme Procedure: async thunk
 -- C Function: scm_async (thunk)
     Create a new user async for the procedure THUNK.

 -- Scheme Procedure: async-mark a
 -- C Function: scm_async_mark (a)
     Mark the user async A for future execution.

 -- Scheme Procedure: run-asyncs list_of_a
 -- C Function: scm_run_asyncs (list_of_a)
     Execute all thunks from the marked asyncs of the list LIST_OF_A.

Dynamic Roots
=============

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.

 -- Scheme Procedure: call-with-dynamic-root thunk handler
 -- C Function: scm_call_with_dynamic_root (thunk, handler)
     Evaluate (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.)

 -- Scheme Procedure: dynamic-root
 -- C Function: scm_dynamic_root ()
     Return an object representing the current 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.

 -- Scheme Procedure: quit [exit_val]
     Throw back to the error handler of the current dynamic root.

     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.

 -- Scheme Procedure: batch-mode?
     Returns a boolean indicating whether the interpreter is in batch mode.

 -- Scheme Procedure: set-batch-mode?! arg
     If ARG is true, switches the interpreter to batch mode. The #f case has not been implemented.

Threads
=======

[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.

Low level thread primitives
---------------------------

 -- Scheme Procedure: call-with-new-thread thunk error-handler
     Evaluate (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.

 -- Scheme Procedure: join-thread thread
     Suspend execution of the calling thread until the target THREAD terminates, unless the target THREAD has already terminated.

 -- Scheme Procedure: yield
     If one or more threads are waiting to execute, calling yield forces an immediate context switch to one of them. Otherwise, yield has no effect.

 -- Scheme Procedure: make-mutex
     Create a new mutex object.

 -- Scheme Procedure: lock-mutex mutex
     Lock MUTEX. If the mutex is already locked, the calling thread blocks until the mutex becomes available. The function returns when the calling thread owns the lock on MUTEX.  Locking a mutex that a thread already owns will succeed right away and will not block the thread.  That is, Guile's mutexes are recursive.

 -- Scheme Procedure: try-mutex mutex
     Try to lock MUTEX. If the mutex is already locked by someone else, return #f.  Else lock the mutex and return #t.

 -- Scheme Procedure: unlock-mutex mutex
     Unlocks MUTEX if the calling thread owns the lock on MUTEX.  Calling unlock-mutex on a mutex not owned by the current thread results in undefined behaviour. Once a mutex has been unlocked, one thread blocked on MUTEX is awakened and grabs the mutex lock.  Every call to lock-mutex by this thread must be matched with a call to unlock-mutex.  Only the last call to unlock-mutex will actually unlock the mutex.

 -- Scheme Procedure: make-condition-variable

 -- Scheme Procedure: wait-condition-variable cond-var mutex [time]
     Wait until COND-VAR has been signalled.  While waiting, MUTEX is atomically unlocked (as with unlock-mutex) and is locked again when this function returns.  When TIME is given, it specifies a point in time where the waiting should be aborted.  It can be either a integer as returned by current-time or a pair as returned by gettimeofday.  When the waiting is aborted the mutex is locked and #f is returned.  When the condition variable is in fact signalled, the mutex is also locked and #t is returned.

 -- Scheme Procedure: signal-condition-variable cond-var

 -- Scheme Procedure: signal-condition-variable cond-var

Higher level thread procedures
------------------------------

Higher level thread procedures are available by loading the (ice-9 threads) module.  These provide standardized thread creation and mutex interaction.

 -- Scheme Procedure: %thread-handler tag args...

     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.]

 -- macro: make-thread proc [args...]
     Apply PROC to ARGS in a new thread formed by call-with-new-thread using %thread-handler as the error handler.

 -- macro: begin-thread first [rest...]
     Evaluate forms FIRST and REST in a new thread formed by call-with-new-thread using %thread-handler as the error handler.

 -- macro: with-mutex m [body...]
     Lock mutex M, evaluate BODY, and then unlock M. These sub-operations form the branches of a dynamic-wind.

 -- macro: monitor first [rest...]
     Evaluate forms FIRST and REST under a newly created anonymous mutex, using with-mutex.

     [FIXME: Is there any way to access the mutex?]

Fluids
======

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 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 Threads).

Fluids can be used to simulate the desirable effects of dynamically scoped variables.  Dynamically scoped variables are useful when you want to set a variable to a value during some dynamic extent in the execution of your program and have them revert to their original value when the control flow is outside of this dynamic extent.  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.  The values stored in a fluid can be accessed with fluid-ref and fluid-set!.

 -- Scheme Procedure: make-fluid
 -- C Function: scm_make_fluid ()
     Return a newly created fluid. Fluids are objects of a certain type (a smob) that can hold one SCM value per dynamic root.  That is, modifications to this value are only visible to code that executes within the same dynamic root as the modifying code.  When a new dynamic root is constructed, it inherits the values from its parent.  Because each thread executes in its own dynamic root, you can use fluids for thread local storage.

 -- Scheme Procedure: fluid? obj
 -- C Function: scm_fluid_p (obj)
     Return #t iff OBJ is a fluid; otherwise, return #f.

 -- Scheme Procedure: fluid-ref fluid
 -- C Function: scm_fluid_ref (fluid)
     Return the value associated with FLUID in the current dynamic root.  If FLUID has not been set, then return #f.

 -- Scheme Procedure: fluid-set! fluid value
 -- C Function: scm_fluid_set_x (fluid, value)
     Set the value associated with FLUID in the current dynamic root.

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.

 -- Scheme Procedure: with-fluids* fluids values thunk
 -- C Function: scm_with_fluids (fluids, values, thunk)
     Set FLUIDS to VALUES temporary, and call THUNK. FLUIDS must be a list of fluids and VALUES must be the same number of their values to be applied.  Each substitution is done in the order given.  THUNK must be a procedure with no argument. it is called inside a dynamic-wind and the fluids are set/restored when control enter or leaves the established dynamic extent.

 -- Scheme Macro: with-fluids ((fluid value) ...) body...
     Execute BODY... while each FLUID is set to the corresponding VALUE.  Both FLUID and VALUE are evaluated and FLUID must yield a fluid.  BODY... is executed inside a dynamic-wind and the fluids are set/restored when control enter or leaves the established dynamic extent.