Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Mar 2005 02:45:34 GMT
From:      David Xu <davidxu@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 72814 for review
Message-ID:  <200503100245.j2A2jYgS094597@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=72814

Change 72814 by davidxu@davidxu_celeron on 2005/03/10 02:45:07

	Implement system and process scope concept for pthread.
	Not tested!

Affected files ...

.. //depot/projects/davidxu_thread/src/sys/kern/kern_thr.c#8 edit
.. //depot/projects/davidxu_thread/src/sys/kern/kern_thread.c#5 edit
.. //depot/projects/davidxu_thread/src/sys/sys/proc.h#7 edit
.. //depot/projects/davidxu_thread/src/sys/sys/thr.h#3 edit

Differences ...

==== //depot/projects/davidxu_thread/src/sys/kern/kern_thr.c#8 (text+ko) ====

@@ -83,37 +83,23 @@
 		return (EPROCLIM);
 	}
 
-	scope_sys = thr_scope_sys;
+	scope_sys = (uap->flags & THR_SYSTEM_SCOPE) != 0;
 	/* Initialize our td and new ksegrp.. */
 	newtd = thread_alloc();
-	if (scope_sys)
-		newkg = ksegrp_alloc();
-	else
-		newkg = kg;
+
 	/*
 	 * Try the copyout as soon as we allocate the td so we don't have to
 	 * tear things down in a failure case below.
 	 */
 	id = newtd->td_tid;
 	if ((error = copyout(&id, uap->id, sizeof(long)))) {
-		if (scope_sys)
-			ksegrp_free(newkg);
 		thread_free(newtd);
 		return (error);
 	}
-
 	bzero(&newtd->td_startzero,
 	    __rangeof(struct thread, td_startzero, td_endzero));
 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
 	    __rangeof(struct thread, td_startcopy, td_endcopy));
-
-	if (scope_sys) {
-		bzero(&newkg->kg_startzero,
-		    __rangeof(struct ksegrp, kg_startzero, kg_endzero));
-		bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
-		    __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
-	}
-
 	newtd->td_proc = td->td_proc;
 	newtd->td_ucred = crhold(td->td_ucred);
 
@@ -121,25 +107,53 @@
 	cpu_set_upcall(newtd, td);
 	error = set_mcontext(newtd, &ctx.uc_mcontext);
 	if (error != 0) {
-		if (scope_sys)
-			ksegrp_free(newkg);
 		thread_free(newtd);
 		crfree(td->td_ucred);
 		goto out;
 	}
 
+	if ((td->td_proc->p_flag & P_HADTHREADS) == 0)
+		p->p_procscopegrp = kg;
+		
+	if (scope_sys) {
+		newkg = ksegrp_alloc();
+		bzero(&newkg->kg_startzero,
+		    __rangeof(struct ksegrp, kg_startzero, kg_endzero));
+		bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
+		    __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
+		PROC_LOCK(td->td_proc);
+	} else {
+retry:
+		PROC_LOCK(td->td_proc);
+		if ((newkg = p->p_procscopegrp) == NULL) {
+			PROC_UNLOCK(p);
+			newkg = ksegrp_alloc();
+			bzero(&newkg->kg_startzero,
+			    __rangeof(struct ksegrp, kg_startzero, kg_endzero));
+			bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
+			    __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
+			PROC_LOCK(p);
+			if (p->p_procscopegrp == NULL)
+				p->p_procscopegrp = newkg;
+			else {
+				PROC_UNLOCK(p);
+				ksegrp_free(newkg);
+				goto retry;
+			}
+		}
+	}
+
 	/* Link the thread and kse into the ksegrp and make it runnable. */
-	PROC_LOCK(td->td_proc);
 	if (scope_sys) {
-			sched_init_concurrency(newkg);
+		sched_init_concurrency(newkg);
 	} else {
 		if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
-			sched_set_concurrency(kg,
+			sched_set_concurrency(newkg,
 			    thr_concurrency ? thr_concurrency : (2*mp_ncpus));
 		}
 	}
 			
-	td->td_proc->p_flag |= P_HADTHREADS; 
+	td->td_proc->p_flag |= P_HADTHREADS;
 	newtd->td_sigmask = td->td_sigmask;
 	mtx_lock_spin(&sched_lock);
 	if (scope_sys)

==== //depot/projects/davidxu_thread/src/sys/kern/kern_thread.c#5 (text+ko) ====

@@ -610,6 +610,8 @@
 				 * This is probably not fair so think of
  				 * a better answer.
 				 */
+				if (kg == p->p_procscopegrp)
+					p->p_procscopegrp = NULL;
 				sched_exit_ksegrp(FIRST_KSEGRP_IN_PROC(p), td);
 				sched_set_concurrency(kg, 0); /* XXX TEMP */
 				ksegrp_unlink(kg);

==== //depot/projects/davidxu_thread/src/sys/sys/proc.h#7 (text+ko) ====

@@ -572,6 +572,7 @@
 	int		p_suspcount;	/* (c) Num threads in suspended mode. */
 	struct thread	*p_xthread;	/* (c) Trap thread */
 	int		p_boundary_count;/* (c) Num threads at user boundary */
+	struct ksegrp	*p_procscopegrp;
 /* End area that is zeroed on creation. */
 #define	p_endzero	p_magic
 

==== //depot/projects/davidxu_thread/src/sys/sys/thr.h#3 (text+ko) ====

@@ -30,7 +30,10 @@
 #ifndef _SYS_THR_H_
 #define	_SYS_THR_H_
 
-#define	THR_SUSPENDED	0x0001	/* Create the thread in the suspended state. */
+/* Create the thread in the suspended state. */
+#define	THR_SUSPENDED		0x0001
+/* Create the system scope thread. */
+#define	THR_SYSTEM_SCOPE	0x0002
 
 /* 
  * See pthread_*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200503100245.j2A2jYgS094597>