mscorlib(4.0.0.0) API with additions
SpinWait.cs
2 
3 namespace System.Threading
4 {
6  [__DynamicallyInvokable]
7  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
8  public struct SpinWait
9  {
10  internal const int YIELD_THRESHOLD = 10;
11 
12  internal const int SLEEP_0_EVERY_HOW_MANY_TIMES = 5;
13 
14  internal const int SLEEP_1_EVERY_HOW_MANY_TIMES = 20;
15 
16  private int m_count;
17 
20  [__DynamicallyInvokable]
21  public int Count
22  {
23  [__DynamicallyInvokable]
24  get
25  {
26  return m_count;
27  }
28  }
29 
32  [__DynamicallyInvokable]
33  public bool NextSpinWillYield
34  {
35  [__DynamicallyInvokable]
36  get
37  {
38  if (m_count <= 10)
39  {
40  return PlatformHelper.IsSingleProcessor;
41  }
42  return true;
43  }
44  }
45 
47  [__DynamicallyInvokable]
48  public void SpinOnce()
49  {
51  {
52  CdsSyncEtwBCLProvider.Log.SpinWait_NextSpinWillYield();
53  int num = (m_count >= 10) ? (m_count - 10) : m_count;
54  if (num % 20 == 19)
55  {
56  Thread.Sleep(1);
57  }
58  else if (num % 5 == 4)
59  {
60  Thread.Sleep(0);
61  }
62  else
63  {
64  Thread.Yield();
65  }
66  }
67  else
68  {
69  Thread.SpinWait(4 << m_count);
70  }
71  m_count = ((m_count == int.MaxValue) ? 10 : (m_count + 1));
72  }
73 
75  [__DynamicallyInvokable]
76  public void Reset()
77  {
78  m_count = 0;
79  }
80 
84  [__DynamicallyInvokable]
85  public static void SpinUntil(Func<bool> condition)
86  {
87  SpinUntil(condition, -1);
88  }
89 
97  [__DynamicallyInvokable]
98  public static bool SpinUntil(Func<bool> condition, TimeSpan timeout)
99  {
100  long num = (long)timeout.TotalMilliseconds;
101  if (num < -1 || num > int.MaxValue)
102  {
103  throw new ArgumentOutOfRangeException("timeout", timeout, Environment.GetResourceString("SpinWait_SpinUntil_TimeoutWrong"));
104  }
105  return SpinUntil(condition, (int)timeout.TotalMilliseconds);
106  }
107 
115  [__DynamicallyInvokable]
116  public static bool SpinUntil(Func<bool> condition, int millisecondsTimeout)
117  {
118  if (millisecondsTimeout < -1)
119  {
120  throw new ArgumentOutOfRangeException("millisecondsTimeout", millisecondsTimeout, Environment.GetResourceString("SpinWait_SpinUntil_TimeoutWrong"));
121  }
122  if (condition == null)
123  {
124  throw new ArgumentNullException("condition", Environment.GetResourceString("SpinWait_SpinUntil_ArgumentNull"));
125  }
126  uint num = 0u;
127  if (millisecondsTimeout != 0 && millisecondsTimeout != -1)
128  {
129  num = TimeoutHelper.GetTime();
130  }
131  SpinWait spinWait = default(SpinWait);
132  while (!condition())
133  {
134  if (millisecondsTimeout == 0)
135  {
136  return false;
137  }
138  spinWait.SpinOnce();
139  if (millisecondsTimeout != -1 && spinWait.NextSpinWillYield && millisecondsTimeout <= TimeoutHelper.GetTime() - num)
140  {
141  return false;
142  }
143  }
144  return true;
145  }
146  }
147 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Reset()
Resets the spin counter.
Definition: SpinWait.cs:76
Provides support for spin-based waiting.
Definition: SpinWait.cs:8
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
Definition: TimeSpan.cs:180
bool NextSpinWillYield
Gets whether the next call to M:System.Threading.SpinWait.SpinOnce will yield the processor,...
Definition: SpinWait.cs:34
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static void SpinUntil(Func< bool > condition)
Spins until the specified condition is satisfied.
Definition: SpinWait.cs:85
void SpinOnce()
Performs a single spin.
Definition: SpinWait.cs:48
int Count
Gets the number of times M:System.Threading.SpinWait.SpinOnce has been called on this instance.
Definition: SpinWait.cs:22
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
static bool SpinUntil(Func< bool > condition, int millisecondsTimeout)
Spins until the specified condition is satisfied or until the specified timeout is expired.
Definition: SpinWait.cs:116
static void Sleep(int millisecondsTimeout)
Suspends the current thread for the specified number of milliseconds.
Definition: Thread.cs:746
static bool Yield()
Causes the calling thread to yield execution to another thread that is ready to run on the current pr...
Definition: Thread.cs:797
static bool SpinUntil(Func< bool > condition, TimeSpan timeout)
Spins until the specified condition is satisfied or until the specified timeout is expired.
Definition: SpinWait.cs:98
static void SpinWait(int iterations)
Causes a thread to wait the number of times defined by the iterations parameter.
Definition: Thread.cs:779
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18