summaryrefslogtreecommitdiff
path: root/src/utilcode
diff options
context:
space:
mode:
authorKoundinya Veluri <kouvel@users.noreply.github.com>2019-01-10 17:51:53 -0800
committerGitHub <noreply@github.com>2019-01-10 17:51:53 -0800
commit616fea550548af750b575f3c304d1a9b4b6ef9a6 (patch)
treedc5468ff6b745ef2aac69e899f9616a494dd48a9 /src/utilcode
parent459b58a7766707fb059a5762c7d72cb0af42a6ff (diff)
downloadcoreclr-616fea550548af750b575f3c304d1a9b4b6ef9a6.tar.gz
coreclr-616fea550548af750b575f3c304d1a9b4b6ef9a6.tar.bz2
coreclr-616fea550548af750b575f3c304d1a9b4b6ef9a6.zip
Normalize a few more spin-wait loops (#21586)
Normalize a few more spin-wait loops - Fixed a few more spin-waits to normalize the spin-wait duration between processors - These spin-waits have so far not needed to be retuned to avoid unreasonably long spin-wait durations. They can be retuned as necessary in the future. - Added a version of YieldProcessorNormalized() that normalizes based on spin-wait counts tuned for pre-Skylake processors for spin-wait loops that have not been retuned. - Moved some files around to make YieldProcessorNormalized() and the like available in more places. Initialization is still only done in the VM. Uses outside the VM will use the defaults, where there would be no significant change from before. - Made YieldProcessor() private outside of the GC and added System_YieldProcessor() for when the system-defined implementation is intended to be used
Diffstat (limited to 'src/utilcode')
-rw-r--r--src/utilcode/CMakeLists.txt1
-rw-r--r--src/utilcode/utsem.cpp42
-rw-r--r--src/utilcode/yieldprocessornormalized.cpp10
3 files changed, 15 insertions, 38 deletions
diff --git a/src/utilcode/CMakeLists.txt b/src/utilcode/CMakeLists.txt
index f591e7cbec..fa9abb73c8 100644
--- a/src/utilcode/CMakeLists.txt
+++ b/src/utilcode/CMakeLists.txt
@@ -55,6 +55,7 @@ set(UTILCODE_COMMON_SOURCES
pedecoder.cpp
winfix.cpp
longfilepathwrappers.cpp
+ yieldprocessornormalized.cpp
)
# These source file do not yet compile on Linux.
diff --git a/src/utilcode/utsem.cpp b/src/utilcode/utsem.cpp
index a8b7729734..d6a6e95f70 100644
--- a/src/utilcode/utsem.cpp
+++ b/src/utilcode/utsem.cpp
@@ -232,25 +232,8 @@ HRESULT UTSemReadWrite::LockRead()
}
// Delay by approximately 2*i clock cycles (Pentium III).
- // This is brittle code - future processors may of course execute this
- // faster or slower, and future code generators may eliminate the loop altogether.
- // The precise value of the delay is not critical, however, and I can't think
- // of a better way that isn't machine-dependent.
- int sum = 0;
-
- for (int delayCount = i; --delayCount; )
- {
- sum += delayCount;
- YieldProcessor(); // indicate to the processor that we are spining
- }
-
- if (sum == 0)
- {
- // never executed, just to fool the compiler into thinking sum is live here,
- // so that it won't optimize away the loop.
- static char dummy;
- dummy++;
- }
+ YieldProcessorNormalizedForPreSkylakeCount(i);
+
// exponential backoff: wait a factor longer in the next iteration
i *= g_SpinConstants.dwBackoffFactor;
} while (i < g_SpinConstants.dwMaximumDuration);
@@ -341,25 +324,8 @@ HRESULT UTSemReadWrite::LockWrite()
}
// Delay by approximately 2*i clock cycles (Pentium III).
- // This is brittle code - future processors may of course execute this
- // faster or slower, and future code generators may eliminate the loop altogether.
- // The precise value of the delay is not critical, however, and I can't think
- // of a better way that isn't machine-dependent.
- int sum = 0;
-
- for (int delayCount = i; --delayCount; )
- {
- sum += delayCount;
- YieldProcessor(); // indicate to the processor that we are spining
- }
-
- if (sum == 0)
- {
- // never executed, just to fool the compiler into thinking sum is live here,
- // so that it won't optimize away the loop.
- static char dummy;
- dummy++;
- }
+ YieldProcessorNormalizedForPreSkylakeCount(i);
+
// exponential backoff: wait a factor longer in the next iteration
i *= g_SpinConstants.dwBackoffFactor;
} while (i < g_SpinConstants.dwMaximumDuration);
diff --git a/src/utilcode/yieldprocessornormalized.cpp b/src/utilcode/yieldprocessornormalized.cpp
new file mode 100644
index 0000000000..79d91f8c48
--- /dev/null
+++ b/src/utilcode/yieldprocessornormalized.cpp
@@ -0,0 +1,10 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+#include "stdafx.h"
+
+// Defaults are for when InitializeYieldProcessorNormalized has not yet been called or when no measurement is done, and are
+// tuned for Skylake processors
+unsigned int g_yieldsPerNormalizedYield = 1; // current value is for Skylake processors, this is expected to be ~8 for pre-Skylake
+unsigned int g_optimalMaxNormalizedYieldsPerSpinIteration = 7;