You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fbf6be5
Browse filesBrowse the repository at this point in the historyBrowse files
authored
A few fixes in the threadpool semaphore. Unify Windows/Unix implementation of LIFO policy. (#123921)
Re: #123159
Changes:
- Correctly handle `Backoff.Exponential(0)`.
Embarrassing bug.
To get exponentially growing random spin count for an iteration we
generate pseudorandom `uint` and do `>> (32 - attempt)`. Since C# masks
the shift operand with 31, when `attempt==0` we end up not shifting at
all, and the first iteration gets a large random spin count.
That caused many noisy results and interestingly some improvements (in
scenarios that benefit from very long spins).
- Unified implementation of LIFO policy with lightweight minimal
implementation of LIFO waiting.
Once we are done spinning, we block threads and when workers are needed
again wake them in LIFO order.
Unix WaitSubsystem is pretty heavy for these needs. It supports
Interruptible waits, waiting on multiple objects, etc... None of that is
interesting here. Most calls into the subsystem take a global
process-wide lock which can contend under load with other uses, or a
worker-waking threads may contend with the workers going to sleep,
etc...
Windows used an opaque `GetQueuedCompletionStatus` for the side effect
of releasing threads in LIFO order when completion is posted, with
unknown overheads and interactions, even though typically it is more
efficient than Unix WaitSubsystem.
The portable implementation seems to be faster than either of the
platform-specific ones.
(measured by disabling spinning and running a few latency-sensitive
benchmarks).
The portable implementation is also easier to reason about and to debug
anomalies.
- Adaptive spinning in the threadpool based on estimates of CPU core
availability.
Spinning in threadpool is very tricky and spinning benefits differ
greatly between scenarios. For some scenarios the longer the spin the
better. But there are scenarios that benefit when the threadpool
releases cores quickly once it sees no work. No preset fixed spin count
is going to be good for everything.
Adaptive approach appears to be necessary to improve some scenarios
without regressing many others.
We can further improve the heuristic, if there are more ideas.
---------
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Jan Kotas <[email protected]>
Copy file name to clipboardExpand all lines: docs/coding-guidelines/interop-guidelines.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ internal static partial class Interop
34
34
...
35
35
internalstaticpartialclassInterop
36
36
{
37
-
internalstaticpartialclassmincore { ... }
37
+
internalstaticpartialclassMincore { ... }
38
38
}
39
39
```
40
40
- With few exceptions, the only methods that should be defined in these interop types are DllImports.
@@ -49,7 +49,9 @@ internal static partial class Interop
49
49
```
50
50
\Common\src\Interop
51
51
\Windows
52
-
\mincore
52
+
\Kernel32
53
+
... interop files
54
+
\Mincore
53
55
... interop files
54
56
\Unix
55
57
\libc
@@ -71,8 +73,8 @@ As shown above, platforms may be additive, in that an assembly may use functiona
71
73
\libc
72
74
\Interop.strerror.cs
73
75
\Windows
74
-
\mincore
75
-
\Interop.OutputDebugString.cs
76
+
\Mincore
77
+
\Interop.WaitOnAddress.cs <-- Also contains WakeByAddressSingle
76
78
```
77
79
78
80
- If structs/constants will be used on their own without an associated DllImport, or if they may be used with multiple DllImports not in the same file, they should be declared in a separate file.
@@ -81,9 +83,9 @@ As shown above, platforms may be additive, in that an assembly may use functiona
81
83
```
82
84
\Common\src\Interop
83
85
\Windows
84
-
\mincore
85
-
\Interop.DuplicateHandle_SafeTokenHandle.cs
86
-
\Interop.DuplicateHandle_IntPtr.cs
86
+
\Kernel32
87
+
\Interop.DuplicateHandle_SafeFileHandle.cs
88
+
\Interop.DuplicateHandle_SafePipeHandle.cs
87
89
```
88
90
89
91
- The library names used per-platform are stored in internal constants in the Interop class in a private Libraries class in a per-platform file named Interop.Libraries.cs. These constants are then used for all DllImports to that library, rather than having the string duplicated each time, e.g.
@@ -94,12 +96,9 @@ internal static partial class Interop // contents of Common\src\Interop\Windows\
0 commit comments