PERFORCE change 65512 for review

David Xu davidxu at FreeBSD.org
Fri Nov 19 20:31:53 PST 2004


http://perforce.freebsd.org/chv.cgi?CH=65512

Change 65512 by davidxu at davidxu_alona on 2004/11/20 04:31:12

	Follow the changes made in thr_private.h.

Affected files ...

.. //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_create.c#2 edit

Differences ...

==== //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_create.c#2 (text+ko) ====

@@ -41,60 +41,34 @@
 #include <sys/time.h>
 #include <machine/reg.h>
 #include <pthread.h>
+#include <sys/signalvar.h>
+
 #include "thr_private.h"
 #include "libc_private.h"
 
 static void free_thread(struct pthread *curthread, struct pthread *thread);
 static int  create_stack(struct pthread_attr *pattr);
-static void free_stack(struct pthread_attr *pattr);
-static void thread_start(struct pthread *curthread,
-		void *(*start_routine) (void *), void *arg);
+static void free_stack(struct pthread *curthread, struct pthread_attr *pattr);
+static void thread_start(struct pthread *curthread);
 
 __weak_reference(_pthread_create, pthread_create);
 
-/*
- * Some notes on new thread creation and first time initializion
- * to enable multi-threading.
- *
- * There are basically two things that need to be done.
- *
- *   1) The internal library variables must be initialized.
- *   2) Upcalls need to be enabled to allow multiple threads
- *      to be run.
- *
- * The first may be done as a result of other pthread functions
- * being called.  When _thr_initial is null, _libpthread_init is
- * called to initialize the internal variables; this also creates
- * or sets the initial thread.  It'd be nice to automatically
- * have _libpthread_init called on program execution so we don't
- * have to have checks throughout the library.
- *
- * The second part is only triggered by the creation of the first
- * thread (other than the initial/main thread).  If the thread
- * being created is a scope system thread, then a new KSE/KSEG
- * pair needs to be allocated.  Also, if upcalls haven't been
- * enabled on the initial thread's KSE, they must be now that
- * there is more than one thread; this could be delayed until
- * the initial KSEG has more than one thread.
- */
 int
 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
 	       void *(*start_routine) (void *), void *arg)
 {
+	ucontext_t uc;
+	sigset_t sigmask;
 	struct pthread *curthread, *new_thread;
-	struct kse *kse = NULL;
-	struct kse_group *kseg = NULL;
-	kse_critical_t crit;
 	int ret = 0;
 
-	if (_thr_initial == NULL)
-		_libpthread_init(NULL);
+	_thr_check_init();
 
 	/*
 	 * Turn on threaded mode, if failed, it is unnecessary to
 	 * do further work.
 	 */
-	if (_kse_isthreaded() == 0 && _kse_setthreaded(1)) {
+	if (_thr_isthreaded() == 0 && _thr_setthreaded(1)) {
 		return (EAGAIN);
 	}
 	curthread = _get_curthread();
@@ -126,75 +100,48 @@
 				 */
 			}
 		}
+
 		if (_thread_scope_system > 0)
 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
-		else if ((_thread_scope_system < 0)
-		    && (thread != &_thr_sig_daemon))
+		else if (_thread_scope_system < 0)
 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
+
 		if (create_stack(&new_thread->attr) != 0) {
 			/* Insufficient memory to create a stack: */
 			ret = EAGAIN;
+			new_thread->isdead = 1;
 			_thr_free(curthread, new_thread);
-		}
-		else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) &&
-		    (((kse = _kse_alloc(curthread, 1)) == NULL)
-		    || ((kseg = _kseg_alloc(curthread)) == NULL))) {
-			/* Insufficient memory to create a new KSE/KSEG: */
-			ret = EAGAIN;
-			if (kse != NULL) {
-				kse->k_kcb->kcb_kmbx.km_flags |= KMF_DONE;
-				_kse_free(curthread, kse);
-			}
-			free_stack(&new_thread->attr);
-			_thr_free(curthread, new_thread);
-		}
-		else {
-			if (kseg != NULL) {
-				/* Add the KSE to the KSEG's list of KSEs. */
-				TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_kgqe);
-				kseg->kg_ksecount = 1;
-				kse->k_kseg = kseg;
-				kse->k_schedq = &kseg->kg_schedq;
-			}
+		} else {
+			new_thread->tid = 0;
+			new_thread->isdead = 0;
+			new_thread->sigseqno = 0;
+			new_thread->lock_switch = 0;
+			new_thread->idle = 0;
+			new_thread->rtld_bits = 0;
 			/*
 			 * Write a magic value to the thread structure
 			 * to help identify valid ones:
 			 */
 			new_thread->magic = THR_MAGIC;
 
-			new_thread->slice_usec = -1;
 			new_thread->start_routine = start_routine;
 			new_thread->arg = arg;
 			new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
-			    PTHREAD_CANCEL_DEFERRED;
+				    PTHREAD_CANCEL_DEFERRED;
 
 			/* No thread is wanting to join to this one: */
 			new_thread->joiner = NULL;
+			new_thread->join_status.thread = NULL;
+			new_thread->critical_count = 0;
+			new_thread->sflags = 0;
+			getcontext(&uc);
+			SIGFILLSET(uc.uc_sigmask);
+			uc.uc_stack.ss_sp = new_thread->attr.stackaddr_attr;
+			uc.uc_stack.ss_size = new_thread->attr.stacksize_attr;
+			makecontext(&uc, (void (*)(void))thread_start, 1,
+			            new_thread);
 
-			/* Initialize the signal frame: */
-			new_thread->curframe = NULL;
-
 			/*
-			 * Initialize the machine context.
-			 * Enter a critical region to get consistent context.
-			 */
-			crit = _kse_critical_enter();
-			THR_GETCONTEXT(&new_thread->tcb->tcb_tmbx.tm_context);
-			/* Initialize the thread for signals: */
-			new_thread->sigmask = curthread->sigmask;
-			_kse_critical_leave(crit);
-
-			new_thread->tcb->tcb_tmbx.tm_udata = new_thread;
-			new_thread->tcb->tcb_tmbx.tm_context.uc_sigmask =
-			    new_thread->sigmask;
-			new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size =
-			    new_thread->attr.stacksize_attr;
-			new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp =
-			    new_thread->attr.stackaddr_attr;
-			makecontext(&new_thread->tcb->tcb_tmbx.tm_context,
-			    (void (*)(void))thread_start, 3, new_thread,
-			    start_routine, arg);
-			/*
 			 * Check if this thread is to inherit the scheduling
 			 * attributes from its parent:
 			 */
@@ -204,7 +151,7 @@
 				 * Lock the scheduling lock to get consistent
 				 * scheduling parameters.
 				 */
-				THR_SCHED_LOCK(curthread, curthread);
+				THR_LOCK(curthread);
 				new_thread->base_priority =
 				    curthread->base_priority &
 				    ~THR_SIGNAL_PRIORITY;
@@ -213,7 +160,7 @@
 				    ~THR_SIGNAL_PRIORITY;
 				new_thread->attr.sched_policy =
 				    curthread->attr.sched_policy;
-				THR_SCHED_UNLOCK(curthread, curthread);
+				THR_UNLOCK(curthread);
 			} else {
 				/*
 				 * Use just the thread priority, leaving the
@@ -221,7 +168,7 @@
 				 * default values:
 				 */
 				new_thread->base_priority =
-				    new_thread->attr.prio;
+			    		new_thread->attr.prio;
 			}
 			new_thread->active_priority = new_thread->base_priority;
 			new_thread->inherited_priority = 0;
@@ -235,57 +182,50 @@
 			new_thread->cleanup = NULL;
 			new_thread->flags = 0;
 			new_thread->tlflags = 0;
+			new_thread->sigbackout = NULL;
 			new_thread->continuation = NULL;
 			new_thread->wakeup_time.tv_sec = -1;
-			new_thread->lock_switch = 0;
-			sigemptyset(&new_thread->sigpend);
+			new_thread->timeout = 0;
+			new_thread->error = 0;
+			SIGEMPTYSET(new_thread->sigpend);
 			new_thread->check_pending = 0;
+			new_thread->refcount = 0;
 			new_thread->locklevel = 0;
 			new_thread->rdlock_count = 0;
-			new_thread->sigstk.ss_sp = 0;
-			new_thread->sigstk.ss_size = 0;
-			new_thread->sigstk.ss_flags = SS_DISABLE;
-			new_thread->oldsigmask = NULL;
-
+			new_thread->data.mutex = 0;
+			new_thread->interrupted = 0;
+			new_thread->priority_mutex_count = 0;
+			new_thread->rtld_bits = 0;
 			if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
 				new_thread->state = PS_SUSPENDED;
 				new_thread->flags = THR_FLAGS_SUSPENDED;
 			}
 			else
 				new_thread->state = PS_RUNNING;
-
 			/*
-			 * System scope threads have their own kse and
-			 * kseg.  Process scope threads are all hung
-			 * off the main process kseg.
+			 * Schedule the new thread.
 			 */
-			if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) {
-				new_thread->kseg = _kse_initial->k_kseg;
-				new_thread->kse = _kse_initial;
-			}
-			else {
-				kse->k_curthread = NULL;
-				kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
-				new_thread->kse = kse;
-				new_thread->kseg = kse->k_kseg;
-				kse->k_kcb->kcb_kmbx.km_udata = kse;
-				kse->k_kcb->kcb_kmbx.km_curthread = NULL;
-			}
-
+			SIGFILLSET(sigmask);
 			/*
-			 * Schedule the new thread starting a new KSEG/KSE
-			 * pair if necessary.
+			 * Thread created by thr_create() inherits currrent thread
+			 * sigmask, however, before new thread setup itself correctly,
+			 * it can not handle signal, so we should masks all signals here.
 			 */
-			ret = _thr_schedule_add(curthread, new_thread);
-			if (ret != 0)
+			__sys_sigprocmask(SIG_SETMASK, &sigmask, &curthread->sigmask);
+			new_thread->sigmask = curthread->sigmask;
+			/* Add the new thread. */
+			_thr_link(curthread, new_thread);
+			ret = thr_create(&uc, &new_thread->tid, 0);
+			__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
+			if (ret != 0) {
+				_thr_unlink(curthread, new_thread);
 				free_thread(curthread, new_thread);
-			else {
+			} else {
 				/* Return a pointer to the thread structure: */
 				(*thread) = new_thread;
 			}
 		}
 	}
-
 	/* Return the status: */
 	return (ret);
 }
@@ -293,12 +233,8 @@
 static void
 free_thread(struct pthread *curthread, struct pthread *thread)
 {
-	free_stack(&thread->attr);
-	if ((thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) {
-		/* Free the KSE and KSEG. */
-		_kseg_free(thread->kseg);
-		_kse_free(curthread, thread->kse);
-	}
+	free_stack(curthread, &thread->attr);
+	curthread->isdead = 1;
 	_thr_free(curthread, thread);
 }
 
@@ -319,28 +255,29 @@
 }
 
 static void
-free_stack(struct pthread_attr *pattr)
+free_stack(struct pthread *curthread, struct pthread_attr *pattr)
 {
-	struct kse *curkse;
-	kse_critical_t crit;
-
 	if ((pattr->flags & THR_STACK_USER) == 0) {
-		crit = _kse_critical_enter();
-		curkse = _get_curkse();
-		KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
+		THR_LOCK_ACQUIRE(curthread, &_thread_list_lock);
 		/* Stack routines don't use malloc/free. */
 		_thr_stack_free(pattr);
-		KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
-		_kse_critical_leave(crit);
+		THR_LOCK_RELEASE(curthread, &_thread_list_lock);
 	}
 }
 
 static void
-thread_start(struct pthread *curthread, void *(*start_routine) (void *),
-    void *arg)
+thread_start(struct pthread *curthread)
 {
+	_tcb_set(curthread->tcb);
+
+	/* Thread was created with all signals blocked, unblock them. */
+	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
+
+	if (curthread->flags & THR_FLAGS_SUSPENDED)
+		_thr_sched_switch(curthread);
+
 	/* Run the current thread's start routine with argument: */
-	pthread_exit(start_routine(arg));
+	pthread_exit(curthread->start_routine(curthread->arg));
 
 	/* This point should never be reached. */
 	PANIC("Thread has resumed after exit");


More information about the p4-projects mailing list