PERFORCE change 65500 for review

David Xu davidxu at FreeBSD.org
Fri Nov 19 19:17:18 PST 2004


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

Change 65500 by davidxu at davidxu_alona on 2004/11/20 03:16:17

	1. no sched queue. 
	2. init default pthread attribute's stack guard size to page size,
	   buggy libpthread left it zero, and no protection for thread
	   use default pthread attr.

Affected files ...

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

Differences ...

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

@@ -211,7 +211,8 @@
 void
 _libpthread_init(struct pthread *curthread)
 {
-	int fd;
+	int fd, first = 0;
+	sigset_t sigset, oldset;
 
 	/* Check if this function has already been called: */
 	if ((_thr_initial != NULL) && (curthread == NULL))
@@ -259,63 +260,36 @@
 
 	/* Initialize pthread private data. */
 	init_private();
-	_kse_init();
+	_thr_kern_init();
 
-	/* Initialize the initial kse and kseg. */
-	_kse_initial = _kse_alloc(NULL, _thread_scope_system > 0);
-	if (_kse_initial == NULL)
-		PANIC("Can't allocate initial kse.");
-	_kse_initial->k_kseg = _kseg_alloc(NULL);
-	if (_kse_initial->k_kseg == NULL)
-		PANIC("Can't allocate initial kseg.");
-	_kse_initial->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
-	_kse_initial->k_schedq = &_kse_initial->k_kseg->kg_schedq;
-
-	TAILQ_INSERT_TAIL(&_kse_initial->k_kseg->kg_kseq, _kse_initial, k_kgqe);
-	_kse_initial->k_kseg->kg_ksecount = 1;
-
 	/* Set the initial thread. */
 	if (curthread == NULL) {
+		first = 1;
 		/* Create and initialize the initial thread. */
 		curthread = _thr_alloc(NULL);
 		if (curthread == NULL)
 			PANIC("Can't allocate initial thread");
-		_thr_initial = curthread;
 		init_main_thread(curthread);
-	} else {
-		/*
-		 * The initial thread is the current thread.  It is
-		 * assumed that the current thread is already initialized
-		 * because it is left over from a fork().
-		 */
-		_thr_initial = curthread;
 	}
-	_kse_initial->k_kseg->kg_threadcount = 0;
-	_thr_initial->kse = _kse_initial;
-	_thr_initial->kseg = _kse_initial->k_kseg;
-	_thr_initial->active = 1;
 
 	/*
 	 * Add the thread to the thread list and to the KSEG's thread
          * queue.
 	 */
-	THR_LIST_ADD(_thr_initial);
-	KSEG_THRQ_ADD(_kse_initial->k_kseg, _thr_initial);
+	THR_LIST_ADD(curthread);
+	_thread_active_threads = 1;
 
-	/* Setup the KSE/thread specific data for the current KSE/thread. */
-	_thr_initial->kse->k_curthread = _thr_initial;
-	_kcb_set(_thr_initial->kse->k_kcb);
-	_tcb_set(_thr_initial->kse->k_kcb, _thr_initial->tcb);
-	_thr_initial->kse->k_flags |= KF_INITIALIZED;
+	/* Setup the thread specific data */
+	_tcb_set(curthread->tcb);
 
-	_thr_signal_init();
-	_kse_critical_leave(&_thr_initial->tcb->tcb_tmbx);
-	/*
-	 * activate threaded mode as soon as possible if we are
-	 * being debugged
-	 */
-	if (_libkse_debug)
-		_kse_setthreaded(1);
+	if (first) {
+		_thr_initial = curthread;
+		SIGFILLSET(sigset);
+		__sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
+		_thr_signal_init();
+		_thread_inited = 1;
+		__sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
+	}
 }
 
 /*
@@ -326,8 +300,8 @@
 init_main_thread(struct pthread *thread)
 {
 	/* Setup the thread attributes. */
+	thr_self(&thread->tid);
 	thread->attr = _pthread_attr_default;
-	thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
 	/*
 	 * Set up the thread stack.
 	 *
@@ -362,23 +336,12 @@
 	 */
 	thread->magic = THR_MAGIC;
 
-	thread->slice_usec = -1;
 	thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
 	thread->name = strdup("initial thread");
 
 	/* Initialize the thread for signals: */
 	SIGEMPTYSET(thread->sigmask);
 
-	/*
-	 * Set up the thread mailbox.  The threads saved context
-	 * is also in the mailbox.
-	 */
-	thread->tcb->tcb_tmbx.tm_udata = thread;
-	thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size =
-	    thread->attr.stacksize_attr;
-	thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp =
-	    thread->attr.stackaddr_attr;
-
 	/* Default the priority of the initial thread: */
 	thread->base_priority = THR_DEFAULT_PRIORITY;
 	thread->active_priority = THR_DEFAULT_PRIORITY;
