This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add normalized equivalent of YieldProcessor, retune some spin loops #13670
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ namespace Internal.Runtime.Augments | |
{ | ||
public class RuntimeThread : CriticalFinalizerObject | ||
{ | ||
private static int s_optimalMaxSpinWaitsPerSpinIteration; | ||
|
||
internal RuntimeThread() { } | ||
|
||
public static RuntimeThread Create(ThreadStart start) => new Thread(start); | ||
|
@@ -186,6 +188,33 @@ public void DisableComObjectEagerCleanup() | |
private extern bool JoinInternal(int millisecondsTimeout); | ||
|
||
public static void Sleep(int millisecondsTimeout) => Thread.Sleep(millisecondsTimeout); | ||
|
||
[DllImport(JitHelpers.QCall)] | ||
[SuppressUnmanagedCodeSecurity] | ||
private static extern int GetOptimalMaxSpinWaitsPerSpinIterationInternal(); | ||
|
||
/// <summary> | ||
/// Max value to be passed into <see cref="SpinWait(int)"/> for optimal delaying. This value is normalized to be | ||
/// appropriate for the processor. | ||
/// </summary> | ||
internal static int OptimalMaxSpinWaitsPerSpinIteration | ||
{ | ||
get | ||
{ | ||
if (s_optimalMaxSpinWaitsPerSpinIteration != 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks this one can be converted to readonly field initialized with GetOptimalMaxSpinWaitsPerSpinIterationInternal() so we can avoid checking 0 value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want do that since the first call would trigger the measurement that takes about 10 ms. Static construction of RuntimeThread probably happens during startup for most apps. |
||
{ | ||
return s_optimalMaxSpinWaitsPerSpinIteration; | ||
} | ||
|
||
// This is done lazily because the first call to the function below in the process triggers a measurement that | ||
// takes a nontrivial amount of time. See Thread::InitializeYieldProcessorNormalized(), which describes and | ||
// calculates this value. | ||
s_optimalMaxSpinWaitsPerSpinIteration = GetOptimalMaxSpinWaitsPerSpinIterationInternal(); | ||
Debug.Assert(s_optimalMaxSpinWaitsPerSpinIteration > 0); | ||
return s_optimalMaxSpinWaitsPerSpinIteration; | ||
} | ||
} | ||
|
||
public static void SpinWait(int iterations) => Thread.SpinWait(iterations); | ||
public static bool Yield() => Thread.Yield(); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the formatting here reads strangely to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's formatted similarly to:
where
This is how I typically format multi-line expressions, trying to align parentheses and putting each type of expression (&& or ||) separately, one condition per line unless the whole expression fits on one line. What would you suggest instead? I can separate parts of it into locals if you prefer.