|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +// Undefine YieldProcessor to encourage using the normalized versions below instead. System_YieldProcessor() can be used where |
| 8 | +// the intention is to use the system-default implementation of YieldProcessor(). |
| 9 | +#define HAS_SYSTEM_YIELDPROCESSOR |
| 10 | +FORCEINLINE void System_YieldProcessor() { YieldProcessor(); } |
| 11 | +#ifdef YieldProcessor |
| 12 | +#undef YieldProcessor |
| 13 | +#endif |
| 14 | +#define YieldProcessor Dont_Use_YieldProcessor |
| 15 | + |
| 16 | +const unsigned int MinNsPerNormalizedYield = 37; // measured typically 37-46 on post-Skylake |
| 17 | +const unsigned int NsPerOptimalMaxSpinIterationDuration = 272; // approx. 900 cycles, measured 281 on pre-Skylake, 263 on post-Skylake |
| 18 | + |
| 19 | +extern unsigned int g_yieldsPerNormalizedYield; |
| 20 | +extern unsigned int g_optimalMaxNormalizedYieldsPerSpinIteration; |
| 21 | + |
| 22 | +void InitializeYieldProcessorNormalizedCrst(); |
| 23 | +void EnsureYieldProcessorNormalizedInitialized(); |
| 24 | + |
| 25 | +class YieldProcessorNormalizationInfo |
| 26 | +{ |
| 27 | +private: |
| 28 | + unsigned int yieldsPerNormalizedYield; |
| 29 | + unsigned int optimalMaxNormalizedYieldsPerSpinIteration; |
| 30 | + unsigned int optimalMaxYieldsPerSpinIteration; |
| 31 | + |
| 32 | +public: |
| 33 | + YieldProcessorNormalizationInfo() |
| 34 | + : yieldsPerNormalizedYield(g_yieldsPerNormalizedYield), |
| 35 | + optimalMaxNormalizedYieldsPerSpinIteration(g_optimalMaxNormalizedYieldsPerSpinIteration), |
| 36 | + optimalMaxYieldsPerSpinIteration(yieldsPerNormalizedYield * optimalMaxNormalizedYieldsPerSpinIteration) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + friend void YieldProcessorNormalized(const YieldProcessorNormalizationInfo &); |
| 41 | + friend void YieldProcessorNormalized(const YieldProcessorNormalizationInfo &, unsigned int); |
| 42 | + friend void YieldProcessorNormalizedForPreSkylakeCount(const YieldProcessorNormalizationInfo &, unsigned int); |
| 43 | + friend void YieldProcessorWithBackOffNormalized(const YieldProcessorNormalizationInfo &, unsigned int); |
| 44 | +}; |
| 45 | + |
| 46 | +// See YieldProcessorNormalized() for preliminary info. Typical usage: |
| 47 | +// if (!condition) |
| 48 | +// { |
| 49 | +// YieldProcessorNormalizationInfo normalizationInfo; |
| 50 | +// do |
| 51 | +// { |
| 52 | +// YieldProcessorNormalized(normalizationInfo); |
| 53 | +// } while (!condition); |
| 54 | +// } |
| 55 | +FORCEINLINE void YieldProcessorNormalized(const YieldProcessorNormalizationInfo &normalizationInfo) |
| 56 | +{ |
| 57 | + unsigned int n = normalizationInfo.yieldsPerNormalizedYield; |
| 58 | + _ASSERTE(n != 0); |
| 59 | + do |
| 60 | + { |
| 61 | + System_YieldProcessor(); |
| 62 | + } while (--n != 0); |
| 63 | +} |
| 64 | + |
| 65 | +// Delays execution of the current thread for a short duration. Unlike YieldProcessor(), an effort is made to normalize the |
| 66 | +// delay across processors. The actual delay may be meaningful in several ways, including but not limited to the following: |
| 67 | +// - The delay should be long enough that a tiny spin-wait like the following has a decent likelihood of observing a new value |
| 68 | +// for the condition (when changed by a different thread) on each iteration, otherwise it may unnecessary increase CPU usage |
| 69 | +// and decrease scalability of the operation. |
| 70 | +// while(!condition) |
| 71 | +// { |
| 72 | +// YieldProcessorNormalized(); |
| 73 | +// } |
| 74 | +// - The delay should be short enough that a tiny spin-wait like above would not miss multiple cross-thread changes to the |
| 75 | +// condition, otherwise it may unnecessarily increase latency of the operation |
| 76 | +// - In reasonably short spin-waits, the actual delay may not matter much. In unreasonably long spin-waits that progress in |
| 77 | +// yield count per iteration for each failed check of the condition, the progression can significantly magnify the second |
| 78 | +// issue above on later iterations. |
| 79 | +// - This function and variants are intended to provide a decent balance between the above issues, as ideal solutions to each |
| 80 | +// issue have trade-offs between them. If latency of the operation is far more important in the scenario, consider using |
| 81 | +// System_YieldProcessor() instead, which would issue a delay that is typically <= the delay issued by this method. |
| 82 | +FORCEINLINE void YieldProcessorNormalized() |
| 83 | +{ |
| 84 | + YieldProcessorNormalized(YieldProcessorNormalizationInfo()); |
| 85 | +} |
| 86 | + |
| 87 | +// See YieldProcessorNormalized(count) for preliminary info. Typical usage: |
| 88 | +// if (!moreExpensiveCondition) |
| 89 | +// { |
| 90 | +// YieldProcessorNormalizationInfo normalizationInfo; |
| 91 | +// do |
| 92 | +// { |
| 93 | +// YieldProcessorNormalized(normalizationInfo, 2); |
| 94 | +// } while (!moreExpensiveCondition); |
| 95 | +// } |
| 96 | +FORCEINLINE void YieldProcessorNormalized(const YieldProcessorNormalizationInfo &normalizationInfo, unsigned int count) |
| 97 | +{ |
| 98 | + _ASSERTE(count != 0); |
| 99 | + |
| 100 | + if (sizeof(SIZE_T) <= sizeof(unsigned int)) |
| 101 | + { |
| 102 | + // On platforms with a small SIZE_T, prevent overflow on the multiply below. normalizationInfo.yieldsPerNormalizedYield |
| 103 | + // is limited to MinNsPerNormalizedYield by InitializeYieldProcessorNormalized(). |
| 104 | + const unsigned int MaxCount = (unsigned int)SIZE_MAX / MinNsPerNormalizedYield; |
| 105 | + if (count > MaxCount) |
| 106 | + { |
| 107 | + count = MaxCount; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + SIZE_T n = (SIZE_T)count * normalizationInfo.yieldsPerNormalizedYield; |
| 112 | + _ASSERTE(n != 0); |
| 113 | + do |
| 114 | + { |
| 115 | + System_YieldProcessor(); |
| 116 | + } while (--n != 0); |
| 117 | +} |
| 118 | + |
| 119 | +// See YieldProcessorNormalized() for preliminary info. This function repeats the delay 'count' times. This overload is |
| 120 | +// preferred over the single-count overload when multiple yields are desired per spin-wait iteration. Typical usage: |
| 121 | +// while(!moreExpensiveCondition) |
| 122 | +// { |
| 123 | +// YieldProcessorNormalized(2); |
| 124 | +// } |
| 125 | +FORCEINLINE void YieldProcessorNormalized(unsigned int count) |
| 126 | +{ |
| 127 | + YieldProcessorNormalized(YieldProcessorNormalizationInfo(), count); |
| 128 | +} |
| 129 | + |
| 130 | +// Please DO NOT use this function in new code! See YieldProcessorNormalizedForPreSkylakeCount(preSkylakeCount) for preliminary |
| 131 | +// info. Typical usage: |
| 132 | +// if (!condition) |
| 133 | +// { |
| 134 | +// YieldProcessorNormalizationInfo normalizationInfo; |
| 135 | +// do |
| 136 | +// { |
| 137 | +// YieldProcessorNormalizedForPreSkylakeCount(normalizationInfo, 100); |
| 138 | +// } while (!condition); |
| 139 | +// } |
| 140 | +FORCEINLINE void YieldProcessorNormalizedForPreSkylakeCount( |
| 141 | + const YieldProcessorNormalizationInfo &normalizationInfo, |
| 142 | + unsigned int preSkylakeCount) |
| 143 | +{ |
| 144 | + _ASSERTE(preSkylakeCount != 0); |
| 145 | + |
| 146 | + if (sizeof(SIZE_T) <= sizeof(unsigned int)) |
| 147 | + { |
| 148 | + // On platforms with a small SIZE_T, prevent overflow on the multiply below. normalizationInfo.yieldsPerNormalizedYield |
| 149 | + // is limited to MinNsPerNormalizedYield by InitializeYieldProcessorNormalized(). |
| 150 | + const unsigned int MaxCount = (unsigned int)SIZE_MAX / MinNsPerNormalizedYield; |
| 151 | + if (preSkylakeCount > MaxCount) |
| 152 | + { |
| 153 | + preSkylakeCount = MaxCount; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + const unsigned int PreSkylakeCountToSkylakeCountDivisor = 8; |
| 158 | + SIZE_T n = (SIZE_T)preSkylakeCount * normalizationInfo.yieldsPerNormalizedYield / PreSkylakeCountToSkylakeCountDivisor; |
| 159 | + if (n == 0) |
| 160 | + { |
| 161 | + n = 1; |
| 162 | + } |
| 163 | + do |
| 164 | + { |
| 165 | + System_YieldProcessor(); |
| 166 | + } while (--n != 0); |
| 167 | +} |
| 168 | + |
| 169 | +// Please DO NOT use this function in new code! This function is to be used for old spin-wait loops that have not been retuned |
| 170 | +// for recent processors, and especially where the yield count may be unreasonably high. The function scales the yield count in |
| 171 | +// an attempt to normalize the total delay across processors, to approximately the total delay that would be issued on a |
| 172 | +// pre-Skylake processor. New code should be tuned with YieldProcessorNormalized() or variants instead. Typical usage: |
| 173 | +// while(!condition) |
| 174 | +// { |
| 175 | +// YieldProcessorNormalizedForPreSkylakeCount(100); |
| 176 | +// } |
| 177 | +FORCEINLINE void YieldProcessorNormalizedForPreSkylakeCount(unsigned int preSkylakeCount) |
| 178 | +{ |
| 179 | + YieldProcessorNormalizedForPreSkylakeCount(YieldProcessorNormalizationInfo(), preSkylakeCount); |
| 180 | +} |
| 181 | + |
| 182 | +// See YieldProcessorNormalized() for preliminary info. This function is to be used when there is a decent possibility that the |
| 183 | +// condition would not be satisfied within a short duration. The current implementation increases the delay per spin-wait |
| 184 | +// iteration exponentially up to a limit. Typical usage: |
| 185 | +// if (!conditionThatMayNotBeSatisfiedSoon) |
| 186 | +// { |
| 187 | +// YieldProcessorNormalizationInfo normalizationInfo; |
| 188 | +// do |
| 189 | +// { |
| 190 | +// YieldProcessorWithBackOffNormalized(normalizationInfo); // maybe Sleep(0) occasionally |
| 191 | +// } while (!conditionThatMayNotBeSatisfiedSoon); |
| 192 | +// } |
| 193 | +FORCEINLINE void YieldProcessorWithBackOffNormalized( |
| 194 | + const YieldProcessorNormalizationInfo &normalizationInfo, |
| 195 | + unsigned int spinIteration) |
| 196 | +{ |
| 197 | + // normalizationInfo.optimalMaxNormalizedYieldsPerSpinIteration cannot exceed the value below based on calculations done in |
| 198 | + // InitializeYieldProcessorNormalized() |
| 199 | + const unsigned int MaxOptimalMaxNormalizedYieldsPerSpinIteration = |
| 200 | + NsPerOptimalMaxSpinIterationDuration * 3 / (MinNsPerNormalizedYield * 2) + 1; |
| 201 | + _ASSERTE(normalizationInfo.optimalMaxNormalizedYieldsPerSpinIteration <= MaxOptimalMaxNormalizedYieldsPerSpinIteration); |
| 202 | + |
| 203 | + // This shift value should be adjusted based on the asserted condition below |
| 204 | + const UINT8 MaxShift = 3; |
| 205 | + static_assert_no_msg(((unsigned int)1 << (MaxShift + 1)) >= MaxOptimalMaxNormalizedYieldsPerSpinIteration); |
| 206 | + |
| 207 | + unsigned int n; |
| 208 | + if (spinIteration <= MaxShift && |
| 209 | + ((unsigned int)1 << spinIteration) < normalizationInfo.optimalMaxNormalizedYieldsPerSpinIteration) |
| 210 | + { |
| 211 | + n = ((unsigned int)1 << spinIteration) * normalizationInfo.yieldsPerNormalizedYield; |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + n = normalizationInfo.optimalMaxYieldsPerSpinIteration; |
| 216 | + } |
| 217 | + _ASSERTE(n != 0); |
| 218 | + do |
| 219 | + { |
| 220 | + System_YieldProcessor(); |
| 221 | + } while (--n != 0); |
| 222 | +} |
0 commit comments