llvm-project
27c1d904 - [libc] Derive the default thread stack size from RLIMIT_STACK (#244571)

Commit
2 days ago
[libc] Derive the default thread stack size from RLIMIT_STACK (#244571) Our default thread stack size was a hard-coded 64KB, which is a lot smaller than what most programs expect. As far as I can tell, there was never a deliberate discussion on the default value. The current value (first 32KB, then bumped to 64KB) was set back when thrd_create was first implemented and has stuck around since. This patch makes the default follow the soft RLIMIT_STACK limit: - the limit is read once at startup (in do_start, before constructors and main run), so later setrlimit calls don't change the default - if the limit is RLIM_INFINITY (or too large to be page-aligned), we use 2MB - the value is raised to PTHREAD_STACK_MIN and rounded up to the page size POSIX leaves the default stack size unspecified, and this is the kind of thing that has no one-size-fits-all solution. Using RLIMIT_STACK is an idea I got from glibc. I'm implementing that here because it makes sense to me, and I think it's the behavior most applications expect. For contrast, musl uses a smaller default stack size (128KB -- still larger than ours), but it offers a non-standard extension (pthread_setattr_default_np) to change it. This is the kind of thing that would make sense as a config option, but I don't want to add it if there isn't a demand for it. This default stack size applies to pthread_attr_init, pthread_create with a null attr, and thrd_create. Since the default is no longer a compile-time constant, DEFAULT_PTHREAD_ATTR becomes a default_pthread_attr() function, and the stacksize argument of Thread::run now defaults to 0. Thread::run already treated 0 as "use the default" (support for this was added back during the review of the patch which introduced Thread::DEFAULT_STACKSIZE, https://reviews.llvm.org/D148290#4316958), so I'm reusing that instead of e.g. switching to an optional. The integration test re-execs itself with different RLIMIT_STACK values to check the startup behavior, including that lowering the limit inside main doesn't affect the default. Assisted-by: Gemini
Author
Parents
Loading