Skip to content

Graphs

Every node belongs to a Graph.

new Graph() uses GraphOptions.Default. A graph contains the dependency network, root owner and dispatcher; disposing it releases its owned computations.

The Active Graph

The create* factories resolve Graph.Current. Activate a graph around their calls; dispose the activation handle to restore the previous graph.

Example

The activation applies to the calls inside its scope:

graph.fsx
// Creating a graph does not automatically activate it
let graph = new Graph ()
let graph1 = new Graph ()
assert Graph.TryCurrent.IsNone

// You can manually activate a graph
let active = graph.Activate (): System.IDisposable
assert (Graph.Current = graph)

// The next graph is appended to the stack
let active1 = graph1.Activate ()
assert (Graph.Current = graph1)

// The `Run` member activates the graph, runs the body, and
// then pops the graph from the stack.
graph.Run (fun () -> assert (Graph.Current = graph))
assert (Graph.Current = graph1)

// Dispose an 'activate' instance to pop it from the active
// graph stack
active1.Dispose ()
assert (Graph.Current = graph)

Threads and Graphs

With the default Guarded affinity, an activation applies only on the activating thread. Unchecked and Serialised activations flow with the execution context. Graph access must still obey the threading contract.

let inner, restored, onOtherThread =
    use outerGraph = new Graph ()
    use innerGraph = new Graph ()
    use _ = outerGraph.Activate ()

    let inner =
        use _ = innerGraph.Activate ()
        obj.ReferenceEquals (Graph.Current, innerGraph)

    let restored = obj.ReferenceEquals (Graph.Current, outerGraph)

    let mutable onOtherThread = true
    let thread = Threading.Thread (fun () -> onOtherThread <- Graph.TryCurrent.IsSome)
    thread.Start ()
    thread.Join ()

    inner, restored, onOtherThread

printfn "inner active: %b, outer restored: %b, visible on another thread: %b" inner restored onOtherThread
Console
inner active: true, outer restored: true, visible on another thread: false

C#

Graph.Run is an F# extension member.

From C#, Ranvier.CSharp provides it as an extension method; see C#.

Edit this page