From owner-p4-projects@FreeBSD.ORG Sat Nov 20 04:21:39 2004 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 5B15916A4D0; Sat, 20 Nov 2004 04:21:39 +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 206E616A4CE for ; Sat, 20 Nov 2004 04:21:39 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id E580043D49 for ; Sat, 20 Nov 2004 04:21:38 +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 iAK4LcZs005787 for ; Sat, 20 Nov 2004 04:21:38 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.12.11/8.12.11/Submit) id iAK4LcEI005784 for perforce@freebsd.org; Sat, 20 Nov 2004 04:21:38 GMT (envelope-from davidxu@freebsd.org) Date: Sat, 20 Nov 2004 04:21:38 GMT Message-Id: <200411200421.iAK4LcEI005784@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 65509 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 04:21:40 -0000 http://perforce.freebsd.org/chv.cgi?CH=65509 Change 65509 by davidxu@davidxu_alona on 2004/11/20 04:21:13 1. Fix buggy libpthread code, always call fork handler even if there is only one thred. 2. Reinit all mutexs thread holding in child process. Affected files ... .. //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_fork.c#2 edit Differences ... ==== //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_fork.c#2 (text+ko) ==== @@ -57,25 +57,20 @@ sigset_t sigset, oldset; struct pthread *curthread; struct pthread_atfork *af; + struct pthread_mutex *m; pid_t ret; int errsave; - if (!_kse_isthreaded()) + if (!_thr_is_inited()) return (__sys_fork()); curthread = _get_curthread(); /* - * Masks all signals until we reach a safe point in - * _kse_single_thread, and the signal masks will be - * restored in that function, for M:N thread, all - * signals were already masked in kernel atomically, - * we only need to do this for bound thread. + * Masks all signals until we reach a safe point. */ - if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) { - SIGFILLSET(sigset); - __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset); - } + SIGFILLSET(sigset); + __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset); _pthread_mutex_lock(&_thr_atfork_mutex); @@ -86,30 +81,50 @@ } /* Fork a new process: */ - if ((_kse_isthreaded() != 0) && (__malloc_lock != NULL)) { + if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) _spinlock(__malloc_lock); - } + if ((ret = __sys_fork()) == 0) { /* Child process */ - errsave = errno; + errsave = errno; + + /* clear aother thread locked us. */ + _lock_reinit(&curthread->lock); + thr_self(&curthread->tid); + + /* reinitialize libc spinlocks, this includes __malloc_lock. */ + _thr_spinlock_init(); + + /* + * reinitialize all mutexes the curthread owns, these include + * _thr_fork_mutex. + */ + TAILQ_FOREACH(m, &curthread->mutexq, m_qe) { + _thr_mutex_reinit(curthread, &m); + } + /* Reinitialize lib kernel. */ + _thr_single_thread(curthread); + + /* Restore signal mask. */ + __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); - /* Kernel signal mask is restored in _kse_single_thread */ - _kse_single_thread(curthread); + _pthread_mutex_unlock(&_thr_atfork_mutex); /* Run down atfork child handlers. */ TAILQ_FOREACH(af, &_thr_atfork_list, qe) { if (af->child != NULL) af->child(); } - _thr_mutex_reinit(&_thr_atfork_mutex); } else { - if ((_kse_isthreaded() != 0) && (__malloc_lock != NULL)) { + /* Parent process */ + errsave = errno; + + if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) _spinunlock(__malloc_lock); - } - errsave = errno; - if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) { - __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); - } + + /* Restore signal mask. */ + __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); + /* Run down atfork parent handlers. */ TAILQ_FOREACH(af, &_thr_atfork_list, qe) { if (af->parent != NULL)