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
2065  : _Task_impl_base(_Ct, _Scheduler_arg)
2066  {
2067  }
_Task_impl_base(_CancellationTokenState *_PTokenState, scheduler_ptr _Scheduler_arg)
Definition: ppltasks.h:1463
template<typename _ReturnType >
virtual Concurrency::details::_Task_impl< _ReturnType >::~_Task_impl ( )
inlinevirtual
2070  {
2071  // We must invoke _DeregisterCancellation in the derived class destructor. Calling it in the base class destructor could cause
2072  // a partially initialized _Task_impl to be in the list of registrations for a cancellation token.
2074  }
void _DeregisterCancellation()
Definition: ppltasks.h:1631

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.

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

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: