31 #if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
34 #if (_MSC_VER >= 1900)
37 namespace extensibility {
38 typedef ::std::condition_variable condition_variable_t;
39 typedef ::std::mutex critical_section_t;
40 typedef ::std::unique_lock< ::std::mutex> scoped_critical_section_t;
42 typedef ::Concurrency::event event_t;
43 typedef ::Concurrency::reader_writer_lock reader_writer_lock_t;
44 typedef ::Concurrency::reader_writer_lock::scoped_lock scoped_rw_lock_t;
45 typedef ::Concurrency::reader_writer_lock::scoped_lock_read scoped_read_lock_t;
47 typedef ::Concurrency::details::_ReentrantBlockingLock recursive_lock_t;
48 typedef recursive_lock_t::_Scoped_lock scoped_recursive_lock_t;
51 #endif // _MSC_VER >= 1900
54 #include "pplx/pplx.h"
56 #if defined(__ANDROID__)
58 void cpprest_init(JavaVM*);
63 #if _MSC_FULL_VER < 160040219
64 #error ERROR: Visual Studio 2010 SP1 or later is required to build ppltasks
75 #if defined(__cplusplus_winrt)
79 #include <winapifamily.h>
80 #ifndef _UITHREADCTXT_SUPPORT
85 #include <winapifamily.h>
87 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
89 #define _UITHREADCTXT_SUPPORT 0
90 #elif WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP
92 #define _UITHREADCTXT_SUPPORT 0
94 #define _UITHREADCTXT_SUPPORT 1
99 #define _UITHREADCTXT_SUPPORT 0
104 #if _UITHREADCTXT_SUPPORT
105 #include <uithreadctxt.h>
108 #pragma detect_mismatch("_PPLTASKS_WITH_WINRT", "1")
110 #pragma detect_mismatch("_PPLTASKS_WITH_WINRT", "0")
115 #define _DBG_ONLY(X) X
118 #endif // #ifdef _DEBUG
125 template<
class _E> exception_ptr make_exception_ptr(_E _Except)
127 return copy_exception(_Except);
131 #ifndef _PPLTASK_ASYNC_LOGGING
132 #if _MSC_VER >= 1800 && defined(__cplusplus_winrt)
133 #define _PPLTASK_ASYNC_LOGGING 1 // Only enable async logging under dev12 winrt
135 #define _PPLTASK_ASYNC_LOGGING 0
140 #pragma pack(push,_CRT_PACKING)
142 #if defined(_MSC_VER)
143 #pragma warning(push)
144 #pragma warning(disable: 28197)
145 #pragma warning(disable: 4100) // Unreferenced formal parameter - needed for document generation
146 #pragma warning(disable: 4127) // constant express in if condition - we use it for meta programming
150 #pragma push_macro("new")
176 template <
typename _Type>
class task;
177 template <>
class task<void>;
180 #ifndef PPL_TASK_SAVE_FRAME_COUNT
182 #define PPL_TASK_SAVE_FRAME_COUNT 10
184 #define PPL_TASK_SAVE_FRAME_COUNT 1
196 #if PPL_TASK_SAVE_FRAME_COUNT > 1
197 #if defined(__cplusplus_winrt) && !defined(_DEBUG)
198 #pragma message ("WARNING: Redefinning PPL_TASK_SAVE_FRAME_COUNT under Release build for non-desktop applications is not supported; only one frame will be captured!")
199 #define _CAPTURE_CALLSTACK() ::pplx::details::_TaskCreationCallstack::_CaptureSingleFrameCallstack(_ReturnAddress())
201 #define _CAPTURE_CALLSTACK() ::pplx::details::_TaskCreationCallstack::_CaptureMultiFramesCallstack(PPL_TASK_SAVE_FRAME_COUNT)
204 #define _CAPTURE_CALLSTACK() ::pplx::details::_TaskCreationCallstack::_CaptureSingleFrameCallstack(_ReturnAddress())
232 return ::pplx::details::_TaskCollection_t::_Is_cancellation_requested();
245 inline __declspec(noreturn) void _pplx_cdecl cancel_current_task()
262 void* _M_SingleFrame;
263 std::vector<void *> _M_frames;
267 _M_SingleFrame =
nullptr;
274 _csc._M_SingleFrame = _SingleFrame;
283 _csc._M_frames.resize(_CaptureFrames);
285 _csc._M_frames.resize(::pplx::details::platform::CaptureCallstack(&_csc._M_frames[0], 2, _CaptureFrames));
289 typedef unsigned char _Unit_type;
299 template<
typename _Ty>
308 typedef _Unit_type _Type;
311 template<
typename _T>
314 static const bool _Value =
true;
320 static const bool _Value =
false;
323 template <
typename _Ty>
329 template <
typename _Ty>
335 template <
typename _T>
340 #if defined(__cplusplus_winrt)
341 template <
typename _Type>
344 typedef _Type _Value;
347 template <
typename _Type>
348 struct _Unhat<_Type^>
350 typedef _Type _Value;
353 value struct _NonUserType {
public:
int _Dummy; };
355 template <
typename _Type,
bool _IsValueTypeOrRefType = __is_val
id_winrt_type(_Type)>
356 struct _ValueTypeOrRefType
358 typedef _NonUserType _Value;
361 template <
typename _Type>
362 struct _ValueTypeOrRefType<_Type, true>
364 typedef _Type _Value;
367 template <
typename _T1,
typename _T2>
368 _T2 _ProgressTypeSelector(Windows::Foundation::IAsyncOperationWithProgress<_T1,_T2>^);
370 template <
typename _T1>
371 _T1 _ProgressTypeSelector(Windows::Foundation::IAsyncActionWithProgress<_T1>^);
373 template <
typename _Type>
374 struct _GetProgressType
376 typedef decltype(_ProgressTypeSelector(stdx::declval<_Type>())) _Value;
379 template <typename _Type>
382 static const bool _Value = __is_base_of(Windows::Foundation::IAsyncInfo,
typename _Unhat<_Type>::_Value);
385 template <
typename _T>
386 _TypeSelectorAsyncOperation _AsyncOperationKindSelector(Windows::Foundation::IAsyncOperation<_T>^);
388 _TypeSelectorAsyncAction _AsyncOperationKindSelector(Windows::Foundation::IAsyncAction^);
390 template <
typename _T1,
typename _T2>
391 _TypeSelectorAsyncOperationWithProgress _AsyncOperationKindSelector(Windows::Foundation::IAsyncOperationWithProgress<_T1, _T2>^);
393 template <
typename _T>
394 _TypeSelectorAsyncActionWithProgress _AsyncOperationKindSelector(Windows::Foundation::IAsyncActionWithProgress<_T>^);
396 template <typename _Type, bool _IsAsync = _IsIAsyncInfo<_Type>::_Value>
397 struct _TaskTypeTraits
399 typedef typename _UnwrapTaskType<_Type>::_Type _TaskRetType;
400 typedef decltype(_AsyncOperationKindSelector(stdx::declval<_Type>())) _AsyncKind;
401 typedef typename _NormalizeVoidToUnitType<_TaskRetType>::_Type _NormalizedTaskRetType;
403 static const
bool _IsAsyncTask = _IsAsync;
404 static const
bool _IsUnwrappedTaskOrAsync = _IsUnwrappedAsyncSelector<_AsyncKind>::_Value;
407 template<typename _Type>
408 struct _TaskTypeTraits<_Type, true >
410 typedef decltype(((_Type)
nullptr)->GetResults()) _TaskRetType;
411 typedef _TaskRetType _NormalizedTaskRetType;
412 typedef decltype(_AsyncOperationKindSelector((_Type)
nullptr)) _AsyncKind;
414 static const
bool _IsAsyncTask = true;
415 static const
bool _IsUnwrappedTaskOrAsync = _IsUnwrappedAsyncSelector<_AsyncKind>::_Value;
419 template <
typename _Type>
422 static const bool _Value =
false;
425 template <
typename _Type,
bool _IsAsync = false>
428 typedef typename _UnwrapTaskType<_Type>::_Type _TaskRetType;
429 typedef decltype(_AsyncOperationKindSelector(stdx::declval<_Type>())) _AsyncKind;
430 typedef typename _NormalizeVoidToUnitType<_TaskRetType>::_Type _NormalizedTaskRetType;
432 static const bool _IsAsyncTask =
false;
437 template <
typename _Function>
auto _IsCallable(_Function _Func,
int) -> decltype(_Func(), std::true_type()) { (void)(_Func);
return std::true_type(); }
438 template <
typename _Function> std::false_type _IsCallable(_Function, ...) {
return std::false_type(); }
443 typedef void _TaskRetType;
445 typedef _Unit_type _NormalizedTaskRetType;
447 static const bool _IsAsyncTask =
false;
448 static const bool _IsUnwrappedTaskOrAsync =
false;
451 template<
typename _Type>
454 template<
typename _Func>
459 template <
typename _Function,
typename _Type>
auto _ReturnTypeHelper(_Type t, _Function _Func,
int,
int) -> decltype(_Func(_To_task(t)));
460 template <
typename _Function,
typename _Type>
auto _ReturnTypeHelper(_Type t, _Function _Func,
int, ...) -> decltype(_Func(t));
461 template <
typename _Function,
typename _Type>
auto _ReturnTypeHelper(_Type t, _Function _Func, ...) ->
_BadContinuationParamType;
463 template <
typename _Function,
typename _Type>
auto _IsTaskHelper(_Type t, _Function _Func,
int,
int) -> decltype(_Func(_To_task(t)), std::true_type());
464 template <
typename _Function,
typename _Type> std::false_type _IsTaskHelper(_Type t, _Function _Func,
int, ...);
466 template <
typename _Function>
auto _VoidReturnTypeHelper(_Function _Func,
int,
int) -> decltype(_Func(_To_task_void(_Func)));
467 template <
typename _Function>
auto _VoidReturnTypeHelper(_Function _Func,
int, ...) -> decltype(_Func());
469 template <
typename _Function>
auto _VoidIsTaskHelper(_Function _Func,
int,
int) -> decltype(_Func(_To_task_void(_Func)), std::true_type());
470 template <
typename _Function> std::false_type _VoidIsTaskHelper(_Function _Func,
int, ...);
472 template<
typename _Function,
typename _ExpectedParameterType>
475 typedef decltype(_ReturnTypeHelper(stdx::declval<_ExpectedParameterType>(),stdx::declval<_Function>(), 0, 0)) _FuncRetType;
476 static_assert(!std::is_same<_FuncRetType,_BadContinuationParamType>::value,
"incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below)");
478 typedef decltype(_IsTaskHelper(stdx::declval<_ExpectedParameterType>(),stdx::declval<_Function>(), 0, 0)) _Takes_task;
481 template<
typename _Function>
484 typedef decltype(_VoidReturnTypeHelper(stdx::declval<_Function>(), 0, 0)) _FuncRetType;
485 typedef decltype(_VoidIsTaskHelper(stdx::declval<_Function>(), 0, 0)) _Takes_task;
488 template<
typename _Function,
typename _ReturnType>
509 template <
typename _TaskType,
typename _FuncRetType>
521 static const bool _IsAsyncTask =
false;
522 static const bool _IsUnwrappedTaskOrAsync =
false;
535 static void _pplx_cdecl _Bridge(
void *_PData)
538 _Holder _ThunkHolder(_PThunk);
558 _Holder& operator=(
const _Holder&);
561 std::function<void()> _M_func;
576 static void _ScheduleFuncWithAutoInline(
const std::function<
void ()> & _Func, _TaskInliningMode_t _InliningMode)
578 _TaskCollection_t::_RunTask(&_TaskProcThunk::_Bridge,
new _TaskProcThunk(_Func), _InliningMode);
583 typedef std::function<void(void)> _CallbackFunction;
585 #if defined (__cplusplus_winrt)
605 _M_context._M_captureMethod = _S_captureDeferred;
609 _M_context._M_pContextCallback =
nullptr;
614 void _Resolve(
bool _CaptureCurrent)
616 if(_M_context._M_captureMethod == _S_captureDeferred)
618 _M_context._M_pContextCallback =
nullptr;
622 if (_IsCurrentOriginSTA())
626 #if _UITHREADCTXT_SUPPORT
630 HRESULT _Hr = CaptureUiThreadContext(&_M_context._M_pContextCallback);
633 _M_context._M_pContextCallback =
nullptr;
643 HRESULT _Hr = CoGetObjectContext(IID_IContextCallback, reinterpret_cast<void **>(&_M_context._M_pContextCallback));
646 _M_context._M_pContextCallback =
nullptr;
652 _Assign(_Src._M_context._M_pContextCallback);
657 _M_context._M_pContextCallback = _Src._M_context._M_pContextCallback;
658 _Src._M_context._M_pContextCallback =
nullptr;
666 _Assign(_Src._M_context._M_pContextCallback);
675 _M_context._M_pContextCallback = _Src._M_context._M_pContextCallback;
676 _Src._M_context._M_pContextCallback =
nullptr;
681 bool _HasCapturedContext()
const
683 _ASSERTE(_M_context._M_captureMethod != _S_captureDeferred);
684 return (_M_context._M_pContextCallback !=
nullptr);
687 void _CallInContext(_CallbackFunction _Func)
const
689 if (!_HasCapturedContext())
695 ComCallData callData;
696 ZeroMemory(&callData,
sizeof(callData));
697 callData.pUserDefined =
reinterpret_cast<void *
>(&_Func);
699 HRESULT _Hr = _M_context._M_pContextCallback->ContextCallback(&_Bridge, &callData, IID_ICallbackWithNoReentrancyToApplicationSTA, 5,
nullptr);
702 throw ::Platform::Exception::CreateException(_Hr);
709 return (_M_context._M_pContextCallback == _Rhs._M_context._M_pContextCallback);
714 return !(operator==(_Rhs));
720 if (_M_context._M_captureMethod != _S_captureDeferred && _M_context._M_pContextCallback !=
nullptr)
722 _M_context._M_pContextCallback->Release();
726 void _Assign(IContextCallback *_PContextCallback)
728 _M_context._M_pContextCallback = _PContextCallback;
729 if (_M_context._M_captureMethod != _S_captureDeferred && _M_context._M_pContextCallback !=
nullptr)
731 _M_context._M_pContextCallback->AddRef();
735 static HRESULT __stdcall _Bridge(ComCallData *_PParam)
737 _CallbackFunction *pFunc =
reinterpret_cast<_CallbackFunction *
>(_PParam->pUserDefined);
743 static bool _IsCurrentOriginSTA()
746 APTTYPEQUALIFIER _AptTypeQualifier;
748 HRESULT hr = CoGetApartmentType(&_AptType, &_AptTypeQualifier);
757 case APTTYPE_MAINSTA:
769 IContextCallback *_M_pContextCallback;
770 size_t _M_captureMethod;
773 static const size_t _S_captureDeferred = 1;
804 bool _HasCapturedContext()
const
809 void _Resolve(
bool)
const
813 void _CallInContext(_CallbackFunction _Func)
const
831 template<
typename _Type>
834 void Set(
const _Type& _type)
847 #if defined (__cplusplus_winrt)
849 template<
typename _Type>
852 void Set(_Type^
const & _type)
859 return _M_Result.Get();
864 ::Platform::Agile<_Type^> _M_Result;
870 template<
typename _Type>
871 struct _ResultHolder<std::vector<_Type^>>
873 void Set(
const std::vector<_Type^>& _type)
875 _Result.reserve(_type.size());
877 for (
auto _PTask = _type.begin(); _PTask != _type.end(); ++_PTask)
879 _Result.emplace_back(*_PTask);
883 std::vector<_Type^> Get()
886 std::vector<_Type^> _Return;
887 _Return.reserve(_Result.size());
889 for (
auto _PTask = _Result.begin(); _PTask != _Result.end(); ++_PTask)
891 _Return.push_back(_PTask->Get());
897 std::vector< ::Platform::Agile<_Type^> > _Result;
900 template<
typename _Type>
901 struct _ResultHolder<std::pair<_Type^, void*> >
903 void Set(
const std::pair<_Type^, size_t>& _type)
908 std::pair<_Type^, size_t> Get()
910 return std::make_pair(_M_Result.first.Get(), _M_Result.second);
913 std::pair< ::Platform::Agile<_Type^>,
size_t> _M_Result;
924 void ReportUnhandledError()
926 #if _MSC_VER >= 1800 && defined(__cplusplus_winrt)
927 if (_M_winRTException !=
nullptr)
929 ::Platform::Details::ReportUnhandledError(_M_winRTException);
935 _M_exceptionObserved(0), _M_stdException(_E), _M_stackTrace(_stackTrace)
936 #if defined (__cplusplus_winrt)
937 , _M_winRTException(
nullptr)
942 #if defined (__cplusplus_winrt)
944 _M_exceptionObserved(0), _M_winRTException(_E), _M_stackTrace(_stackTrace)
952 if (_M_exceptionObserved == 0)
957 _REPORT_PPLTASK_UNOBSERVED_EXCEPTION();
961 void _RethrowUserException()
963 if (_M_exceptionObserved == 0)
965 atomic_exchange(_M_exceptionObserved, 1l);
968 #if defined (__cplusplus_winrt)
969 if (_M_winRTException !=
nullptr)
971 throw _M_winRTException;
974 std::rethrow_exception(_M_stdException);
979 atomic_long _M_exceptionObserved;
982 std::exception_ptr _M_stdException;
983 #if defined (__cplusplus_winrt)
984 ::Platform::Exception^ _M_winRTException;
995 #if defined (__cplusplus_winrt)
996 template<
typename _AsyncOperationType,
typename _CompletionHandlerType,
typename _Result>
1000 ref struct _AsyncInfoImpl
abstract : Windows::Foundation::IAsyncOperation<_Result>
1004 ::Platform::Agile<_AsyncOperationType> _M_asyncInfo;
1006 Windows::Foundation::AsyncOperationCompletedHandler<_Result>^ _M_CompletedHandler;
1008 _AsyncInfoImpl( _AsyncOperationType _AsyncInfo ) : _M_asyncInfo(_AsyncInfo) {}
1011 virtual void Cancel() { _M_asyncInfo.Get()->Cancel(); }
1012 virtual void Close() { _M_asyncInfo.Get()->Close(); }
1014 virtual property Windows::Foundation::HResult ErrorCode
1016 Windows::Foundation::HResult
get()
1018 return _M_asyncInfo.Get()->ErrorCode;
1022 virtual property UINT Id
1026 return _M_asyncInfo.Get()->Id;
1030 virtual property Windows::Foundation::AsyncStatus Status
1032 Windows::Foundation::AsyncStatus
get()
1034 return _M_asyncInfo.Get()->Status;
1038 virtual _Result GetResults() {
throw std::runtime_error(
"derived class must implement"); }
1040 virtual property Windows::Foundation::AsyncOperationCompletedHandler<_Result>^ Completed
1042 Windows::Foundation::AsyncOperationCompletedHandler<_Result>^
get()
1044 return _M_CompletedHandler;
1047 void set(Windows::Foundation::AsyncOperationCompletedHandler<_Result>^ value)
1049 _M_CompletedHandler = value;
1050 _M_asyncInfo.Get()->Completed = ref
new _CompletionHandlerType([&](_AsyncOperationType, Windows::Foundation::AsyncStatus status) {
1051 _M_CompletedHandler->Invoke(
this, status);
1060 template<
typename _Result,
typename _Progress>
1061 ref struct _IAsyncOperationWithProgressToAsyncOperationConverter
sealed :
1062 _AsyncInfoImpl<Windows::Foundation::IAsyncOperationWithProgress<_Result,_Progress>^,
1063 Windows::Foundation::AsyncOperationWithProgressCompletedHandler<_Result,_Progress>,
1067 _IAsyncOperationWithProgressToAsyncOperationConverter(Windows::Foundation::IAsyncOperationWithProgress<_Result,_Progress>^ _Operation) :
1068 _AsyncInfoImpl<Windows::Foundation::IAsyncOperationWithProgress<_Result,_Progress>^,
1069 Windows::Foundation::AsyncOperationWithProgressCompletedHandler<_Result,_Progress>,
1070 _Result>(_Operation) {}
1073 virtual _Result GetResults()
override {
return _M_asyncInfo.Get()->GetResults(); }
1079 ref struct _IAsyncActionToAsyncOperationConverter
sealed :
1080 _AsyncInfoImpl<Windows::Foundation::IAsyncAction^,
1081 Windows::Foundation::AsyncActionCompletedHandler,
1082 details::_Unit_type>
1085 _IAsyncActionToAsyncOperationConverter(Windows::Foundation::IAsyncAction^ _Operation) :
1086 _AsyncInfoImpl<Windows::Foundation::IAsyncAction^,
1087 Windows::Foundation::AsyncActionCompletedHandler,
1088 details::_Unit_type>(_Operation) {}
1091 virtual details::_Unit_type GetResults()
override
1094 _M_asyncInfo.Get()->GetResults();
1095 return details::_Unit_type();
1102 template<
typename _Progress>
1103 ref struct _IAsyncActionWithProgressToAsyncOperationConverter
sealed :
1104 _AsyncInfoImpl<Windows::Foundation::IAsyncActionWithProgress<_Progress>^,
1105 Windows::Foundation::AsyncActionWithProgressCompletedHandler<_Progress>,
1106 details::_Unit_type>
1109 _IAsyncActionWithProgressToAsyncOperationConverter(Windows::Foundation::IAsyncActionWithProgress<_Progress>^ _Action) :
1110 _AsyncInfoImpl<Windows::Foundation::IAsyncActionWithProgress<_Progress>^,
1111 Windows::Foundation::AsyncActionWithProgressCompletedHandler<_Progress>,
1112 details::_Unit_type>(_Action) {}
1114 virtual details::_Unit_type GetResults()
override
1117 _M_asyncInfo.Get()->GetResults();
1118 return details::_Unit_type();
1154 #if defined (__cplusplus_winrt)
1162 #if defined (__cplusplus_winrt)
1180 _Arbitrary._Resolve(
false);
1198 static task_continuation_context use_current()
1200 task_continuation_context _Current(
true);
1201 _Current._Resolve(
true);
1208 task_continuation_context(
bool _DeferCapture =
false) : details::_ContextCallback(_DeferCapture)
1218 bool _M_hasPresetCreationCallstack;
1223 _M_hasPresetCreationCallstack =
true;
1224 _M_presetCreationCallstack = _callstack;
1228 _M_hasPresetCreationCallstack =
false;
1250 _M_HasCancellationToken(false),
1251 _M_HasScheduler(false)
1260 _M_CancellationToken(_Token),
1262 _M_HasCancellationToken(true),
1263 _M_HasScheduler(false)
1273 _M_ContinuationContext(_ContinuationContext),
1274 _M_HasCancellationToken(false),
1275 _M_HasScheduler(false)
1284 _M_CancellationToken(_Token),
1285 _M_ContinuationContext(_ContinuationContext),
1286 _M_HasCancellationToken(false),
1287 _M_HasScheduler(false)
1294 template<
typename _SchedType>
1296 : _M_Scheduler(std::move(_Scheduler)),
1299 _M_HasCancellationToken(false),
1300 _M_HasScheduler(true)
1308 : _M_Scheduler(&_Scheduler),
1311 _M_HasCancellationToken(false),
1312 _M_HasScheduler(true)
1320 : _M_Scheduler(std::move(_Scheduler)),
1323 _M_HasCancellationToken(false),
1324 _M_HasScheduler(true)
1332 : _M_Scheduler(_TaskOptions.get_scheduler()),
1333 _M_CancellationToken(_TaskOptions.get_cancellation_token()),
1334 _M_ContinuationContext(_TaskOptions.get_continuation_context()),
1335 _M_HasCancellationToken(_TaskOptions.has_cancellation_token()),
1336 _M_HasScheduler(_TaskOptions.has_scheduler())
1345 _M_CancellationToken = _Token;
1346 _M_HasCancellationToken =
true;
1354 _M_ContinuationContext = _ContinuationContext;
1362 return _M_HasCancellationToken;
1370 return _M_CancellationToken;
1378 return _M_ContinuationContext;
1386 return _M_HasScheduler;
1394 return _M_Scheduler;
1407 bool _M_HasCancellationToken;
1408 bool _M_HasScheduler;
1413 inline _Internal_task_options & _get_internal_task_options(task_options &options)
1415 return options._M_InternalTaskOptions;
1417 inline const _Internal_task_options & _get_internal_task_options(
const task_options &options)
1419 return options._M_InternalTaskOptions;
1422 struct _Task_impl_base;
1425 template<
typename _ReturnType>
1428 typedef std::shared_ptr<_Task_impl<_ReturnType>> _Type;
1433 typedef std::shared_ptr<_Task_impl_base> _Task_ptr_base;
1440 bool _M_isTaskBasedContinuation;
1443 _TaskInliningMode_t _M_inliningMode;
1445 virtual _Task_ptr_base _GetTaskImplBase()
const = 0;
1455 #if _PPLTASK_ASYNC_LOGGING
1457 const ::Platform::Guid _PPLTaskCausalityPlatformID(0x7A76B220, 0xA758, 0x4E6E, 0xB0, 0xE0, 0xD7, 0xC6, 0xD7, 0x4A, 0x88, 0xFE);
1459 __declspec(selectany) volatile
long _isCausalitySupported = 0;
1461 inline
bool _IsCausalitySupported()
1463 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
1464 if (_isCausalitySupported == 0)
1466 long _causality = 1;
1467 OSVERSIONINFOEX _osvi = {};
1468 _osvi.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
1471 _osvi.dwMajorVersion = 6;
1472 _osvi.dwMinorVersion = 3;
1474 DWORDLONG _conditionMask = 0;
1475 VER_SET_CONDITION( _conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL );
1476 VER_SET_CONDITION( _conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL );
1478 if ( ::VerifyVersionInfo(&_osvi, VER_MAJORVERSION | VER_MINORVERSION, _conditionMask))
1483 _isCausalitySupported = _causality;
1484 return _causality == 2;
1487 return _isCausalitySupported == 2 ?
true :
false;
1494 struct _TaskEventLogger
1496 _Task_impl_base *_M_task;
1498 bool _M_taskPostEventStarted;
1501 void _LogScheduleTask(
bool _isContinuation)
1503 if (details::_IsCausalitySupported())
1505 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationCreation(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1506 _PPLTaskCausalityPlatformID, reinterpret_cast<unsigned long long>(_M_task),
1507 _isContinuation ?
"pplx::PPLTask::ScheduleContinuationTask" :
"pplx::PPLTask::ScheduleTask", 0);
1508 _M_scheduled =
true;
1513 void _LogCancelTask()
1515 if (details::_IsCausalitySupported())
1517 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationRelation(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Important, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1518 _PPLTaskCausalityPlatformID, reinterpret_cast<unsigned long long>(_M_task), ::Windows::Foundation::Diagnostics::CausalityRelation::Cancel);
1524 void _LogTaskCompleted();
1527 void _LogTaskExecutionStarted() { }
1530 void _LogTaskExecutionCompleted()
1532 if (_M_taskPostEventStarted && details::_IsCausalitySupported())
1534 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1535 ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::CompletionNotification);
1540 void _LogWorkItemStarted()
1542 if (details::_IsCausalitySupported())
1544 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkStart(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1545 _PPLTaskCausalityPlatformID, reinterpret_cast<unsigned long long>(_M_task), ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::Execution);
1550 void _LogWorkItemCompleted()
1552 if (details::_IsCausalitySupported())
1554 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1555 ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::Execution);
1557 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkStart(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
1558 _PPLTaskCausalityPlatformID, reinterpret_cast<unsigned long long>(_M_task), ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::CompletionNotification);
1559 _M_taskPostEventStarted =
true;
1563 _TaskEventLogger(_Task_impl_base *_task): _M_task(_task)
1565 _M_scheduled =
false;
1566 _M_taskPostEventStarted =
false;
1571 struct _TaskWorkItemRAIILogger
1573 _TaskEventLogger &_M_logger;
1574 _TaskWorkItemRAIILogger(_TaskEventLogger &_taskHandleLogger): _M_logger(_taskHandleLogger)
1576 _M_logger._LogWorkItemStarted();
1579 ~_TaskWorkItemRAIILogger()
1581 _M_logger._LogWorkItemCompleted();
1583 _TaskWorkItemRAIILogger &operator =(
const _TaskWorkItemRAIILogger &);
1587 inline void _LogCancelTask(_Task_impl_base *) {}
1590 void _LogScheduleTask(
bool) {}
1591 void _LogCancelTask() {}
1592 void _LogWorkItemStarted() {}
1593 void _LogWorkItemCompleted() {}
1594 void _LogTaskExecutionStarted() {}
1595 void _LogTaskExecutionCompleted() {}
1596 void _LogTaskCompleted() {}
1620 template<
typename _ReturnType,
typename _DerivedTaskHandle,
typename _BaseTaskHandle>
1623 _PPLTaskHandle(
const typename _Task_ptr<_ReturnType>::_Type & _PTask) : _M_pTask(_PTask)
1630 _M_pTask->_M_taskEventLogger._LogTaskCompleted();
1633 virtual void invoke()
const
1637 _ASSERTE((
bool)_M_pTask);
1638 if (!_M_pTask->_TransitionedToStarted())
1640 static_cast<const _DerivedTaskHandle *
>(
this)->_SyncCancelAndPropagateException();
1644 _M_pTask->_M_taskEventLogger._LogTaskExecutionStarted();
1648 static_cast<const _DerivedTaskHandle *
>(
this)->_Perform();
1652 _M_pTask->_Cancel(
true);
1656 _M_pTask->_Cancel(
true);
1658 #if defined (__cplusplus_winrt)
1659 catch(::Platform::Exception^ _E)
1661 _M_pTask->_CancelWithException(_E);
1666 _M_pTask->_CancelWithException(std::current_exception());
1668 _M_pTask->_M_taskEventLogger._LogTaskExecutionCompleted();
1673 _Task_ptr_base _GetTaskImplBase()
const
1678 typename _Task_ptr<_ReturnType>::_Type _M_pTask;
1690 enum _TaskInternalState
1700 #if defined(_MSC_VER)
1701 #pragma warning(push)
1702 #pragma warning(disable: 4355)
1705 : _M_TaskState(_Created),
1706 _M_fFromAsync(
false), _M_fUnwrappedTask(
false),
1707 _M_pRegistration(
nullptr), _M_Continuations(
nullptr), _M_TaskCollection(_Scheduler_arg),
1708 _M_taskEventLogger(
this)
1711 _M_pTokenState = _PTokenState;
1712 _ASSERTE(_M_pTokenState !=
nullptr);
1713 if (_M_pTokenState != _CancellationTokenState::_None())
1714 _M_pTokenState->_Reference();
1716 #if defined(_MSC_VER)
1717 #pragma warning(pop)
1722 _ASSERTE(_M_pTokenState !=
nullptr);
1723 if (_M_pTokenState != _CancellationTokenState::_None())
1725 _M_pTokenState->_Release();
1731 bool _DoWait =
true;
1733 #if defined (__cplusplus_winrt)
1734 if (_IsNonBlockingThread())
1738 if (!_IsCompleted() && !_IsCanceled())
1761 _M_TaskCollection._Wait();
1778 _M_TaskCollection._RunAndWait();
1791 _ASSERTE(_IsCanceled());
1793 #if defined (__cplusplus_winrt)
1794 catch(::Platform::Exception^ _E)
1797 if(!_HasUserException())
1799 _CancelWithException(_E);
1802 _M_exceptionHolder->_RethrowUserException();
1808 if(!_HasUserException())
1810 _CancelWithException(std::current_exception());
1813 _M_exceptionHolder->_RethrowUserException();
1820 if (_M_fUnwrappedTask)
1822 _M_TaskCollection._Wait();
1827 if (_HasUserException())
1829 _M_exceptionHolder->_RethrowUserException();
1831 else if (_IsCanceled())
1835 _ASSERTE(_IsCompleted());
1858 virtual bool _CancelAndRunContinuations(
bool _SynchronousCancel,
bool _UserException,
bool _PropagatedFromAncestor,
const std::shared_ptr<_ExceptionHolder>& _ExHolder) = 0;
1860 bool _Cancel(
bool _SynchronousCancel)
1863 return _CancelAndRunContinuations(_SynchronousCancel,
false,
false, _M_exceptionHolder);
1866 bool _CancelWithExceptionHolder(
const std::shared_ptr<_ExceptionHolder>& _ExHolder,
bool _PropagatedFromAncestor)
1869 return _CancelAndRunContinuations(
true,
true, _PropagatedFromAncestor, _ExHolder);
1872 #if defined (__cplusplus_winrt)
1873 bool _CancelWithException(::Platform::Exception^ _Exception)
1876 _ASSERTE(!_HasUserException());
1877 return _CancelAndRunContinuations(
true,
true,
false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationCallstack()));
1881 bool _CancelWithException(
const std::exception_ptr& _Exception)
1884 _ASSERTE(!_HasUserException());
1885 return _CancelAndRunContinuations(
true,
true,
false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationCallstack()));
1888 void _RegisterCancellation(std::weak_ptr<_Task_impl_base> _WeakPtr)
1890 _ASSERTE(details::_CancellationTokenState::_IsValid(_M_pTokenState));
1892 auto _CancellationCallback = [_WeakPtr](){
1895 auto _task = _WeakPtr.lock();
1896 if (_task !=
nullptr)
1897 _task->_Cancel(
false);
1901 _M_pTokenState->_RegisterCallback(_M_pRegistration);
1904 void _DeregisterCancellation()
1906 if (_M_pRegistration !=
nullptr)
1908 _M_pTokenState->_DeregisterCallback(_M_pRegistration);
1909 _M_pRegistration->_Release();
1910 _M_pRegistration =
nullptr;
1916 return (_M_TaskState == _Created);
1921 return (_M_TaskState == _Started);
1924 bool _IsPendingCancel()
1926 return (_M_TaskState == _PendingCancel);
1931 return (_M_TaskState == _Completed);
1936 return (_M_TaskState == _Canceled);
1939 bool _HasUserException()
1941 return static_cast<bool>(_M_exceptionHolder);
1944 const std::shared_ptr<_ExceptionHolder>& _GetExceptionHolder()
1946 _ASSERTE(_HasUserException());
1947 return _M_exceptionHolder;
1950 bool _IsApartmentAware()
1952 return _M_fFromAsync;
1955 void _SetAsync(
bool _Async =
true)
1957 _M_fFromAsync = _Async;
1962 return _M_pTaskCreationCallstack;
1967 _M_pTaskCreationCallstack = _Callstack;
1979 void _ScheduleTask(_UnrealizedChore_t * _PTaskHandle, _TaskInliningMode_t _InliningMode)
1983 _M_TaskCollection._ScheduleTask(_PTaskHandle, _InliningMode);
1991 _ASSERTE(_IsCanceled());
2006 if (!_HasUserException())
2008 _CancelWithException(std::current_exception());
2023 _Task_ptr_base _ImplBase = _PTaskHandle->_GetTaskImplBase();
2024 if (_IsCanceled() && !_PTaskHandle->_M_isTaskBasedContinuation)
2026 if (_HasUserException())
2030 _ImplBase->_CancelWithExceptionHolder(_GetExceptionHolder(),
true);
2036 _ImplBase->_Cancel(
true);
2043 _ASSERTE(_IsCompleted() || _PTaskHandle->_M_isTaskBasedContinuation);
2044 _ASSERTE(!_ImplBase->_IsCanceled());
2045 return _ImplBase->_ScheduleContinuationTask(_PTaskHandle);
2049 delete _PTaskHandle;
2056 _M_taskEventLogger._LogScheduleTask(
true);
2058 if (_PTaskHandle->_M_continuationContext._HasCapturedContext())
2064 if (_PTaskHandle->_M_inliningMode != details::_ForceInline)
2066 _PTaskHandle->_M_inliningMode = details::_DefaultAutoInline;
2068 _ScheduleFuncWithAutoInline([_PTaskHandle]() {
2071 auto _TaskImplPtr = _PTaskHandle->_GetTaskImplBase();
2072 if (details::_ContextCallback::_CaptureCurrent() == _PTaskHandle->_M_continuationContext)
2074 _TaskImplPtr->_ScheduleTask(_PTaskHandle, details::_ForceInline);
2089 auto _PTaskHandle1 = _PTaskHandle;
2090 _PTaskHandle->_M_continuationContext._CallInContext( [_PTaskHandle1, _TaskImplPtr](){
2091 _TaskImplPtr->_ScheduleTask(_PTaskHandle1, details::_ForceInline);
2094 #if defined (__cplusplus_winrt)
2095 catch(::Platform::Exception^ _E)
2097 _TaskImplPtr->_CancelWithException(_E);
2102 _TaskImplPtr->_CancelWithException(std::current_exception());
2105 }, _PTaskHandle->_M_inliningMode);
2109 _ScheduleTask(_PTaskHandle, _PTaskHandle->_M_inliningMode);
2126 enum { _Nothing, _Schedule, _Cancel, _CancelWithException } _Do = _Nothing;
2132 if (_IsCompleted() || (_IsCanceled() && _PTaskHandle->_M_isTaskBasedContinuation))
2136 else if (_IsCanceled())
2138 if (_HasUserException())
2140 _Do = _CancelWithException;
2150 _PTaskHandle->_M_next = _M_Continuations;
2151 _M_Continuations = _PTaskHandle;
2161 _PTaskHandle->_GetTaskImplBase()->_ScheduleContinuationTask(_PTaskHandle);
2168 _PTaskHandle->_GetTaskImplBase()->_Cancel(
true);
2170 delete _PTaskHandle;
2173 case _CancelWithException:
2177 _PTaskHandle->_GetTaskImplBase()->_CancelWithExceptionHolder(_GetExceptionHolder(),
true);
2179 delete _PTaskHandle;
2190 void _RunTaskContinuations()
2194 _ContinuationList _Cur = _M_Continuations, _Next;
2195 _M_Continuations =
nullptr;
2200 _Next = _Cur->_M_next;
2201 _RunContinuation(_Cur);
2206 #if defined (__cplusplus_winrt)
2207 static bool _IsNonBlockingThread()
2210 APTTYPEQUALIFIER _AptTypeQualifier;
2212 HRESULT hr = CoGetApartmentType(&_AptType, &_AptTypeQualifier);
2221 case APTTYPE_MAINSTA:
2225 switch(_AptTypeQualifier)
2231 case APTTYPEQUALIFIER_NA_ON_STA:
2232 case APTTYPEQUALIFIER_NA_ON_MAINSTA:
2240 #if _UITHREADCTXT_SUPPORT
2243 if (SUCCEEDED(CaptureUiThreadContext(
nullptr)))
2252 template<
typename _ReturnType,
typename>
2253 static void _AsyncInit(
const typename _Task_ptr<_ReturnType>::_Type & _OuterTask,
2254 Windows::Foundation::IAsyncOperation<
typename details::_ValueTypeOrRefType<_ReturnType>::_Value>^ _AsyncOp)
2268 _AsyncOp->Completed = ref
new Windows::Foundation::AsyncOperationCompletedHandler<_ReturnType>(
2269 [_OuterTask](Windows::Foundation::IAsyncOperation<typename details::_ValueTypeOrRefType<_ReturnType>::_Value>^ _Operation, Windows::Foundation::AsyncStatus _Status)
mutable
2271 if (_Status == Windows::Foundation::AsyncStatus::Canceled)
2273 _OuterTask->_Cancel(
true);
2275 else if (_Status == Windows::Foundation::AsyncStatus::Error)
2277 _OuterTask->_CancelWithException(::Platform::Exception::ReCreateException(static_cast<int>(_Operation->ErrorCode.Value)));
2281 _ASSERTE(_Status == Windows::Foundation::AsyncStatus::Completed);
2282 _OuterTask->_FinalizeAndRunContinuations(_Operation->GetResults());
2290 const_cast<_Task_ptr<_ReturnType>::_Type &
>(_OuterTask).reset();
2292 _OuterTask->_SetUnwrappedAsyncOp(_AsyncOp);
2296 template<
typename _ReturnType,
typename _InternalReturnType>
2297 static void _AsyncInit(
const typename _Task_ptr<_ReturnType>::_Type& _OuterTask,
const task<_InternalReturnType> & _UnwrappedTask)
2299 _ASSERTE(_OuterTask->_M_fUnwrappedTask && !_OuterTask->_IsCanceled());
2308 _UnwrappedTask._Then([_OuterTask] (task<_InternalReturnType> _AncestorTask) {
2310 if (_AncestorTask._GetImpl()->_IsCompleted())
2312 _OuterTask->_FinalizeAndRunContinuations(_AncestorTask._GetImpl()->_GetResult());
2316 _ASSERTE(_AncestorTask._GetImpl()->_IsCanceled());
2317 if (_AncestorTask._GetImpl()->_HasUserException())
2321 _OuterTask->_CancelWithExceptionHolder(_AncestorTask._GetImpl()->_GetExceptionHolder(),
false);
2325 _OuterTask->_Cancel(
true);
2328 },
nullptr, details::_DefaultAutoInline);
2332 scheduler_ptr _GetScheduler()
const
2334 return _M_TaskCollection._GetScheduler();
2338 volatile _TaskInternalState _M_TaskState;
2343 bool _M_fUnwrappedTask;
2348 std::shared_ptr<_ExceptionHolder> _M_exceptionHolder;
2350 ::pplx::extensibility::critical_section_t _M_ContinuationsCritSec;
2353 _CancellationTokenState * _M_pTokenState;
2356 _CancellationTokenRegistration * _M_pRegistration;
2358 typedef _ContinuationTaskHandleBase * _ContinuationList;
2359 _ContinuationList _M_Continuations;
2365 _TaskCreationCallstack _M_pTaskCreationCallstack;
2367 _TaskEventLogger _M_taskEventLogger;
2370 _Task_impl_base(
const _Task_impl_base&);
2371 _Task_impl_base
const & operator=(_Task_impl_base
const&);
2374 #if _PPLTASK_ASYNC_LOGGING
2375 inline void _TaskEventLogger::_LogTaskCompleted()
2379 ::Windows::Foundation::AsyncStatus _State;
2380 if (_M_task->_IsCompleted())
2381 _State = ::Windows::Foundation::AsyncStatus::Completed;
2382 else if (_M_task->_HasUserException())
2383 _State = ::Windows::Foundation::AsyncStatus::Error;
2385 _State = ::Windows::Foundation::AsyncStatus::Canceled;
2387 if (details::_IsCausalitySupported())
2389 ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library,
2390 _PPLTaskCausalityPlatformID, reinterpret_cast<unsigned long long>(_M_task), _State);
2405 template<
typename _ReturnType>
2406 struct _Task_impl :
public _Task_impl_base
2408 #if defined (__cplusplus_winrt)
2409 typedef Windows::Foundation::IAsyncOperation<typename details::_ValueTypeOrRefType<_ReturnType>::_Value> _AsyncOperationType;
2410 #endif // defined(__cplusplus_winrt)
2411 _Task_impl(_CancellationTokenState * _Ct, scheduler_ptr _Scheduler_arg)
2412 : _Task_impl_base(_Ct, _Scheduler_arg)
2414 #if defined (__cplusplus_winrt)
2415 _M_unwrapped_async_op =
nullptr;
2419 virtual ~_Task_impl()
2423 _DeregisterCancellation();
2426 virtual bool _CancelAndRunContinuations(
bool _SynchronousCancel,
bool _UserException,
bool _PropagatedFromAncestor,
const std::shared_ptr<_ExceptionHolder> & _ExceptionHolder_arg)
2428 bool _RunContinuations =
false;
2433 _ASSERTE(_SynchronousCancel && !_IsCompleted());
2435 _ASSERTE(!_IsCanceled() || _PropagatedFromAncestor);
2438 _ASSERTE(!_HasUserException());
2441 (void)_PropagatedFromAncestor;
2443 if (_M_TaskState == _Canceled)
2450 _ASSERTE(_M_TaskState != _Completed);
2451 _M_exceptionHolder = _ExceptionHolder_arg;
2458 if (_IsCompleted() || _IsCanceled() || (_IsPendingCancel() && !_SynchronousCancel))
2460 _ASSERTE(!_IsCompleted() || !_HasUserException());
2463 _ASSERTE(!_SynchronousCancel || !_HasUserException());
2466 if (_SynchronousCancel)
2469 _M_TaskState = _Canceled;
2473 _RunContinuations =
true;
2477 _ASSERTE(!_UserException);
2481 #if defined (__cplusplus_winrt)
2482 if (_M_unwrapped_async_op !=
nullptr)
2485 _M_unwrapped_async_op->Cancel();
2488 _M_TaskCollection._Cancel();
2494 _M_TaskState = _PendingCancel;
2496 _M_taskEventLogger._LogCancelTask();
2503 if (_RunContinuations)
2505 _M_TaskCollection._Complete();
2507 if (_M_Continuations)
2510 _ScheduleFuncWithAutoInline([=](){ _RunTaskContinuations(); }, details::_DefaultAutoInline);
2516 void _FinalizeAndRunContinuations(_ReturnType _Result)
2518 _M_Result.Set(_Result);
2529 _ASSERTE(!_HasUserException() && !_IsCompleted());
2536 _M_TaskState = _Completed;
2538 _M_TaskCollection._Complete();
2539 _RunTaskContinuations();
2545 bool _TransitionedToStarted()
2549 _ASSERTE(!_IsCanceled());
2550 if (_IsPendingCancel())
2553 _ASSERTE(_IsCreated());
2554 _M_TaskState = _Started;
2558 #if defined (__cplusplus_winrt)
2559 void _SetUnwrappedAsyncOp(_AsyncOperationType^ _AsyncOp)
2563 if (_IsPendingCancel())
2565 _ASSERTE(!_IsCanceled());
2570 _M_unwrapped_async_op = _AsyncOp;
2578 return _IsCompleted() || _IsCanceled();
2581 _ReturnType _GetResult()
2583 return _M_Result.Get();
2586 _ResultHolder<_ReturnType> _M_Result;
2587 #if defined (__cplusplus_winrt)
2588 _AsyncOperationType^ _M_unwrapped_async_op;
2592 template<
typename _ResultType>
2601 typedef std::vector<typename _Task_ptr<_ResultType>::_Type> _TaskList;
2604 _M_fHasValue(
false), _M_fIsCanceled(
false)
2608 bool _HasUserException()
2610 return _M_exceptionHolder !=
nullptr;
2615 for(
auto _TaskIt = _M_tasks.begin(); _TaskIt != _M_tasks.end(); ++_TaskIt )
2617 _ASSERTE(!_M_fHasValue && !_M_fIsCanceled);
2619 (*_TaskIt)->_Cancel(
true);
2625 ::pplx::extensibility::critical_section_t _M_taskListCritSec;
2627 std::shared_ptr<_ExceptionHolder> _M_exceptionHolder;
2629 bool _M_fIsCanceled;
2633 inline std::function<_Unit_type(void)> _MakeVoidToUnitFunc(
const std::function<
void(
void)>& _Func)
2635 return [=]() -> _Unit_type { _Func();
return _Unit_type(); };
2638 template <
typename _Type>
2639 std::function<_Type(_Unit_type)> _MakeUnitToTFunc(
const std::function<_Type(
void)>& _Func)
2641 return [=](_Unit_type) -> _Type {
return _Func(); };
2644 template <
typename _Type>
2645 std::function<_Unit_type(_Type)> _MakeTToUnitFunc(
const std::function<
void(_Type)>& _Func)
2647 return [=](_Type t) -> _Unit_type { _Func(t);
return _Unit_type(); };
2650 inline std::function<_Unit_type(_Unit_type)> _MakeUnitToUnitFunc(
const std::function<
void(
void)>& _Func)
2652 return [=](_Unit_type) -> _Unit_type { _Func();
return _Unit_type(); };
2673 template<
typename _ResultType>
2682 : _M_Impl(std::make_shared<details::_Task_completion_event_impl<_ResultType>>())
2702 bool set(_ResultType _Result)
const
2711 bool _RunContinuations =
false;
2715 if (!_IsTriggered())
2717 _M_Impl->_M_value.Set(_Result);
2718 _M_Impl->_M_fHasValue =
true;
2720 _Tasks.swap(_M_Impl->_M_tasks);
2721 _RunContinuations =
true;
2725 if (_RunContinuations)
2727 for(
auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2730 if ((*_TaskIt)->_IsPendingCancel())
2731 (*_TaskIt)->_Cancel(
true);
2737 (*_TaskIt)->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2740 if (_M_Impl->_HasUserException())
2742 _M_Impl->_M_exceptionHolder.reset();
2750 template<
typename _E>
2752 bool set_exception(_E _Except) const
2755 return _Cancel(std::make_exception_ptr(_Except), _CAPTURE_CALLSTACK());
2766 bool set_exception(std::exception_ptr _ExceptionPtr) const
2769 return _Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK());
2779 return _CancelInternal();
2786 template<
typename _ExHolderType>
2790 if(_StoreException(_ExHolder, _SetExceptionAddressHint))
2792 _Canceled = _CancelInternal();
2793 _ASSERTE(_Canceled);
2807 template<
typename _ExHolderType>
2811 if (!_IsTriggered() && !_M_Impl->_HasUserException())
2815 _M_Impl->_M_exceptionHolder = _ToExceptionHolder(_ExHolder, _SetExceptionAddressHint);
2826 return _M_Impl->_M_fHasValue || _M_Impl->_M_fIsCanceled;
2831 static std::shared_ptr<details::_ExceptionHolder> _ToExceptionHolder(
const std::shared_ptr<details::_ExceptionHolder>& _ExHolder,
const details::_TaskCreationCallstack&)
2836 static std::shared_ptr<details::_ExceptionHolder> _ToExceptionHolder(std::exception_ptr _ExceptionPtr,
const details::_TaskCreationCallstack &_SetExceptionAddressHint)
2838 return std::make_shared<details::_ExceptionHolder>(_ExceptionPtr, _SetExceptionAddressHint);
2842 template <
typename T>
friend class task;
2843 template <
typename T>
friend class task_completion_event;
2845 typedef typename details::_Task_completion_event_impl<_ResultType>::_TaskList _TaskList;
2850 bool _CancelInternal()
const
2854 _ASSERTE(!_M_Impl->_M_fHasValue);
2855 if (_M_Impl->_M_fIsCanceled)
2861 bool _Cancel =
false;
2864 _ASSERTE(!_M_Impl->_M_fHasValue);
2865 if (!_M_Impl->_M_fIsCanceled)
2867 _M_Impl->_M_fIsCanceled =
true;
2868 _Tasks.swap(_M_Impl->_M_tasks);
2873 bool _UserException = _M_Impl->_HasUserException();
2877 for(
auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2882 (*_TaskIt)->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder,
true);
2886 (*_TaskIt)->_Cancel(
true);
2897 void _RegisterTask(
const typename details::_Task_ptr<_ResultType>::_Type & _TaskParam)
2902 if(_M_Impl->_HasUserException())
2904 _TaskParam->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder,
true);
2906 else if (_M_Impl->_M_fHasValue)
2908 _TaskParam->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2912 _M_Impl->_M_tasks.push_back(_TaskParam);
2916 std::shared_ptr<details::_Task_completion_event_impl<_ResultType>> _M_Impl;
2952 return _M_unitEvent.set(details::_Unit_type());
2955 template<
typename _E>
2957 bool set_exception(_E _Except) const
2959 return _M_unitEvent._Cancel(std::make_exception_ptr(_Except), _CAPTURE_CALLSTACK());
2970 bool set_exception(std::exception_ptr _ExceptionPtr) const
2973 return _M_unitEvent._Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK());
2982 _M_unitEvent._Cancel();
2989 void _Cancel(
const std::shared_ptr<details::_ExceptionHolder>& _ExHolder)
const
2991 _M_unitEvent._Cancel(_ExHolder);
3001 return _M_unitEvent._StoreException(_ExHolder);
3009 return _M_unitEvent._IsTriggered();
3013 template <
typename T>
friend class task;
3019 void _RegisterTask(details::_Task_ptr<details::_Unit_type>::_Type _TaskParam)
3021 _M_unitEvent._RegisterTask(_TaskParam);
3025 task_completion_event<details::_Unit_type> _M_unitEvent;
3037 template<
typename _ReturnType,
typename _Ty>
3038 auto _IsValidTaskCtor(_Ty _Param,
int,
int,
int,
int) -> decltype(_Param(), std::true_type());
3040 #if defined (__cplusplus_winrt)
3042 template<
typename _ReturnType,
typename _Ty>
3043 auto _IsValidTaskCtor(_Ty _Param,
int,
int,
int,...) -> decltype(_Param->GetResults(), std::true_type());
3047 template<
typename _ReturnType,
typename _Ty>
3048 auto _IsValidTaskCtor(_Ty _Param,
int,
int, ...) -> decltype(_Param.set(stdx::declval<_ReturnType>()), std::true_type());
3050 template<
typename _ReturnType,
typename _Ty>
3051 auto _IsValidTaskCtor(_Ty _Param,
int, ...) -> decltype(_Param.set(), std::true_type());
3054 template<
typename _ReturnType,
typename _Ty>
3055 std::false_type _IsValidTaskCtor(_Ty _Param, ...);
3057 template<
typename _ReturnType,
typename _Ty>
3058 void _ValidateTaskConstructorArgs(_Ty _Param)
3060 static_assert(std::is_same<decltype(_IsValidTaskCtor<_ReturnType>(_Param,0,0,0,0)),std::true_type>::value,
3061 #
if defined (__cplusplus_winrt)
3062 "incorrect argument for task constructor; can be a callable object, an asynchronous operation, or a task_completion_event"
3064 "incorrect argument for task constructor; can be a callable object or a task_completion_event"
3067 #if defined (__cplusplus_winrt)
3068 static_assert(!(std::is_same<_Ty,_ReturnType>::value && details::_IsIAsyncInfo<_Ty>::_Value),
3069 "incorrect template argument for task; consider using the return type of the async operation");
3073 #if defined (__cplusplus_winrt)
3077 template<
typename _Ty>
3078 static auto _IsValidCreateAsync(_Ty _Param,
int,
int,
int,
int) -> decltype(_Param(), std::true_type());
3081 template<
typename _Ty>
3082 static auto _IsValidCreateAsync(_Ty _Param,
int,
int,
int, ...) -> decltype(_Param(
cancellation_token::none()), std::true_type());
3085 template<
typename _Ty>
3086 static auto _IsValidCreateAsync(_Ty _Param,
int,
int, ...) -> decltype(_Param(details::_ProgressReporterCtorArgType()), std::true_type());
3089 template<
typename _Ty>
3090 static auto _IsValidCreateAsync(_Ty _Param,
int, ...) -> decltype(_Param(details::_ProgressReporterCtorArgType(),
cancellation_token::none()), std::true_type());
3093 template<
typename _Ty>
3094 static std::false_type _IsValidCreateAsync(_Ty _Param, ...);
3101 template<
typename _InpType,
typename _OutType>
3105 static auto _Perform(std::function<_OutType(_InpType)> _Func) -> decltype(_Func)
3111 template<
typename _OutType>
3115 static auto _Perform(std::function<_OutType(
void)> _Func) -> decltype(details::_MakeUnitToTFunc<_OutType>(_Func))
3117 return details::_MakeUnitToTFunc<_OutType>(_Func);
3121 template<
typename _InType>
3125 static auto _Perform(std::function<
void(_InType)> _Func) -> decltype(details::_MakeTToUnitFunc<_InType>(_Func))
3127 return details::_MakeTToUnitFunc<_InType>(_Func);
3135 static auto _Perform(std::function<
void(
void)> _Func) -> decltype(details::_MakeUnitToUnitFunc(_Func))
3137 return details::_MakeUnitToUnitFunc(_Func);
3143 template<
typename _RetType>
3147 static auto _Perform(std::function<_RetType(
void)> _Func) -> decltype(_Func)
3157 static auto _Perform(std::function<
void(
void)> _Func) -> decltype(details::_MakeVoidToUnitFunc(_Func))
3159 return details::_MakeVoidToUnitFunc(_Func);
3177 template<
typename _ReturnType>
3250 template<
typename _Ty>
3252 explicit
task(_Ty _Param)
3255 details::_ValidateTaskConstructorArgs<_ReturnType,_Ty>(_Param);
3259 _SetTaskCreationCallstack(_CAPTURE_CALLSTACK());
3261 _TaskInitMaybeFunctor(_Param, details::_IsCallable(_Param,0));
3298 template<
typename _Ty>
3302 details::_ValidateTaskConstructorArgs<_ReturnType,_Ty>(_Param);
3304 _CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler());
3306 _SetTaskCreationCallstack(details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : _CAPTURE_CALLSTACK());
3308 _TaskInitMaybeFunctor(_Param, details::_IsCallable(_Param,0));
3363 task(
task&& _Other): _M_Impl(std::move(_Other._M_Impl)) {}
3378 if (
this != &_Other)
3380 _M_Impl = _Other._M_Impl;
3398 if (
this != &_Other)
3400 _M_Impl = std::move(_Other._M_Impl);
3425 template<
typename _Function>
3427 auto then(const _Function& _Func) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType
3430 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
3431 return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions);
3458 template<
typename _Function>
3460 auto then(const _Function& _Func,
task_options _TaskOptions) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType
3462 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
3463 return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions);
3494 template<
typename _Function>
3496 auto then(const _Function& _Func, cancellation_token _CancellationToken, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType
3498 task_options _TaskOptions(_CancellationToken, _ContinuationContext);
3499 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
3500 return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions);
3516 throw invalid_operation(
"wait() cannot be called on a default constructed task.");
3519 return _M_Impl->_Wait();
3534 _ReturnType
get()
const
3538 throw invalid_operation(
"get() cannot be called on a default constructed task.");
3546 return _M_Impl->_GetResult();
3562 throw invalid_operation(
"is_done() cannot be called on a default constructed task.");
3565 return _M_Impl->_IsDone();
3578 throw invalid_operation(
"scheduler() cannot be called on a default constructed task.");
3581 return _M_Impl->_GetScheduler();
3595 throw invalid_operation(
"is_apartment_aware() cannot be called on a default constructed task.");
3597 return _M_Impl->_IsApartmentAware();
3609 return (_M_Impl == _Rhs._M_Impl);
3621 return !operator==(_Rhs);
3629 _ASSERTE(_Ct !=
nullptr);
3631 if (_Ct != details::_CancellationTokenState::_None())
3633 _M_Impl->_RegisterCancellation(_M_Impl);
3640 const typename details::_Task_ptr<_ReturnType>::_Type &
_GetImpl()
const
3648 void _SetImpl(
const typename details::_Task_ptr<_ReturnType>::_Type & _Impl)
3657 void _SetImpl(
typename details::_Task_ptr<_ReturnType>::_Type && _Impl)
3660 _M_Impl = std::move(_Impl);
3668 _GetImpl()->_SetAsync(_Async);
3676 _GetImpl()->_SetTaskCreationCallstack(_callstack);
3684 template<
typename _Function>
3686 details::_TaskInliningMode_t _InliningMode = details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType
3689 auto _Scheduler = _GetImpl()->_GetScheduler();
3695 template <
typename T>
friend class task;
3699 template <
typename _InternalReturnType,
typename _Function,
typename _TypeSelection>
3700 struct _InitialTaskHandle :
3701 details::_PPLTaskHandle<_ReturnType, _InitialTaskHandle<_InternalReturnType, _Function, _TypeSelection>, details::_UnrealizedChore_t>
3703 _Function _M_function;
3704 _InitialTaskHandle(
const typename details::_Task_ptr<_ReturnType>::_Type & _TaskImpl,
const _Function & _func)
3705 : details::_PPLTaskHandle<_ReturnType, _InitialTaskHandle<_InternalReturnType, _Function, _TypeSelection>, details::_UnrealizedChore_t>::_PPLTaskHandle(_TaskImpl)
3706 , _M_function(_func)
3710 virtual ~_InitialTaskHandle() {}
3712 template <
typename _Func>
3713 auto _LogWorkItemAndInvokeUserLambda(_Func && _func) const -> decltype(_func())
3715 details::_TaskWorkItemRAIILogger _LogWorkItem(this->_M_pTask->_M_taskEventLogger);
3716 CASABLANCA_UNREFERENCED_PARAMETER(_LogWorkItem);
3720 void _Perform()
const
3722 _Init(_TypeSelection());
3725 void _SyncCancelAndPropagateException()
const
3727 this->_M_pTask->_Cancel(
true);
3735 void _Init(details::_TypeSelectorNoAsync)
const
3737 this->_M_pTask->_FinalizeAndRunContinuations(_LogWorkItemAndInvokeUserLambda(_Init_func_transformer<_InternalReturnType>::_Perform(_M_function)));
3748 void _Init(details::_TypeSelectorAsyncOperationOrTask)
const
3750 details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(this->_M_pTask, _LogWorkItemAndInvokeUserLambda(_M_function));
3753 #if defined (__cplusplus_winrt)
3759 void _Init(details::_TypeSelectorAsyncAction)
const
3761 details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(this->_M_pTask, ref
new details::_IAsyncActionToAsyncOperationConverter(_LogWorkItemAndInvokeUserLambda(_M_function)));
3769 void _Init(details::_TypeSelectorAsyncOperationWithProgress)
const
3771 typedef details::_GetProgressType<decltype(_M_function())>::_Value _ProgressType;
3773 details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(this->_M_pTask,
3774 ref
new details::_IAsyncOperationWithProgressToAsyncOperationConverter<_InternalReturnType,_ProgressType>(_LogWorkItemAndInvokeUserLambda(_M_function)));
3782 void _Init(details::_TypeSelectorAsyncActionWithProgress)
const
3784 typedef details::_GetProgressType<decltype(_M_function())>::_Value _ProgressType;
3786 details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(this->_M_pTask,
3787 ref
new details::_IAsyncActionWithProgressToAsyncOperationConverter<_ProgressType>(_LogWorkItemAndInvokeUserLambda(_M_function)));
3796 template <
typename _InternalReturnType,
typename _ContinuationReturnType,
typename _Function,
typename _IsTaskBased,
typename _TypeSelection>
3797 struct _ContinuationTaskHandle :
3798 details::_PPLTaskHandle<typename details::_NormalizeVoidToUnitType<_ContinuationReturnType>::_Type,
3799 _ContinuationTaskHandle<_InternalReturnType, _ContinuationReturnType, _Function, _IsTaskBased, _TypeSelection>, details::_ContinuationTaskHandleBase>
3801 typedef typename details::_NormalizeVoidToUnitType<_ContinuationReturnType>::_Type _NormalizedContinuationReturnType;
3803 typename details::_Task_ptr<_ReturnType>::_Type _M_ancestorTaskImpl;
3804 _Function _M_function;
3806 _ContinuationTaskHandle(
const typename details::_Task_ptr<_ReturnType>::_Type & _AncestorImpl,
3807 const typename details::_Task_ptr<_NormalizedContinuationReturnType>::_Type & _ContinuationImpl,
3808 const _Function & _Func,
const task_continuation_context & _Context, details::_TaskInliningMode_t _InliningMode)
3809 : details::_PPLTaskHandle<typename details::_NormalizeVoidToUnitType<_ContinuationReturnType>::_Type,
3810 _ContinuationTaskHandle<_InternalReturnType, _ContinuationReturnType, _Function, _IsTaskBased, _TypeSelection>, details::_ContinuationTaskHandleBase>
3811 ::_PPLTaskHandle(_ContinuationImpl)
3812 , _M_ancestorTaskImpl(_AncestorImpl)
3813 , _M_function(_Func)
3815 this->_M_isTaskBasedContinuation = _IsTaskBased::value;
3816 this->_M_continuationContext = _Context;
3817 this->_M_continuationContext._Resolve(_AncestorImpl->_IsApartmentAware());
3818 this->_M_inliningMode = _InliningMode;
3821 virtual ~_ContinuationTaskHandle() {}
3823 template <
typename _Func,
typename _Arg>
3824 auto _LogWorkItemAndInvokeUserLambda(_Func && _func, _Arg && _value) const -> decltype(_func(std::forward<_Arg>(_value)))
3826 details::_TaskWorkItemRAIILogger _LogWorkItem(this->_M_pTask->_M_taskEventLogger);
3827 CASABLANCA_UNREFERENCED_PARAMETER(_LogWorkItem);
3828 return _func(std::forward<_Arg>(_value));
3831 void _Perform()
const
3833 _Continue(_IsTaskBased(), _TypeSelection());
3836 void _SyncCancelAndPropagateException()
const
3838 if (_M_ancestorTaskImpl->_HasUserException())
3842 this->_M_pTask->_CancelWithExceptionHolder(_M_ancestorTaskImpl->_GetExceptionHolder(),
true);
3848 this->_M_pTask->_Cancel(
true);
3857 void _Continue(std::false_type, details::_TypeSelectorNoAsync)
const
3859 this->_M_pTask->_FinalizeAndRunContinuations(
3860 _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _ContinuationReturnType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult()));
3871 void _Continue(std::false_type, details::_TypeSelectorAsyncOperationOrTask)
const
3873 typedef typename details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType;
3875 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(
3877 _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult())
3881 #if defined (__cplusplus_winrt)
3887 void _Continue(std::false_type, details::_TypeSelectorAsyncAction)
const
3889 typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType;
3891 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(
3893 ref
new details::_IAsyncActionToAsyncOperationConverter(
3894 _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult())));
3902 void _Continue(std::false_type, details::_TypeSelectorAsyncOperationWithProgress)
const
3904 typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType;
3906 auto _OpWithProgress = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult());
3907 typedef details::_GetProgressType<decltype(_OpWithProgress)>::_Value _ProgressType;
3909 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(
3911 ref
new details::_IAsyncOperationWithProgressToAsyncOperationConverter<_ContinuationReturnType, _ProgressType>(_OpWithProgress));
3919 void _Continue(std::false_type, details::_TypeSelectorAsyncActionWithProgress)
const
3921 typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType;
3923 auto _OpWithProgress = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult());
3924 typedef details::_GetProgressType<decltype(_OpWithProgress)>::_Value _ProgressType;
3926 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(
3928 ref
new details::_IAsyncActionWithProgressToAsyncOperationConverter<_ProgressType>(_OpWithProgress));
3938 void _Continue(std::true_type, details::_TypeSelectorNoAsync)
const
3940 typedef task<_InternalReturnType> _FuncInputType;
3941 task<_InternalReturnType> _ResultTask;
3942 _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl));
3943 this->_M_pTask->_FinalizeAndRunContinuations(
3944 _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_FuncInputType, _ContinuationReturnType>::_Perform(_M_function), std::move(_ResultTask)));
3956 void _Continue(std::true_type, details::_TypeSelectorAsyncOperationOrTask)
const
3959 task<_InternalReturnType> _ResultTask;
3960 _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl));
3961 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(this->_M_pTask,
3962 _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask)));
3965 #if defined (__cplusplus_winrt)
3973 void _Continue(std::true_type, details::_TypeSelectorAsyncAction)
const
3976 task<_InternalReturnType> _ResultTask;
3977 _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl));
3978 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(this->_M_pTask,
3979 ref
new details::_IAsyncActionToAsyncOperationConverter(_LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask))));
3989 void _Continue(std::true_type, details::_TypeSelectorAsyncOperationWithProgress)
const
3992 task<_InternalReturnType> _ResultTask;
3993 _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl));
3995 typedef details::_GetProgressType<decltype(_M_function(_ResultTask))>::_Value _ProgressType;
3997 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(this->_M_pTask,
3998 ref
new details::_IAsyncOperationWithProgressToAsyncOperationConverter<_ContinuationReturnType, _ProgressType>(
3999 _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask))));
4009 void _Continue(std::true_type, details::_TypeSelectorAsyncActionWithProgress)
const
4012 task<_InternalReturnType> _ResultTask;
4013 _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl));
4015 typedef details::_GetProgressType<decltype(_M_function(_ResultTask))>::_Value _ProgressType;
4017 details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(this->_M_pTask,
4018 ref
new details::_IAsyncActionWithProgressToAsyncOperationConverter<_ProgressType>(
4019 _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask))));
4027 template<
typename _InternalReturnType,
typename _Function>
4028 void _TaskInitWithFunctor(
const _Function& _Func)
4030 typedef typename details::_InitFunctorTypeTraits<_InternalReturnType, decltype(_Func())> _Async_type_traits;
4032 _M_Impl->_M_fFromAsync = _Async_type_traits::_IsAsyncTask;
4033 _M_Impl->_M_fUnwrappedTask = _Async_type_traits::_IsUnwrappedTaskOrAsync;
4034 _M_Impl->_M_taskEventLogger._LogScheduleTask(
false);
4035 _M_Impl->_ScheduleTask(
new _InitialTaskHandle<_InternalReturnType, _Function, typename _Async_type_traits::_AsyncKind>(_GetImpl(), _Func), details::_NoInline);
4041 void _TaskInitNoFunctor(task_completion_event<_ReturnType>& _Event)
4043 _Event._RegisterTask(_M_Impl);
4046 #if defined (__cplusplus_winrt)
4047 void _TaskInitAsyncOp(Windows::Foundation::IAsyncOperation<
typename details::_ValueTypeOrRefType<_ReturnType>::_Value>^ _AsyncOp)
4052 _M_Impl->_M_fFromAsync =
true;
4056 _M_Impl->_M_TaskState = details::_Task_impl_base::_Started;
4058 details::_Task_impl_base::_AsyncInit<_ReturnType, _ReturnType>(_M_Impl, _AsyncOp);
4064 void _TaskInitNoFunctor(Windows::Foundation::IAsyncOperation<
typename details::_ValueTypeOrRefType<_ReturnType>::_Value>^ _AsyncOp)
4066 _TaskInitAsyncOp(_AsyncOp);
4072 template<
typename _Progress>
4073 void _TaskInitNoFunctor(Windows::Foundation::IAsyncOperationWithProgress<
typename details::_ValueTypeOrRefType<_ReturnType>::_Value, _Progress>^ _AsyncOp)
4075 _TaskInitAsyncOp(ref
new details::_IAsyncOperationWithProgressToAsyncOperationConverter<
typename details::_ValueTypeOrRefType<_ReturnType>::_Value, _Progress>(_AsyncOp));
4082 template<
typename _Function>
4083 void _TaskInitMaybeFunctor(_Function & _Func, std::true_type)
4085 _TaskInitWithFunctor<_ReturnType, _Function>(_Func);
4091 template<
typename _Ty>
4092 void _TaskInitMaybeFunctor(_Ty & _Param, std::false_type)
4094 _TaskInitNoFunctor(_Param);
4097 template<
typename _InternalReturnType,
typename _Function>
4098 auto _ThenImpl(
const _Function& _Func,
const task_options& _TaskOptions)
const ->
typename details::_ContinuationTypeTraits<_Function, _InternalReturnType>::_TaskOfType
4102 throw invalid_operation(
"then() cannot be called on a default constructed task.");
4105 details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
4106 auto _Scheduler = _TaskOptions.has_scheduler() ? _TaskOptions.get_scheduler() : _GetImpl()->_GetScheduler();
4107 auto _CreationStack = details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : details::_TaskCreationCallstack();
4108 return _ThenImpl<_InternalReturnType, _Function>(_Func, _PTokenState, _TaskOptions.get_continuation_context(), _Scheduler, _CreationStack);
4114 template<
typename _InternalReturnType,
typename _Function>
4115 auto _ThenImpl(
const _Function& _Func, details::_CancellationTokenState *_PTokenState,
const task_continuation_context& _ContinuationContext, scheduler_ptr _Scheduler, details::_TaskCreationCallstack _CreationStack,
4116 details::_TaskInliningMode_t _InliningMode = details::_NoInline) const -> typename details::_ContinuationTypeTraits<_Function, _InternalReturnType>::_TaskOfType
4120 throw invalid_operation(
"then() cannot be called on a default constructed task.");
4123 typedef details::_FunctionTypeTraits<_Function, _InternalReturnType> _Function_type_traits;
4124 typedef details::_TaskTypeTraits<typename _Function_type_traits::_FuncRetType> _Async_type_traits;
4125 typedef typename _Async_type_traits::_TaskRetType _TaskType;
4132 if (_PTokenState ==
nullptr)
4134 if (_Function_type_traits::_Takes_task::value)
4136 _PTokenState = details::_CancellationTokenState::_None();
4140 _PTokenState = _GetImpl()->_M_pTokenState;
4144 task<_TaskType> _ContinuationTask;
4145 _ContinuationTask._CreateImpl(_PTokenState, _Scheduler);
4147 _ContinuationTask._GetImpl()->_M_fFromAsync = (_GetImpl()->_M_fFromAsync || _Async_type_traits::_IsAsyncTask);
4148 _ContinuationTask._GetImpl()->_M_fUnwrappedTask = _Async_type_traits::_IsUnwrappedTaskOrAsync;
4149 _ContinuationTask._SetTaskCreationCallstack(_CreationStack);
4151 _GetImpl()->_ScheduleContinuation(
new _ContinuationTaskHandle<_InternalReturnType, _TaskType, _Function, typename _Function_type_traits::_Takes_task, typename _Async_type_traits::_AsyncKind>(
4152 _GetImpl(), _ContinuationTask._GetImpl(), _Func, _ContinuationContext, _InliningMode));
4154 return _ContinuationTask;
4158 typename details::_Task_ptr<_ReturnType>::_Type _M_Impl;
4241 template<
typename _Ty>
4245 details::_ValidateTaskConstructorArgs<void,_Ty>(_Param);
4247 _M_unitTask._CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler());
4249 _M_unitTask._SetTaskCreationCallstack(details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : _CAPTURE_CALLSTACK());
4251 _TaskInitMaybeFunctor(_Param, details::_IsCallable(_Param,0));
4279 task(
const task& _Other): _M_unitTask(_Other._M_unitTask){}
4306 task(task&& _Other) : _M_unitTask(std::move(_Other._M_unitTask)) {}
4319 task& operator=(
const task& _Other)
4321 if (
this != &_Other)
4323 _M_unitTask = _Other._M_unitTask;
4339 task& operator=(task&& _Other)
4341 if (
this != &_Other)
4343 _M_unitTask = std::move(_Other._M_unitTask);
4372 template<
typename _Function>
4374 auto then(const _Function& _Func, task_options _TaskOptions = task_options()) const -> typename details::_ContinuationTypeTraits<_Function,
void>::_TaskOfType
4376 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
4377 return _M_unitTask._ThenImpl<void, _Function>(_Func, _TaskOptions);
4408 template<
typename _Function>
4410 auto then(const _Function& _Func, cancellation_token _CancellationToken, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function,
void>::_TaskOfType
4412 task_options _TaskOptions(_CancellationToken, _ContinuationContext);
4413 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
4414 return _M_unitTask._ThenImpl<void, _Function>(_Func, _TaskOptions);
4428 return _M_unitTask.wait();
4456 return _M_unitTask.is_done();
4467 return _M_unitTask.scheduler();
4479 return _M_unitTask.is_apartment_aware();
4491 return (_M_unitTask == _Rhs._M_unitTask);
4503 return !operator==(_Rhs);
4511 _M_unitTask._CreateImpl(_Ct, _Scheduler);
4517 const details::_Task_ptr<details::_Unit_type>::_Type &
_GetImpl()
const
4519 return _M_unitTask._M_Impl;
4525 void _SetImpl(
const details::_Task_ptr<details::_Unit_type>::_Type & _Impl)
4527 _M_unitTask._SetImpl(_Impl);
4533 void _SetImpl(details::_Task_ptr<details::_Unit_type>::_Type && _Impl)
4535 _M_unitTask._SetImpl(std::move(_Impl));
4543 _M_unitTask._SetAsync(_Async);
4551 _M_unitTask._SetTaskCreationCallstack(_callstack);
4557 template<
typename _Function>
4559 details::_TaskInliningMode_t _InliningMode = details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function,
void>::_TaskOfType
4562 auto _Scheduler = _GetImpl()->_GetScheduler();
4568 template <
typename T>
friend class task;
4576 _M_unitTask._TaskInitNoFunctor(_Event._M_unitEvent);
4579 #if defined (__cplusplus_winrt)
4580 void _TaskInitNoFunctor(Windows::Foundation::IAsyncAction^ _AsyncAction)
4585 _M_unitTask._TaskInitAsyncOp(ref
new details::_IAsyncActionToAsyncOperationConverter(_AsyncAction));
4591 template<
typename _P>
4592 void _TaskInitNoFunctor(Windows::Foundation::IAsyncActionWithProgress<_P>^ _AsyncActionWithProgress)
4594 _M_unitTask._TaskInitAsyncOp(ref
new details::_IAsyncActionWithProgressToAsyncOperationConverter<_P>(_AsyncActionWithProgress));
4601 template<
typename _Function>
4602 void _TaskInitMaybeFunctor(_Function & _Func, std::true_type)
4604 _M_unitTask._TaskInitWithFunctor<void, _Function>(_Func);
4610 template<
typename _T>
4611 void _TaskInitMaybeFunctor(_T & _Param, std::false_type)
4613 _TaskInitNoFunctor(_Param);
4617 task<details::_Unit_type> _M_unitTask;
4626 #if defined (__cplusplus_winrt)
4628 template<
typename _Ty>
4629 _Ty _GetUnwrappedType(Windows::Foundation::IAsyncOperation<_Ty>^);
4631 void _GetUnwrappedType(Windows::Foundation::IAsyncAction^);
4633 template<
typename _Ty,
typename _Progress>
4634 _Ty _GetUnwrappedType(Windows::Foundation::IAsyncOperationWithProgress<_Ty, _Progress>^);
4636 template<
typename _Progress>
4637 void _GetUnwrappedType(Windows::Foundation::IAsyncActionWithProgress<_Progress>^);
4641 template<
typename _Ty>
4642 _Ty _GetUnwrappedType(task<_Ty>);
4645 template<
typename _Ty>
4646 auto _GetUnwrappedReturnType(_Ty _Arg,
int) -> decltype(_GetUnwrappedType(_Arg));
4648 template<
typename _Ty>
4649 _Ty _GetUnwrappedReturnType(_Ty, ...);
4658 template<
typename _Ty>
4659 _Ty _GetTaskType(task_completion_event<_Ty>, std::false_type);
4662 template<
typename _Ty>
4663 auto _GetTaskType(_Ty _NonFunc, std::false_type) -> decltype(_GetUnwrappedType(_NonFunc));
4666 template<
typename _Ty>
4667 auto _GetTaskType(_Ty _Func, std::true_type) -> decltype(_GetUnwrappedReturnType(_Func(), 0));
4670 void _GetTaskType(std::function<
void()>, std::true_type);
4673 template<
typename _Ty>
4674 auto _FilterValidTaskType(_Ty _Param,
int) -> decltype(_GetTaskType(_Param, _IsCallable(_Param, 0)));
4676 template<
typename _Ty>
4677 _BadArgType _FilterValidTaskType(_Ty _Param, ...);
4679 template<
typename _Ty>
4682 typedef decltype(_FilterValidTaskType(stdx::declval<_Ty>(), 0)) _Type;
4714 template<
typename _Ty>
4716 auto create_task(_Ty _Param, task_options _TaskOptions = task_options()) ->
task<typename details::_TaskTypeFromParam<_Ty>::_Type>
4719 #
if defined (__cplusplus_winrt)
4720 "incorrect argument for create_task; can be a callable object, an asynchronous operation, or a task_completion_event"
4722 "incorrect argument for create_task; can be a callable object or a task_completion_event"
4725 details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK());
4727 return _CreatedTask;
4761 template<
typename _ReturnType>
4763 task<_ReturnType> create_task(const task<_ReturnType>& _Task)
4765 task<_ReturnType> _CreatedTask(_Task);
4766 return _CreatedTask;
4769 #if defined (__cplusplus_winrt)
4772 template<
typename _T>
4773 task<_T> _To_task_helper(Windows::Foundation::IAsyncOperation<_T>^ op)
4775 return task<_T>(op);
4778 template<
typename _T,
typename _Progress>
4779 task<_T> _To_task_helper(Windows::Foundation::IAsyncOperationWithProgress<_T, _Progress>^ op)
4781 return task<_T>(op);
4784 inline task<void> _To_task_helper(Windows::Foundation::IAsyncAction^ op)
4786 return task<void>(op);
4789 template<
typename _Progress>
4790 task<void> _To_task_helper(Windows::Foundation::IAsyncActionWithProgress<_Progress>^ op)
4792 return task<void>(op);
4795 template<
typename _ProgressType>
4796 class _ProgressDispatcherBase
4800 virtual ~_ProgressDispatcherBase()
4804 virtual void _Report(
const _ProgressType& _Val) = 0;
4807 template<
typename _ProgressType,
typename _ClassPtrType>
4808 class _ProgressDispatcher :
public _ProgressDispatcherBase<_ProgressType>
4812 virtual ~_ProgressDispatcher()
4816 _ProgressDispatcher(_ClassPtrType _Ptr) : _M_ptr(_Ptr)
4820 virtual void _Report(
const _ProgressType& _Val)
4822 _M_ptr->_FireProgress(_Val);
4827 _ClassPtrType _M_ptr;
4829 class _ProgressReporterCtorArgType{};
4844 template<
typename _ProgressType>
4845 class progress_reporter
4847 typedef std::shared_ptr<details::_ProgressDispatcherBase<_ProgressType>> _PtrType;
4858 void report(
const _ProgressType& _Val)
const
4860 _M_dispatcher->_Report(_Val);
4863 template<
typename _ClassPtrType>
4864 static progress_reporter _CreateReporter(_ClassPtrType _Ptr)
4866 progress_reporter _Reporter;
4867 details::_ProgressDispatcherBase<_ProgressType> *_PDispatcher =
new details::_ProgressDispatcher<_ProgressType, _ClassPtrType>(_Ptr);
4868 _Reporter._M_dispatcher = _PtrType(_PDispatcher);
4871 progress_reporter() {}
4874 progress_reporter(details::_ProgressReporterCtorArgType);
4876 _PtrType _M_dispatcher;
4884 enum _AsyncStatusInternal
4889 _AsyncCompleted = 1,
4893 _AsyncCancelPending,
4902 enum _AsyncResultType
4904 SingleResult = 0x0001,
4905 MultipleResults = 0x0002
4912 struct _ZeroArgumentFunctor { };
4913 struct _OneArgumentFunctor { };
4914 struct _TwoArgumentFunctor { };
4923 template<
typename _Class,
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4924 _Arg1 _Arg1ClassHelperThunk(_ReturnType (_Class::*)(_Arg1, _Arg2)
const);
4927 template<
typename _Class,
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4928 _Arg2 _Arg2ClassHelperThunk(_ReturnType (_Class::*)(_Arg1, _Arg2)
const);
4930 template<
typename _Class,
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4931 _ReturnType _ReturnTypeClassHelperThunk(_ReturnType (_Class::*)(_Arg1, _Arg2)
const);
4933 template<
typename _Class,
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4934 _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType (_Class::*)(_Arg1, _Arg2)
const);
4940 template<
typename _Class,
typename _ReturnType,
typename _Arg1>
4941 _Arg1 _Arg1ClassHelperThunk(_ReturnType (_Class::*)(_Arg1)
const);
4944 template<
typename _Class,
typename _ReturnType,
typename _Arg1>
4945 void _Arg2ClassHelperThunk(_ReturnType (_Class::*)(_Arg1)
const);
4947 template<
typename _Class,
typename _ReturnType,
typename _Arg1>
4948 _ReturnType _ReturnTypeClassHelperThunk(_ReturnType (_Class::*)(_Arg1)
const);
4950 template<
typename _Class,
typename _ReturnType,
typename _Arg1>
4951 _OneArgumentFunctor _ArgumentCountHelper(_ReturnType (_Class::*)(_Arg1)
const);
4957 template<
typename _Class,
typename _ReturnType>
4958 void _Arg1ClassHelperThunk(_ReturnType (_Class::*)()
const);
4961 template<
typename _Class,
typename _ReturnType>
4962 void _Arg2ClassHelperThunk(_ReturnType (_Class::*)()
const);
4965 template<
typename _Class,
typename _ReturnType>
4966 _ReturnType _ReturnTypeClassHelperThunk(_ReturnType (_Class::*)()
const);
4968 template<
typename _Class,
typename _ReturnType>
4969 _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType (_Class::*)()
const);
4977 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4978 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2));
4980 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4981 _Arg2 _Arg2PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2));
4983 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4984 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2));
4986 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4987 _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)(_Arg1, _Arg2));
4989 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4990 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2));
4992 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4993 _Arg2 _Arg2PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2));
4995 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4996 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2));
4998 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
4999 _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)(_Arg1, _Arg2));
5001 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
5002 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2));
5004 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
5005 _Arg2 _Arg2PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2));
5007 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
5008 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2));
5010 template<
typename _ReturnType,
typename _Arg1,
typename _Arg2>
5011 _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)(_Arg1, _Arg2));
5016 template<
typename _ReturnType,
typename _Arg1>
5017 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1));
5019 template<
typename _ReturnType,
typename _Arg1>
5020 void _Arg2PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1));
5022 template<
typename _ReturnType,
typename _Arg1>
5023 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)(_Arg1));
5025 template<
typename _ReturnType,
typename _Arg1>
5026 _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)(_Arg1));
5028 template<
typename _ReturnType,
typename _Arg1>
5029 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1));
5031 template<
typename _ReturnType,
typename _Arg1>
5032 void _Arg2PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1));
5034 template<
typename _ReturnType,
typename _Arg1>
5035 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)(_Arg1));
5037 template<
typename _ReturnType,
typename _Arg1>
5038 _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)(_Arg1));
5040 template<
typename _ReturnType,
typename _Arg1>
5041 _Arg1 _Arg1PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1));
5043 template<
typename _ReturnType,
typename _Arg1>
5044 void _Arg2PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1));
5046 template<
typename _ReturnType,
typename _Arg1>
5047 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)(_Arg1));
5049 template<
typename _ReturnType,
typename _Arg1>
5050 _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)(_Arg1));
5055 template<
typename _ReturnType>
5056 void _Arg1PFNHelperThunk(_ReturnType(__cdecl *)());
5058 template<
typename _ReturnType>
5059 void _Arg2PFNHelperThunk(_ReturnType(__cdecl *)());
5061 template<
typename _ReturnType>
5062 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)());
5064 template<
typename _ReturnType>
5065 _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)());
5067 template<
typename _ReturnType>
5068 void _Arg1PFNHelperThunk(_ReturnType(__stdcall *)());
5070 template<
typename _ReturnType>
5071 void _Arg2PFNHelperThunk(_ReturnType(__stdcall *)());
5073 template<
typename _ReturnType>
5074 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)());
5076 template<
typename _ReturnType>
5077 _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)());
5079 template<
typename _ReturnType>
5080 void _Arg1PFNHelperThunk(_ReturnType(__fastcall *)());
5082 template<
typename _ReturnType>
5083 void _Arg2PFNHelperThunk(_ReturnType(__fastcall *)());
5085 template<
typename _ReturnType>
5086 _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)());
5088 template<
typename _ReturnType>
5089 _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)());
5091 template<
typename _T>
5092 struct _FunctorArguments
5094 static const size_t _Count = 0;
5098 struct _FunctorArguments<_OneArgumentFunctor>
5100 static const size_t _Count = 1;
5104 struct _FunctorArguments<_TwoArgumentFunctor>
5106 static const size_t _Count = 2;
5109 template<
typename _T>
5110 struct _FunctorTypeTraits
5112 typedef decltype(_ArgumentCountHelper(&(_T::operator()))) _ArgumentCountType;
5113 static const
size_t _ArgumentCount = _FunctorArguments<_ArgumentCountType>::_Count;
5115 typedef decltype(_ReturnTypeClassHelperThunk(&(_T::operator()))) _ReturnType;
5116 typedef decltype(_Arg1ClassHelperThunk(&(_T::operator()))) _Argument1Type;
5117 typedef decltype(_Arg2ClassHelperThunk(&(_T::operator()))) _Argument2Type;
5120 template<typename _T>
5121 struct _FunctorTypeTraits<_T *>
5123 typedef decltype(_ArgumentCountHelper(stdx::declval<_T*>())) _ArgumentCountType;
5124 static const
size_t _ArgumentCount = _FunctorArguments<_ArgumentCountType>::_Count;
5126 typedef decltype(_ReturnTypePFNHelperThunk(
stdx::declval<_T*>())) _ReturnType;
5127 typedef decltype(_Arg1PFNHelperThunk(
stdx::declval<_T*>())) _Argument1Type;
5128 typedef decltype(_Arg2PFNHelperThunk(
stdx::declval<_T*>())) _Argument2Type;
5131 template<typename _T>
5132 struct _ProgressTypeTraits
5134 static const bool _TakesProgress =
false;
5135 typedef void _ProgressType;
5138 template<
typename _T>
5139 struct _ProgressTypeTraits<progress_reporter<_T>>
5141 static const bool _TakesProgress =
true;
5142 typedef typename _T _ProgressType;
5146 template<typename _T, size_t count = _FunctorTypeTraits<_T>::_ArgumentCount>
5147 struct _CAFunctorOptions
5149 static const bool _TakesProgress =
false;
5150 static const bool _TakesToken =
false;
5151 typedef void _ProgressType;
5154 template<
typename _T>
5155 struct _CAFunctorOptions<_T, 1>
5159 typedef typename _FunctorTypeTraits<_T>::_Argument1Type _Argument1Type;
5163 static const bool _TakesProgress = _ProgressTypeTraits<_Argument1Type>::_TakesProgress;
5164 static const bool _TakesToken = !_TakesProgress;
5165 typedef typename _ProgressTypeTraits<_Argument1Type>::_ProgressType _ProgressType;
5168 template<
typename _T>
5169 struct _CAFunctorOptions<_T, 2>
5173 typedef typename _FunctorTypeTraits<_T>::_Argument1Type _Argument1Type;
5177 static const bool _TakesProgress =
true;
5178 static const bool _TakesToken =
true;
5179 typedef typename _ProgressTypeTraits<_Argument1Type>::_ProgressType _ProgressType;
5193 template<
typename _AsyncSelector,
typename _ReturnType>
5194 struct _SelectorTaskGenerator
5196 template<
typename _Function>
5197 static task<_ReturnType> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5199 task_options _taskOptinos(_Cts.get_token());
5200 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5201 return task<_ReturnType>(_Func(), _taskOptinos);
5204 template<
typename _Function>
5205 static task<_ReturnType> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5207 task_options _taskOptinos(_Cts.get_token());
5208 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5209 return task<_ReturnType>(_Func(_Cts.get_token()), _taskOptinos);
5212 template<
typename _Function,
typename _ProgressObject>
5213 static task<_ReturnType> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5215 task_options _taskOptinos(_Cts.get_token());
5216 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5217 return task<_ReturnType>(_Func(_Progress), _taskOptinos);
5220 template<
typename _Function,
typename _ProgressObject>
5221 static task<_ReturnType> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5223 task_options _taskOptinos(_Cts.get_token());
5224 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5225 return task<_ReturnType>(_Func(_Progress, _Cts.get_token()), _taskOptinos);
5229 template<
typename _AsyncSelector>
5230 struct _SelectorTaskGenerator<_AsyncSelector, void>
5232 template<
typename _Function>
5233 static task<void> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5235 task_options _taskOptinos(_Cts.get_token());
5236 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5237 return task<void>(_Func(), _taskOptinos);
5240 template<
typename _Function>
5241 static task<void> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5243 task_options _taskOptinos(_Cts.get_token());
5244 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5245 return task<void>(_Func(_Cts.get_token()), _taskOptinos);
5248 template<
typename _Function,
typename _ProgressObject>
5249 static task<void> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5251 task_options _taskOptinos(_Cts.get_token());
5252 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5253 return task<void>(_Func(_Progress), _taskOptinos);
5256 template<
typename _Function,
typename _ProgressObject>
5257 static task<void> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5259 task_options _taskOptinos(_Cts.get_token());
5260 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5261 return task<void>(_Func(_Progress, _Cts.get_token()), _taskOptinos);
5268 template<
typename _ReturnType>
5269 struct _SelectorTaskGenerator<_TypeSelectorNoAsync, _ReturnType>
5272 #pragma warning(push)
5273 #pragma warning(disable: 4702)
5274 template<
typename _Function>
5275 static task<_ReturnType> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5277 task_options _taskOptinos(_Cts.get_token());
5278 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5279 return task<_ReturnType>( [=]() -> _ReturnType {
5280 _Task_generator_oversubscriber_t _Oversubscriber;
5285 #pragma warning(pop)
5287 template<
typename _Function>
5288 static task<_ReturnType> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5290 task_options _taskOptinos(_Cts.get_token());
5291 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5292 return task<_ReturnType>( [=]() -> _ReturnType {
5293 _Task_generator_oversubscriber_t _Oversubscriber;
5295 return _Func(_Cts.get_token());
5299 template<
typename _Function,
typename _ProgressObject>
5300 static task<_ReturnType> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5302 task_options _taskOptinos(_Cts.get_token());
5303 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5304 return task<_ReturnType>( [=]() -> _ReturnType {
5305 _Task_generator_oversubscriber_t _Oversubscriber;
5307 return _Func(_Progress);
5311 template<
typename _Function,
typename _ProgressObject>
5312 static task<_ReturnType> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5314 task_options _taskOptinos(_Cts.get_token());
5315 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5316 return task<_ReturnType>( [=]() -> _ReturnType {
5317 _Task_generator_oversubscriber_t _Oversubscriber;
5319 return _Func(_Progress, _Cts.get_token());
5325 struct _SelectorTaskGenerator<_TypeSelectorNoAsync, void>
5327 template<
typename _Function>
5328 static task<void> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5330 task_options _taskOptinos(_Cts.get_token());
5331 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5332 return task<void>( [=]() {
5333 _Task_generator_oversubscriber_t _Oversubscriber;
5339 template<
typename _Function>
5340 static task<void> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5342 task_options _taskOptinos(_Cts.get_token());
5343 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5344 return task<void>( [=]() {
5345 _Task_generator_oversubscriber_t _Oversubscriber;
5347 _Func(_Cts.get_token());
5351 template<
typename _Function,
typename _ProgressObject>
5352 static task<void> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5354 task_options _taskOptinos(_Cts.get_token());
5355 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5356 return task<void>( [=]() {
5357 _Task_generator_oversubscriber_t _Oversubscriber;
5363 template<
typename _Function,
typename _ProgressObject>
5364 static task<void> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5366 task_options _taskOptinos(_Cts.get_token());
5367 details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack);
5368 return task<void>( [=]() {
5369 _Task_generator_oversubscriber_t _Oversubscriber;
5371 _Func(_Progress, _Cts.get_token());
5379 template<
typename _ReturnType>
5380 struct _SelectorTaskGenerator<_TypeSelectorAsyncTask, _ReturnType>
5382 template<
typename _Function>
5383 static task<_ReturnType> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5388 template<
typename _Function>
5389 static task<_ReturnType> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5391 return _Func(_Cts.get_token());
5394 template<
typename _Function,
typename _ProgressObject>
5395 static task<_ReturnType> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5397 return _Func(_Progress);
5400 template<
typename _Function,
typename _ProgressObject>
5401 static task<_ReturnType> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5403 return _Func(_Progress, _Cts.get_token());
5408 struct _SelectorTaskGenerator<_TypeSelectorAsyncTask, void>
5410 template<
typename _Function>
5411 static task<void> _GenerateTask_0(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5416 template<
typename _Function>
5417 static task<void> _GenerateTask_1C(
const _Function& _Func, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5419 return _Func(_Cts.get_token());
5422 template<
typename _Function,
typename _ProgressObject>
5423 static task<void> _GenerateTask_1P(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5425 return _Func(_Progress);
5428 template<
typename _Function,
typename _ProgressObject>
5429 static task<void> _GenerateTask_2PC(
const _Function& _Func,
const _ProgressObject& _Progress, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5431 return _Func(_Progress, _Cts.get_token());
5435 template<
typename _Generator,
bool _TakesToken,
bool TakesProgress>
5436 struct _TaskGenerator
5440 template<
typename _Generator>
5441 struct _TaskGenerator<_Generator, false, false>
5443 template<
typename _Function,
typename _ClassPtr,
typename _ProgressType>
5444 static auto _GenerateTask(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5445 -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack))
5447 return _Generator::_GenerateTask_0(_Func, _Cts, _callstack);
5451 template<
typename _Generator>
5452 struct _TaskGenerator<_Generator, true, false>
5454 template<
typename _Function,
typename _ClassPtr,
typename _ProgressType>
5455 static auto _GenerateTask(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5456 -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack))
5458 return _Generator::_GenerateTask_1C(_Func, _Cts, _callstack);
5462 template<
typename _Generator>
5463 struct _TaskGenerator<_Generator, false, true>
5465 template<
typename _Function,
typename _ClassPtr,
typename _ProgressType>
5466 static auto _GenerateTask(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5467 -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack))
5469 return _Generator::_GenerateTask_1P(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _callstack);
5473 template<
typename _Generator>
5474 struct _TaskGenerator<_Generator, true, true>
5476 template<
typename _Function,
typename _ClassPtr,
typename _ProgressType>
5477 static auto _GenerateTask(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5478 -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack))
5480 return _Generator::_GenerateTask_2PC(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _callstack);
5506 template<
typename _Function,
typename _ProgressType,
typename _ReturnType,
typename _TaskTraits,
bool _TakesToken,
bool _TakesProgress>
5507 struct _AsyncAttributes
5511 template<
typename _Function,
typename _ProgressType,
typename _ReturnType,
typename _TaskTraits,
bool _TakesToken>
5512 struct _AsyncAttributes<_Function, _ProgressType, _ReturnType, _TaskTraits, _TakesToken, true>
5514 typedef typename Windows::Foundation::IAsyncOperationWithProgress<_ReturnType, _ProgressType> _AsyncBaseType;
5515 typedef typename Windows::Foundation::AsyncOperationProgressHandler<_ReturnType, _ProgressType> _ProgressDelegateType;
5516 typedef typename Windows::Foundation::AsyncOperationWithProgressCompletedHandler<_ReturnType, _ProgressType> _CompletionDelegateType;
5517 typedef typename _ReturnType _ReturnType;
5518 typedef typename _ProgressType _ProgressType;
5519 typedef typename _TaskTraits::_AsyncKind _AsyncKind;
5520 typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator;
5521 typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, true> _TaskGenerator;
5523 static const bool _TakesProgress =
true;
5524 static const bool _TakesToken = _TakesToken;
5526 template<
typename _Function,
typename _ClassPtr>
5527 static task<_ReturnType> _Generate_Task(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5529 return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _callstack);
5533 template<
typename _Function,
typename _ProgressType,
typename _ReturnType,
typename _TaskTraits,
bool _TakesToken>
5534 struct _AsyncAttributes<_Function, _ProgressType, _ReturnType, _TaskTraits, _TakesToken, false>
5536 typedef typename Windows::Foundation::IAsyncOperation<_ReturnType> _AsyncBaseType;
5537 typedef _Zip _ProgressDelegateType;
5538 typedef typename Windows::Foundation::AsyncOperationCompletedHandler<_ReturnType> _CompletionDelegateType;
5539 typedef typename _ReturnType _ReturnType;
5540 typedef typename _TaskTraits::_AsyncKind _AsyncKind;
5541 typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator;
5542 typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, false> _TaskGenerator;
5544 static const bool _TakesProgress =
false;
5545 static const bool _TakesToken = _TakesToken;
5547 template<
typename _Function,
typename _ClassPtr>
5548 static task<_ReturnType> _Generate_Task(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5550 return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _callstack);
5554 template<
typename _Function,
typename _ProgressType,
typename _TaskTraits,
bool _TakesToken>
5555 struct _AsyncAttributes<_Function, _ProgressType, void, _TaskTraits, _TakesToken, true>
5557 typedef typename Windows::Foundation::IAsyncActionWithProgress<_ProgressType> _AsyncBaseType;
5558 typedef typename Windows::Foundation::AsyncActionProgressHandler<_ProgressType> _ProgressDelegateType;
5559 typedef typename Windows::Foundation::AsyncActionWithProgressCompletedHandler<_ProgressType> _CompletionDelegateType;
5560 typedef void _ReturnType;
5561 typedef typename _ProgressType _ProgressType;
5562 typedef typename _TaskTraits::_AsyncKind _AsyncKind;
5563 typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator;
5564 typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, true> _TaskGenerator;
5566 static const bool _TakesProgress =
true;
5567 static const bool _TakesToken = _TakesToken;
5569 template<
typename _Function,
typename _ClassPtr>
5570 static task<_ReturnType> _Generate_Task(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5572 return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _callstack);
5576 template<
typename _Function,
typename _ProgressType,
typename _TaskTraits,
bool _TakesToken>
5577 struct _AsyncAttributes<_Function, _ProgressType, void, _TaskTraits, _TakesToken, false>
5579 typedef typename Windows::Foundation::IAsyncAction _AsyncBaseType;
5580 typedef _Zip _ProgressDelegateType;
5581 typedef typename Windows::Foundation::AsyncActionCompletedHandler _CompletionDelegateType;
5582 typedef void _ReturnType;
5583 typedef typename _TaskTraits::_AsyncKind _AsyncKind;
5584 typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator;
5585 typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, false> _TaskGenerator;
5587 static const bool _TakesProgress =
false;
5588 static const bool _TakesToken = _TakesToken;
5590 template<
typename _Function,
typename _ClassPtr>
5591 static task<_ReturnType> _Generate_Task(
const _Function& _Func, _ClassPtr _Ptr, cancellation_token_source _Cts,
const _TaskCreationCallstack & _callstack)
5593 return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _callstack);
5597 template<
typename _Function>
5598 struct _AsyncLambdaTypeTraits
5600 typedef typename _FunctorTypeTraits<_Function>::_ReturnType _ReturnType;
5601 typedef typename _FunctorTypeTraits<_Function>::_Argument1Type _Argument1Type;
5602 typedef typename _CAFunctorOptions<_Function>::_ProgressType _ProgressType;
5604 static const bool _TakesProgress = _CAFunctorOptions<_Function>::_TakesProgress;
5605 static const bool _TakesToken = _CAFunctorOptions<_Function>::_TakesToken;
5607 typedef typename _TaskTypeTraits<_ReturnType> _TaskTraits;
5608 typedef typename _AsyncAttributes<_Function, _ProgressType, typename _TaskTraits::_TaskRetType, _TaskTraits, _TakesToken, _TakesProgress> _AsyncAttributes;
5618 template <
typename _Attributes, _AsyncResultType resultType = SingleResult >
5619 ref class _AsyncInfoBase
abstract : _Attributes::_AsyncBaseType
5624 _M_currentStatus(_AsyncStatusInternal::_AsyncCreated),
5626 _M_completeDelegate(nullptr),
5627 _M_CompleteDelegateAssigned(0),
5630 _M_id = ::pplx::details::platform::GetNextAsyncId();
5634 virtual typename _Attributes::_ReturnType GetResults()
5636 throw ::Platform::Exception::CreateException(E_UNEXPECTED);
5639 virtual property unsigned int Id
5643 _CheckValidStateForAsyncInfoCall();
5648 void set(
unsigned int id)
5650 _CheckValidStateForAsyncInfoCall();
5654 throw ::Platform::Exception::CreateException(E_INVALIDARG);
5656 else if (_M_currentStatus != _AsyncStatusInternal::_AsyncCreated)
5658 throw ::Platform::Exception::CreateException(E_ILLEGAL_METHOD_CALL);
5665 virtual property Windows::Foundation::AsyncStatus Status
5667 Windows::Foundation::AsyncStatus
get()
5669 _CheckValidStateForAsyncInfoCall();
5671 _AsyncStatusInternal _Current = _M_currentStatus;
5679 case _AsyncCancelPending:
5680 _Current = _AsyncCanceled;
5683 _Current = _AsyncStarted;
5689 return static_cast<Windows::Foundation::AsyncStatus
>(_Current);
5693 virtual property Windows::Foundation::HResult ErrorCode
5695 Windows::Foundation::HResult
get()
5697 _CheckValidStateForAsyncInfoCall();
5699 Windows::Foundation::HResult _Hr;
5700 _Hr.Value = _M_errorCode;
5705 virtual property typename _Attributes::_ProgressDelegateType^ Progress
5707 typename typename _Attributes::_ProgressDelegateType^
get()
5709 return _GetOnProgress();
5712 void set(
typename _Attributes::_ProgressDelegateType^ _ProgressHandler)
5714 _PutOnProgress(_ProgressHandler);
5718 virtual void Cancel()
5720 if (_TransitionToState(_AsyncCancelPending))
5726 virtual void Close()
5728 if (_TransitionToState(_AsyncClosed))
5734 if (_M_currentStatus != _AsyncClosed)
5736 throw ::Platform::Exception::CreateException(E_ILLEGAL_STATE_CHANGE);
5741 virtual property typename _Attributes::_CompletionDelegateType^ Completed
5743 typename _Attributes::_CompletionDelegateType^
get()
5745 _CheckValidStateForDelegateCall();
5746 return _M_completeDelegate;
5749 void set(
typename _Attributes::_CompletionDelegateType^ _CompleteHandler)
5751 _CheckValidStateForDelegateCall();
5753 if (InterlockedIncrement(&_M_CompleteDelegateAssigned) == 1)
5755 _M_completeDelegateContext = _ContextCallback::_CaptureCurrent();
5756 _M_completeDelegate = _CompleteHandler;
5760 if (_IsTerminalState())
5767 throw ::Platform::Exception::CreateException(E_ILLEGAL_DELEGATE_ASSIGNMENT);
5778 if (_TransitionToState(_AsyncStarted))
5784 throw ::Platform::Exception::CreateException(E_ILLEGAL_STATE_CHANGE);
5789 void _FireCompletion()
5791 _TryTransitionToCompleted();
5794 if (_M_completeDelegate !=
nullptr && InterlockedIncrement(&_M_CallbackMade) == 1)
5796 _M_completeDelegateContext._CallInContext([=] {
5797 _M_completeDelegate((_Attributes::_AsyncBaseType^)
this, this->Status);
5798 _M_completeDelegate =
nullptr;
5803 virtual typename _Attributes::_ProgressDelegateType^ _GetOnProgress()
5805 throw ::Platform::Exception::CreateException(E_UNEXPECTED);
5808 virtual void _PutOnProgress(
typename _Attributes::_ProgressDelegateType^ _ProgressHandler)
5810 throw ::Platform::Exception::CreateException(E_UNEXPECTED);
5813 bool _TryTransitionToCompleted()
5815 return _TransitionToState(_AsyncStatusInternal::_AsyncCompleted);
5818 bool _TryTransitionToCancelled()
5820 return _TransitionToState(_AsyncStatusInternal::_AsyncCanceled);
5823 bool _TryTransitionToError(
const HRESULT error)
5825 _InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(&_M_errorCode), error, S_OK);
5826 return _TransitionToState(_AsyncStatusInternal::_AsyncError);
5832 inline void _CheckValidStateForDelegateCall()
5834 if (_M_currentStatus == _AsyncClosed)
5836 throw ::Platform::Exception::CreateException(E_ILLEGAL_METHOD_CALL);
5843 inline void _CheckValidStateForResultsCall()
5845 _AsyncStatusInternal _Current = _M_currentStatus;
5847 if (_Current == _AsyncError)
5849 throw ::Platform::Exception::CreateException(_M_errorCode);
5851 #pragma warning(push)
5852 #pragma warning(disable: 4127) // Conditional expression is constant
5854 if (resultType == SingleResult)
5855 #pragma warning(pop)
5857 if (_Current != _AsyncCompleted)
5859 throw ::Platform::Exception::CreateException(E_ILLEGAL_METHOD_CALL);
5863 else if (_Current != _AsyncStarted &&
5864 _Current != _AsyncCancelPending &&
5865 _Current != _AsyncCanceled &&
5866 _Current != _AsyncCompleted)
5868 throw ::Platform::Exception::CreateException(E_ILLEGAL_METHOD_CALL);
5875 inline bool _ContinueAsyncOperation()
5877 return (_M_currentStatus == _AsyncStarted);
5883 virtual void _OnStart() = 0;
5884 virtual void _OnClose() = 0;
5885 virtual void _OnCancel() = 0;
5893 inline void _CheckValidStateForAsyncInfoCall()
5895 _AsyncStatusInternal _Current = _M_currentStatus;
5896 if (_Current == _AsyncClosed)
5898 throw ::Platform::Exception::CreateException(E_ILLEGAL_METHOD_CALL);
5900 else if (_Current == _AsyncCreated)
5902 throw ::Platform::Exception::CreateException(E_ASYNC_OPERATION_NOT_STARTED);
5907 inline bool _TransitionToState(
const _AsyncStatusInternal _NewState)
5909 _AsyncStatusInternal _Current = _M_currentStatus;
5915 case _AsyncStatusInternal::_AsyncStarted:
5916 if (_Current != _AsyncCreated)
5921 case _AsyncStatusInternal::_AsyncCompleted:
5922 if (_Current != _AsyncStarted && _Current != _AsyncCancelPending)
5927 case _AsyncStatusInternal::_AsyncCancelPending:
5928 if (_Current != _AsyncStarted)
5933 case _AsyncStatusInternal::_AsyncCanceled:
5934 if (_Current != _AsyncStarted && _Current != _AsyncCancelPending)
5939 case _AsyncStatusInternal::_AsyncError:
5940 if (_Current != _AsyncStarted && _Current != _AsyncCancelPending)
5945 case _AsyncStatusInternal::_AsyncClosed:
5946 if (!_IsTerminalState(_Current))
5959 _AsyncStatusInternal _RetState =
static_cast<_AsyncStatusInternal
>(
5960 _InterlockedCompareExchange(reinterpret_cast<volatile LONG*>(&_M_currentStatus),
5962 static_cast<LONG>(_Current)));
5967 return (_RetState == _Current);
5970 inline bool _IsTerminalState()
5972 return _IsTerminalState(_M_currentStatus);
5975 inline bool _IsTerminalState(_AsyncStatusInternal status)
5977 return (status == _AsyncError ||
5978 status == _AsyncCanceled ||
5979 status == _AsyncCompleted ||
5980 status == _AsyncClosed);
5985 _ContextCallback _M_completeDelegateContext;
5986 typename _Attributes::_CompletionDelegateType^
volatile _M_completeDelegate;
5987 _AsyncStatusInternal
volatile _M_currentStatus;
5988 HRESULT
volatile _M_errorCode;
5990 long volatile _M_CompleteDelegateAssigned;
5991 long volatile _M_CallbackMade;
5998 template<
typename _Attributes,
bool _HasProgress, _AsyncResultType _ResultType = SingleResult >
5999 ref class _AsyncProgressBase
abstract : _AsyncInfoBase<_Attributes, _ResultType>
6003 template<
typename _Attributes, _AsyncResultType _ResultType>
6004 ref class _AsyncProgressBase<_Attributes, true, _ResultType>
abstract : _AsyncInfoBase<_Attributes, _ResultType>
6008 _AsyncProgressBase() : _AsyncInfoBase<_Attributes, _ResultType>(),
6009 _M_progressDelegate(nullptr)
6013 virtual typename _Attributes::_ProgressDelegateType^ _GetOnProgress()
override
6015 _CheckValidStateForDelegateCall();
6016 return _M_progressDelegate;
6019 virtual void _PutOnProgress(
typename _Attributes::_ProgressDelegateType^ _ProgressHandler)
override
6021 _CheckValidStateForDelegateCall();
6022 _M_progressDelegate = _ProgressHandler;
6023 _M_progressDelegateContext = _ContextCallback::_CaptureCurrent();
6026 void _FireProgress(
const typename _Attributes::_ProgressType& _ProgressValue)
6028 if (_M_progressDelegate !=
nullptr)
6030 _M_progressDelegateContext._CallInContext([=] {
6031 _M_progressDelegate((_Attributes::_AsyncBaseType^)
this, _ProgressValue);
6038 _ContextCallback _M_progressDelegateContext;
6039 typename _Attributes::_ProgressDelegateType^ _M_progressDelegate;
6042 template<
typename _Attributes, _AsyncResultType _ResultType = SingleResult>
6043 ref class _AsyncBaseProgressLayer
abstract : _AsyncProgressBase<_Attributes, _Attributes::_TakesProgress, _ResultType>
6054 template<
typename _Attributes,
typename _ReturnType>
6055 ref class _AsyncTaskThunkBase
abstract : _AsyncBaseProgressLayer<_Attributes>
6059 virtual _ReturnType GetResults()
override
6061 _CheckValidStateForResultsCall();
6062 return _M_task.get();
6067 typedef task<_ReturnType> _TaskType;
6069 _AsyncTaskThunkBase(
const _TaskType& _Task)
6074 _AsyncTaskThunkBase()
6080 virtual void _OnStart()
override
6082 _M_task.then( [=](_TaskType _Antecedent) {
6087 catch(task_canceled&)
6089 _TryTransitionToCancelled();
6091 catch(::Platform::Exception^ _Ex)
6093 _TryTransitionToError(_Ex->HResult);
6097 _TryTransitionToError(E_FAIL);
6106 cancellation_token_source _M_cts;
6109 template<
typename _Attributes>
6110 ref class _AsyncTaskThunk : _AsyncTaskThunkBase<_Attributes, typename _Attributes::_ReturnType>
6114 _AsyncTaskThunk(
const _TaskType& _Task) :
6115 _AsyncTaskThunkBase(_Task)
6125 virtual void _OnClose()
override
6129 virtual void _OnCancel()
override
6138 template<
typename _Function>
6139 ref class _AsyncTaskGeneratorThunk
sealed : _AsyncTaskThunk<typename _AsyncLambdaTypeTraits<_Function>::_AsyncAttributes>
6143 typedef typename _AsyncLambdaTypeTraits<_Function>::_AsyncAttributes _Attributes;
6144 typedef typename _AsyncTaskThunk<_Attributes> _Base;
6145 typedef typename _Attributes::_AsyncBaseType _AsyncBaseType;
6147 _AsyncTaskGeneratorThunk(
const _Function& _Func,
const _TaskCreationCallstack &_callstack) : _M_func(_Func), _M_creationCallstack(_callstack)
6160 virtual void _OnStart()
override
6166 _M_task = _Attributes::_Generate_Task(_M_func,
this, _M_cts, _M_creationCallstack);
6170 virtual void _OnCancel()
override
6176 _TaskCreationCallstack _M_creationCallstack;
6221 template<
typename _Function>
6223 details::_AsyncTaskGeneratorThunk<_Function> ^create_async(const _Function& _Func)
6225 static_assert(std::is_same<decltype(details::_IsValidCreateAsync(_Func,0,0,0,0)),std::true_type>::value,
6226 "argument to create_async must be a callable object taking zero, one or two arguments");
6227 return ref
new details::_AsyncTaskGeneratorThunk<_Function>(_Func, _CAPTURE_CALLSTACK());
6235 template<
typename _Type>
6238 _RunAllParam() : _M_completeCount(0), _M_numTasks(0)
6242 void _Resize(
size_t _Len,
bool _SkipVector =
false)
6247 _M_vector._Result.resize(_Len);
6251 task_completion_event<_Unit_type> _M_completed;
6252 _ResultHolder<std::vector<_Type> > _M_vector;
6253 _ResultHolder<_Type> _M_mergeVal;
6254 atomic_size_t _M_completeCount;
6258 template<
typename _Type>
6259 struct _RunAllParam<std::vector<_Type> >
6261 _RunAllParam() : _M_completeCount(0), _M_numTasks(0)
6265 void _Resize(
size_t _Len,
bool _SkipVector =
false)
6271 _M_vector.resize(_Len);
6275 task_completion_event<_Unit_type> _M_completed;
6276 std::vector<_ResultHolder<std::vector<_Type> > > _M_vector;
6277 atomic_size_t _M_completeCount;
6283 struct _RunAllParam<_Unit_type>
6285 _RunAllParam() : _M_completeCount(0), _M_numTasks(0)
6289 void _Resize(
size_t _Len)
6294 task_completion_event<_Unit_type> _M_completed;
6295 atomic_size_t _M_completeCount;
6299 inline void _JoinAllTokens_Add(
const cancellation_token_source& _MergedSrc, _CancellationTokenState *_PJoinedTokenState)
6301 if (_PJoinedTokenState !=
nullptr && _PJoinedTokenState != _CancellationTokenState::_None())
6303 cancellation_token _T = cancellation_token::_FromImpl(_PJoinedTokenState);
6304 _T.register_callback( [=](){
6305 _MergedSrc.cancel();
6310 template<
typename _ElementType,
typename _Function,
typename _TaskType>
6311 void _WhenAllContinuationWrapper(_RunAllParam<_ElementType>* _PParam, _Function _Func, task<_TaskType>& _Task)
6313 if (_Task._GetImpl()->_IsCompleted())
6316 if (atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks)
6319 _PParam->_M_completed.set(_Unit_type());
6326 _ASSERTE(_Task._GetImpl()->_IsCanceled());
6327 if (_Task._GetImpl()->_HasUserException())
6330 _PParam->_M_completed._Cancel(_Task._GetImpl()->_GetExceptionHolder());
6334 _PParam->_M_completed._Cancel();
6337 if (atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks)
6344 template<
typename _ElementType,
typename _Iterator>
6347 static task<std::vector<_ElementType>> _Perform(
const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End)
6349 _CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
6351 auto _PParam =
new _RunAllParam<_ElementType>();
6352 cancellation_token_source _MergedSource;
6355 task_options _Options(_TaskOptions);
6356 _Options.set_cancellation_token(_MergedSource.get_token());
6357 task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options);
6359 auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type) -> std::vector<_ElementType> {
6360 return _PParam->_M_vector.Get();
6366 _JoinAllTokens_Add(_MergedSource, _PTokenState);
6367 _PParam->_Resize(static_cast<size_t>(std::distance(_Begin, _End)));
6371 size_t _TaskNum = 0;
6372 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6375 _JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState);
6377 _PParam->_Resize(_TaskNum);
6381 if( _Begin == _End )
6383 _PParam->_M_completed.set(_Unit_type());
6389 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6391 if (_PTask->is_apartment_aware())
6393 _ReturnTask._SetAsync();
6396 _PTask->_Then([_PParam, _Index](task<_ElementType> _ResultTask) {
6398 auto _PParamCopy = _PParam;
6399 auto _IndexCopy = _Index;
6400 auto _Func = [_PParamCopy, _IndexCopy, &_ResultTask](){
6401 _PParamCopy->_M_vector._Result[_IndexCopy] = _ResultTask._GetImpl()->_GetResult();
6404 _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask);
6405 }, _CancellationTokenState::_None());
6415 template<
typename _ElementType,
typename _Iterator>
6416 struct _WhenAllImpl<std::vector<_ElementType>, _Iterator>
6418 static task<std::vector<_ElementType>> _Perform(
const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End)
6420 _CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
6422 auto _PParam =
new _RunAllParam<std::vector<_ElementType>>();
6423 cancellation_token_source _MergedSource;
6426 task_options _Options(_TaskOptions);
6427 _Options.set_cancellation_token(_MergedSource.get_token());
6428 task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options);
6430 auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type) -> std::vector<_ElementType> {
6431 _ASSERTE(_PParam->_M_completeCount == _PParam->_M_numTasks);
6432 std::vector<_ElementType> _Result;
6433 for(
size_t _I = 0; _I < _PParam->_M_numTasks; _I++)
6435 const std::vector<_ElementType>& _Vec = _PParam->_M_vector[_I].Get();
6436 _Result.insert(_Result.end(), _Vec.begin(), _Vec.end());
6444 _JoinAllTokens_Add(_MergedSource, _PTokenState);
6445 _PParam->_Resize(static_cast<size_t>(std::distance(_Begin, _End)));
6449 size_t _TaskNum = 0;
6450 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6453 _JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState);
6455 _PParam->_Resize(_TaskNum);
6459 if( _Begin == _End )
6461 _PParam->_M_completed.set(_Unit_type());
6467 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6469 if (_PTask->is_apartment_aware())
6471 _ReturnTask._SetAsync();
6474 _PTask->_Then([_PParam, _Index](task<std::vector<_ElementType>> _ResultTask) {
6476 auto _PParamCopy = _PParam;
6477 auto _IndexCopy = _Index;
6478 auto _Func = [_PParamCopy, _IndexCopy, &_ResultTask]() {
6479 _PParamCopy->_M_vector[_IndexCopy].Set(_ResultTask._GetImpl()->_GetResult());
6482 _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask);
6483 }, _CancellationTokenState::_None());
6493 template<
typename _Iterator>
6494 struct _WhenAllImpl<void, _Iterator>
6496 static task<void> _Perform(
const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End)
6498 _CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
6500 auto _PParam =
new _RunAllParam<_Unit_type>();
6501 cancellation_token_source _MergedSource;
6504 task_options _Options(_TaskOptions);
6505 _Options.set_cancellation_token(_MergedSource.get_token());
6506 task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options);
6508 auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type) {
6514 _JoinAllTokens_Add(_MergedSource, _PTokenState);
6515 _PParam->_Resize(static_cast<size_t>(std::distance(_Begin, _End)));
6519 size_t _TaskNum = 0;
6520 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6523 _JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState);
6525 _PParam->_Resize(_TaskNum);
6529 if( _Begin == _End )
6531 _PParam->_M_completed.set(_Unit_type());
6536 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6538 if (_PTask->is_apartment_aware())
6540 _ReturnTask._SetAsync();
6543 _PTask->_Then([_PParam](task<void> _ResultTask) {
6544 auto _Func = [](){};
6545 _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask);
6546 }, _CancellationTokenState::_None());
6554 template<
typename _ReturnType>
6555 task<std::vector<_ReturnType>> _WhenAllVectorAndValue(
const task<std::vector<_ReturnType>>& _VectorTask,
const task<_ReturnType>& _ValueTask,
6556 bool _OutputVectorFirst)
6558 auto _PParam =
new _RunAllParam<_ReturnType>();
6559 cancellation_token_source _MergedSource;
6562 task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _MergedSource.get_token());
6564 auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type) -> std::vector<_ReturnType> {
6565 _ASSERTE(_PParam->_M_completeCount == 2);
6566 auto _Result = _PParam->_M_vector.Get();
6567 auto _mergeVal = _PParam->_M_mergeVal.Get();
6569 if (_OutputVectorFirst ==
true)
6571 _Result.push_back(_mergeVal);
6575 _Result.insert(_Result.begin(), _mergeVal);
6581 _JoinAllTokens_Add(_MergedSource, _VectorTask._GetImpl()->_M_pTokenState);
6582 _JoinAllTokens_Add(_MergedSource, _ValueTask._GetImpl()->_M_pTokenState);
6585 _PParam->_Resize(2,
true);
6587 if (_VectorTask.is_apartment_aware() || _ValueTask.is_apartment_aware())
6589 _ReturnTask._SetAsync();
6591 _VectorTask._Then([_PParam](task<std::vector<_ReturnType>> _ResultTask) {
6592 auto _PParamCopy = _PParam;
6593 auto _Func = [_PParamCopy, &_ResultTask]() {
6594 auto _ResultLocal = _ResultTask._GetImpl()->_GetResult();
6595 _PParamCopy->_M_vector.Set(_ResultLocal);
6598 _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask);
6599 }, _CancellationTokenState::_None());
6600 _ValueTask._Then([_PParam](task<_ReturnType> _ResultTask) {
6601 auto _PParamCopy = _PParam;
6602 auto _Func = [_PParamCopy, &_ResultTask]() {
6603 auto _ResultLocal = _ResultTask._GetImpl()->_GetResult();
6604 _PParamCopy->_M_mergeVal.Set(_ResultLocal);
6607 _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask);
6608 }, _CancellationTokenState::_None());
6637 template <
typename _Iterator>
6638 auto when_all(_Iterator _Begin, _Iterator _End,
const task_options& _TaskOptions = task_options())
6639 -> decltype (details::_WhenAllImpl<
typename std::iterator_traits<_Iterator>::value_type::result_type, _Iterator>::_Perform(_TaskOptions, _Begin, _End))
6641 typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType;
6642 return details::_WhenAllImpl<_ElementType, _Iterator>::_Perform(_TaskOptions, _Begin, _End);
6670 template<
typename _ReturnType>
6671 task<std::vector<_ReturnType>> operator&&(
const task<_ReturnType> & _Lhs,
const task<_ReturnType> & _Rhs)
6673 task<_ReturnType> _PTasks[2] = {_Lhs, _Rhs};
6674 return when_all(_PTasks, _PTasks+2);
6702 template<
typename _ReturnType>
6703 task<std::vector<_ReturnType>> operator&&(
const task<std::vector<_ReturnType>> & _Lhs,
const task<_ReturnType> & _Rhs)
6705 return details::_WhenAllVectorAndValue(_Lhs, _Rhs,
true);
6733 template<
typename _ReturnType>
6734 task<std::vector<_ReturnType>> operator&&(
const task<_ReturnType> & _Lhs,
const task<std::vector<_ReturnType>> & _Rhs)
6736 return details::_WhenAllVectorAndValue(_Rhs, _Lhs,
false);
6764 template<
typename _ReturnType>
6765 task<std::vector<_ReturnType>> operator&&(
const task<std::vector<_ReturnType>> & _Lhs,
const task<std::vector<_ReturnType>> & _Rhs)
6767 task<std::vector<_ReturnType>> _PTasks[2] = {_Lhs, _Rhs};
6768 return when_all(_PTasks, _PTasks+2);
6796 inline task<void> operator&&(
const task<void> & _Lhs,
const task<void> & _Rhs)
6798 task<void> _PTasks[2] = {_Lhs, _Rhs};
6799 return when_all(_PTasks, _PTasks+2);
6805 template <
typename _CompletionType>
6808 _RunAnyParam() : _M_exceptionRelatedToken(nullptr), _M_completeCount(0), _M_numTasks(0), _M_fHasExplicitToken(false)
6813 if (_CancellationTokenState::_IsValid(_M_exceptionRelatedToken))
6814 _M_exceptionRelatedToken->_Release();
6816 task_completion_event<_CompletionType> _M_Completed;
6817 cancellation_token_source _M_cancellationSource;
6818 _CancellationTokenState * _M_exceptionRelatedToken;
6819 atomic_size_t _M_completeCount;
6821 bool _M_fHasExplicitToken;
6824 template<
typename _CompletionType,
typename _Function,
typename _TaskType>
6825 void _WhenAnyContinuationWrapper(_RunAnyParam<_CompletionType> * _PParam,
const _Function & _Func, task<_TaskType>& _Task)
6827 bool _IsTokenCancled = !_PParam->_M_fHasExplicitToken && _Task._GetImpl()->_M_pTokenState != _CancellationTokenState::_None() && _Task._GetImpl()->_M_pTokenState->_IsCanceled();
6828 if (_Task._GetImpl()->_IsCompleted() && !_IsTokenCancled)
6831 if (atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks)
6838 _ASSERTE(_Task._GetImpl()->_IsCanceled() || _IsTokenCancled);
6839 if (_Task._GetImpl()->_HasUserException() && !_IsTokenCancled)
6841 if (_PParam->_M_Completed._StoreException(_Task._GetImpl()->_GetExceptionHolder()))
6844 _PParam->_M_exceptionRelatedToken = _Task._GetImpl()->_M_pTokenState;
6845 _ASSERTE(_PParam->_M_exceptionRelatedToken);
6847 if (_PParam->_M_exceptionRelatedToken != _CancellationTokenState::_None())
6849 _PParam->_M_exceptionRelatedToken->_Reference();
6854 if (atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks)
6857 if (!_PParam->_M_Completed._IsTriggered())
6860 if (!_PParam->_M_fHasExplicitToken)
6862 if (_PParam->_M_exceptionRelatedToken)
6864 _JoinAllTokens_Add(_PParam->_M_cancellationSource, _PParam->_M_exceptionRelatedToken);
6870 _JoinAllTokens_Add(_PParam->_M_cancellationSource, _Task._GetImpl()->_M_pTokenState);
6874 _PParam->_M_Completed._Cancel();
6881 template<
typename _ElementType,
typename _Iterator>
6884 static task<std::pair<_ElementType, size_t>> _Perform(
const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End)
6886 if( _Begin == _End )
6888 throw invalid_operation(
"when_any(begin, end) cannot be called on an empty container.");
6890 _CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
6891 auto _PParam =
new _RunAnyParam<std::pair<std::pair<_ElementType, size_t>, _CancellationTokenState *>>();
6895 _JoinAllTokens_Add(_PParam->_M_cancellationSource, _PTokenState);
6896 _PParam->_M_fHasExplicitToken =
true;
6899 task_options _Options(_TaskOptions);
6900 _Options.set_cancellation_token(_PParam->_M_cancellationSource.get_token());
6901 task<std::pair<std::pair<_ElementType, size_t>, _CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _Options);
6904 auto _CancellationSource = _PParam->_M_cancellationSource;
6906 _PParam->_M_numTasks =
static_cast<size_t>(std::distance(_Begin, _End));
6908 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6910 if (_PTask->is_apartment_aware())
6912 _Any_tasks_completed._SetAsync();
6915 _PTask->_Then([_PParam, _Index](task<_ElementType> _ResultTask) {
6916 auto _PParamCopy = _PParam;
6917 auto _IndexCopy = _Index;
6918 auto _Func = [&_ResultTask, _PParamCopy, _IndexCopy]() {
6919 _PParamCopy->_M_Completed.set(std::make_pair(std::make_pair(_ResultTask._GetImpl()->_GetResult(), _IndexCopy), _ResultTask._GetImpl()->_M_pTokenState));
6922 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
6923 }, _CancellationTokenState::_None());
6929 return _Any_tasks_completed._Then([=](std::pair<std::pair<_ElementType, size_t>, _CancellationTokenState *> _Result) -> std::pair<_ElementType, size_t> {
6930 _ASSERTE(_Result.second);
6933 _JoinAllTokens_Add(_CancellationSource, _Result.second);
6935 return _Result.first;
6940 template<
typename _Iterator>
6941 struct _WhenAnyImpl<void, _Iterator>
6943 static task<size_t> _Perform(
const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End)
6945 if( _Begin == _End )
6947 throw invalid_operation(
"when_any(begin, end) cannot be called on an empty container.");
6950 _CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() :
nullptr;
6951 auto _PParam =
new _RunAnyParam<std::pair<size_t, _CancellationTokenState *>>();
6955 _JoinAllTokens_Add(_PParam->_M_cancellationSource, _PTokenState);
6956 _PParam->_M_fHasExplicitToken =
true;
6959 task_options _Options(_TaskOptions);
6960 _Options.set_cancellation_token(_PParam->_M_cancellationSource.get_token());
6961 task<std::pair<size_t, _CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _Options);
6964 auto _CancellationSource = _PParam->_M_cancellationSource;
6966 _PParam->_M_numTasks =
static_cast<size_t>(std::distance(_Begin, _End));
6968 for (
auto _PTask = _Begin; _PTask != _End; ++_PTask)
6970 if (_PTask->is_apartment_aware())
6972 _Any_tasks_completed._SetAsync();
6975 _PTask->_Then([_PParam, _Index](task<void> _ResultTask) {
6976 auto _PParamCopy = _PParam;
6977 auto _IndexCopy = _Index;
6978 auto _Func = [&_ResultTask, _PParamCopy, _IndexCopy]() {
6979 _PParamCopy->_M_Completed.set(std::make_pair(_IndexCopy, _ResultTask._GetImpl()->_M_pTokenState));
6981 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
6982 }, _CancellationTokenState::_None());
6988 return _Any_tasks_completed._Then([=](std::pair<size_t, _CancellationTokenState *> _Result) ->
size_t {
6989 _ASSERTE(_Result.second);
6992 _JoinAllTokens_Add(_CancellationSource, _Result.second);
6994 return _Result.first;
7020 template<
typename _Iterator>
7021 auto when_any(_Iterator _Begin, _Iterator _End,
const task_options& _TaskOptions = task_options())
7022 -> decltype (details::_WhenAnyImpl<
typename std::iterator_traits<_Iterator>::value_type::result_type, _Iterator>::_Perform(_TaskOptions, _Begin, _End))
7024 typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType;
7025 return details::_WhenAnyImpl<_ElementType, _Iterator>::_Perform(_TaskOptions, _Begin, _End);
7052 template<
typename _Iterator>
7053 auto when_any(_Iterator _Begin, _Iterator _End, cancellation_token _CancellationToken)
7054 -> decltype (details::_WhenAnyImpl<
typename std::iterator_traits<_Iterator>::value_type::result_type, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End))
7056 typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType;
7057 return details::_WhenAnyImpl<_ElementType, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End);
7086 template<
typename _ReturnType>
7087 task<_ReturnType> operator||(
const task<_ReturnType> & _Lhs,
const task<_ReturnType> & _Rhs)
7089 auto _PParam =
new details::_RunAnyParam<std::pair<_ReturnType, size_t>>();
7091 task<std::pair<_ReturnType, size_t>> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token());
7094 auto _ReturnTask = _Any_tasks_completed._Then([=](std::pair<_ReturnType, size_t> _Ret) -> _ReturnType {
7095 _ASSERTE(_Ret.second);
7096 _JoinAllTokens_Add(_PParam->_M_cancellationSource, reinterpret_cast<details::_CancellationTokenState *>(_Ret.second));
7100 if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware())
7102 _ReturnTask._SetAsync();
7105 _PParam->_M_numTasks = 2;
7106 auto _Continuation = [_PParam](task<_ReturnType> _ResultTask) {
7108 auto _PParamCopy = _PParam;
7109 auto _Func = [&_ResultTask, _PParamCopy]() {
7110 _PParamCopy->_M_Completed.set(std::make_pair(_ResultTask._GetImpl()->_GetResult(),
reinterpret_cast<size_t>(_ResultTask._GetImpl()->_M_pTokenState)));
7112 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
7115 _Lhs._Then(_Continuation, details::_CancellationTokenState::_None());
7116 _Rhs._Then(_Continuation, details::_CancellationTokenState::_None());
7147 template<
typename _ReturnType>
7148 task<std::vector<_ReturnType>> operator||(
const task<std::vector<_ReturnType>> & _Lhs,
const task<_ReturnType> & _Rhs)
7150 auto _PParam =
new details::_RunAnyParam<std::pair<std::vector<_ReturnType>, details::_CancellationTokenState *>>();
7152 task<std::pair<std::vector<_ReturnType>, details::_CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token());
7156 auto _ReturnTask = _Any_tasks_completed._Then([=](std::pair<std::vector<_ReturnType>, details::_CancellationTokenState *> _Ret) -> std::vector<_ReturnType> {
7157 _ASSERTE(_Ret.second);
7158 _JoinAllTokens_Add(_PParam->_M_cancellationSource, _Ret.second);
7162 if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware())
7164 _ReturnTask._SetAsync();
7167 _PParam->_M_numTasks = 2;
7168 _Lhs._Then([_PParam](task<std::vector<_ReturnType>> _ResultTask) {
7170 auto _PParamCopy = _PParam;
7171 auto _Func = [&_ResultTask, _PParamCopy]() {
7172 auto _Result = _ResultTask._GetImpl()->_GetResult();
7173 _PParamCopy->_M_Completed.set(std::make_pair(_Result, _ResultTask._GetImpl()->_M_pTokenState));
7175 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
7176 }, details::_CancellationTokenState::_None());
7179 _Rhs._Then([_PParam](task<_ReturnType> _ResultTask)
7181 auto _PParamCopy = _PParam;
7182 auto _Func = [&_ResultTask, _PParamCopy]() {
7183 auto _Result = _ResultTask._GetImpl()->_GetResult();
7185 std::vector<_ReturnType> _Vec;
7186 _Vec.push_back(_Result);
7187 _PParamCopy->_M_Completed.set(std::make_pair(_Vec, _ResultTask._GetImpl()->_M_pTokenState));
7189 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
7190 }, details::_CancellationTokenState::_None());
7221 template<
typename _ReturnType>
7222 task<std::vector<_ReturnType>> operator||(
const task<_ReturnType> & _Lhs,
const task<std::vector<_ReturnType>> & _Rhs)
7224 return _Rhs || _Lhs;
7253 inline task<void> operator||(
const task<void> & _Lhs,
const task<void> & _Rhs)
7255 auto _PParam =
new details::_RunAnyParam<std::pair<details::_Unit_type, details::_CancellationTokenState *>>();
7257 task<std::pair<details::_Unit_type, details::_CancellationTokenState *>> _Any_task_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token());
7260 auto _ReturnTask = _Any_task_completed._Then([=](std::pair<details::_Unit_type, details::_CancellationTokenState *> _Ret) {
7261 _ASSERTE(_Ret.second);
7262 details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _Ret.second);
7265 if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware())
7267 _ReturnTask._SetAsync();
7270 _PParam->_M_numTasks = 2;
7271 auto _Continuation = [_PParam](task<void> _ResultTask)
mutable {
7273 auto _PParam1 = _PParam;
7274 auto _Func = [&_ResultTask, _PParam1]() {
7275 _PParam1->_M_Completed.set(std::make_pair(details::_Unit_type(), _ResultTask._GetImpl()->_M_pTokenState));
7277 _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask);
7280 _Lhs._Then(_Continuation, details::_CancellationTokenState::_None());
7281 _Rhs._Then(_Continuation, details::_CancellationTokenState::_None());
7286 template<
typename _Ty>
7287 task<_Ty> task_from_result(_Ty _Param,
const task_options& _TaskOptions = task_options())
7289 task_completion_event<_Ty> _Tce;
7291 return create_task(_Tce, _TaskOptions);
7295 #if _MSC_VER == 1600
7296 inline task<bool> task_from_result(
bool _Param)
7298 task_completion_event<bool> _Tce;
7300 return create_task(_Tce, task_options());
7303 inline task<void> task_from_result(
const task_options& _TaskOptions = task_options())
7305 task_completion_event<void> _Tce;
7307 return create_task(_Tce, _TaskOptions);
7310 template<
typename _TaskType,
typename _ExType>
7311 task<_TaskType> task_from_exception(_ExType _Exception,
const task_options& _TaskOptions = task_options())
7313 task_completion_event<_TaskType> _Tce;
7314 _Tce.set_exception(_Exception);
7315 return create_task(_Tce, _TaskOptions);
7328 task<bool> do_while(std::function<task<bool>(
void)> func)
7330 task<bool> first = func();
7331 return first.then([=](
bool guard) -> task<bool> {
7333 return do_while(func);
7343 #pragma pop_macro("new")
7345 #if defined(_MSC_VER)
7346 #pragma warning(pop)
7350 #endif // (defined(_MSC_VER) && (_MSC_VER >= 1800))
7353 #ifndef _LWRCASE_CNCRRNCY
7354 #define _LWRCASE_CNCRRNCY
7362 #endif // _PPLXTASKS_H
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: pplxtasks.h:2776
A generic RAII wrapper for locks that implements the critical_section interface cpprest_synchronizati...
Definition: pplxlinux.h:264
task_options(std::shared_ptr< _SchedType > _Scheduler)
Task option that specify a scheduler with shared lifetime
Definition: pplxtasks.h:1295
bool _StoreException(const std::shared_ptr< details::_ExceptionHolder > &_ExHolder) const
Method that stores an exception in the task completion event. This is used internally by when_any...
Definition: pplxtasks.h:2999
task_options(const task_options &_TaskOptions)
Task option copy constructor
Definition: pplxtasks.h:1331
void _SetAsync(bool _Async=true)
Sets a property determining whether the task is apartment aware.
Definition: pplxtasks.h:3666
Definition: pplxtasks.h:1436
The task_completion_event class allows you to delay the execution of a task until a condition is sati...
Definition: pplxtasks.h:2934
bool has_scheduler() const
Indicates whether a scheduler n was specified by the user
Definition: pplxtasks.h:1384
task()
Constructs a task object.
Definition: pplxtasks.h:4204
void _ScheduleTask(_UnrealizedChore_t *_PTaskHandle, _TaskInliningMode_t _InliningMode)
Helper function to schedule the task on the Task Collection.
Definition: pplxtasks.h:1979
The pplx namespace provides classes and functions that give you access to the Concurrency Runtime...
Definition: pplx.h:81
void result_type
The type of the result an object of this class produces.
Definition: pplxtasks.h:4180
task_status wait() const
Waits for this task to reach a terminal state. It is possible for wait to execute the task inline...
Definition: pplxtasks.h:3512
Definition: pplxtasks.h:1588
task & operator=(task &&_Other)
Replaces the contents of one task object with another.
Definition: pplxtasks.h:3396
Callstack container, which is used to capture and preserve callstacks in ppltasks. Members of this class is examined by vc debugger, thus there will be no public access methods. Please note that names of this class should be kept stable for debugger examining.
Definition: pplxtasks.h:257
void _SetImpl(const typename details::_Task_ptr< _ReturnType >::_Type &_Impl)
Set the implementation of the task to be the supplied implementaion.
Definition: pplxtasks.h:3648
__declspec(noinline) auto then(const _Function &_Func) const -> typename details::_ContinuationTypeTraits< _Function, _ReturnType >::_TaskOfType
Adds a continuation task to this task.
Definition: pplxtasks.h:3426
task(const task &_Other)
Constructs a task object.
Definition: pplxtasks.h:3336
bool _Cancel(_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint=details::_TaskCreationCallstack()) const
Internal method to cancel the task_completion_event with the exception provided. Any task created usi...
Definition: pplxtasks.h:2787
Definition: pplxtasks.h:832
_PPLXIMP std::shared_ptr< pplx::scheduler_interface > _pplx_cdecl get_ambient_scheduler()
Gets the ambient scheduler to be used by the PPL constructs
Definition: pplxtasks.h:2593
struct __declspec(novtable) scheduler_interface
Scheduler Interface
Definition: pplxinterface.h:64
void _CreateImpl(details::_CancellationTokenState *_Ct, scheduler_ptr _Scheduler)
Create an underlying task implementation.
Definition: pplxtasks.h:4509
Definition: pplxtasks.h:297
static task_continuation_context use_default()
Creates the default task continuation context.
Definition: pplxtasks.h:1152
Definition: pplxtasks.h:324
auto _Then(const _Function &_Func, details::_CancellationTokenState *_PTokenState, details::_TaskInliningMode_t _InliningMode=details::_ForceInline) const -> typename details::_ContinuationTypeTraits< _Function, void >::_TaskOfType
An internal version of then that takes additional flags and executes the continuation inline...
Definition: pplxtasks.h:4558
bool _pplx_cdecl is_task_cancellation_requested()
Returns an indication of whether the task that is currently executing has received a request to cance...
Definition: pplxtasks.h:230
const details::_Task_ptr< _ReturnType >::_Type & _GetImpl() const
Return the underlying implementation for this task.
Definition: pplxtasks.h:3640
Definition: pplxtasks.h:581
The _PPLTaskHandle is the strong-typed task handle base. All user task functions need to be wrapped i...
Definition: pplxtasks.h:1621
virtual bool _CancelAndRunContinuations(bool _SynchronousCancel, bool _UserException, bool _PropagatedFromAncestor, const std::shared_ptr< _ExceptionHolder > &_ExceptionHolder_arg)
Requests cancellation on the task and schedules continuations if the task can be transitioned to a te...
Definition: pplxtasks.h:2426
void set_cancellation_token(cancellation_token _Token)
Sets the given token in the options
Definition: pplxtasks.h:1343
bool operator!=(const task< void > &_Rhs) const
Determines whether two task objects represent different internal tasks.
Definition: pplxtasks.h:4501
void _Cancel() const
Cancel the task_completion_event. Any task created using this event will be marked as canceled if it ...
Definition: pplxtasks.h:2980
__declspec(noinline) bool set_exception(std
Propagates an exception to all tasks associated with this event.
Definition: pplxtasks.h:2969
bool is_done() const
Determines if the task is completed.
Definition: pplxtasks.h:3558
Definition: pplxtasks.h:1599
Represents a pointer to a scheduler. This class exists to allow the the specification of a shared lif...
Definition: pplxinterface.h:74
void _SetTaskCreationCallstack(const details::_TaskCreationCallstack &_callstack)
Sets a field in the task impl to the return callstack for calls to the task constructors and the then...
Definition: pplxtasks.h:4549
This class describes an exception thrown by the PPL tasks layer in order to force the current task to...
Definition: pplxcancellation_token.h:56
void _RunContinuation(_ContinuationTaskHandleBase *_PTaskHandle)
Function executes a continuation. This function is recorded by a parent task implementation when a co...
Definition: pplxtasks.h:2021
task_options(cancellation_token _Token)
Task option that specify a cancellation token
Definition: pplxtasks.h:1258
task & operator=(const task &_Other)
Replaces the contents of one task object with another.
Definition: pplxtasks.h:3376
void _SetImpl(const details::_Task_ptr< details::_Unit_type >::_Type &_Impl)
Set the implementation of the task to be the supplied implementaion.
Definition: pplxtasks.h:4525
Definition: pplxtasks.h:420
bool has_cancellation_token() const
Indicates whether a cancellation token was specified by the user
Definition: pplxtasks.h:1360
Definition: pplxtasks.h:293
task_options(scheduler_ptr _Scheduler)
Task option that specify a scheduler
Definition: pplxtasks.h:1319
Definition: pplxtasks.h:457
scheduler_ptr scheduler() const
Returns the scheduler for this task
Definition: pplxtasks.h:3574
Definition: pplxtasks.h:291
task_completion_event()
Constructs a task_completion_event object.
Definition: pplxtasks.h:2681
cancellation_token get_cancellation_token() const
Returns the cancellation token
Definition: pplxtasks.h:1368
task_options()
Default list of task creation options
Definition: pplxtasks.h:1246
Definition: pplxtasks.h:489
Definition: pplxtasks.h:426
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
bool set(_ResultType _Result) const
Sets the task completion event.
Definition: pplxtasks.h:2702
The tasks queued to the task_group or structured_task_group object completed successfully.
Definition: pplxinterface.h:143
Definition: pplxtasks.h:1426
Definition: pplxcancellation_token.h:256
The cancellation_token class represents the ability to determine whether some operation has been requ...
Definition: pplxcancellation_token.h:712
task_status wait() const
Waits for this task to reach a terminal state. It is possible for wait to execute the task inline...
Definition: pplxtasks.h:4426
Definition: pplxtasks.h:296
bool is_apartment_aware() const
Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such ...
Definition: pplxtasks.h:3591
Definition: pplxtasks.h:473
bool operator==(const task< void > &_Rhs) const
Determines whether two task objects represent the same internal task.
Definition: pplxtasks.h:4489
Definition: pplxtasks.h:510
Represents the allowed options for creating a task
Definition: pplxtasks.h:1238
task_options(scheduler_interface &_Scheduler)
Task option that specify a scheduler reference
Definition: pplxtasks.h:1307
bool operator!=(const task< _ReturnType > &_Rhs) const
Determines whether two task objects represent different internal tasks.
Definition: pplxtasks.h:3619
scheduler_ptr scheduler() const
Returns the scheduler for this task
Definition: pplxtasks.h:4465
Definition: pplxtasks.h:4671
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:4173
bool is_apartment_aware() const
Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such ...
Definition: pplxtasks.h:4477
bool _StoreException(_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint=details::_TaskCreationCallstack()) const
Internal method that stores an exception in the task completion event. This is used internally by whe...
Definition: pplxtasks.h:2808
Definition: pplxtasks.h:300
bool operator==(const task< _ReturnType > &_Rhs) const
Determines whether two task objects represent the same internal task.
Definition: pplxtasks.h:3607
const details::_Task_ptr< details::_Unit_type >::_Type & _GetImpl() const
Return the underlying implementation for this task.
Definition: pplxtasks.h:4517
_ReturnType result_type
The type of the result an object of this class produces.
Definition: pplxtasks.h:3185
static cancellation_token none()
Returns a cancellation token which can never be subject to cancellation.
Definition: pplxcancellation_token.h:724
Definition: astreambuf.h:37
task_continuation_context get_continuation_context() const
Returns the continuation context
Definition: pplxtasks.h:1376
bool _IsTriggered() const
Tests whether current event has been either Set, or Canceled.
Definition: pplxtasks.h:2824
void _SetImpl(typename details::_Task_ptr< _ReturnType >::_Type &&_Impl)
Set the implementation of the task to be the supplied implementaion using a move instead of a copy...
Definition: pplxtasks.h:3657
Definition: pplxtasks.h:4680
__declspec(noinline) bool set_exception(std
Propagates an exception to all tasks associated with this event.
Definition: pplxtasks.h:2765
Definition: pplxtasks.h:312
Definition: pplxtasks.h:156
void _Cancel(const std::shared_ptr< details::_ExceptionHolder > &_ExHolder) const
Cancel the task_completion_event with the exception holder provided. Any task created using this even...
Definition: pplxtasks.h:2989
auto _Then(const _Function &_Func, details::_CancellationTokenState *_PTokenState, details::_TaskInliningMode_t _InliningMode=details::_ForceInline) const -> typename details::_ContinuationTypeTraits< _Function, _ReturnType >::_TaskOfType
An internal version of then that takes additional flags and always execute the continuation inline by...
Definition: pplxtasks.h:3685
task_options(cancellation_token _Token, task_continuation_context _ContinuationContext)
Task option that specify a cancellation token and a continuation context. This is valid only for cont...
Definition: pplxtasks.h:1282
Definition: pplxtasks.h:1216
bool _IsTriggered() const
Test whether current event has been either Set, or Canceled.
Definition: pplxtasks.h:3007
task()
Constructs a task object.
Definition: pplxtasks.h:3209
Definition: pplxtasks.h:292
void _SetAsync(bool _Async=true)
Sets a property determining whether the task is apartment aware.
Definition: pplxtasks.h:4541
bool set() const
Sets the task completion event.
Definition: pplxtasks.h:2950
void _SetImpl(details::_Task_ptr< details::_Unit_type >::_Type &&_Impl)
Set the implementation of the task to be the supplied implementaion using a move instead of a copy...
Definition: pplxtasks.h:4533
Definition: pplxtasks.h:921
Definition: pplxtasks.h:295
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplxinterface.h:130
__declspec(noinline) explicit task(_Ty _Param)
Constructs a task object.
Definition: pplxtasks.h:3251
scheduler_ptr get_scheduler() const
Returns the scheduler
Definition: pplxtasks.h:1392
void _SetTaskCreationCallstack(const details::_TaskCreationCallstack &_callstack)
Sets a field in the task impl to the return callstack for calls to the task constructors and the then...
Definition: pplxtasks.h:3674
task(task &&_Other)
Constructs a task object.
Definition: pplxtasks.h:3363
The implementation of a first-class task. This structure contains the task group used to execute the ...
Definition: pplxtasks.h:1423
void _ScheduleContinuation(_ContinuationTaskHandleBase *_PTaskHandle)
Schedule the actual continuation. This will either schedule the function on the continuation task's i...
Definition: pplxtasks.h:2124
Helper object used for LWT invocation.
Definition: pplxtasks.h:528
Definition: pplxcancellation_token.h:301
void _CreateImpl(details::_CancellationTokenState *_Ct, scheduler_ptr _Scheduler)
Create an underlying task implementation.
Definition: pplxtasks.h:3627
The task_group or structured_task_group object was canceled. One or more tasks may not have executed...
Definition: pplxinterface.h:149
The base implementation of a first-class task. This class contains all the non-type specific implemen...
Definition: pplxtasks.h:1688
The task_continuation_context class allows you to specify where you would like a continuation to be e...
Definition: pplxtasks.h:1131
The task_completion_event class allows you to delay the execution of a task until a condition is sati...
Definition: pplxtasks.h:2674
task_group_status task_status
A type that represents the terminal state of a task. Valid values are completed and canceled...
Definition: pplxtasks.h:174
task_options(task_continuation_context _ContinuationContext)
Task option that specify a continuation context. This is valid only for continuations (then) ...
Definition: pplxtasks.h:1270
This class describes an exception thrown when an invalid operation is performed that is not more accu...
Definition: pplxcancellation_token.h:99
void set_continuation_context(task_continuation_context _ContinuationContext)
Sets the given continuation context in the options
Definition: pplxtasks.h:1352
Definition: pplxtasks.h:294
bool is_done() const
Determines if the task is completed.
Definition: pplxtasks.h:4454