SpinWait.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #if NET_4_0 || BOOTSTRAP_NET_4_0
  25. using System;
  26. namespace System.Threading
  27. {
  28. public struct SpinWait
  29. {
  30. // The number of step until SpinOnce yield on multicore machine
  31. const int step = 5;
  32. const int maxTime = 50;
  33. static readonly bool isSingleCpu = (Environment.ProcessorCount == 1);
  34. int ntime;
  35. public void SpinOnce ()
  36. {
  37. if (isSingleCpu) {
  38. // On a single-CPU system, spinning does no good
  39. Thread.Yield ();
  40. } else {
  41. if ((ntime = ntime == maxTime ? maxTime : ntime + 1) % step == 0)
  42. Thread.Yield ();
  43. else
  44. // Multi-CPU system might be hyper-threaded, let other thread run
  45. Thread.SpinWait (ntime << 1);
  46. }
  47. }
  48. public static void SpinUntil (Func<bool> predicate)
  49. {
  50. SpinWait sw = new SpinWait ();
  51. while (!predicate ())
  52. sw.SpinOnce ();
  53. }
  54. public static bool SpinUntil (Func<bool> predicate, TimeSpan ts)
  55. {
  56. return SpinUntil (predicate, (int)ts.TotalMilliseconds);
  57. }
  58. public static bool SpinUntil (Func<bool> predicate, int milliseconds)
  59. {
  60. SpinWait sw = new SpinWait ();
  61. Watch watch = Watch.StartNew ();
  62. while (!predicate ()) {
  63. if (watch.ElapsedMilliseconds > milliseconds)
  64. return false;
  65. sw.SpinOnce ();
  66. }
  67. return true;
  68. }
  69. public void Reset ()
  70. {
  71. ntime = 0;
  72. }
  73. public bool NextSpinWillYield {
  74. get {
  75. return isSingleCpu ? true : ntime % step == 0;
  76. }
  77. }
  78. public int Count {
  79. get {
  80. return ntime;
  81. }
  82. }
  83. }
  84. }
  85. #endif