SpinWait.cs
Go to the documentation of this file.
1 // SpinWait.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24 
25 #if NET_4_0
26 using System;
27 
28 namespace System.Threading
29 {
30  public struct SpinWait
31  {
32  // The number of step until SpinOnce yield on multicore machine
33  const int step = 10;
34  const int maxTime = 200;
35  static readonly bool isSingleCpu = (Environment.ProcessorCount == 1);
36 
37  int ntime;
38 
39  public void SpinOnce ()
40  {
41  ntime += 1;
42 
43  if (isSingleCpu) {
44  // On a single-CPU system, spinning does no good
45  Yield ();
46  } else {
47  if (ntime % step == 0)
48  Yield ();
49  else
50  // Multi-CPU system might be hyper-threaded, let other thread run
51  Thread.SpinWait (Math.Min (ntime, maxTime) << 1);
52  }
53  }
54 
55  // Spicy Pixel: Thread.Yield () is not supported in .NET < 4
56  const int s1step = 20;
57  int s1time;
58  void Yield ()
59  {
60  // Replace sched_yield by Thread.Sleep(0) which does almost the same thing
61  // (going back in kernel mode and yielding) but avoid the branching and unmanaged bridge
62  //Thread.Sleep (0); // Thread.Yield() is better
63 
64  /*
65  http://stackoverflow.com/questions/1413630/switchtothread-thread-yield-vs-thread-sleep0-vs-thead-sleep1
66 
67  SwitchToThread [win32] / Thread.Yield [.NET 4 Beta 1]: yields to any thread on same processor
68  Advantage: about twice as fast as Thread.Sleep(0)
69  Disadvantage: yields only to threads on same processor
70 
71  Thread.Sleep(0): yields to any thread of same or higher priority on any processor
72  Advantage: faster than Thread.Sleep(1)
73  Disadvantage: yields only to threads of same or higher priority
74 
75  Thread.Sleep(1): yields to any thread on any processor
76  Advantage: yields to any thread on any processor
77  Disadvantage: slowest option (Thread.Sleep(1) will usually suspend the thread by about 15ms if timeBeginPeriod/timeEndPeriod [win32] are not used)
78  */
79 
80  // Because Sleep(1) pulls us out of the queue for 10-15ms or so,
81  // don't do it that often.
82  s1time += 1;
83 
84  if(s1time % s1step == 0)
85  Thread.Sleep(1);
86  else
87  Thread.Sleep(0);
88  }
89 
90  public static void SpinUntil (Func<bool> condition)
91  {
92  SpinWait sw = new SpinWait ();
93  while (!condition ())
94  sw.SpinOnce ();
95  }
96 
97  public static bool SpinUntil (Func<bool> condition, TimeSpan timeout)
98  {
99  return SpinUntil (condition, (int)timeout.TotalMilliseconds);
100  }
101 
102  public static bool SpinUntil (Func<bool> condition, int millisecondsTimeout)
103  {
104  SpinWait sw = new SpinWait ();
105  Watch watch = Watch.StartNew ();
106 
107  while (!condition ()) {
108  if (watch.ElapsedMilliseconds > millisecondsTimeout)
109  return false;
110  sw.SpinOnce ();
111  }
112 
113  return true;
114  }
115 
116  public void Reset ()
117  {
118  ntime = 0;
119  s1time = 0;
120  }
121 
122  public bool NextSpinWillYield {
123  get {
124  return isSingleCpu ? true : ntime % step == 0;
125  }
126  }
127 
128  public int Count {
129  get {
130  return ntime;
131  }
132  }
133  }
134 }
135 #endif
static void SpinUntil(Func< bool > condition)
Definition: SpinWait.cs:90
static bool SpinUntil(Func< bool > condition, int millisecondsTimeout)
Definition: SpinWait.cs:102
static bool SpinUntil(Func< bool > condition, TimeSpan timeout)
Definition: SpinWait.cs:97