Node:Dynamic Roots, Next:, Previous:Asyncs, Up:Scheduling



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

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

dynamic-root Scheme Procedure
scm_dynamic_root () C Function
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.

quit [exit_val] Scheme Procedure
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.

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

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