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 std::get_if<Node::DependentAsync>(&(_node->_handle))->use_count.fetch_add(
206 1, std::memory_order_relaxed
207 );
208 }
209}
210
211// Function: _decref
212inline void AsyncTask::_decref() {
213 if(_node && std::get_if<Node::DependentAsync>(&(_node->_handle))->use_count.fetch_sub(
214 1, std::memory_order_acq_rel
215 ) == 1) {
216 recycle(_node);
217 }
218}
219
220// Copy Constructor
221inline AsyncTask::AsyncTask(const AsyncTask& rhs) :
222 _node{rhs._node} {
223 _incref();
224}
225
226// Move Constructor
228 _node {rhs._node} {
229 rhs._node = nullptr;
230}
231
232// Destructor
234 _decref();
235}
236
237// Copy assignment
239 _decref();
240 _node = rhs._node;
241 _incref();
242 return *this;
243}
244
245// Move assignment
247 _decref();
248 _node = rhs._node;
249 rhs._node = nullptr;
250 return *this;
251}
252
253// Function: exception
254inline std::exception_ptr AsyncTask::exception_ptr() const {
255 return _node ? _node->_exception_ptr : nullptr;
256}
257
258// Function: has_exception
259inline bool AsyncTask::has_exception_ptr() const {
260 return _node ? (_node->_exception_ptr != nullptr) : false;
261}
262
263// Function: empty
264inline bool AsyncTask::empty() const {
265 return _node == nullptr;
266}
267
268// Function: reset
269inline void AsyncTask::reset() {
270 _decref();
271 _node = nullptr;
272}
273
274// Function: hash_value
275inline size_t AsyncTask::hash_value() const {
276 return std::hash<Node*>{}(_node);
277}
278
279// Function: use_count
280inline size_t AsyncTask::use_count() const {
281 return _node == nullptr ? size_t{0} :
282 std::get_if<Node::DependentAsync>(&(_node->_handle))->use_count.load(
283 std::memory_order_relaxed
284 );
285}
286
287// Function: is_done
288inline bool AsyncTask::is_done() const {
289 return _node == nullptr ? true: (_node->_estate.load(std::memory_order_acquire) & ESTATE::FINISHED);
290}
291
292} // end of namespace tf ----------------------------------------------------
293
294
295
~AsyncTask()
destroys the managed dependent-async task if this is the last owner
Definition async_task.hpp:233
AsyncTask & operator=(const AsyncTask &rhs)
copy-assigns the dependent-async task from rhs
Definition async_task.hpp:238
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:280
void reset()
release the managed object of this
Definition async_task.hpp:269
bool has_exception_ptr() const
queries if the task has an exception pointer
Definition async_task.hpp:259
size_t hash_value() const
obtains the hashed value of this dependent-async task
Definition async_task.hpp:275
std::exception_ptr exception_ptr() const
retrieves the exception pointer of this task
Definition async_task.hpp:254
bool empty() const
checks if this dependent-async task is associated with any task
Definition async_task.hpp:264
bool is_done() const
checks if this dependent-async task finishes
Definition async_task.hpp:288
taskflow namespace
Definition small_vector.hpp:20