Threads currently have 2-4 MiB stacks by default (depending on the
platform). This is fine on MMU platforms, where this stack space is not
actually allocated until it is used, but tends to waste a large amount
of memory on no-MMU platforms.
This patch adds a PTHREADS_STACK_DEFAULT_SIZE Kconfig option that allows
the user to override the default stack size at build time. This allows
the user to select a reasonable default stack size for the software that
runs on their system, and avoids the need to patch every package to add
calls to pthread_attr_setstacksize().
An alternative to this patch would be to change the hardcoded default
stack size on no-MMU platforms, but it is difficult to choose an
appropriate value because the minimum required stack depends on the
software in use. This would also be a breaking change.
It may also be desirable to completely move the default stack size
configuration to Kconfig, with conditional defaults for each platform.
This would avoid special casing zero as the default value.
Signed-off-by: Ben Wolsieffer <ben.wolsieffer(a)hefring.com>
---
extra/Configs/Config.in | 8 ++++++++
libpthread/nptl/init.c | 20 +++++++++++++-------
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/extra/Configs/Config.in b/extra/Configs/Config.in
index 6bbb6f572..ecb55ef3f 100644
--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -628,6 +628,14 @@ config PTHREADS_DEBUG_SUPPORT
If you are doing development and want to debug applications using
uClibc's pthread library, answer Y. Otherwise, answer N.
+config PTHREADS_STACK_DEFAULT_SIZE
+ int "Default thread stack size"
+ default 0
+ help
+ Set the default thread stack size. If set to zero, an architecture
+ specific default is used. This option is useful on MMU-less systems
+ where the stack size is fixed and the default stack size may be
+ excessively large and waste memory.
config UCLIBC_HAS_SYSLOG
bool "Syslog support"
diff --git a/libpthread/nptl/init.c b/libpthread/nptl/init.c
index 5d25ded7d..f5c144e7b 100644
--- a/libpthread/nptl/init.c
+++ b/libpthread/nptl/init.c
@@ -35,6 +35,12 @@
#include <bits/kernel-features.h>
#include <stdio.h>
+#if __PTHREADS_STACK_DEFAULT_SIZE__
+#define PTHREAD_STACK_DEFAULT_SIZE __PTHREADS_STACK_DEFAULT_SIZE__
+#else
+#define PTHREAD_STACK_DEFAULT_SIZE ARCH_STACK_DEFAULT_SIZE
+#endif
+
/* Size and alignment of static TLS block. */
size_t __static_tls_size;
size_t __static_tls_align_m1;
@@ -278,17 +284,17 @@ __pthread_initialize_minimal_internal (void)
struct rlimit limit;
if (getrlimit (RLIMIT_STACK, &limit) != 0
|| limit.rlim_cur == RLIM_INFINITY)
- /* The system limit is not usable. Use an architecture-specific
- default. */
- limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
- else if (limit.rlim_cur < PTHREAD_STACK_MIN)
+ /* The system limit is not usable. Use a user-specified or
+ architecture-specific default. */
+ limit.rlim_cur = PTHREAD_STACK_DEFAULT_SIZE;
+ if (limit.rlim_cur < PTHREAD_STACK_MIN)
/* The system limit is unusably small.
Use the minimal size acceptable. */
limit.rlim_cur = PTHREAD_STACK_MIN;
- /* Do not exceed architecture specific default */
- if (limit.rlim_cur > ARCH_STACK_DEFAULT_SIZE)
- limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
+ /* Do not exceed the user-specified or architecture-specific default */
+ if (limit.rlim_cur > PTHREAD_STACK_DEFAULT_SIZE)
+ limit.rlim_cur = PTHREAD_STACK_DEFAULT_SIZE;
/* Make sure it meets the minimum size that allocate_stack
(allocatestack.c) will demand, which depends on the page size. */
--
2.42.0