90 #include <type_traits>
91 #include <unordered_set>
120 class PrivateToken final {
122 PrivateToken() =
default;
172 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
Callback&& callback,
int condition_number = 0);
182 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
const std::string& name,
Callback&& callback,
183 int condition_number = 0);
193 int condition_number = 0);
205 int condition_number = 0);
215 explicit GraphTask(PrivateToken token,
Callback&& callback,
int condition_number);
223 explicit GraphTask(PrivateToken token,
const std::string& name,
Callback&& callback,
int condition_number);
262 template <
class GraphEngineT>
263 void execute(GraphEngineT* graph_engine);
294 void precede(
const std::shared_ptr<GraphTask>& task);
307 void succeed(
const std::shared_ptr<GraphTask>& task);
496 template <
typename TypeT>
497 struct SharedAllocator;
499 std::vector<std::shared_ptr<GraphTask>> prepare_execution();
501 std::optional<int> invoke(
bool once);
503 void set_ready_callback(
Callback&& callback);
505 void notify(
int condition_number,
bool first_invocation =
true);
509 bool mark_predecessor_satisfied(
bool active,
bool* has_active,
bool first_invocation =
true);
511 bool mark_ready(
bool enable);
513 void update_status(
Status status);
515 bool detect_cycle(
const GraphTask* task, std::unordered_set<const GraphTask*>& visited,
516 std::unordered_set<const GraphTask*>& recursion_stack, uint32_t& depth, uint32_t max_depth)
const;
518 bool reaches_via_successors(
const GraphTask* start_node,
519 const std::vector<std::weak_ptr<GraphTask>>& start_successors,
522 static void clear_invalid_task(
const std::shared_ptr<GraphTask>& task);
525 std::unique_ptr<Impl> impl_;
540 template <
class GraphEngineT>
542 auto self = shared_from_this();
543 auto tasks = std::make_shared<std::vector<std::shared_ptr<GraphTask>>>(prepare_execution());
544 std::weak_ptr<std::vector<std::shared_ptr<GraphTask>>> weak_tasks = tasks;
546 for (
const auto& task : *tasks) {
547 task->set_ready_callback([weak_tasks, node = task.get(), graph_engine]() {
548 auto tasks = weak_tasks.lock();
554 constexpr
bool kHaspriority =
555 VLINK_HAS_MEMBER(GraphEngineT, post_task_with_priority(std::declval<Callback>(), std::declval<uint16_t>()));
556 [[maybe_unused]] constexpr uint8_t kPriorityType = 2;
562 auto task_func = [tasks, node]() {
563 auto ret = node->invoke(
true);
570 auto post_task = [graph_engine](
auto&& func) ->
bool {
571 using Ret = decltype(graph_engine->post_task(std::forward<decltype(func)>(func)));
573 if constexpr (std::is_same_v<Ret, bool>) {
574 return graph_engine->post_task(std::forward<decltype(func)>(func));
576 graph_engine->post_task(std::forward<decltype(func)>(func));
583 if constexpr (kHaspriority) {
584 auto post_task_with_priority = [graph_engine, node](
auto&& func) ->
bool {
586 decltype(graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), node->get_priority()));
588 if constexpr (std::is_same_v<Ret, bool>) {
589 return graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), node->get_priority());
591 graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), node->get_priority());
597 if (graph_engine->get_type() == kPriorityType) {
598 posted = post_task_with_priority(std::move(task_func));
600 posted = post_task(std::move(task_func));
603 posted = post_task_with_priority(std::move(task_func));
606 posted = post_task(std::move(task_func));
615 self->mark_ready(
true);
621 task->precede(target_task);
626 task->succeed(target_task);
DAG node carrying a callback, predecessor and successor links, and run-time state.
Definition: graph_task.h:110
void remove_succeed_task(const std::shared_ptr< GraphTask > &task)
Removes an incoming edge created by succeed.
void precede(const std::shared_ptr< GraphTask > &task)
Declares that this node must complete before task starts.
void set_name(const std::string &name)
Sets the node name used in DOT output and status callbacks.
void cancel()
Cancels this node and propagates the cancellation downstream.
void set_priority(uint16_t priority)
Sets the dispatch priority used by priority-aware engines.
static std::shared_ptr< GraphTask > create_condition(const std::string &name, ConditionCallback &&callback, int condition_number=0)
Creates a named condition node.
void set_max_recursion_depth(uint32_t depth)
Sets the maximum recursion depth that bounds DFS traversals.
static std::shared_ptr< GraphTask > create(const std::string &name, Callback &&callback, int condition_number=0)
Creates a named regular work node.
void execute(GraphEngineT *graph_engine)
Submits the reachable sub-graph to graph_engine.
Definition: graph_task.h:541
std::vector< std::weak_ptr< GraphTask > > get_succeed_task_list() const
Returns the current successor list as weak pointers.
static std::shared_ptr< GraphTask > create(Callback &&callback, int condition_number=0)
Creates a regular work node.
Status
Run-time execution state of a node within an execute pass.
Definition: graph_task.h:131
@ kStatusInActive
Not yet submitted or cancelled.
Definition: graph_task.h:132
std::string get_name() const
Returns the node name.
bool unregister_status_callback(uint32_t id)
Removes a previously registered status callback by id.
std::string export_to_dot() const
Exports the reachable sub-graph as a Graphviz DOT document.
void set_policy(Policy policy)
Sets the execution policy for this node.
int get_condition_number() const
Returns the incoming branch value required to activate this node.
Status get_status() const
Returns the current execution status of this node.
static std::shared_ptr< GraphTask > create_condition(ConditionCallback &&callback, int condition_number=0)
Creates a condition node whose return value selects a successor branch.
uint32_t get_max_recursion_depth() const
Returns the maximum recursion depth used for cycle detection.
void set_condition_number(int condition_number)
Sets the incoming branch value required to activate this node.
void remove_precede_task(const std::shared_ptr< GraphTask > &task)
Removes an outgoing edge created by precede.
uint16_t get_priority() const
Returns the configured dispatch priority.
Policy get_policy() const
Returns the configured execution policy.
void clear_status_callbacks()
Removes every status callback subscription on this node.
Policy
Policy controlling how many times the task may run within a pass.
Definition: graph_task.h:141
std::string get_group_name() const
Returns the group name.
uint32_t register_status_callback(StatusCallback &&callback)
Subscribes a callback to status transitions on this node.
std::vector< std::weak_ptr< GraphTask > > get_precede_task_list() const
Returns the current predecessor list as weak pointers.
void set_group_name(const std::string &name)
Sets a group name used to visually cluster nodes in DOT output.
bool has_cycle() const
Detects whether the reachable sub-graph contains a cycle.
bool is_condition_task() const
Reports whether this node was created via create_condition.
void succeed(const std::shared_ptr< GraphTask > &task)
Declares that task must complete before this node starts.
Pool-backed type-erased callables: copyable vlink::Function and move-only vlink::MoveFunction.
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLINK_EXPORT
Definition: macros.h:81
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
std::shared_ptr< GraphTask > GraphTaskPtr
Convenience alias for the canonical shared_ptr<GraphTask> handle.
Definition: graph_task.h:534
Compile-time type-trait helpers used across the VLink codebase.
#define VLINK_HAS_MEMBER(T, member)
Macro Definitions.
Definition: traits.h:325