Source-Changes-HG archive

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

[src/trunk]: src Support KTHREAD_JOINABLE/kthread_join(). Also fixes earlier...



details:   https://anonhg.NetBSD.org/src/rev/d90d38f42c90
branches:  trunk
changeset: 755301:d90d38f42c90
user:      pooka <pooka%NetBSD.org@localhost>
date:      Mon May 31 23:09:29 2010 +0000

description:
Support KTHREAD_JOINABLE/kthread_join().  Also fixes earlier bug
where all pthreads were created non-detached.

diffstat:

 lib/librumpuser/rumpuser_pth.c      |  42 +++++++++++++++++++++++++++++++++---
 sys/rump/include/rump/rumpuser.h    |   6 +++-
 sys/rump/librump/rumpkern/threads.c |  27 ++++++++++++++++++++---
 3 files changed, 65 insertions(+), 10 deletions(-)

diffs (161 lines):

diff -r f3d4a0a4ead7 -r d90d38f42c90 lib/librumpuser/rumpuser_pth.c
--- a/lib/librumpuser/rumpuser_pth.c    Mon May 31 22:31:07 2010 +0000
+++ b/lib/librumpuser/rumpuser_pth.c    Mon May 31 23:09:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser_pth.c,v 1.2 2010/05/18 14:58:41 pooka Exp $   */
+/*     $NetBSD: rumpuser_pth.c,v 1.3 2010/05/31 23:09:30 pooka Exp $   */
 
 /*
  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: rumpuser_pth.c,v 1.2 2010/05/18 14:58:41 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_pth.c,v 1.3 2010/05/31 23:09:30 pooka Exp $");
 #endif /* !lint */
 
 #ifdef __linux__
@@ -209,17 +209,38 @@
 #endif
 
 int
-rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname)
+rumpuser_thread_create(void *(*f)(void *), void *arg, const char *thrname,
+       int joinable, void **ptcookie)
 {
        pthread_t ptid;
+       pthread_t *ptidp;
+       pthread_attr_t pattr;
        int rv;
 
-       rv = pthread_create(&ptid, NULL, f, arg);
+       if ((rv = pthread_attr_init(&pattr)) != 0)
+               return rv;
+
+       if (joinable) {
+               NOFAIL(ptidp = malloc(sizeof(*ptidp)));
+               pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
+       } else {
+               ptidp = &ptid;
+               pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
+       }
+
+       rv = pthread_create(ptidp, &pattr, f, arg);
 #ifdef __NetBSD__
        if (rv == 0 && thrname)
                pthread_setname_np(ptid, thrname, NULL);
 #endif
 
+       if (joinable) {
+               assert(ptcookie);
+               *ptcookie = ptidp;
+       }
+
+       pthread_attr_destroy(&pattr);
+
        return rv;
 }
 
@@ -230,6 +251,19 @@
        pthread_exit(NULL);
 }
 
+int
+rumpuser_thread_join(void *ptcookie)
+{
+       pthread_t *pt = ptcookie;
+       int rv;
+
+       KLOCK_WRAP((rv = pthread_join(*pt, NULL)));
+       if (rv == 0)
+               free(pt);
+
+       return rv;
+}
+
 void
 rumpuser_mutex_init(struct rumpuser_mtx **mtx)
 {
diff -r f3d4a0a4ead7 -r d90d38f42c90 sys/rump/include/rump/rumpuser.h
--- a/sys/rump/include/rump/rumpuser.h  Mon May 31 22:31:07 2010 +0000
+++ b/sys/rump/include/rump/rumpuser.h  Mon May 31 23:09:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser.h,v 1.41 2010/05/18 14:58:41 pooka Exp $      */
+/*     $NetBSD: rumpuser.h,v 1.42 2010/05/31 23:09:29 pooka Exp $      */
 
 /*
  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
@@ -115,8 +115,10 @@
 void rumpuser_thrinit(kernel_lockfn, kernel_unlockfn, int);
 void rumpuser_biothread(void *);
 
-int  rumpuser_thread_create(void *(*f)(void *), void *, const char *);
+int  rumpuser_thread_create(void *(*f)(void *), void *, const char *, int,
+                           void **);
 void rumpuser_thread_exit(void);
+int  rumpuser_thread_join(void *);
 
 struct rumpuser_mtx;
 
diff -r f3d4a0a4ead7 -r d90d38f42c90 sys/rump/librump/rumpkern/threads.c
--- a/sys/rump/librump/rumpkern/threads.c       Mon May 31 22:31:07 2010 +0000
+++ b/sys/rump/librump/rumpkern/threads.c       Mon May 31 23:09:29 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: threads.c,v 1.9 2010/05/28 16:44:14 pooka Exp $        */
+/*     $NetBSD: threads.c,v 1.10 2010/05/31 23:09:29 pooka Exp $       */
 
 /*
  * Copyright (c) 2007-2009 Antti Kantee.  All Rights Reserved.
@@ -29,9 +29,10 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.9 2010/05/28 16:44:14 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.10 2010/05/31 23:09:29 pooka Exp $");
 
 #include <sys/param.h>
+#include <sys/atomic.h>
 #include <sys/kmem.h>
 #include <sys/kthread.h>
 #include <sys/systm.h>
@@ -150,12 +151,17 @@
                strlcpy(l->l_name, thrname, MAXCOMLEN);
        }
                
-       rv = rumpuser_thread_create(threadbouncer, k, thrname);
+       rv = rumpuser_thread_create(threadbouncer, k, thrname,
+           (flags & KTHREAD_JOINABLE) == KTHREAD_JOINABLE, &l->l_ctxlink);
        if (rv)
                return rv;
 
-       if (newlp)
+       if (newlp) {
                *newlp = l;
+       } else {
+               KASSERT((flags & KTHREAD_JOINABLE) == 0);
+       }
+
        return 0;
 }
 
@@ -166,6 +172,19 @@
        if ((curlwp->l_pflag & LP_MPSAFE) == 0)
                KERNEL_UNLOCK_LAST(NULL);
        rump_lwp_release(curlwp);
+       /* unschedule includes membar */
        rump_unschedule();
        rumpuser_thread_exit();
 }
+
+int
+kthread_join(struct lwp *l)
+{
+       int rv;
+
+       KASSERT(l->l_ctxlink != NULL);
+       rv = rumpuser_thread_join(l->l_ctxlink);
+       membar_consumer();
+
+       return rv;
+}



Home | Main Index | Thread Index | Old Index