STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Public Attributes | List of all members
Concurrency::details::_Task_impl< _ReturnType > Struct Template Reference

The implementation of a first-class task. This structure contains the task group used to execute the task function and handles the scheduling. The _Task_impl is created as a shared_ptr member of the the public task class, so its destruction is handled automatically. More...

#include <ppltasks.h>

Inheritance diagram for Concurrency::details::_Task_impl< _ReturnType >:
Concurrency::details::_Task_impl_base

Public Member Functions

 _Task_impl (_CancellationTokenState *_Ct, scheduler_ptr _Scheduler_arg)
 
virtual ~_Task_impl ()
 
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 terminal state. More...
 
void _FinalizeAndRunContinuations (_ReturnType _Result)
 
bool _TransitionedToStarted ()
 
bool _IsDone ()
 
_ReturnType _GetResult ()
 
- Public Member Functions inherited from Concurrency::details::_Task_impl_base
 _Task_impl_base (_CancellationTokenState *_PTokenState, scheduler_ptr _Scheduler_arg)
 
virtual ~_Task_impl_base ()
 
task_status _Wait ()
 
bool _Cancel (bool _SynchronousCancel)
 
bool _CancelWithExceptionHolder (const std::shared_ptr< _ExceptionHolder > &_ExHolder, bool _PropagatedFromAncestor)
 
bool _CancelWithException (const std::exception_ptr &_Exception)
 
void _RegisterCancellation (std::weak_ptr< _Task_impl_base > _WeakPtr)
 
void _DeregisterCancellation ()
 
bool _IsCreated ()
 
bool _IsStarted ()
 
bool _IsPendingCancel ()
 
bool _IsCompleted ()
 
bool _IsCanceled ()
 
bool _HasUserException ()
 
const std::shared_ptr< _ExceptionHolder > & _GetExceptionHolder ()
 
bool _IsApartmentAware ()
 
void _SetAsync (bool _Async=true)
 
_TaskCreationCallstack _GetTaskCreationCallstack ()
 
void _SetTaskCreationCallstack (const _TaskCreationCallstack &_Callstack)
 
void _ScheduleTask (_UnrealizedChore_t *_PTaskHandle, _TaskInliningMode_t _InliningMode)
 Helper function to schedule the task on the Task Collection. More...
 
void _RunContinuation (_ContinuationTaskHandleBase *_PTaskHandle)
 Function executes a continuation. This function is recorded by a parent task implementation when a continuation is created in order to execute later. More...
 
void _ScheduleContinuationTask (_ContinuationTaskHandleBase *_PTaskHandle)
 
void _ScheduleContinuation (_ContinuationTaskHandleBase *_PTaskHandle)
 Schedule the actual continuation. This will either schedule the function on the continuation task's implementation if the task has completed or append it to a list of functions to execute when the task actually does complete. More...
 
void _RunTaskContinuations ()
 
scheduler_ptr _GetScheduler () const
 

Public Attributes

_ResultHolder< _ReturnType > _M_Result
 
- Public Attributes inherited from Concurrency::details::_Task_impl_base
volatile _TaskInternalState _M_TaskState
 
bool _M_fFromAsync
 
bool _M_fUnwrappedTask
 
std::shared_ptr< _ExceptionHolder_M_exceptionHolder
 
::Concurrency::extensibility::critical_section_t _M_ContinuationsCritSec
 
_ContinuationList _M_Continuations
 
_CancellationTokenState_M_pTokenState
 
_CancellationTokenRegistration_M_pRegistration
 
::Concurrency::details::_TaskCollection_t _M_TaskCollection
 
_TaskCreationCallstack _M_pTaskCreationCallstack
 
_TaskEventLogger _M_taskEventLogger
 

Additional Inherited Members

- Public Types inherited from Concurrency::details::_Task_impl_base
enum  _TaskInternalState {
  _Created, _Started, _PendingCancel, _Completed,
  _Canceled
}
 
typedef _ContinuationTaskHandleBase_ContinuationList
 
- Static Public Member Functions inherited from Concurrency::details::_Task_impl_base
template<typename _ReturnType , typename _InternalReturnType >
static void _AsyncInit (const typename _Task_ptr< _ReturnType >::_Type &_OuterTask, const task< _InternalReturnType > &_UnwrappedTask)
 

Detailed Description

template<typename _ReturnType>
struct Concurrency::details::_Task_impl< _ReturnType >

The implementation of a first-class task. This structure contains the task group used to execute the task function and handles the scheduling. The _Task_impl is created as a shared_ptr member of the the public task class, so its destruction is handled automatically.

Template Parameters
_ReturnTypeThe result type of this task.

Constructor & Destructor Documentation

