Thu Mar 8 16:40:45 2012 UTC ()
Actually use the stack thread attributes when creating a new thread.


(joerg)
diff -r1.127 -r1.128 src/lib/libpthread/pthread.c

cvs diff -r1.127 -r1.128 src/lib/libpthread/pthread.c (expand / switch to unified diff)

--- src/lib/libpthread/pthread.c 2012/03/08 16:33:45 1.127
+++ src/lib/libpthread/pthread.c 2012/03/08 16:40:45 1.128
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pthread.c,v 1.127 2012/03/08 16:33:45 joerg Exp $ */ 1/* $NetBSD: pthread.c,v 1.128 2012/03/08 16:40:45 joerg Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran. 8 * by Nathan J. Williams and Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: pthread.c,v 1.127 2012/03/08 16:33:45 joerg Exp $"); 33__RCSID("$NetBSD: pthread.c,v 1.128 2012/03/08 16:40:45 joerg Exp $");
34 34
35#define __EXPOSE_STACK 1 35#define __EXPOSE_STACK 1
36 36
37#include <sys/param.h> 37#include <sys/param.h>
38#include <sys/exec_elf.h> 38#include <sys/exec_elf.h>
39#include <sys/mman.h> 39#include <sys/mman.h>
40#include <sys/sysctl.h> 40#include <sys/sysctl.h>
41#include <sys/lwpctl.h> 41#include <sys/lwpctl.h>
42#include <sys/tls.h> 42#include <sys/tls.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <dlfcn.h> 45#include <dlfcn.h>
46#include <err.h> 46#include <err.h>
@@ -307,43 +307,58 @@ static void @@ -307,43 +307,58 @@ static void
307pthread__scrubthread(pthread_t t, char *name, int flags) 307pthread__scrubthread(pthread_t t, char *name, int flags)
308{ 308{
309 309
310 t->pt_state = PT_STATE_RUNNING; 310 t->pt_state = PT_STATE_RUNNING;
311 t->pt_exitval = NULL; 311 t->pt_exitval = NULL;
312 t->pt_flags = flags; 312 t->pt_flags = flags;
313 t->pt_cancel = 0; 313 t->pt_cancel = 0;
314 t->pt_errno = 0; 314 t->pt_errno = 0;
315 t->pt_name = name; 315 t->pt_name = name;
316 t->pt_lid = 0; 316 t->pt_lid = 0;
317} 317}
318 318
319static int 319static int
320pthread__newstack(pthread_t newthread) 320pthread__newstack(pthread_t newthread, const pthread_attr_t *attr)
321{ 321{
322 void *stackbase, *redzone; 322 void *stackbase, *redzone;
 323 size_t stacksize;
 324 bool mapped_stack = false;
323 325
324 stackbase = mmap(NULL, pthread__stacksize, 326 if (attr != NULL) {
325 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, (off_t)0); 327 pthread_attr_getstack(attr, &stackbase, &stacksize);
326 if (stackbase == MAP_FAILED) 328 } else {
327 return ENOMEM; 329 stackbase = NULL;
 330 stacksize = 0;
 331 }
 332 if (stacksize == 0)
 333 stacksize = pthread__stacksize;
 334
 335 if (stackbase == NULL) {
 336 stackbase = mmap(NULL, stacksize,
 337 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
 338 if (stackbase == MAP_FAILED)
 339 return ENOMEM;
 340 mapped_stack = true;
 341 }
328 newthread->pt_stack.ss_size = pthread__stacksize - pthread__pagesize; 342 newthread->pt_stack.ss_size = pthread__stacksize - pthread__pagesize;
329 newthread->pt_stack.ss_sp = stackbase; 343 newthread->pt_stack.ss_sp = stackbase;
330#ifdef __MACHINE_STACK_GROWS_UP 344#ifdef __MACHINE_STACK_GROWS_UP
331 redzone = (char *)stackbase + newthread->pt_stack.ss_size; 345 redzone = (char *)stackbase + newthread->pt_stack.ss_size;
332#else 346#else
333 redzone = (char *)stackbase; 347 redzone = (char *)stackbase;
334#endif 348#endif
335 if (mprotect(redzone, pthread__pagesize, PROT_NONE) == -1) { 349 if (mprotect(redzone, pthread__pagesize, PROT_NONE) == -1) {
336 munmap(stackbase, pthread__stacksize); 350 if (mapped_stack)
 351 munmap(stackbase, pthread__stacksize);
337 return EPERM; 352 return EPERM;
338 } 353 }
339 return 0; 354 return 0;
340} 355}
341 356
342int 357int
343pthread_create(pthread_t *thread, const pthread_attr_t *attr, 358pthread_create(pthread_t *thread, const pthread_attr_t *attr,
344 void *(*startfunc)(void *), void *arg) 359 void *(*startfunc)(void *), void *arg)
345{ 360{
346 pthread_t newthread; 361 pthread_t newthread;
347 pthread_attr_t nattr; 362 pthread_attr_t nattr;
348 struct pthread_attr_private *p; 363 struct pthread_attr_private *p;
349 char * volatile name; 364 char * volatile name;
@@ -401,27 +416,27 @@ pthread_create(pthread_t *thread, const  @@ -401,27 +416,27 @@ pthread_create(pthread_t *thread, const
401 } 416 }
402 417
403 /* 418 /*
404 * If necessary set up a stack, allocate space for a pthread_st, 419 * If necessary set up a stack, allocate space for a pthread_st,
405 * and initialize it. 420 * and initialize it.
406 */ 421 */
407 if (newthread == NULL) { 422 if (newthread == NULL) {
408 newthread = malloc(sizeof(*newthread)); 423 newthread = malloc(sizeof(*newthread));
409 if (newthread == NULL) { 424 if (newthread == NULL) {
410 free(name); 425 free(name);
411 return ENOMEM; 426 return ENOMEM;
412 } 427 }
413 428
414 if (pthread__newstack(newthread)) { 429 if (pthread__newstack(newthread, attr)) {
415 free(newthread); 430 free(newthread);
416 free(name); 431 free(name);
417 return ENOMEM; 432 return ENOMEM;
418 } 433 }
419 434
420 /* This is used only when creating the thread. */ 435 /* This is used only when creating the thread. */
421 _INITCONTEXT_U(&newthread->pt_uc); 436 _INITCONTEXT_U(&newthread->pt_uc);
422 newthread->pt_uc.uc_stack = newthread->pt_stack; 437 newthread->pt_uc.uc_stack = newthread->pt_stack;
423 newthread->pt_uc.uc_link = NULL; 438 newthread->pt_uc.uc_link = NULL;
424#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) 439#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
425 newthread->pt_tls = NULL; 440 newthread->pt_tls = NULL;
426#endif 441#endif
427 442