STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
Concurrency::details::_SpinWait< _YieldCount > Class Template Reference

Implements busy wait with no backoff More...

#include <concrt.h>

Public Types

typedef void(__cdecl * _YieldFunction) ()
 

Public Member Functions

 _SpinWait (_YieldFunction _YieldMethod=_UnderlyingYield)
 Construct a spin wait object More...
 
void _SetSpinCount (unsigned int _Count)
 Set a dynamic spin count. More...
 
bool _SpinOnce ()
 Spins for one time quantum,until a maximum spin is reached. More...
 

Protected Types

enum  _SpinState {
  _StateInitial, _StateSpin, _StateYield, _StateBlock,
  _StateSingle
}
 State of the spin wait class. More...
 

Protected Member Functions

void _DoYield ()
 Yields its time slice using the specified yieldFunciton More...
 
void _Reset ()
 Resets the counts and state to the default. More...
 
unsigned long _NumberOfSpins ()
 Determines the current spin count More...
 
bool _ShouldSpinAgain ()
 Determines whether maximum spin has been reached More...
 

Protected Attributes

unsigned long _M_currentSpin
 
unsigned long _M_currentYield
 
_SpinState _M_state
 
_YieldFunction _M_yieldFunction
 

Detailed Description

template<unsigned int _YieldCount = 1>
class Concurrency::details::_SpinWait< _YieldCount >

Implements busy wait with no backoff

Member Typedef Documentation

template<unsigned int _YieldCount = 1>
typedef void(__cdecl * Concurrency::details::_SpinWait< _YieldCount >::_YieldFunction) ()

Member Enumeration Documentation

template<unsigned int _YieldCount = 1>
enum Concurrency::details::_SpinWait::_SpinState
protected

State of the spin wait class.

Enumerator
_StateInitial 
_StateSpin 
_StateYield 
_StateBlock 
_StateSingle 

Constructor & Destructor Documentation

template<unsigned int _YieldCount = 1>
Concurrency::details::_SpinWait< _YieldCount >::_SpinWait ( _YieldFunction  _YieldMethod = _UnderlyingYield)
inline

Construct a spin wait object

615  : _M_yieldFunction(_YieldMethod), _M_state(_StateInitial)
616  {
617  // Defer initialization of other fields to _SpinOnce().
618  }
_YieldFunction _M_yieldFunction
Definition: concrt.h:779
_SpinState _M_state
Definition: concrt.h:778

Member Function Documentation

template<unsigned int _YieldCount = 1>
void Concurrency::details::_SpinWait< _YieldCount >::_DoYield ( )
inlineprotected

Yields its time slice using the specified yieldFunciton

724  {
725  bool _ShouldYield = (_YieldCount != 0);
726  if (_ShouldYield)
727  {
730  }
731  else
732  {
733  _YieldProcessor();
734  }
735  }
_YieldFunction _M_yieldFunction
Definition: concrt.h:779
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
#define NULL
Definition: crtdbg.h:30
void _YieldProcessor()
Definition: concrt.h:76
template<unsigned int _YieldCount = 1>
unsigned long Concurrency::details::_SpinWait< _YieldCount >::_NumberOfSpins ( )
inlineprotected

Determines the current spin count

Returns
The number of spins to execute for this iteration
760  {
761  return 1;
762  }
template<unsigned int _YieldCount = 1>
void Concurrency::details::_SpinWait< _YieldCount >::_Reset ( )
inlineprotected

Resets the counts and state to the default.

742  {
744 
745  // Reset to the default spin value. The value specified
746  // by the client is ignored on a reset.
748 
750  }
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
_SpinState _M_state
Definition: concrt.h:778
static _CRTIMP unsigned int __cdecl _Value()
void _SetSpinCount(unsigned int _Count)
Set a dynamic spin count.
Definition: concrt.h:624
template<unsigned int _YieldCount = 1>
void Concurrency::details::_SpinWait< _YieldCount >::_SetSpinCount ( unsigned int  _Count)
inline

Set a dynamic spin count.

625  {
627  if (_Count == 0)
628  {
629  // Specify a count of 0 if we are on a single proc.
631  }
632  else
633  {
635  _M_currentYield = _YieldCount;
637  }
638  }
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
_SpinState _M_state
Definition: concrt.h:778
unsigned long _M_currentYield
Definition: concrt.h:777
unsigned long _M_currentSpin
Definition: concrt.h:776
_Diff _Count
Definition: algorithm:1941
template<unsigned int _YieldCount = 1>
bool Concurrency::details::_SpinWait< _YieldCount >::_ShouldSpinAgain ( )
inlineprotected

Determines whether maximum spin has been reached

Returns
false if spin count has reached steady state, true otherwise.
772  {
773  return (--_M_currentSpin > 0);
774  }
unsigned long _M_currentSpin
Definition: concrt.h:776
template<unsigned int _YieldCount = 1>
bool Concurrency::details::_SpinWait< _YieldCount >::_SpinOnce ( )
inline

Spins for one time quantum,until a maximum spin is reached.

Returns
false if spin count has reached steady state, true otherwise.

If the spin count is not changing do not spin again because there is either only one processor, or the maximum spin has been reached and blocking is probably a better solution. However, if called again, SpinOnce will spin for a maximum spin count.

653  {
654  switch (_M_state)
655  {
656  case _StateSpin:
657  {
658  unsigned long _Count = _NumberOfSpins();
659 
660  for (unsigned long _I = 0; _I < _Count; _I++)
661  {
662  _YieldProcessor();
663  }
664 
665  if (!_ShouldSpinAgain())
666  {
668  }
669 
670  return true;
671  }
672 
673  case _StateYield:
675  if (--_M_currentYield == 0)
676  {
678  }
679 
680  // Execute the yield
681  _DoYield();
682  return true;
683 
684  case _StateBlock:
685  // Reset to defaults if client does not block
686  _Reset();
687  return false;
688 
689  case _StateSingle:
690  // No need to spin on a single processor: just execute the yield
691  _DoYield();
692  return false;
693 
694  case _StateInitial:
695  // Reset counters to their default value and Spin once.
696  _Reset();
697  return _SpinOnce();
698  default:
699  // Unreached
700  return false;
701  };
702  }
#define _CONCRT_ASSERT(x)
Definition: concrt.h:137
bool _SpinOnce()
Spins for one time quantum,until a maximum spin is reached.
Definition: concrt.h:652
void _Reset()
Resets the counts and state to the default.
Definition: concrt.h:741
void _YieldProcessor()
Definition: concrt.h:76
_SpinState _M_state
Definition: concrt.h:778
unsigned long _M_currentYield
Definition: concrt.h:777
bool _ShouldSpinAgain()
Determines whether maximum spin has been reached
Definition: concrt.h:771
void _DoYield()
Yields its time slice using the specified yieldFunciton
Definition: concrt.h:723
_Diff _Count
Definition: algorithm:1941
unsigned long _NumberOfSpins()
Determines the current spin count
Definition: concrt.h:759

Member Data Documentation

template<unsigned int _YieldCount = 1>
unsigned long Concurrency::details::_SpinWait< _YieldCount >::_M_currentSpin
protected
template<unsigned int _YieldCount = 1>
unsigned long Concurrency::details::_SpinWait< _YieldCount >::_M_currentYield
protected
template<unsigned int _YieldCount = 1>
_SpinState Concurrency::details::_SpinWait< _YieldCount >::_M_state
protected
template<unsigned int _YieldCount = 1>
_YieldFunction Concurrency::details::_SpinWait< _YieldCount >::_M_yieldFunction
protected

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