template<typename _ReturnType >
Concurrency::details::_Task_impl< _ReturnType >::_Task_impl ( _CancellationTokenState _Ct,
scheduler_ptr  _Scheduler_arg 
)
inline
2355  : _Task_impl_base(_Ct, _Scheduler_arg)
2356  {
2357 #if defined (__cplusplus_winrt)
2358  _M_unwrapped_async_op = nullptr;
2359 #endif /* defined (__cplusplus_winrt) */
2360  }
_Task_impl_base(_CancellationTokenState *_PTokenState, scheduler_ptr _Scheduler_arg)
Definition: ppltasks.h:1657
template<typename _ReturnType >
virtual Concurrency::details::_Task_impl< _ReturnType >::~_Task_impl ( )
inlinevirtual
2363  {
2364  // We must invoke _DeregisterCancellation in the derived class destructor. Calling it in the base class destructor could cause
2365  // a partially initialized _Task_impl to be in the list of registrations for a cancellation token.
2367  }
void _DeregisterCancellation()
Definition: ppltasks.h:1854

Member Function Documentation

template<typename _ReturnType >
virtual bool Concurrency::details::_Task_impl< _ReturnType >::_CancelAndRunContinuations ( bool  _SynchronousCancel,
bool  _UserException,
bool  _PropagatedFromAncestor,
const std::shared_ptr< _ExceptionHolder > &  _ExHolder 
)
inlinevirtual

Requests cancellation on the task and schedules continuations if the task can be transitioned to a terminal state.

Parameters
_SynchronousCancelSet to true if the cancel takes place as a result of the task body encountering an exception, or because an ancestor or task_completion_event the task was registered with were canceled with an exception. A synchronous cancel is one that assures the task could not be running on a different thread at the time the cancellation is in progress. An asynchronous cancel is one where the thread performing the cancel has no control over the thread that could be executing the task, that is the task could execute concurrently while the cancellation is in progress.
_UserExceptionWhether an exception other than the internal runtime cancellation exceptions caused this cancellation.
_PropagatedFromAncestorWhether this exception came from an ancestor task or a task_completion_event as opposed to an exception that was encountered by the task itself. Only valid when _UserException is set to true.
_ExHolderThe exception holder that represents the exception. Only valid when _UserException is set to true.

Implements Concurrency::details::_Task_impl_base.

