Next: Parallel Forms, Previous: Critical Sections, Up: Scheduling
A fluid is an object that can store one value per dynamic state. Each thread has a current dynamic state, and when accessing a fluid, this current dynamic state is used to provide the actual value. In this way, fluids can be used for thread local storage, but they are in fact more flexible: dynamic states are objects of their own and can be made current for more than one thread at the same time, or only be made current temporarily, for example.
Fluids can also 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!
.
Return a newly created fluid. Fluids are objects that can hold one value per dynamic state. That is, modifications to this value are only visible to code that executes with the same dynamic state as the modifying code. When a new dynamic state is constructed, it inherits the values from its parent. Because each thread normally executes with its own dynamic state, you can use fluids for thread local storage.
Return
#t
iff obj is a fluid; otherwise, return#f
.
Return the value associated with fluid in the current dynamic root. If fluid has not been set, then return
#f
.
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.
Set fluid to value temporarily, and call thunk. thunk must be a procedure with no argument.
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.
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.
The function
scm_c_with_fluids
is likescm_with_fluids
except that it takes a C function to call instead of a Scheme thunk.The function
scm_c_with_fluid
is similar but only allows one fluid to be set instead of a list.
This function must be used inside a pair of calls to
scm_dynwind_begin
andscm_dynwind_end
(see Dynamic Wind). During the dynwind context, the fluid fluid is set to val.More precisely, the value of the fluid is swapped with a `backup' value whenever the dynwind context is entered or left. The backup value is initialized with the val argument.
Return a copy of the dynamic state object parent or of the current dynamic state when parent is omitted.
Return
#t
if obj is a dynamic state object; return#f
otherwise.
Return non-zero if obj is a dynamic state object; return zero otherwise.
Return the current dynamic state object.
Set the current dynamic state object to state and return the previous current dynamic state object.
Call proc while state is the current dynamic state object.