xtd 1.0.0
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "basic_task.hpp"
6
8namespace xtd {
10 namespace threading {
12 namespace tasks {
19 template<>
20 class task<void> : public xtd::threading::tasks::basic_task<> {
21 public:
22 struct promise_type {
25 std::coroutine_handle<promise_type> this_;
26
27 auto final_suspend() noexcept {
28 struct final_awaiter {
29 promise_type& promise;
30
31 bool await_ready() noexcept {return false;}
32 void await_resume() noexcept {}
33 void await_suspend(std::coroutine_handle<promise_type> handle) noexcept {
34 if (!promise.task_->is_completed()) promise.task_->start();
35 promise.this_.destroy();
36 }
37 };
38 return final_awaiter {*this};
39 }
40 xtd::threading::tasks::task<> get_return_object() {
42 this_ = std::coroutine_handle<promise_type>::from_promise(*this);
43 return *task_;
44 }
45 std::suspend_never initial_suspend() {return {};}
46 void return_void() {}
47 void unhandled_exception() {exception_ = task_->xtd::threading::tasks::basic_task<>::data_->exception;}
48 };
49
50 struct awaiter {
52
53 bool await_ready() const noexcept {return task_.is_completed();}
54 void await_resume() {
55 task_.on_await_resume();
56 if (task_.is_faulted()) task_.rethrow_exception();
57 }
58 void await_suspend(std::coroutine_handle<> handle) {task_.continue_with_([handle] {handle.resume();});}
59 };
60
62 task();
64
66
68 task(auto action);
70 task(auto action, const xtd::any_object& state);
71 task(auto action, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token);
73
75
77 [[nodiscard]] static auto from_result() -> task {
78 auto t = task {};
79 t.basic_task<>::data_->status = xtd::threading::tasks::task_status::ran_to_completion;
80 t.basic_task<>::data_->end_event.set();
81 return t;
82 }
84
86
88 auto operator co_await() noexcept {return awaiter {*this};}
90 };
91
93 inline task<void>::task() {
94 lock_(this->static_data_.sync_root) {
95 this->static_data_.tasks.add(this->data_->unique_id, new_sptr<task<>>(*this));
96 }
97 }
98 inline task<void>::task(auto action) : basic_task<> {action} {
99 lock_(this->static_data_.sync_root) {
100 this->static_data_.tasks.add(this->data_->unique_id, new_sptr<task<>>(*this));
101 }
102 }
103 inline task<void>::task(auto action, const xtd::threading::cancellation_token& cancellation_token) : basic_task<> {action, cancellation_token} {
104 lock_(this->static_data_.sync_root) {
105 this->static_data_.tasks.add(this->data_->unique_id, new_sptr<task<>>(*this));
106 }
107 }
108 inline task<void>::task(auto action, const xtd::any_object& state) : basic_task<> {action, state} {
109 lock_(this->static_data_.sync_root) {
110 this->static_data_.tasks.add(this->data_->unique_id, new_sptr<task<>>(*this));
111 }
112 }
113 inline task<void>::task(auto action, const xtd::any_object& state, const xtd::threading::cancellation_token& cancellation_token) : basic_task<> {action, state, cancellation_token} {
114 lock_(this->static_data_.sync_root) {
115 this->static_data_.tasks.add(this->data_->unique_id, new_sptr<task<>>(*this));
116 }
117 }
119 }
120 }
121}
122
123#include "task_result.hpp"
Contains xtd::threading::tasks::basic_task <> class.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:30
Represents an exception whose state is captured at a certain point in code.
Definition exception_dispatch_info.hpp:30
Propagates notification that operations should be canceled.
Definition cancellation_token.hpp:39
Represents an asynchronous operation.
Definition task_result.hpp:20
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:68
@ ran_to_completion
The task completed execution successfully.
Definition task_status.hpp:35
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
auto new_sptr(args_t &&... args) -> xtd::sptr< type_t >
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
@ t
The T key.
Definition console_key.hpp:126
The xtd::threading::tasks namespace provides types that simplify the work of writing concurrent and a...
Definition itask.hpp:14
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::threading::tasks::task <result_t> class.