89 #include <type_traits>
90 #include <unordered_set>
155 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
Callback&& callback,
int condition_number = 0);
165 [[nodiscard]]
static std::shared_ptr<GraphTask>
create(
const std::string& name,
Callback&& callback,
166 int condition_number = 0);
176 int condition_number = 0);
188 int condition_number = 0);
201 template <
class GraphEngineT>
202 void execute(GraphEngineT* graph_engine);
227 void precede(
const std::shared_ptr<GraphTask>& task);
238 void succeed(
const std::shared_ptr<GraphTask>& task);
436 int invoke(
bool once);
440 void notify(
int condition_number);
444 bool mark_predecessor_satisfied(
bool active,
bool* has_active);
446 void update_status(
Status status);
448 bool detect_cycle(
const GraphTask* task, std::unordered_set<const GraphTask*>& visited,
449 std::unordered_set<const GraphTask*>& recursion_stack, uint32_t& depth, uint32_t max_depth)
const;
451 bool reaches_via_successors(
const GraphTask* start_node,
452 const std::vector<std::weak_ptr<GraphTask>>& start_successors,
455 static void clear_invalid_task(
const std::shared_ptr<GraphTask>& task);
458 std::unique_ptr<Impl> impl_;
473 template <
class GraphEngineT>
475 auto self = shared_from_this();
478 constexpr
bool kHaspriority =
VLINK_HAS_MEMBER(GraphEngineT, post_task_with_priority);
479 [[maybe_unused]] constexpr uint8_t kPriorityType = 2;
485 auto task_func = [
self, task]() {
486 if VLIKELY (task.get() !=
self.get()) {
490 int ret = task->invoke(
true);
497 auto post_task = [graph_engine](
auto&& func) ->
bool {
498 using Ret = decltype(graph_engine->post_task(std::forward<decltype(func)>(func)));
500 if constexpr (std::is_same_v<Ret, bool>) {
501 return graph_engine->post_task(std::forward<decltype(func)>(func));
503 graph_engine->post_task(std::forward<decltype(func)>(func));
510 if constexpr (kHaspriority) {
511 auto post_task_with_priority = [graph_engine, task](
auto&& func) ->
bool {
513 decltype(graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), task->get_priority()));
515 if constexpr (std::is_same_v<Ret, bool>) {
516 return graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), task->get_priority());
518 graph_engine->post_task_with_priority(std::forward<decltype(func)>(func), task->get_priority());
524 if (graph_engine->get_type() == kPriorityType) {
525 posted = post_task_with_priority(std::move(task_func));
527 posted = post_task(std::move(task_func));
530 posted = post_task(std::move(task_func));
533 posted = post_task(std::move(task_func));
545 task->precede(target_task);
550 task->succeed(target_task);
DAG node carrying a callback, predecessor and successor links, and run-time state.
Definition: graph_task.h:109
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:474
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:114
@ kStatusInActive
Not yet submitted or cancelled.
Definition: graph_task.h:115
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 configured branch count.
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.
void process_and_traverse(FindTaskCallback &&callback)
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 number of outgoing condition branches.
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.
GraphTask(Callback &&callback, int condition_number)
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:124
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.
GraphTask(const std::string &name, ConditionCallback &&callback, int condition_number)
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.
GraphTask(const std::string &name, Callback &&callback, int condition_number)
void succeed(const std::shared_ptr< GraphTask > &task)
Declares that task must complete before this node starts.
GraphTask(ConditionCallback &&callback, int condition_number)
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:289
#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:467
Compile-time type-trait helpers used across the VLink codebase.
#define VLINK_HAS_MEMBER(T, member)
Macro Definitions
Definition: traits.h:316