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 yieldFunction 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

589  : _M_yieldFunction(_YieldMethod), _M_state(_StateInitial)
590  {
591  // Defer initialization of other fields to _SpinOnce().
592  }
_YieldFunction _M_yieldFunction
Definition: concrt.h:756
_SpinState _M_state
Definition: concrt.h:755

Member Function Documentation

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

Yields its time slice using the specified yieldFunction

698  {
699 #pragma warning ( push )
700 #pragma warning ( disable : 6326 ) // potential comparison of a constant with another constant
701  bool _ShouldYield = (_YieldCount != 0);
702 #pragma warning ( pop )
703  if (_ShouldYield)
704  {
707  }
708  else
709  {
710  _YieldProcessor();
711  }
712  }
#define NULL
Definition: vcruntime.h:236
_YieldFunction _M_yieldFunction
Definition: concrt.h:756
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
void _YieldProcessor()
Definition: concrt.h:80
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
737  {
738  return 1;
739  }
template<unsigned int _YieldCount = 1>
void Concurrency::details::_SpinWait< _YieldCount >::_Reset ( )
inlineprotected

Resets the counts and state to the default.

719  {
721 
722  // Reset to the default spin value. The value specified
723  // by the client is ignored on a reset.
725 
727  }
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
static _CONCRTIMP unsigned int __cdecl _Value()
_SpinState _M_state
Definition: concrt.h:755
void _SetSpinCount(unsigned int _Count)
Set a dynamic spin count.
Definition: concrt.h:598
template<unsigned int _YieldCount = 1>
void Concurrency::details::_SpinWait< _YieldCount >::_SetSpinCount ( unsigned int  _Count)
inline

Set a dynamic spin count.

599  {
601  if (_Count == 0)
602  {
603  // Specify a count of 0 if we are on a single proc.
605  }
606  else
607  {
609  _M_currentYield = _YieldCount;
611  }
612  }
unsigned int _Count
Definition: xcomplex:668
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
_SpinState _M_state
Definition: concrt.h:755
unsigned long _M_currentYield
Definition: concrt.h:754
unsigned long _M_currentSpin
Definition: concrt.h:753
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.
749  {
750  return (--_M_currentSpin > 0);
751  }
unsigned long _M_currentSpin
Definition: concrt.h:753
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.

627  {
628  switch (_M_state)
629  {
630  case _StateSpin:
631  {
632  unsigned long _Count = _NumberOfSpins();
633 
634  for (unsigned long _I = 0; _I < _Count; _I++)
635  {
636  _YieldProcessor();
637  }
638 
639  if (!_ShouldSpinAgain())
640  {
642  }
643 
644  return true;
645  }
646 
647  case _StateYield:
649  if (--_M_currentYield == 0)
650  {
652  }
653 
654  // Execute the yield
655  _DoYield();
656  return true;
657 
658  case _StateBlock:
659  // Reset to defaults if client does not block
660  _Reset();
661  return false;
662 
663  case _StateSingle:
664  // No need to spin on a single processor: just execute the yield
665  _DoYield();
666  return false;
667 
668  case _StateInitial:
669  // Reset counters to their default value and Spin once.
670  _Reset();
671  return _SpinOnce();
672  default:
673  // Unreached
674  return false;
675  };
676  }
unsigned int _Count
Definition: xcomplex:668
#define _CONCRT_ASSERT(x)
Definition: concrt.h:123
bool _SpinOnce()
Spins for one time quantum,until a maximum spin is reached.
Definition: concrt.h:626
void _Reset()
Resets the counts and state to the default.
Definition: concrt.h:718
void _YieldProcessor()
Definition: concrt.h:80
_SpinState _M_state
Definition: concrt.h:755
unsigned long _M_currentYield
Definition: concrt.h:754
bool _ShouldSpinAgain()
Determines whether maximum spin has been reached
Definition: concrt.h:748
void _DoYield()
Yields its time slice using the specified yieldFunction
Definition: concrt.h:697
unsigned long _NumberOfSpins()
Determines the current spin count
Definition: concrt.h:736

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: