diff options
| author | cl <cl@NetBSD.org> | 2003-11-27 16:30:54 +0000 |
|---|---|---|
| committer | cl <cl@NetBSD.org> | 2003-11-27 16:30:54 +0000 |
| commit | 2c9b7b1eb7cf6199e202d971ea1e1b784bbeaa0e (patch) | |
| tree | d10a35fef8c2699a9ae40b580cc8c3267cd2390b /lib/libpthread | |
| parent | 4f9a822f1f7fb5c8d0a802b121c9fd4036d4b41c (diff) | |
Set default stack size to the current limit on the stack size as set
with the shell's command to change limits. Make the PTHREAD_STACKSIZE
environment variable override the default stack size. The old fixed
stack size behaviour can be enable with PT_FIXEDSTACKSIZE_LG when building
libpthread.
Diffstat (limited to 'lib/libpthread')
| -rw-r--r-- | lib/libpthread/Makefile | 5 | ||||
| -rw-r--r-- | lib/libpthread/pthread.3 | 16 | ||||
| -rw-r--r-- | lib/libpthread/pthread_int.h | 23 | ||||
| -rw-r--r-- | lib/libpthread/pthread_stack.c | 64 |
4 files changed, 97 insertions, 11 deletions
diff --git a/lib/libpthread/Makefile b/lib/libpthread/Makefile index c9098515329..7ab322b93dd 100644 --- a/lib/libpthread/Makefile +++ b/lib/libpthread/Makefile @@ -1,8 +1,11 @@ -# $NetBSD: Makefile,v 1.23 2003/11/12 02:44:22 christos Exp $ +# $NetBSD: Makefile,v 1.24 2003/11/27 16:30:54 cl Exp $ # WARNS= 2 +# Define PT_FIXEDSTACKSIZE_LG to set a fixed stacksize +#CPPFLAGS+=-DPT_FIXEDSTACKSIZE_LG=18 + .include <bsd.own.mk> .if exists(${.CURDIR}/arch/${MACHINE_ARCH}) diff --git a/lib/libpthread/pthread.3 b/lib/libpthread/pthread.3 index 0407b8cc158..b6e01444658 100644 --- a/lib/libpthread/pthread.3 +++ b/lib/libpthread/pthread.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: pthread.3,v 1.2 2003/07/26 19:25:06 salo Exp $ +.\" $NetBSD: pthread.3,v 1.3 2003/11/27 16:30:54 cl Exp $ .\" .\" Copyright (c) 2003 Hubert Feyrer <hubertf@NetBSD.org> .\" and Thomas Klausner <wiz@NetBSD.org> @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 21, 2003 +.Dd November 27, 2003 .Dt PTHREAD 3 .Os .Sh NAME @@ -88,6 +88,18 @@ program found in Integer value giving the round-robin interval in ms. The default is 100. If set to 0, timer-based round-robin scheduling is disabled. +.It Ev PTHREAD_STACKSIZE +Integer value giving the stack size in kilobytes. +This allows to set a smaller stack size than the default stack size. +The default stack size is the current limit on the stack size as +set with the shell's command to change limits +.Ic ( limit +for +.Xr csh 1 , +or +.Ic ulimit +for +.Xr sh 1 ) . .El .Sh SEE ALSO .Xr pthread_attr 3 , diff --git a/lib/libpthread/pthread_int.h b/lib/libpthread/pthread_int.h index a38dad267dc..9675cfaa60a 100644 --- a/lib/libpthread/pthread_int.h +++ b/lib/libpthread/pthread_int.h @@ -1,4 +1,4 @@ -/* $NetBSD: pthread_int.h,v 1.21 2003/11/25 22:36:32 christos Exp $ */ +/* $NetBSD: pthread_int.h,v 1.22 2003/11/27 16:30:54 cl Exp $ */ /*- * Copyright (c) 2001,2002,2003 The NetBSD Foundation, Inc. @@ -203,9 +203,24 @@ struct pthread_lock_ops { #define PT_ATTR_MAGIC 0x22220002 #define PT_ATTR_DEAD 0xDEAD0002 -#define PT_STACKSIZE_LG 18 -#define PT_STACKSIZE (1<<(PT_STACKSIZE_LG)) -#define PT_STACKMASK (PT_STACKSIZE-1) +#ifdef PT_FIXEDSTACKSIZE_LG + +#define PT_STACKSIZE_LG PT_FIXEDSTACKSIZE_LG +#define PT_STACKSIZE (1<<(PT_STACKSIZE_LG)) +#define PT_STACKMASK (PT_STACKSIZE-1) + +#else /* PT_FIXEDSTACKSIZE_LG */ + +extern int pt_stacksize_lg; +extern size_t pt_stacksize; +extern vaddr_t pt_stackmask; + +#define PT_STACKSIZE_LG pt_stacksize_lg +#define PT_STACKSIZE pt_stacksize +#define PT_STACKMASK pt_stackmask + +#endif /* PT_FIXEDSTACKSIZE_LG */ + #define PT_UPCALLSTACKS 16 diff --git a/lib/libpthread/pthread_stack.c b/lib/libpthread/pthread_stack.c index 4e95466375f..058b38dacff 100644 --- a/lib/libpthread/pthread_stack.c +++ b/lib/libpthread/pthread_stack.c @@ -1,4 +1,4 @@ -/* $NetBSD: pthread_stack.c,v 1.8 2003/07/17 21:07:39 nathanw Exp $ */ +/* $NetBSD: pthread_stack.c,v 1.9 2003/11/27 16:30:54 cl Exp $ */ /*- * Copyright (c) 2001 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pthread_stack.c,v 1.8 2003/07/17 21:07:39 nathanw Exp $"); +__RCSID("$NetBSD: pthread_stack.c,v 1.9 2003/11/27 16:30:54 cl Exp $"); #include <err.h> #include <errno.h> @@ -48,13 +48,29 @@ __RCSID("$NetBSD: pthread_stack.c,v 1.8 2003/07/17 21:07:39 nathanw Exp $"); #include <unistd.h> #include <sys/queue.h> #include <sys/mman.h> +#include <sys/resource.h> #include <sched.h> #include "pthread.h" #include "pthread_int.h" static pthread_t -pthread__stackid_setup(void *base, int size); +pthread__stackid_setup(void *base, size_t size); + +#ifndef PT_FIXEDSTACKSIZE_LG +/* + * We have to initialize the pt_stack* variables here because mutexes + * are used before pthread_init() and thus pthread__initmain() are + * called. Since mutexes only save the stack pointer and not a + * pointer to the thread data, it is safe to change the mapping from + * stack pointer to thread data afterwards. + */ +#define _STACKSIZE_LG 18 +int pt_stacksize_lg = _STACKSIZE_LG; +size_t pt_stacksize = 1 << _STACKSIZE_LG; +vaddr_t pt_stackmask = (1 << _STACKSIZE_LG) - 1; +#undef _STACKSIZE_LG +#endif /* !PT_FIXEDSTACKSIZE_LG */ /* @@ -89,6 +105,46 @@ pthread__initmain(pthread_t *newt) { void *base; +#ifndef PT_FIXEDSTACKSIZE_LG + struct rlimit slimit; + size_t pagesize; + char *value; + int ret; + + pagesize = (size_t)sysconf(_SC_PAGESIZE); + pt_stacksize = 0; + ret = getrlimit(RLIMIT_STACK, &slimit); + if (ret == -1) + err(1, "Couldn't get stack resource consumption limits"); + value = getenv("PTHREAD_STACKSIZE"); + if (value) { + pt_stacksize = atoi(value) * 1024; + if (pt_stacksize > slimit.rlim_cur) + pt_stacksize = (size_t)slimit.rlim_cur; + } + if (pt_stacksize == 0) + pt_stacksize = (size_t)slimit.rlim_cur; + if (pt_stacksize < 4 * pagesize) + errx(1, "Stacksize limit is too low, minimum %zd kbyte.", + 4 * pagesize / 1024); + + pt_stacksize_lg = -1; + while (pt_stacksize) { + pt_stacksize >>= 1; + pt_stacksize_lg++; + } + + pt_stacksize = (1 << pt_stacksize_lg); + pt_stackmask = pt_stacksize - 1; + + /* + * XXX The "initial" thread stack can be smaller than + * requested because we don't control the end of the stack. + * On i386 the stack usually ends at 0xbfc00000 and for + * requested sizes >=8MB, we get a 4MB smaller stack. + */ +#endif /* PT_FIXEDSTACKSIZE_LG */ + base = (void *) (pthread__sp() & ~PT_STACKMASK); *newt = pthread__stackid_setup(base, PT_STACKSIZE); @@ -96,7 +152,7 @@ pthread__initmain(pthread_t *newt) static pthread_t /*ARGSUSED*/ -pthread__stackid_setup(void *base, int size) +pthread__stackid_setup(void *base, size_t size) { pthread_t t; size_t pagesize; |
