VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
graph_task.h File Reference

DAG-based task graph with condition branching, cycle detection, and DOT export. More...

#include <functional>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "./macros.h"
#include "./traits.h"
Include dependency graph for graph_task.h:

Go to the source code of this file.

Classes

 Node in a directed acyclic task graph supporting condition branching and parallel execution. More...

Namespaces

Typedefs

using vlink::GraphTaskPtr = std::shared_ptr<GraphTask>

Detailed Description

DAG-based task graph with condition branching, cycle detection, and DOT export.

GraphTask implements a directed acyclic graph (DAG) of work units. Tasks define their dependencies by calling precede() (this task must finish before the target) or succeed() (the target must finish before this task). The entire graph is then submitted to any MessageLoop, MultiLoop or ThreadPool via execute().

Task types:

Factory Callback signature Use case
create(callback) void() Regular work task
create_condition(callback) int() Branch selector (returns branch id)

Condition task: A condition task returns an integer selecting which successor sub-graph to activate. All other successors are skipped. This enables if/switch style DAG branching.

Execution policies:

Policy Behaviour
kPolicyOnce Task runs exactly once per execute() call (default)
kPolicyMultiple Task may run multiple times in one execute() pass
kPolicyWaitAll Task waits for all predecessors before running (default DAG rule)

Operator syntax for building graphs:

auto A = vlink::GraphTask::create("A", []{ step_a(); });
auto B = vlink::GraphTask::create("B", []{ step_b(); });
auto C = vlink::GraphTask::create("C", []{ step_c(); });
A-- > B-- > C; // A depends on B, B depends on C (execution order: C, B, A)
Note
  • execute() traverses the reachable sub-graph and dispatches all ready tasks to the engine.
  • has_cycle() detects cycles using DFS; cycles cause undefined behaviour at runtime.
  • export_to_dot() produces a Graphviz DOT string for visualisation.
Example
vlink::MultiLoop engine(4);
engine.async_run();
auto load = vlink::GraphTask::create("load", [] { load_data(); });
auto proc = vlink::GraphTask::create("proc", [] { process(); });
auto save = vlink::GraphTask::create("save", [] { save_data(); });
// A-- > B calls A->succeed(B), meaning B must complete before A.
// So save-- > proc-- > load produces execution order: load, proc, save.
save-- > proc-- > load;
assert(!save->has_cycle());
save->execute(&engine);