Module Name:    src
Committed By:   pooka
Date:           Mon May 31 23:09:30 UTC 2010

Modified Files:
        src/lib/librumpuser: rumpuser_pth.c
        src/sys/rump/include/rump: rumpuser.h
        src/sys/rump/librump/rumpkern: threads.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/lib/librumpuser/rumpuser_pth.c
cvs rdiff -u -r1.41 -r1.42 src/sys/rump/include/rump/rumpuser.h
cvs rdiff -u -r1.9 -r1.10 src/sys/rump/librump/rumpkern/threads.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/librumpuser/rumpuser_pth.c
diff -u src/lib/librumpuser/rumpuser_pth.c:1.2 src/lib/librumpuser/rumpuser_pth.c:1.3
--- src/lib/librumpuser/rumpuser_pth.c:1.2	Tue May 18 14:58:41 2010
+++ src/lib/librumpuser/rumpuser_pth.c	Mon May 31 23:09:30 2010
@@ -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)
 {

Index: src/sys/rump/include/rump/rumpuser.h
diff -u src/sys/rump/include/rump/rumpuser.h:1.41 src/sys/rump/include/rump/rumpuser.h:1.42
--- src/sys/rump/include/rump/rumpuser.h:1.41	Tue May 18 14:58:41 2010
+++ src/sys/rump/include/rump/rumpuser.h	Mon May 31 23:09:29 2010
@@ -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;
 

Index: src/sys/rump/librump/rumpkern/threads.c
diff -u src/sys/rump/librump/rumpkern/threads.c:1.9 src/sys/rump/librump/rumpkern/threads.c:1.10
--- src/sys/rump/librump/rumpkern/threads.c:1.9	Fri May 28 16:44:14 2010
+++ src/sys/rump/librump/rumpkern/threads.c	Mon May 31 23:09:29 2010
@@ -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;
+}

Reply via email to