Rework ReaderWriterLockSlim to use simpler Interlocked arithmetic.
[mono.git] / mcs / class / corlib / System.Threading / SpinWait.cs
index 73d89609e08d5107b0a693a242571fcbb1ea6596..d46376bc0439bde0dbde55c160a338c87010d14a 100644 (file)
@@ -1,4 +1,3 @@
-#if NET_4_0 || BOOTSTRAP_NET_4_0
 // SpinWait.cs
 //
 // Copyright (c) 2008 Jérémie "Garuma" Laval
@@ -23,6 +22,7 @@
 //
 //
 
+#if NET_4_0 || BOOTSTRAP_NET_4_0
 using System;
 
 namespace System.Threading
@@ -30,25 +30,23 @@ namespace System.Threading
        public struct SpinWait
        {
                // The number of step until SpinOnce yield on multicore machine
-               const           int  step = 10;
-               const           int  maxSpin = 1000;
+               const           int  step = 5;
+               const           int  maxTime = 50;
                static readonly bool isSingleCpu = (Environment.ProcessorCount == 1);
 
                int ntime;
 
                public void SpinOnce ()
                {
-                       if (ntime > maxSpin) {
-                               Thread.Sleep (1);
-                       } else if (isSingleCpu) {
+                       if (isSingleCpu) {
                                // On a single-CPU system, spinning does no good
                                Thread.Yield ();
                        } else {
-                               if (++ntime % step == 0)
+                               if ((ntime = ntime == maxTime ? maxTime : ntime + 1) % step == 0)
                                        Thread.Yield ();
                                else
                                        // Multi-CPU system might be hyper-threaded, let other thread run
-                                       Thread.SpinWait (Math.Min (ntime << 1, maxSpin));
+                                       Thread.SpinWait (ntime << 1);
                        }
                }