2370  {
2371  enum { _Nothing, _RunContinuations, _Cancel } _Do = _Nothing;
2372  {
2374  if (_UserException)
2375  {
2376  _ASSERTE(_SynchronousCancel && !_IsCompleted());
2377  // If the state is _Canceled, the exception has to be coming from an ancestor.
2378  _ASSERTE(!_IsCanceled() || _PropagatedFromAncestor);
2379 
2380  // We should not be canceled with an exception more than once.
2382 
2383  if (_M_TaskState == _Canceled)
2384  {
2385  // If the task has finished cancelling there should not be any continuation records in the array.
2386  return false;
2387  }
2388  else
2389  {
2391  _M_exceptionHolder = _ExceptionHolder_arg;
2392  }
2393  }
2394  else
2395  {
2396  // Completed is a non-cancellable state, and if this is an asynchronous cancel, we're unable to do better than the last async cancel
2397  // which is to say, cancellation is already initiated, so return early.
2398  if (_IsCompleted() || _IsCanceled() || (_IsPendingCancel() && !_SynchronousCancel))
2399  {
2401  return false;
2402  }
2403  _ASSERTE(!_SynchronousCancel || !_HasUserException());
2404  }
2405 
2406  if (_SynchronousCancel)
2407  {
2408  // Be aware that this set must be done BEFORE _M_Scheduled being set, or race will happen between this and wait()
2410  // Cancellation completes the task, so all dependent tasks must be run to cancel them
2411  // They are canceled when they begin running (see _RunContinuation) and see that their
2412  // ancestor has been canceled.
2413  _Do = _RunContinuations;
2414  }
2415  else
2416  {
2417  _ASSERTE(!_UserException);
2418 
2419  if (_IsStarted())
2420  {
2421  // should not initiate cancellation under a lock
2422  _Do = _Cancel;
2423  }
2424 
2425  // The _M_TaskState variable transitions to _Canceled when cancellation is completed (the task is not executing user code anymore).
2426  // In the case of a synchronous cancel, this can happen immediately, whereas with an asynchronous cancel, the task has to move from
2427  // _Started to _PendingCancel before it can move to _Canceled when it is finished executing.
2429 
2431  }
2432  }
2433 
2434  switch (_Do)
2435  {
2436  case _Cancel:
2437  {
2438 #if defined (__cplusplus_winrt)
2439  if (_M_unwrapped_async_op != nullptr)
2440  {
2441  // We will only try to cancel async operation but not unwrapped tasks, since unwrapped tasks cannot be canceled without its token.
2442  _M_unwrapped_async_op->Cancel();
2443  }
2444 #endif /* defined (__cplusplus_winrt) */
2446  break;
2447  }
2448  case _RunContinuations:
2449  {
2450  // Only execute continuations and mark the task as completed if we were able to move the task to the _Canceled state.
2452 
2453  if (_M_Continuations)
2454  {
2455  // Scheduling cancellation with automatic inlining.
2457  }
2458 
2459  break;
2460  }
2461  }
2462  return true;
2463  }
std::shared_ptr< _ExceptionHolder > _M_exceptionHolder
Definition: ppltasks.h:2291
bool _IsCompleted()
Definition: ppltasks.h:1879
bool _HasUserException()
Definition: ppltasks.h:1889
_ContinuationList _M_Continuations
Definition: ppltasks.h:2296
bool _IsCanceled()
Definition: ppltasks.h:1884
::Concurrency::details::_TaskCollection_t _M_TaskCollection
Definition: ppltasks.h:2305
bool _Cancel(bool _SynchronousCancel)
Definition: ppltasks.h:1810
_TaskEventLogger _M_taskEventLogger
Definition: ppltasks.h:2310
#define _ASSERTE(expr)
Definition: crtdbg.h:216
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2281
::Concurrency::extensibility::critical_section_t _M_ContinuationsCritSec
Definition: ppltasks.h:2295
void _LogCancelTask()
Definition: ppltasks.h:1548
bool _IsPendingCancel()
Definition: ppltasks.h:1874
void _Complete()
Definition: pplconcrt.h:224
bool _IsStarted()
Definition: ppltasks.h:1869
void _RunTaskContinuations()
Definition: ppltasks.h:2133
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661
static void _ScheduleFuncWithAutoInline(const std::function< void()> &_Func, _TaskInliningMode_t _InliningMode)
Schedule a functor with automatic inlining. Note that this is "fire and forget" scheduling, which cannot be waited on or canceled after scheduling. This schedule method will perform automatic inlining base on .
Definition: ppltasks.h:533
void _Cancel()
Definition: pplconcrt.h:191
template<typename _ReturnType >
void Concurrency::details::_Task_impl< _ReturnType >::_FinalizeAndRunContinuations ( _ReturnType  _Result)
inline
2466  {
2467  _M_Result.Set(_Result);
2468 
2469  {
2470  //
2471  // Hold this lock to ensure continuations being concurrently either get added
2472  // to the _M_Continuations vector or wait for the result
2473  //
2475 
2476  // A task could still be in the _Created state if it was created with a task_completion_event.
2477  // It could also be in the _Canceled state for the same reason.
2479  if (_IsCanceled())
2480  {
2481  return;
2482  }
2483 
2484  // Always transition to "completed" state, even in the face of unacknowledged pending cancellation
2486  }
2489  }
bool _IsCompleted()
Definition: ppltasks.h:1879
bool _HasUserException()
Definition: ppltasks.h:1889
bool _IsCanceled()
Definition: ppltasks.h:1884
::Concurrency::details::_TaskCollection_t _M_TaskCollection
Definition: ppltasks.h:2305
#define _ASSERTE(expr)
Definition: crtdbg.h:216
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2281
::Concurrency::extensibility::critical_section_t _M_ContinuationsCritSec
Definition: ppltasks.h:2295
void Set(const _Type &_type)
Definition: ppltasks.h:791
void _Complete()
Definition: pplconcrt.h:224
_ResultHolder< _ReturnType > _M_Result
Definition: ppltasks.h:2535
void _RunTaskContinuations()
Definition: ppltasks.h:2133
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661
template<typename _ReturnType >
_ReturnType Concurrency::details::_Task_impl< _ReturnType >::_GetResult ( )
inline
2531  {
2532  return _M_Result.Get();
2533  }
_ResultHolder< _ReturnType > _M_Result
Definition: ppltasks.h:2535
_Type Get()
Definition: ppltasks.h:796
template<typename _ReturnType >
bool Concurrency::details::_Task_impl< _ReturnType >::_IsDone ( )
inline
2526  {
2527  return _IsCompleted() || _IsCanceled();
2528  }
bool _IsCompleted()
Definition: ppltasks.h:1879
bool _IsCanceled()
Definition: ppltasks.h:1884
template<typename _ReturnType >
bool Concurrency::details::_Task_impl< _ReturnType >::_TransitionedToStarted ( )
inline
2495  {
2497  // Canceled state could only result from antecedent task's canceled state, but that code path will not reach here.
2498  _ASSERT(!_IsCanceled());
2499  if (_IsPendingCancel())
2500  return false;
2501 
2502  _ASSERTE(_IsCreated());
2504  return true;
2505  }
bool _IsCanceled()
Definition: ppltasks.h:1884
#define _ASSERTE(expr)
Definition: crtdbg.h:216
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2281
::Concurrency::extensibility::critical_section_t _M_ContinuationsCritSec
Definition: ppltasks.h:2295
bool _IsCreated()
Definition: ppltasks.h:1864
bool _IsPendingCancel()
Definition: ppltasks.h:1874
#define _ASSERT(expr)
Definition: crtdbg.h:212
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661

Member Data Documentation

template<typename _ReturnType >
_ResultHolder<_ReturnType> Concurrency::details::_Task_impl< _ReturnType >::_M_Result

The documentation for this struct was generated from the following file: