From 8cd7aaa105e06f99cbad3e9db677eedec1f2c4b4 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 6 Mar 2024 15:34:33 +0000 Subject: [PATCH 1/2] Remove unnecessary fixme As the FIXME itself notes, there's nothing to fix here. --- library/std/src/sys/pal/windows/thread.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/library/std/src/sys/pal/windows/thread.rs b/library/std/src/sys/pal/windows/thread.rs index a8f1e9b726b..601ba095aa4 100644 --- a/library/std/src/sys/pal/windows/thread.rs +++ b/library/std/src/sys/pal/windows/thread.rs @@ -26,11 +26,6 @@ impl Thread { pub unsafe fn new(stack: usize, p: Box) -> io::Result { let p = Box::into_raw(Box::new(p)); - // FIXME On UNIX, we guard against stack sizes that are too small but - // that's because pthreads enforces that stacks are at least - // PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's - // just that below a certain threshold you can't do anything useful. - // That threshold is application and architecture-specific, however. let ret = c::CreateThread( ptr::null_mut(), stack, From 87183177255e39b2b3016cf82e8ba2fbb5d397f5 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 6 Mar 2024 19:21:34 +0000 Subject: [PATCH 2/2] Document and test minimal stack size on Windows --- library/std/src/sys/pal/windows/thread.rs | 2 ++ library/std/src/thread/tests.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/library/std/src/sys/pal/windows/thread.rs b/library/std/src/sys/pal/windows/thread.rs index 601ba095aa4..970bd9c6ce7 100644 --- a/library/std/src/sys/pal/windows/thread.rs +++ b/library/std/src/sys/pal/windows/thread.rs @@ -26,6 +26,8 @@ impl Thread { pub unsafe fn new(stack: usize, p: Box) -> io::Result { let p = Box::into_raw(Box::new(p)); + // CreateThread rounds up values for the stack size to the nearest page size (at least 4kb). + // If a value of zero is given then the default stack size is used instead. let ret = c::CreateThread( ptr::null_mut(), stack, diff --git a/library/std/src/thread/tests.rs b/library/std/src/thread/tests.rs index 813ede06415..b81efac6761 100644 --- a/library/std/src/thread/tests.rs +++ b/library/std/src/thread/tests.rs @@ -423,3 +423,16 @@ fn scope_join_race() { }); } } + +// Test that the smallest value for stack_size works on Windows. +#[cfg(windows)] +#[test] +fn test_minimal_thread_stack() { + use crate::sync::atomic::AtomicU8; + static COUNT: AtomicU8 = AtomicU8::new(0); + + let builder = thread::Builder::new().stack_size(1); + let before = builder.spawn(|| COUNT.fetch_add(1, Ordering::Relaxed)).unwrap().join().unwrap(); + assert_eq!(before, 0); + assert_eq!(COUNT.load(Ordering::Relaxed), 1); +}