34 template<
typename result_t =
void>
37 template<
typename result_t>
49 template<
typename result_t =
void>
50 class basic_task :
public itask,
public xtd::iasync_result {
69 data_->cancellation_token = cancellation_token;
72 data_->parameterized_func =
func;
73 data_->state = &state;
75 basic_task(
const xtd::func<result_t>&
func,
const xtd::any_object& state,
const xtd::threading::cancellation_token& cancellation_token) {
77 data_->state = &state;
78 data_->cancellation_token = cancellation_token;
83 template<
typename create_result_t>
87 template<
typename create_result_t>
90 data_->cancellation_token = cancellation_token;
92 template<
typename create_result_t>
94 data_->parameterized_func =
func;
95 data_->state = &state;
97 template<
typename create_result_t>
99 data_->parameterized_func =
func;
100 data_->state = &state;
101 data_->cancellation_token = cancellation_token;
109 [[nodiscard]]
auto async_state() const noexcept -> xtd::any_object
override {
return data_->async_state;}
110 [[nodiscard]]
auto creation_options() const noexcept -> xtd::threading::tasks::
task_creation_options {
return data_->creation_options;}
116 [[nodiscard]]
auto id() const noexcept -> xtd::
usize override {
return data_->id;}
120 [[nodiscard]]
auto status() const noexcept -> xtd::threading::tasks::
task_status {
return data_->status;}
126 [[nodiscard]]
static auto completed_task() -> task<result_t>;
127 [[nodiscard]]
static auto current_id() noexcept -> xtd::
usize {
return current_id_;}
128 [[nodiscard]]
static auto factory() noexcept -> const xtd::threading::tasks::task_factory&;
134 auto continue_with(xtd::action<> continuation) ->
void {
135 auto call_now =
false;
137 lock_(data_->sync_root) {
138 if (is_completed()) call_now =
true;
139 else data_->continuation = continuation;
142 if (call_now) continuation();
145 [[noreturn]]
auto rethrow_exception() ->
void {
146 if (is_faulted () && data_->exception)
throw *data_->exception.source_exception();
150 auto run_synchronously() ->
void {
152 data_->task_proc(data_->state,
false);
157 auto start() ->
void override {
162 data_->start_event.set();
170 auto wait(
xtd::int32 milliseconds_timeout) ->
bool override {
171 auto cancellation_token = xtd::threading::cancellation_token {};
172 return wait(milliseconds_timeout, cancellation_token);
177 auto wait(
xtd::int32 milliseconds_timeout, xtd::threading::cancellation_token& cancellation_token) ->
bool override {
178 auto& cancellation_token_wait_handle = cancellation_token.wait_handle();
186 auto wait(
const xtd::time_span& timeout) ->
bool override {
return wait(
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
190 auto wait(
const xtd::time_span& timeout, xtd::threading::cancellation_token& cancellation_token) ->
bool override {
return wait(
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()), cancellation_token);}
196 [[nodiscard]]
static auto from_cancelation(
const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
198 template<
typename from_exception_t>
199 [[nodiscard]]
static auto from_exception(from_exception_t exception) -> task<result_t>;
201 [[nodiscard]]
static auto delay(
const xtd::time_span& delay) -> task<>;
202 [[nodiscard]]
static auto delay(
const xtd::time_span& delay,
const xtd::threading::cancellation_token& cancellation_token) -> task<>;
203 [[nodiscard]]
static auto delay(
xtd::int32 milliseconds_delay) -> task<>;
204 [[nodiscard]]
static auto delay(
xtd::int32 milliseconds_delay,
const xtd::threading::cancellation_token& cancellation_token) -> task<>;
207 [[nodiscard]]
static auto run(
const xtd::func<result_t>&
func,
const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
211 template<
typename collection_t>
214 template<
typename collection_t>
215 static auto wait_all(
const collection_t& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
216 auto task_pointers = std::vector<itask*> {};
217 for (
auto& task : tasks)
218 task_pointers.push_back(
const_cast<decltype(&task)
>(&task));
219 return wait_all(array<itask*> {task_pointers}, milliseconds_timeout);
222 template<
typename collection_t>
223 static auto wait_all(
const collection_t& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
225 template<
typename collection_t>
228 template<
typename collection_t>
229 static xtd::usize wait_any(
const collection_t& tasks,
int32 milliseconds_timeout) {
230 auto task_pointers = std::vector<itask*> {};
231 for (
auto& task : tasks)
232 task_pointers.push_back(
const_cast<decltype(&task)
>(&task));
233 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
236 template<
typename collection_t>
237 static xtd::usize wait_any(
const collection_t& tasks,
const time_span& timeout) {
return wait_any(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
241 template<
typename ...items_t>
243 template<
typename ...items_t>
244 static auto wait_all(
const xtd::time_span& timeout, items_t... items) ->
bool {
return wait_all(
xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
245 template<
typename ...items_t>
246 static auto wait_all(
xtd::int32 milliseconds_timeout, items_t... items) ->
bool {
247 auto task_pointers = std::vector<itask*> {};
248 fill_task_pointers(task_pointers, items...);
249 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
251 template<
typename item_t>
252 static auto wait_all(
const std::initializer_list<item_t>& tasks) ->
bool {
return wait_all(tasks,
timeout::infinite);}
253 template<
typename item_t>
254 static auto wait_all(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
255 auto task_pointers = std::vector<itask*> {};
256 for (
auto& task : tasks)
257 task_pointers.push_back(
const_cast<item_t*
>(&task));
258 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
260 template<
typename item_t>
261 static auto wait_all(
const std::initializer_list<item_t>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
264 auto task_pointers = std::vector<itask*> {};
265 for (
auto& task : tasks)
266 task_pointers.push_back(task.get());
267 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
269 static auto wait_all(
const std::initializer_list<
xtd::sptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
272 auto task_pointers = std::vector<itask*> {};
273 for (
auto& task : tasks)
274 task_pointers.push_back(task.get());
275 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
277 static auto wait_all(
const std::initializer_list<
xtd::uptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
280 auto task_pointers = std::vector<itask*> {};
281 for (
auto& task : tasks)
282 task_pointers.push_back(task.get());
283 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
285 static auto wait_all(
const xtd::array<
xtd::sptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
288 auto task_pointers = std::vector<itask*> {};
289 for (
auto& task : tasks)
290 task_pointers.push_back(task.get());
291 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
293 static auto wait_all(
const xtd::array<
xtd::uptr<itask>>& tasks,
const xtd::time_span& timeout) ->
bool {
return wait_all(tasks,
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
294 static auto wait_all(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
298 for (
auto task : tasks)
304 for (
auto& task : tasks)
305 if (sw.elapsed_milliseconds() > milliseconds_timeout || task->wait(milliseconds_timeout -
xtd::as<xtd::int32>(sw.elapsed_milliseconds())))
return false;
309 template<
typename ...items_t>
311 template<
typename ...items_t>
312 static auto wait_any(
const xtd::time_span& timeout, items_t... items) ->
xtd::usize {
return wait_any(
xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
313 template<
typename ...items_t>
315 auto task_pointers = std::vector<itask*> {};
316 fill_task_pointers(task_pointers, items...);
317 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
319 template<
typename item_t>
321 template<
typename item_t>
322 static auto wait_any(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
323 auto task_pointers = std::vector<itask*> {};
324 for (
auto& task : tasks)
325 task_pointers.push_back(
const_cast<item_t*
>(&task));
326 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
328 template<
typename item_t>
329 static auto wait_any(
const std::initializer_list<item_t>& tasks,
const xtd::time_span& timeout) ->
xtd::usize {
return wait_any(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
332 auto task_pointers = std::vector<itask*> {};
333 for (
auto& task : tasks)
334 task_pointers.push_back(task.get());
335 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
340 auto task_pointers = std::vector<itask*> {};
341 for (
auto& task : tasks)
342 task_pointers.push_back(task.get());
343 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
348 auto task_pointers = std::vector<itask*> {};
349 for (
auto& task : tasks)
350 task_pointers.push_back(task.get());
351 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
356 auto task_pointers = std::vector<itask*> {};
357 for (
auto& task : tasks)
358 task_pointers.push_back(task.get());
359 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
362 static auto wait_any(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
365 auto sleep_duration = 1;
366 const auto max_sleep = 10;
370 for (
auto index =
xtd::usize {0}; index < tasks.length(); ++index)
371 if (tasks[index]->wait(0))
return index;
374 sleep_duration = std::min(sleep_duration + 1, max_sleep);
380 static auto yield() -> task<result_t>;
384 template<
typename task_result_t>
386 template<
typename batic_task_result_t>
387 friend class batic_task;
389 basic_task() =
default;
390 basic_task(basic_task&&) =
default;
391 basic_task(
const basic_task&) =
default;
392 auto operator=(basic_task&&) -> basic_task& =
default;
393 auto operator=(
const basic_task&) -> basic_task& =
default;
395 [[nodiscard]]
auto async_wait_handle() noexcept -> xtd::threading::wait_handle&
override {
return data_->async_event;}
396 [[nodiscard]]
auto completed_synchronously() const noexcept ->
bool override {
return false;}
398 template<
typename item_t,
typename ...items_t>
399 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t&
first, items_t& ... rest) ->
void {
400 itask_pointer.push_back(&
first);
401 fill_task_pointers(itask_pointer, rest...);
403 template<
typename item_t>
404 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t& item) ->
void {
405 itask_pointer.push_back(&item);
408 static auto generate_id() noexcept -> xtd::
usize {
414 using result_type = std::conditional_t<std::is_same_v<result_t, void>, std::uint8_t, result_t>;
417 xtd::threading::manual_reset_event async_event;
418 xtd::any_object async_state;
420 xtd::action<> continuation;
422 const xtd::any_object empty_state;
423 xtd::threading::auto_reset_event end_event;
424 xtd::exception_services::exception_dispatch_info exception;
429 const xtd::any_object* state = &empty_state;
430 xtd::threading::auto_reset_event start_event;
435 auto previous_current_id = current_id_;
439 current_id_ = previous_current_id;
441 if (!continuation.is_empty()) continuation();
447 if constexpr(std::is_same_v<result_t, void>) {
449 else if (!parameterized_func.is_empty()) parameterized_func(state);
451 if (!
func.is_empty()) result =
func();
452 else if (!parameterized_func.is_empty()) result = parameterized_func(state);
456 }
catch (
const xtd::threading::tasks::task_canceled_exception& e) {
459 }
catch (
const xtd::exception& e) {
462 }
catch (
const std::exception& e) {
473 inline static thread_local xtd::usize current_id_ = 0;
Contains xtd::action delegate.
Contains xtd::aggregate_exception exception.
Contains xtd::threading::auto_reset_event exception.
static constexpr xtd::usize max_value
Definition box_integer.hpp:71
static auto start_new() noexcept -> xtd::diagnostics::stopwatch
Initializes a new xtd::diagnostics::stopwatch instance, sets the xtd::diagnostics::stopwatch::elapsed...
static auto capture() -> exception_dispatch_info
Creates an xtd::exception_services::exception_dispatch_info object that represents the specified exce...
Definition exception_dispatch_info.hpp:70
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
auto set() -> bool
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
Represents an interface for an asynchronous operation.
Definition itask.hpp:19
Provides support for creating and scheduling Task objects.
Definition task_factory.hpp:19
Represents an asynchronous operation.
Definition task_result.hpp:20
static auto register_wait_for_single_object(wait_handle &wait_object, const wait_or_timer_callback &callback, const xtd::any_object &state, int32 milliseconds_timeout_interval, bool execute_only_once) -> registered_wait_handle
Registers a delegate to wait for a xtd::threading::wait_handle, specifying a 32-bit signed integer fo...
static auto sleep(int32 milliseconds_timeout) -> void
Suspends the current thread for a specified time.
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition timeout.hpp:41
static auto wait_any(const collection_t &wait_handles) -> xtd::usize
Waits for any of the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:198
Contains xtd::exception_services::exception_dispatch_info class.
Contains xtd::func delegate.
xtd::delegate< result_t(arguments_t... arguments)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.hpp:27
xtd::delegate< void(const xtd::any_object &state, bool timed_out)> wait_or_timer_callback
Represents a method to be called when a xtd::threading::wait_handle is signaled or times out.
Definition wait_or_timer_callback.hpp:30
xtd::delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.hpp:40
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ task_canceled
The task is canceled.
Definition exception_case.hpp:103
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define scope_exit_
Nowadays, every C++ developer is familiar with the Resource Acquisition Is Initialization (RAII) tech...
Definition scope_exit.hpp:128
#define delegate_
The declaration of a delegate type is similar to a method signature. It has a return value and any nu...
Definition delegate.hpp:943
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:68
task_status
Represents the current stage in the lifecycle of a xtd::threading::tasks::task.
Definition task_status.hpp:23
task_creation_options
Specifies flags that control optional behavior for the creation and execution of tasks....
Definition task_creation_options.hpp:23
@ waiting_to_run
The task has been scheduled for execution but has not yet begun executing.
Definition task_status.hpp:29
@ running
The task is running but has not yet completed.
Definition task_status.hpp:31
@ ran_to_completion
The task completed execution successfully.
Definition task_status.hpp:35
@ waiting_for_activation
The task is waiting to be activated and scheduled internally by the Switch infrastructure.
Definition task_status.hpp:27
@ faulted
The task completed due to an unhandled exception.
Definition task_status.hpp:39
@ canceled
The task acknowledged cancellation by throwing an OperationCanceledException with its own Cancellatio...
Definition task_status.hpp:37
@ created
The task has been initialized but has not yet been scheduled.
Definition task_status.hpp:25
@ none
Specifies that the default behavior should be used.
Definition task_creation_options.hpp:25
auto yield() noexcept -> bool
Suggests that the implementation reschedule execution of threads.
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
null_ptr null
Represents a null pointer value.
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
xtd::reference_wrapper_object< type_t > ref
The xtd::ref object is a reference wrapper.
Definition ref.hpp:25
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:26
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
auto as< xtd::int32 >(xtd::any value) -> xtd::int32
Casts a type into another type.
Definition __as_int32.hpp:36
sptr< type_t > new_sptr(args_t &&... args)
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
@ start
Starting of a logical operation.
Definition trace_event_type.hpp:37
@ e
The E key.
Definition console_key.hpp:96
Contains xtd::iasync_result interface.
Contains xtd::threading::tasks::itask interface.
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
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
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
constexpr const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
Contains xtd::new_sptr method.
Contains xtd::optional type.
Contains scope_exit_ keyword.
Contains xtd::diagnostics::stopwatch class.
Contains xtd::threading::tasks::task_canceled_exception exception.
Contains xtd::threading::tasks::task_creation_options enum class.
Contains xtd::threading::tasks::task_status enum class.
Contains xtd::threading::thread_pool class.
Contains xtd::threading::lock class.
Contains xtd::helpers::throw_helper class.
Contains xtd::usize type.