Skip to content

Commit 64a74a2

Browse files
seritoolsmbilker
authored andcommitted
Add fallback for SwitchToThread by using Sleep(0)
Used in `thread::yield_now`. Signed-off-by: Dennis Duda <git@seri.tools>
1 parent 2acfc4e commit 64a74a2

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

library/std/src/sys/windows/c.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,17 @@ compat_fn_with_fallback! {
518518
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD);
519519
FALSE
520520
}
521+
522+
// >= NT 4+
523+
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-switchtothread
524+
pub fn SwitchToThread() -> BOOL {
525+
// A value of zero causes the thread to relinquish the remainder of its time slice to any
526+
// other thread of equal priority that is ready to run. If there are no other threads of
527+
// equal priority ready to run, the function returns immediately, and the thread continues
528+
// execution.
529+
Sleep(0);
530+
TRUE
531+
}
521532
}
522533

523534
compat_fn_optional! {

library/std/src/sys/windows/c/windows_sys.lst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,6 @@ Windows.Win32.System.Threading.STARTF_USESIZE
25492549
Windows.Win32.System.Threading.STARTF_USESTDHANDLES
25502550
Windows.Win32.System.Threading.STARTUPINFOW
25512551
Windows.Win32.System.Threading.STARTUPINFOW_FLAGS
2552-
Windows.Win32.System.Threading.SwitchToThread
25532552
Windows.Win32.System.Threading.TerminateProcess
25542553
Windows.Win32.System.Threading.THREAD_CREATE_RUN_IMMEDIATELY
25552554
Windows.Win32.System.Threading.THREAD_CREATE_SUSPENDED

library/std/src/sys/windows/c/windows_sys.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,6 @@ extern "system" {
408408
pub fn SleepEx(dwmilliseconds: u32, balertable: BOOL) -> u32;
409409
}
410410
#[link(name = "kernel32")]
411-
extern "system" {
412-
pub fn SwitchToThread() -> BOOL;
413-
}
414-
#[link(name = "kernel32")]
415411
extern "system" {
416412
pub fn TerminateProcess(hprocess: HANDLE, uexitcode: u32) -> BOOL;
417413
}

0 commit comments

Comments
 (0)