Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread - do PTHREAD_MLOCK_KLUDGE in pthread__stackid...



details:   https://anonhg.NetBSD.org/src/rev/04b74016a853
branches:  trunk
changeset: 588229:04b74016a853
user:      yamt <yamt%NetBSD.org@localhost>
date:      Sun Feb 12 11:41:53 2006 +0000

description:
- do PTHREAD_MLOCK_KLUDGE in pthread__stackid_setup, rather than callers,
  so that the main thread is not different from others.
  as a side effect, fix memory leak in pthread_create on error.
- make pthread__stackid_setup return a error rather than calling err(2).

diffstat:

 lib/libpthread/pthread.c       |  10 ++--------
 lib/libpthread/pthread_stack.c |  36 +++++++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 21 deletions(-)

diffs (124 lines):

diff -r c4a6d16be168 -r 04b74016a853 lib/libpthread/pthread.c
--- a/lib/libpthread/pthread.c  Sun Feb 12 11:23:31 2006 +0000
+++ b/lib/libpthread/pthread.c  Sun Feb 12 11:41:53 2006 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread.c,v 1.46 2005/10/19 02:44:45 chs Exp $ */
+/*     $NetBSD: pthread.c,v 1.47 2006/02/12 11:41:53 yamt Exp $        */
 
 /*-
  * Copyright (c) 2001,2002,2003 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread.c,v 1.46 2005/10/19 02:44:45 chs Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.47 2006/02/12 11:41:53 yamt Exp $");
 
 #include <err.h>
 #include <errno.h>
@@ -371,12 +371,6 @@
                                free(name);
                        return ret;
                }
-#ifdef PTHREAD_MLOCK_KLUDGE
-               ret = mlock(newthread, sizeof(struct __pthread_st));
-               if (ret < 0) {
-                       return EAGAIN;
-               }
-#endif
        }
 
        /* 2. Set up state. */
diff -r c4a6d16be168 -r 04b74016a853 lib/libpthread/pthread_stack.c
--- a/lib/libpthread/pthread_stack.c    Sun Feb 12 11:23:31 2006 +0000
+++ b/lib/libpthread/pthread_stack.c    Sun Feb 12 11:41:53 2006 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_stack.c,v 1.16 2004/08/17 14:16:00 chs Exp $   */
+/*     $NetBSD: pthread_stack.c,v 1.17 2006/02/12 11:41:53 yamt Exp $  */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_stack.c,v 1.16 2004/08/17 14:16:00 chs Exp $");
+__RCSID("$NetBSD: pthread_stack.c,v 1.17 2006/02/12 11:41:53 yamt Exp $");
 
 #define __EXPOSE_STACK 1
 #include <sys/param.h>
@@ -57,8 +57,7 @@
 #include "pthread.h"
 #include "pthread_int.h"
 
-static pthread_t
-pthread__stackid_setup(void *base, size_t size);
+static int pthread__stackid_setup(void *, size_t, pthread_t *);
 
 #ifndef PT_FIXEDSTACKSIZE_LG
 /* 
@@ -93,8 +92,7 @@
 
        pthread__assert(((intptr_t)addr & PT_STACKMASK) == 0);
 
-       *newt = pthread__stackid_setup(addr, PT_STACKSIZE); 
-       return 0;
+       return pthread__stackid_setup(addr, PT_STACKSIZE, newt); 
 }
 
 
@@ -109,6 +107,7 @@
        pthread_t t;
        void *base;
        size_t size;
+       int error;
 
 #ifndef PT_FIXEDSTACKSIZE_LG
        struct rlimit slimit;
@@ -146,7 +145,11 @@
        base = (void *)(pthread__sp() & ~PT_STACKMASK);
        size = PT_STACKSIZE;
 
-       t = pthread__stackid_setup(base, size);
+       error = pthread__stackid_setup(base, size, &t);
+       if (error) {
+               /* XXX */
+               errx(2, "failed to setup main thread: error=%d", error);
+       }
 
        /*
         * The "safe" area chosen below isn't safe for the initial thread stack
@@ -167,9 +170,9 @@
        *newt = t;
 }
 
-static pthread_t
+static int
 /*ARGSUSED*/
-pthread__stackid_setup(void *base, size_t size)
+pthread__stackid_setup(void *base, size_t size, pthread_t *tp)
 {
        pthread_t t;
        void *redaddr;
@@ -201,10 +204,17 @@
 
        /* Protect the next-to-bottom stack page as a red zone. */
        ret = mprotect(redaddr, pagesize, PROT_NONE);
-       if (ret == -1)
-               err(2, "Couldn't mprotect() stack redzone at %p\n", redaddr);
-
-       return t;
+       if (ret == -1) {
+               return errno;
+       }
+#ifdef PTHREAD_MLOCK_KLUDGE
+       ret = mlock(t, sizeof(struct __pthread_st));
+       if (ret < 0) {
+               return errno;
+       }
+#endif
+       *tp = t;
+       return 0;
 }
 
 



Home | Main Index | Thread Index | Old Index