From 51949784d45225f01c1271fa644a222e2522ea53 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 6 Mar 2024 19:21:34 +0000 Subject: [PATCH] Document and test minimal stack size on Windows --- library/std/src/sys/pal/windows/thread.rs | 2 ++ library/std/src/thread/tests.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/pal/windows/thread.rs b/library/std/src/sys/pal/windows/thread.rs index 601ba095aa49e..970bd9c6ce7e6 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 813ede0641530..5288a1f3753f0 100644 --- a/library/std/src/thread/tests.rs +++ b/library/std/src/thread/tests.rs @@ -4,7 +4,7 @@ use crate::mem; use crate::panic::panic_any; use crate::result; use crate::sync::{ - atomic::{AtomicBool, Ordering}, + atomic::{AtomicBool, AtomicU8, Ordering}, mpsc::{channel, Sender}, Arc, Barrier, }; @@ -423,3 +423,15 @@ fn scope_join_race() { }); } } + +// Test that the smallest value for stack_size works on Windows. +#[cfg(windows)] +#[test] +fn test_minimal_thread_stack() { + static COUNT: AtomicU8 = AtomicU8::new(0); + + let builder = thread::Builder::new().stack_size(1); + let thread_join_handle = builder.spawn(move || COUNT.fetch_add(1, Ordering::Relaxed)).unwrap(); + thread_join_handle.join().unwrap(); + assert_eq!(COUNT.load(Ordering::Relaxed), 1); +}