📄 contextbase.h
字号:
m_fContextChainedStealer = false;
}
/// <summary>
/// Returns whether the given context's steal is chained to the context (true) or some task collection (false)
/// </summary>
bool IsContextChainedStealer() const
{
return m_fContextChainedStealer;
}
/// <summary>
/// Called on both internal and external contexts, either when the are put into an idle pool to
/// be recycled, or when they are ready to be deleted. The API moves the contexts that are in
/// the list of 'stealers' (used for cancellation) to lists in the task collections from which
/// those contexts have stolen chores.
/// </summary>
void DetachStealers();
/// <summary>
/// Gets an arbitrary alias out of the context's alias table.
/// </summary>
_TaskCollection *GetArbitraryAlias(_TaskCollection *pCollection)
{
Hash<_TaskCollection*, _TaskCollection*>::ListNode *pNode = m_aliasTable.Find(pCollection, NULL);
_TaskCollection *pAlias = (pNode != NULL ? pNode->m_value : NULL);
if (pAlias != NULL && pAlias->_IsStaleAlias())
{
m_aliasTable.Delete(pAlias->_OriginalCollection());
delete pAlias;
pAlias = NULL;
}
return pAlias;
}
/// <summary>
/// Adds an arbitrary alias (direct or indirect) to the alias table.
/// </summary>
void AddArbitraryAlias(_TaskCollection *pOriginCollection, _TaskCollection *pAliasCollection)
{
SweepAliasTable();
m_aliasTable.Insert(pOriginCollection, pAliasCollection);
}
/// <summary>
/// Sweeps the alias table for stale entries. Anything considered stale is deleted.
/// </summary>
void SweepAliasTable();
/// <summary>
/// Clears the alias table.
/// </summary>
void ClearAliasTable();
/// <summary>
/// Cancel everything stolen from pCollection outward from this context.
/// </summary>
void CancelStealers(_TaskCollectionBase *pCollection);
/// <summary>
/// Returns the highest inlining depth (tree wise) of a canceled task collection. Note that it will return -1
/// if there is no in-progress cancellation on the context.
/// </summary>
int MinimumCancellationDepth() const
{
//
// If the entire context is canceled, the minimum depth is reported to be zero so as to be less than all inlining depths
// for the purposes of checking cancellation. Note that even if the top collection has inlining depth of zero, it does not matter
// since it **IS** the top collection.
//
return IsCanceledContext() ? 0 : m_minCancellationDepth;
}
// An enumerated type that tells the type of the underlying execution context.
enum ContextKind
{
ExternalContext,
ThreadContext,
UMSThreadContext,
UMSThreadSchedulingUT
};
/// <summary>
/// Returns the type of context
/// </summary>
virtual ContextKind GetContextKind() const = 0;
#if _DEBUG
// _DEBUG helper
virtual DWORD GetThreadId() const = 0;
#endif
protected:
class ScopedCriticalRegion
{
public:
ScopedCriticalRegion(ContextBase* pCB) : m_pCB(pCB)
{
m_pCB->EnterCriticalRegion();
}
~ScopedCriticalRegion()
{
m_pCB->ExitCriticalRegion();
}
private:
const ScopedCriticalRegion& operator=(const ScopedCriticalRegion&); //no assigment operator
ContextBase* m_pCB;
};
//
// Protected data members
//
// Entry for freelist
SLIST_ENTRY m_slNext;
// Unique identifier
unsigned int m_id;
// Critical region counter.
DWORD m_criticalRegionCount;
// Hyper-critical region counter.
DWORD m_hyperCriticalRegionCount;
// Oversubscription count - the number of outstanding Oversubscribe(true) calls on this context.
DWORD m_oversubscribeCount;
// The schedule group the context is associated with.
ScheduleGroupBase *m_pGroup;
// The scheduler instance the context belongs to.
SchedulerBase *m_pScheduler;
// Workqueue for unrealized chores.
WorkQueue *m_pWorkQueue;
// Link to implement the stack of parent contexts for nested schedulers.
ContextBase *m_pParentContext;
// Flag indicating whether the context is blocked.
volatile LONG m_blockedState;
// Memory fence to assist Block/Unblock.
volatile LONG m_contextSwitchingFence;
// Tracks the task collection from which this context stole (if it's a context executing a stolen chore).
_TaskCollectionBase *m_pRootCollection;
// Tracks the task collection currently executing (used to maintain parent/child relationships).
_TaskCollectionBase *m_pExecutingCollection;
// The thread id for the thread backing the context.
DWORD m_threadId;
//
// Protected methods
//
/// <summary>
/// Clean up the work queue for this Context.
/// </summary>
void ReleaseWorkQueue();
/// <summary>
/// Sets the 'this' context into the tls slot as the current context. This is used by internal contexts in
/// their dispatch loops.
/// </summary>
void SetAsCurrentTls();
///<summary>Send a context ETW event</summary>
void TraceContextEvent(ConcRT_EventType eventType, UCHAR level, DWORD schedulerId, DWORD contextId)
{
if (g_TraceOn && level <= g_EnableLevel)
ThrowContextEvent(eventType, level, schedulerId, contextId);
}
static void ThrowContextEvent(ConcRT_EventType eventType, UCHAR level, DWORD schedulerId, DWORD contextId);
private:
//
// Friend declarations
//
friend class SchedulerBase;
friend class ThreadScheduler;
friend class UMSThreadScheduler;
friend class InternalContextBase;
friend class SchedulingRing;
friend class VirtualProcessor;
friend class ScheduleGroupBase;
friend class UMSThreadVirtualProcessor;
friend class ScheduleGroup;
friend class FairScheduleGroup;
friend class CacheLocalScheduleGroup;
friend class _UnrealizedChore;
friend class _TaskCollection;
friend class _StructuredTaskCollection;
friend class UMSSchedulingContext;
template <class T> friend class LockFreeStack;
//
// Private data
//
// Used in finalization to distinguish between blocked and free-list contexts
LONG m_sweeperMarker;
// Flag indicating context kind.
bool m_fIsExternal;
// Keeps track as to whether this context is chained to a context (true) or a schedule group (false) for the purposes of stealing/cancellation.
bool m_fContextChainedStealer;
// Indicates that normal lock validations should not be performed -- the context is shutting down a virtual processor.
bool m_fShutdownValidations;
// Tracks all contexts which stole from any collection on *this* context.
SafeRWList<ListEntry> m_stealers;
// Link for contexts added to m_stealers
ListEntry m_stealChain;
// Reference count of things waiting to be added to the steal chain of this context.
volatile LONG m_cancellationRefCount;
// The inlining depth of a canceled task collection.
volatile LONG m_minCancellationDepth;
// The number of task collections running on this context which have been canceled.
volatile LONG m_canceledCount;
// An indication that the context was shot down as it stole from a canceled collection.
volatile LONG m_canceledContext;
// An indication that there is a pending cancellation of a structured collection on this thread.
volatile LONG m_pendingCancellations;
// The indirect alias for this context. This allows an unstructured task collection to carry into a stolen chore and be
// utilized there without any cross threaded semantics within the task collection.
_TaskCollection *m_pIndirectAlias;
// The table of aliases for this context. This allows transitive indirect aliases as well as direct aliases (which
// are not presently implemented).
Hash<_TaskCollection*, _TaskCollection*> m_aliasTable;
//
// Private member functions
//
/// <summary>
/// When schedulers are nested on the same stack context, the nested scheduler creates a new external context that overrides
/// the previous context. PopContextFromTls will restore the previous context by setting the TLS value appropriately.
/// </summary>
ContextBase* PopContextFromTls();
/// <summary>
/// When schedulers are nested on the same stack context, the nested scheduler creates a new external context that overrides
/// the previous context. PushContextToTls will remember the parent context and set the new context into TLS.
/// </summary>
void PushContextToTls(ContextBase* pParentContext);
/// <summary>
/// Context TLS is cleared during nesting on internal contexts before the external context TLS is correctly setup. If not,
/// code that executes between the clear and setting the new TLS could get confused.
/// </summary>
void ClearContextTls();
};
} // namespace details
} // namespace Concurrency
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -