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 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
 
std::function< void()> _M_InternalCancellation
 
- 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
 
std::mutex _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
static _CRTIMP2 bool __cdecl _IsNonBlockingThread ()
 
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 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
2066  : _Task_impl_base(_Ct, _Scheduler_arg)
2067  {
2068  }
_Task_impl_base(_CancellationTokenState *_PTokenState, scheduler_ptr _Scheduler_arg)
Definition: ppltasks.h:1464
template<typename _ReturnType >
virtual Concurrency::details::_Task_impl< _ReturnType >::~_Task_impl ( )
inlinevirtual
2071  {
2072  // We must invoke _DeregisterCancellation in the derived class destructor. Calling it in the base class destructor could cause
2073  // a partially initialized _Task_impl to be in the list of registrations for a cancellation token.
2075  }
void _DeregisterCancellation()
Definition: ppltasks.h:1632

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.

2078  {
2079  enum { _Nothing, _RunContinuations, _Cancel } _Do = _Nothing;
2080  {
2081  ::std::lock_guard<std::mutex> _LockHolder(_M_ContinuationsCritSec);
2082  if (_UserException)
2083  {
2084  _ASSERTE(_SynchronousCancel && !_IsCompleted());
2085  // If the state is _Canceled, the exception has to be coming from an ancestor.
2086  _ASSERTE(!_IsCanceled() || _PropagatedFromAncestor);
2087 
2088  // We should not be canceled with an exception more than once.
2090 
2091  if (_M_TaskState == _Canceled)
2092  {
2093  // If the task has finished cancelling there should not be any continuation records in the array.
2094  return false;
2095  }
2096  else
2097  {
2099  _M_exceptionHolder = _ExceptionHolder_arg;
2100  }
2101  }
2102  else
2103  {
2104  // Completed is a non-cancellable state, and if this is an asynchronous cancel, we're unable to do better than the last async cancel
2105  // which is to say, cancellation is already initiated, so return early.
2106  if (_IsCompleted() || _IsCanceled() || (_IsPendingCancel() && !_SynchronousCancel))
2107  {
2109  return false;
2110  }
2111  _ASSERTE(!_SynchronousCancel || !_HasUserException());
2112  }
2113 
2114  if (_SynchronousCancel)
2115  {
2116  // Be aware that this set must be done BEFORE _M_Scheduled being set, or race will happen between this and wait()
2118  // Cancellation completes the task, so all dependent tasks must be run to cancel them
2119  // They are canceled when they begin running (see _RunContinuation) and see that their
2120  // ancestor has been canceled.
2121  _Do = _RunContinuations;
2122  }
2123  else
2124  {
2125  _ASSERTE(!_UserException);
2126 
2127  if (_IsStarted())
2128  {
2129  // should not initiate cancellation under a lock
2130  _Do = _Cancel;
2131  }
2132 
2133  // The _M_TaskState variable transitions to _Canceled when cancellation is completed (the task is not executing user code anymore).
2134  // In the case of a synchronous cancel, this can happen immediately, whereas with an asynchronous cancel, the task has to move from
2135  // _Started to _PendingCancel before it can move to _Canceled when it is finished executing.
2137 
2139  }
2140  }
2141 
2142  switch (_Do)
2143  {
2144  case _Cancel:
2145  {
2147  {
2148  // We will only try to cancel async operation but not unwrapped tasks, since unwrapped tasks cannot be canceled without its token.
2150  }
2152  break;
2153  }
2154  case _RunContinuations:
2155  {
2156  // Only execute continuations and mark the task as completed if we were able to move the task to the _Canceled state.
2158 
2159  if (_M_Continuations)
2160  {
2161  // Scheduling cancellation with automatic inlining.
2163  }
2164 
2165  break;
2166  }
2167  case _Nothing:
2168  default:
2169  break;
2170  }
2171  return true;
2172  }
std::shared_ptr< _ExceptionHolder > _M_exceptionHolder
Definition: ppltasks.h:2027
bool _IsCompleted()
Definition: ppltasks.h:1657
bool _HasUserException()
Definition: ppltasks.h:1667
_ContinuationList _M_Continuations
Definition: ppltasks.h:2032
bool _IsCanceled()
Definition: ppltasks.h:1662
::Concurrency::details::_TaskCollection_t _M_TaskCollection
Definition: ppltasks.h:2041
_CRTIMP2 void __thiscall _LogCancelTask()
bool _Cancel(bool _SynchronousCancel)
Definition: ppltasks.h:1597
_TaskEventLogger _M_taskEventLogger
Definition: ppltasks.h:2046
Definition: pplinterface.h:226
#define _ASSERTE(expr)
Definition: crtdbg.h:707
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2017
std::mutex _M_ContinuationsCritSec
Definition: ppltasks.h:2031
bool _IsPendingCancel()
Definition: ppltasks.h:1652
void _Complete()
Definition: pplwin.h:223
bool _IsStarted()
Definition: ppltasks.h:1647
std::function< void()> _M_InternalCancellation
Definition: ppltasks.h:2255
void _Cancel()
Definition: pplwin.h:208
void _RunTaskContinuations()
Definition: ppltasks.h:1901
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:492
template<typename _ReturnType >
void Concurrency::details::_Task_impl< _ReturnType >::_FinalizeAndRunContinuations ( _ReturnType  _Result)
inline
2175  {
2177 
2178  {
2179  //
2180  // Hold this lock to ensure continuations being concurrently either get added
2181  // to the _M_Continuations vector or wait for the result
2182  //
2183  ::std::lock_guard<std::mutex> _LockHolder(_M_ContinuationsCritSec);
2184 
2185  // A task could still be in the _Created state if it was created with a task_completion_event.
2186  // It could also be in the _Canceled state for the same reason.
2188  if (_IsCanceled())
2189  {
2190  return;
2191  }
2192 
2193  // Always transition to "completed" state, even in the face of unacknowledged pending cancellation
2195  }
2198  }
bool _IsCompleted()
Definition: ppltasks.h:1657
bool _HasUserException()
Definition: ppltasks.h:1667
bool _IsCanceled()
Definition: ppltasks.h:1662
::Concurrency::details::_TaskCollection_t _M_TaskCollection
Definition: ppltasks.h:2041
#define _ASSERTE(expr)
Definition: crtdbg.h:707
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2017
std::mutex _M_ContinuationsCritSec
Definition: ppltasks.h:2031
void Set(const _Type &_type)
Definition: ppltasks.h:624
void _Complete()
Definition: pplwin.h:223
_ResultHolder< _ReturnType > _M_Result
Definition: ppltasks.h:2254
void _RunTaskContinuations()
Definition: ppltasks.h:1901
_Result
Definition: corecrt_wconio.h:362
template<typename _ReturnType >
_ReturnType Concurrency::details::_Task_impl< _ReturnType >::_GetResult ( )
inline
2250  {
2251  return _M_Result.Get();
2252  }
_ResultHolder< _ReturnType > _M_Result
Definition: ppltasks.h:2254
_Type Get()
Definition: ppltasks.h:629
template<typename _ReturnType >
bool Concurrency::details::_Task_impl< _ReturnType >::_IsDone ( )
inline
2245  {
2246  return _IsCompleted() || _IsCanceled();
2247  }
bool _IsCompleted()
Definition: ppltasks.h:1657
bool _IsCanceled()
Definition: ppltasks.h:1662
template<typename _ReturnType >
bool Concurrency::details::_Task_impl< _ReturnType >::_TransitionedToStarted ( )
inline
2204  {
2205  ::std::lock_guard<std::mutex> _LockHolder(_M_ContinuationsCritSec);
2206  // Canceled state could only result from antecedent task's canceled state, but that code path will not reach here.
2207  _ASSERT(!_IsCanceled());
2208  if (_IsPendingCancel())
2209  return false;
2210 
2211  _ASSERTE(_IsCreated());
2213  return true;
2214  }
bool _IsCanceled()
Definition: ppltasks.h:1662
#define _ASSERTE(expr)
Definition: crtdbg.h:707
volatile _TaskInternalState _M_TaskState
Definition: ppltasks.h:2017
bool _IsCreated()
Definition: ppltasks.h:1642
std::mutex _M_ContinuationsCritSec
Definition: ppltasks.h:2031
bool _IsPendingCancel()
Definition: ppltasks.h:1652
#define _ASSERT(expr)
Definition: crtdbg.h:703

Member Data Documentation

template<typename _ReturnType >
std::function<void()> Concurrency::details::_Task_impl< _ReturnType >::_M_InternalCancellation
template<typename _ReturnType >
_ResultHolder<_ReturnType> Concurrency::details::_Task_impl< _ReturnType >::_M_Result

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