Skip to content

Commit 4aab32d

Browse files
authored
Document the behaviour of RUST_MIN_STACK=0
While digging through the code I noticed it went out of its way to distinguish RUST_MIN_STACK=0 from the sentinel value of 0, with no justification given. Then I did some searching and discovered at least one person knew that it made Rust use the platform default stack size to work under valgrind better. [0] This took me deeper into the guts of the platform-specific thread code where indeed i saw comments and/or explicit checks about 0 being a special sentinel to discard the 2MiB default in favour of "whatever the platform thread API wants to pick". These docs are already heavily caveated with "this is platform-specific" so I see no harm in specifying this further-platform-specific behaviour. [0]: https://nest.pijul.com/pijul/pijul/discussions/498
1 parent 0f1e965 commit 4aab32d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

library/std/src/thread/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@
125125
//! ## Stack size
126126
//!
127127
//! The default stack size is platform-dependent and subject to change.
128-
//! Currently, it is 2 MiB on all Tier-1 platforms.
128+
//! Currently, it is 2 MiB on all Tier-1 platforms to make programs behave more consistently
129+
//! cross-platform (notably Windows would otherwise default to 1MiB stacks).
129130
//!
130-
//! There are two ways to manually specify the stack size for spawned threads:
131+
//! You can overload Rust's default stack size with one of the two following methods.
132+
//! Passing 0 to either of them will be treated as a request to instead use the
133+
//! platform's "normal" default stack size (e.g. reverting Windows back to 1MiB stacks).
131134
//!
132135
//! * Build the thread with [`Builder`] and pass the desired stack size to [`Builder::stack_size`].
133136
//! * Set the `RUST_MIN_STACK` environment variable to an integer representing the desired stack
@@ -495,7 +498,11 @@ impl Builder {
495498
.unwrap_or(imp::DEFAULT_MIN_STACK_SIZE);
496499

497500
// 0 is our sentinel value, so ensure that we'll never see 0 after
498-
// initialization has run
501+
// initialization has run even if RUST_MIN_STACK=0.
502+
//
503+
// Note that a stack size of 0 is actually a meaningful input, as each platform's
504+
// thread implementation is expected to use it as a signal to use the
505+
// platform's "normal" default thread size instead of Rust's preferred one.
499506
MIN.store(amt + 1, Ordering::Relaxed);
500507
amt
501508
});

0 commit comments

Comments
 (0)