From owner-p4-projects@FreeBSD.ORG Sat Nov 20 03:17:18 2004 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id B440016A4D0; Sat, 20 Nov 2004 03:17:17 +0000 (GMT) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7938116A4CE for ; Sat, 20 Nov 2004 03:17:17 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4F36043D45 for ; Sat, 20 Nov 2004 03:17:17 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.12.11/8.12.11) with ESMTP id iAK3HHfw003532 for ; Sat, 20 Nov 2004 03:17:17 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.12.11/8.12.11/Submit) id iAK3HGUl003529 for perforce@freebsd.org; Sat, 20 Nov 2004 03:17:16 GMT (envelope-from davidxu@freebsd.org) Date: Sat, 20 Nov 2004 03:17:16 GMT Message-Id: <200411200317.iAK3HGUl003529@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to davidxu@freebsd.org using -f From: David Xu To: Perforce Change Reviews Subject: PERFORCE change 65500 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Nov 2004 03:17:18 -0000 http://perforce.freebsd.org/chv.cgi?CH=65500 Change 65500 by davidxu@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; }