dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
NodeTask.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <concepts>
13
14// Project includes
15#include "Task.hpp"
16#include <dfx-core/Node.hpp>
17
18namespace dfx::Runtime
19{
43template<typename Func>
44requires std::invocable<Func, Core::NodePtr>
45class NodeTask : public Task
46{
47public:
55 NodeTask(Func func, Core::NodeWPtr node, int priority = 0)
56 : Task(priority)
57 , _node { node }
58 , _func { std::move(func) }
59 {}
60
69 void run() override
70 {
71 auto node = _node.lock();
72 if (node == nullptr)
73 return ;
74
75 // Ensure only 1 thread enter the node at the same time
76 std::lock_guard lock(*node);
77 _func(node);
78 }
79
80private:
81 Core::NodeWPtr _node;
82 Func _func;
83};
84
96template<typename Func, typename ... Args>
97auto makeNodeTask(Func && func, Args && ... args)
98{
99 return std::make_unique<NodeTask<std::decay_t<Func>>>(std::forward<Func>(func), std::forward<Args>(args)...);
100}
101} // !namespace dfx::Runtime
Base class for all runtime-executed nodes in a dfx dataflow graph.
void run() override
Execute the callable if the node is still alive, under the node lock.
Definition NodeTask.hpp:69
NodeTask(Func func, Core::NodeWPtr node, int priority=0)
Construct a node-bound task.
Definition NodeTask.hpp:55
int priority() const noexcept
Task scheduling priority.
Definition Task.hpp:67
Task(int priority=0)
Construct a task with a given scheduling priority.
Definition Task.hpp:55
std::weak_ptr< Node > NodeWPtr
Weak pointer type for Nodes.
Definition Node.hpp:63
Definition Node.hpp:47
auto makeNodeTask(Func &&func, Args &&... args)
Helper to create a NodeTask with type decay.
Definition NodeTask.hpp:97
STL namespace.