@@ -391,7 +354,9 @@
 	thread->specific = NULL;
 	thread->cleanup = NULL;
 	thread->flags = 0;
+	thread->sigbackout = NULL;
 	thread->continuation = NULL;
+	thread->wakeup_time.tv_sec = -1;
 
 	thread->state = PS_RUNNING;
 	thread->uniqueid = 0;
@@ -400,10 +365,12 @@
 static void
 init_private(void)
 {
-	struct clockinfo clockinfo;
 	size_t len;
 	int mib[2];
 
+	TAILQ_INIT(&_thread_list);
+	TAILQ_INIT(&_thread_gc_list);
+
 	/*
 	 * Avoid reinitializing some things if they don't need to be,
 	 * e.g. after a fork().
@@ -415,57 +382,34 @@
 		len = sizeof (_usrstack);
 		if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
 			PANIC("Cannot get kern.usrstack from sysctl");
-		/* Get the kernel clockrate: */
-		mib[0] = CTL_KERN;
-		mib[1] = KERN_CLOCKRATE;
-		len = sizeof (struct clockinfo);
-		if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0)
-			_clock_res_usec = clockinfo.tick;
-		else
-			_clock_res_usec = CLOCK_RES_USEC;
-
 		_thr_page_size = getpagesize();
 		_thr_guard_default = _thr_page_size;
-		init_once = 1;	/* Don't do this again. */
+	 	_pthread_attr_default.guardsize_attr = _thr_guard_default;
+		
+		TAILQ_INIT(&_thr_atfork_list);
+
+		_lock_init(&_thread_signal_lock);
+		_lock_init(&_mutex_static_lock);
+		_lock_init(&_cond_static_lock);
+		_lock_init(&_rwlock_static_lock);
+		_lock_init(&_keytable_lock);
+		_lock_init(&_thread_list_lock);
+		_thr_spinlock_init();
+		_pthread_mutex_init(&_thr_atfork_mutex, NULL);
 	} else {
-		/*
-		 * Destroy the locks before creating them.  We don't
-		 * know what state they are in so it is better to just
-		 * recreate them.
-		 */
-		_lock_destroy(&_thread_signal_lock);
-		_lock_destroy(&_mutex_static_lock);
-		_lock_destroy(&_rwlock_static_lock);
-		_lock_destroy(&_keytable_lock);
+		_lock_reinit(&_thread_signal_lock);
+		_lock_reinit(&_mutex_static_lock);
+		_lock_reinit(&_cond_static_lock);
+		_lock_reinit(&_rwlock_static_lock);
+		_lock_reinit(&_keytable_lock);
+		_lock_reinit(&_thread_list_lock);
+		/* reinitialized in thr_fork.c */
+#if 0
+		_thr_spinlock_init();
+		_thr_mutex_reinit(&_thr_atfork_mutex);
+#endif
 	}
 
-	/* Initialize everything else. */
-	TAILQ_INIT(&_thread_list);
-	TAILQ_INIT(&_thread_gc_list);
-	TAILQ_INIT(&_thr_atfork_list);
-	_pthread_mutex_init(&_thr_atfork_mutex, NULL);
-
-	/*
-	 * Initialize the lock for temporary installation of signal
-	 * handlers (to support sigwait() semantics) and for the
-	 * process signal mask and pending signal sets.
-	 */
-	if (_lock_init(&_thread_signal_lock, LCK_ADAPTIVE,
-	    _kse_lock_wait, _kse_lock_wakeup) != 0)
-		PANIC("Cannot initialize _thread_signal_lock");
-	if (_lock_init(&_mutex_static_lock, LCK_ADAPTIVE,
-	    _thr_lock_wait, _thr_lock_wakeup) != 0)
-		PANIC("Cannot initialize mutex static init lock");
-	if (_lock_init(&_rwlock_static_lock, LCK_ADAPTIVE,
-	    _thr_lock_wait, _thr_lock_wakeup) != 0)
-		PANIC("Cannot initialize rwlock static init lock");
-	if (_lock_init(&_keytable_lock, LCK_ADAPTIVE,
-	    _thr_lock_wait, _thr_lock_wakeup) != 0)
-		PANIC("Cannot initialize thread specific keytable lock");
-	_thr_spinlock_init();
-
-	/* Clear pending signals and get the process signal mask. */
-	SIGEMPTYSET(_thr_proc_sigpending);
 
 	/* Are we in M:N mode (default) or 1:1 mode? */
 #ifdef SYSTEM_SCOPE_ONLY
@@ -476,9 +420,9 @@
 	else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
 		_thread_scope_system = -1;
 #endif
-
 	/*
-	 * _thread_list_lock and _kse_count are initialized
-	 * by _kse_init()
+	 * _thread_list_lock is initialized
+	 * by _thr_init()
 	 */
+	init_once = 1;
 }


More information about the p4-projects mailing list