Loading...
Searching...
No Matches
async_task.hpp
1#pragma once
2
3#include "graph.hpp"
4
9
10namespace tf {
11
12// ----------------------------------------------------------------------------
13// AsyncTask
14// ----------------------------------------------------------------------------
15
45class AsyncTask {
46
47 friend class Executor;
48
49 public:
50
54 AsyncTask() = default;
55
59 ~AsyncTask();
60
64 AsyncTask(const AsyncTask& rhs);
65
69 AsyncTask(AsyncTask&& rhs);
70
77 AsyncTask& operator = (const AsyncTask& rhs);
78
85
97 bool empty() const;
98
112 void reset();
113
122 size_t hash_value() const;
123
137 size_t use_count() const;
138
157 bool is_done() const;
158
178 std::exception_ptr exception_ptr() const;
179
185 bool has_exception_ptr() const;
186
187 private:
188
189 explicit AsyncTask(Node*);
190
191 Node* _node {nullptr};
192
193 void _incref();
194 void _decref();
195};
196
197// Constructor
198inline AsyncTask::AsyncTask(Node* ptr) : _node{ptr} {
199 _incref();
200}
201
202// Function: _incref
203inline void AsyncTask::_incref() {
204 if(_node) {
205 // increment the refcount packed in the lower 24 bits of _estate —
206 // no std::get_if needed since we know this node is DependentAsync
207 _node->_estate.fetch_add(ESTATE::REFCOUNT_ONE, std::memory_order_relaxed);
208 }
209}
210
211// Function: _decref
212inline void AsyncTask::_decref() {
213 if(_node) {
214 // decrement the refcount packed in the lower 24 bits of _estate.
215 // mask out the state bits before comparing so we only check the refcount.
216 // if it reaches zero, no owners remain and we recycle the node.
217 if((_node->_estate.fetch_sub(ESTATE::REFCOUNT_ONE, std::memory_order_acq_rel)
218 & ESTATE::REFCOUNT_MASK) == ESTATE::REFCOUNT_ONE) {
219 recycle(_node);
220 }
221 }
222}
223
224// Copy Constructor
225inline AsyncTask::AsyncTask(const AsyncTask& rhs) :
226 _node{rhs._node} {
227 _incref();
228}
229
230// Move Constructor
232 _node {rhs._node} {
233 rhs._node = nullptr;
234}
235
236// Destructor
238 _decref();
239}
240
241// Copy assignment
243 _decref();
244 _node = rhs._node;
245 _incref();
246 return *this;
247}
248
249// Move assignment
251 _decref();
252 _node = rhs._node;
253 rhs._node = nullptr;
254 return *this;
255}
256
257// Function: exception
258inline std::exception_ptr AsyncTask::exception_ptr() const {
259 return _node ? _node->_exception_ptr : nullptr;
260}
261
262// Function: has_exception
263inline bool AsyncTask::has_exception_ptr() const {
264 return _node ? (_node->_exception_ptr != nullptr) : false;
265}
266
267// Function: empty
268inline bool AsyncTask::empty() const {
269 return _node == nullptr;
270}
271
272// Function: reset
273inline void AsyncTask::reset() {
274 _decref();
275 _node = nullptr;
276}
277
278// Function: hash_value
279inline size_t AsyncTask::hash_value() const {
280 return std::hash<Node*>{}(_node);
281}
282
283// Function: use_count
284inline size_t AsyncTask::use_count() const {
285 return _node == nullptr ? size_t{0} :
286 static_cast<size_t>(
287 _node->_estate.load(std::memory_order_relaxed) & ESTATE::REFCOUNT_MASK
288 );
289}
290
291// Function: is_done
292inline bool AsyncTask::is_done() const {
293 return _node == nullptr ? true: (_node->_estate.load(std::memory_order_acquire) & ESTATE::FINISHED);
294}
295
296} // end of namespace tf ----------------------------------------------------
~AsyncTask()
destroys the managed dependent-async task if this is the last owner
Definition async_task.hpp:237
AsyncTask & operator=(const AsyncTask &rhs)
copy-assigns the dependent-async task from rhs
Definition async_task.hpp:242
AsyncTask()=default
constructs an empty task handle
size_t use_count() const
returns the number of shared owners that are currently managing this dependent-async task
Definition async_task.hpp:284
void reset()
release the managed object of this
Definition async_task.hpp:273
bool has_exception_ptr() const
queries if the task has an exception pointer
Definition async_task.hpp:263
size_t hash_value() const
obtains the hashed value of this dependent-async task
Definition async_task.hpp:279
std::exception_ptr exception_ptr() const
retrieves the exception pointer of this task
Definition async_task.hpp:258
bool empty() const
checks if this dependent-async task is associated with any task
Definition async_task.hpp:268
bool is_done() const
checks if this dependent-async task finishes
Definition async_task.hpp:292
taskflow namespace
Definition small_vector.hpp:20