Skip to content

Commit

Permalink
Document and test minimal stack size on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Mar 6, 2024
1 parent 8cd7aaa commit 8182860
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions library/std/src/sys/pal/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl Thread {
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
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,
Expand Down
13 changes: 12 additions & 1 deletion library/std/src/thread/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -423,3 +423,14 @@ 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);
builder.spawn(move || COUNT.fetch_add(1, Ordering::Relaxed)).unwrap().join().unwrap();
assert_eq!(COUNT.load(Ordering::Relaxed), 1);
}

0 comments on commit 8182860

Please sign in to comment.