From 28bea3238389ccb2f6a4e85266e457cafa2c8d5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Laval?= Date: Mon, 22 Nov 2010 13:48:38 +0000 Subject: [PATCH] Improve fallback SpinWait to include the initial logic of SpinWait --- .../SplitOrderedList.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mcs/class/corlib/System.Collections.Concurrent/SplitOrderedList.cs b/mcs/class/corlib/System.Collections.Concurrent/SplitOrderedList.cs index 66b67bdabb7..4824d52621e 100644 --- a/mcs/class/corlib/System.Collections.Concurrent/SplitOrderedList.cs +++ b/mcs/class/corlib/System.Collections.Concurrent/SplitOrderedList.cs @@ -465,9 +465,24 @@ namespace System.Collections.Concurrent #if INSIDE_SYSTEM_WEB && !NET_4_0 && !BOOTSTRAP_NET_4_0 internal struct SpinWait { + const int step = 5; + const int maxTime = 50; + static readonly bool isSingleCpu = (Environment.ProcessorCount == 1); + + int ntime; + public void SpinOnce () { - Thread.SpinWait (30); + if (isSingleCpu) { + // On a single-CPU system, spinning does no good + Thread.Sleep (0); + } else { + if ((ntime = ntime == maxTime ? maxTime : ntime + 1) % step == 0) + Thread.Sleep (0); + else + // Multi-CPU system might be hyper-threaded, let other thread run + Thread.SpinWait (ntime << 1); + } } } #endif -- 2.25.1