35 template<
typename result_t =
void>
38 template<
typename result_t>
59 template<
typename result_t>
60 class basic_task :
public xtd::threading::tasks::task_object,
public xtd::threading::tasks::itask,
public xtd::iasync_result {
81 data_->cancellation_token = cancellation_token;
84 data_->parameterized_func =
func;
85 data_->state = &state;
87 basic_task(
const xtd::func<result_t>&
func,
const xtd::any_object& state,
const xtd::threading::cancellation_token& cancellation_token) {
89 data_->state = &state;
90 data_->cancellation_token = cancellation_token;
95 template<
typename create_result_t>
99 template<
typename create_result_t>
102 data_->cancellation_token = cancellation_token;
104 template<
typename create_result_t>
106 data_->parameterized_func =
func;
107 data_->state = &state;
109 template<
typename create_result_t>
111 data_->parameterized_func =
func;
112 data_->state = &state;
113 data_->cancellation_token = cancellation_token;
121 [[nodiscard]]
auto async_state() const noexcept -> xtd::any_object
override {
return data_->async_state;}
122 [[nodiscard]]
auto creation_options() const noexcept -> xtd::threading::tasks::
task_creation_options {
return data_->creation_options;}
128 [[nodiscard]]
auto id() const noexcept -> xtd::
usize override {
return data_->id;}
131 [[nodiscard]]
auto is_empty() const noexcept ->
bool {
return data_->func.is_empty();}
133 [[nodiscard]]
auto status() const noexcept -> xtd::threading::tasks::
task_status {
return data_->status;}
139 [[nodiscard]]
static auto completed_task() -> task<result_t>;
140 [[nodiscard]]
static auto current_id() noexcept -> xtd::
usize {
return current_id_;}
141 [[nodiscard]]
static auto factory() noexcept -> const xtd::threading::tasks::task_factory&;
147 auto continue_with(xtd::action<>&& continuation) -> xtd::threading::tasks::task<>;
148 auto continue_with(xtd::
func<xtd::threading::tasks::task<>>&& continuation_async) -> xtd::threading::tasks::task<xtd::threading::tasks::task<>>;
150 [[noreturn]] auto rethrow_exception() ->
void {
151 if (is_faulted () && data_->exception)
throw *data_->exception.source_exception();
155 auto run_synchronously() ->
void {
158 data_->task_proc(data_->state,
false);
162 auto start() ->
void override {
166 data_->caller_id = ¤t_id_;
169 data_->start_event.set();
177 auto wait(
xtd::int32 milliseconds_timeout) ->
bool override {
178 auto cancellation_token = xtd::threading::cancellation_token {};
179 return wait(milliseconds_timeout, data_->cancellation_token ? *data_->cancellation_token : cancellation_token);
184 auto wait(
xtd::int32 milliseconds_timeout, xtd::threading::cancellation_token& cancellation_token) ->
bool override {
185 auto& cancellation_token_wait_handle = cancellation_token.wait_handle();
194 auto wait(
const xtd::time_span& timeout) ->
bool override {
return wait(
xtd::as<xtd::int32>(timeout.total_milliseconds_duration().count()));}
198 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);}
204 operator bool() const noexcept {
return !
is_empty();}
210 [[nodiscard]]
static auto from_cancelation(
const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
212 template<
typename from_exception_t>
213 [[nodiscard]]
static auto from_exception(from_exception_t exception) -> task<result_t>;
215 [[nodiscard]]
static auto delay(
const xtd::time_span& delay) -> task<>;
216 [[nodiscard]]
static auto delay(
const xtd::time_span& delay,
const xtd::threading::cancellation_token& cancellation_token) -> task<>;
217 [[nodiscard]]
static auto delay(
xtd::int32 milliseconds_delay) -> task<>;
218 [[nodiscard]]
static auto delay(
xtd::int32 milliseconds_delay,
const xtd::threading::cancellation_token& cancellation_token) -> task<>;
221 [[nodiscard]]
static auto run(
const xtd::func<result_t>&
func,
const xtd::threading::cancellation_token& cancellation_token) -> task<result_t>;
225 template<
typename collection_t>
228 template<
typename collection_t>
229 static auto wait_all(
const collection_t& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
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_all(array<itask*> {task_pointers}, milliseconds_timeout);
236 template<
typename collection_t>
237 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()));}
239 template<
typename collection_t>
242 template<
typename collection_t>
243 static xtd::usize wait_any(
const collection_t& tasks,
int32 milliseconds_timeout) {
244 auto task_pointers = std::vector<itask*> {};
245 for (
auto& task : tasks)
246 task_pointers.push_back(
const_cast<decltype(&task)
>(&task));
247 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
250 template<
typename collection_t>
251 static xtd::usize wait_any(
const collection_t& tasks,
const time_span& timeout) {
return wait_any(tasks,
as<int32>(timeout.total_milliseconds_duration().count()));}
253 template<
typename ...items_t>
254 static auto when_all(items_t&&... items) -> task<>;
256 template<
typename ...items_t>
257 static auto when_any(items_t&&... items) -> task<xtd::usize>;
261 template<
typename ...items_t>
263 template<
typename ...items_t>
264 static auto wait_all(
const xtd::time_span& timeout, items_t... items) ->
bool {
return wait_all(
xtd::as<xtd::int32>(timeout.total_milliseconds()), items...);}
265 template<
typename ...items_t>
266 static auto wait_all(
xtd::int32 milliseconds_timeout, items_t... items) ->
bool {
267 auto task_pointers = std::vector<itask*> {};
268 fill_task_pointers(task_pointers, items...);
269 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
271 template<
typename item_t>
272 static auto wait_all(
const std::initializer_list<item_t>& tasks) ->
bool {
return wait_all(tasks,
timeout::infinite);}
273 template<
typename item_t>
274 static auto wait_all(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
275 auto task_pointers = std::vector<itask*> {};
276 for (
auto& task : tasks)
277 task_pointers.push_back(
const_cast<item_t*
>(&task));
278 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
280 template<
typename item_t>
281 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()));}
284 auto task_pointers = std::vector<itask*> {};
285 for (
auto& task : tasks)
286 task_pointers.push_back(task.get());
287 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
289 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()));}
292 auto task_pointers = std::vector<itask*> {};
293 for (
auto& task : tasks)
294 task_pointers.push_back(task.get());
295 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
297 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()));}
300 auto task_pointers = std::vector<itask*> {};
301 for (
auto& task : tasks)
302 task_pointers.push_back(task.get());
303 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
305 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()));}
308 auto task_pointers = std::vector<itask*> {};
309 for (
auto& task : tasks)
310 task_pointers.push_back(task.get());
311 return wait_all(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
313 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()));}
314 static auto wait_all(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
bool {
318 for (
auto task : tasks)
324 for (
auto& task : tasks)
325 if (sw.elapsed_milliseconds() > milliseconds_timeout || task->wait(milliseconds_timeout -
xtd::as<xtd::int32>(sw.elapsed_milliseconds())))
return false;
329 template<
typename ...items_t>
331 template<
typename ...items_t>
332 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...);}
333 template<
typename ...items_t>
335 auto task_pointers = std::vector<itask*> {};
336 fill_task_pointers(task_pointers, items...);
337 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
339 template<
typename item_t>
341 template<
typename item_t>
342 static auto wait_any(
const std::initializer_list<item_t>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
343 auto task_pointers = std::vector<itask*> {};
344 for (
auto& task : tasks)
345 task_pointers.push_back(
const_cast<item_t*
>(&task));
346 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
348 template<
typename item_t>
349 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()));}
352 auto task_pointers = std::vector<itask*> {};
353 for (
auto& task : tasks)
354 task_pointers.push_back(task.get());
355 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
360 auto task_pointers = std::vector<itask*> {};
361 for (
auto& task : tasks)
362 task_pointers.push_back(task.get());
363 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
368 auto task_pointers = std::vector<itask*> {};
369 for (
auto& task : tasks)
370 task_pointers.push_back(task.get());
371 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
376 auto task_pointers = std::vector<itask*> {};
377 for (
auto& task : tasks)
378 task_pointers.push_back(task.get());
379 return wait_any(xtd::array<itask*> {task_pointers}, milliseconds_timeout);
382 static auto wait_any(
const xtd::array<itask*>& tasks,
xtd::int32 milliseconds_timeout) ->
xtd::usize {
385 auto sleep_duration = 1;
386 const auto max_sleep = 10;
390 for (
auto index =
xtd::usize {0}; index < tasks.length(); ++index)
391 if (tasks[index]->wait(0))
return index;
394 sleep_duration = std::min(sleep_duration + 1, max_sleep);
400 static auto yield() -> task<result_t>;
405 auto continue_with_(xtd::action<>&& continuation) ->
void {
406 auto call_now =
false;
408 lock_(data_->sync_root) {
409 if (is_completed()) call_now =
true;
410 else data_->internal_continuation = continuation;
413 if (call_now) continuation();
418 template<
typename task_result_t>
420 template<
typename batic_task_result_t>
421 friend class batic_task;
423 basic_task() =
default;
424 basic_task(basic_task&&) =
default;
425 basic_task(
const basic_task&) =
default;
426 auto operator=(basic_task&&) -> basic_task& =
default;
427 auto operator=(
const basic_task&) -> basic_task& =
default;
429 [[nodiscard]]
auto async_wait_handle() noexcept -> xtd::threading::wait_handle&
override {
return data_->async_event;}
430 [[nodiscard]]
auto completed_synchronously() const noexcept ->
bool override {
return false;}
432 template<
typename item_t,
typename ...items_t>
433 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t&
first, items_t& ... rest) ->
void {
434 itask_pointer.push_back(&
first);
435 fill_task_pointers(itask_pointer, rest...);
437 template<
typename item_t>
438 static auto fill_task_pointers(std::vector<itask*>& itask_pointer, item_t& item) ->
void {
439 itask_pointer.push_back(&item);
442 auto on_await_resume() ->
void {
449 static auto run_continuation(
auto continuation) -> void;
452 using result_type = std::conditional_t<std::is_same_v<result_t, void>, std::uint8_t, result_t>;
455 xtd::threading::manual_reset_event async_event;
456 xtd::any_object async_state;
458 xtd::action<> internal_continuation;
462 const xtd::any_object empty_state;
463 xtd::threading::auto_reset_event end_event;
464 xtd::exception_services::exception_dispatch_info exception;
470 const xtd::any_object* state = &empty_state;
471 xtd::threading::auto_reset_event start_event;
473 xtd::object sync_root;
475 xtd::threading::thread* caller_thread =
null;
479 previous_current_id = current_id_;
484 if (!internal_continuation.is_empty()) internal_continuation();
485 if (continuation) basic_task<result_t>::run_continuation(continuation);
486 else if (continuation_async) basic_task<result_t>::run_continuation(continuation_async);
487 current_id_ = previous_current_id;
488 lock_(static_data_.sync_root) {
489 if (static_data_.tasks.contains_key(unique_id)) static_data_.tasks.remove(unique_id);
496 if constexpr(std::is_same_v<result_t, void>) {
498 else if (!parameterized_func.is_empty()) parameterized_func(state);
500 if (!
func.is_empty()) result =
func();
501 else if (!parameterized_func.is_empty()) result = parameterized_func(state);
505 }
catch (
const xtd::threading::tasks::task_canceled_exception& e) {
508 }
catch (
const xtd::exception& e) {
511 }
catch (
const std::exception& e) {
522 xtd::object sync_root;
523 xtd::collections::generic::dictionary<xtd::usize, sptr<basic_task>> tasks;
527 inline static static_data static_data_;
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:65
exception(const xtd::diagnostics::stack_frame &stack_frame=xtd::diagnostics::stack_frame::current())
Create a new instance of class exception.
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.
Provides support for creating and scheduling Task objects.
Definition task_factory.hpp:22
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 auto current_thread() noexcept -> thread &
Gets the currently running thread.
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::collections::generic::dictionary <key_t, value_t> class.
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(xtd::async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition delegate.hpp:39
@ 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:134
#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:1018
#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
auto new_sptr(args_t &&... args) -> xtd::sptr< type_t >
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.
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
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:245
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:197
constexpr auto is_empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:205
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_object 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.