From owner-p4-projects@FreeBSD.ORG Wed Jan 5 02:42:00 2005 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id E064C16A4CE; Wed, 5 Jan 2005 02:41:59 +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 98E0F16A4CE for ; Wed, 5 Jan 2005 02:41:59 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6630343D39 for ; Wed, 5 Jan 2005 02:41:59 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id j052fx5u069267 for ; Wed, 5 Jan 2005 02:41:59 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id j052fxC7069264 for perforce@freebsd.org; Wed, 5 Jan 2005 02:41:59 GMT (envelope-from davidxu@freebsd.org) Date: Wed, 5 Jan 2005 02:41:59 GMT Message-Id: <200501050241.j052fxC7069264@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 68289 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: Wed, 05 Jan 2005 02:42:00 -0000 http://perforce.freebsd.org/chv.cgi?CH=68289 Change 68289 by davidxu@davidxu_celeron on 2005/01/05 02:41:17 A deadlock can be timeouted if timeout is specified. Affected files ... .. //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_mutex.c#10 edit Differences ... ==== //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_mutex.c#10 (text+ko) ==== @@ -75,7 +75,8 @@ */ static long mutex_handoff(struct pthread *, struct pthread_mutex *); static inline int mutex_self_trylock(struct pthread *, pthread_mutex_t); -static inline int mutex_self_lock(struct pthread *, pthread_mutex_t); +static inline int mutex_self_lock(struct pthread *, pthread_mutex_t, + const struct timespec *abstime); static int mutex_unlock_common(pthread_mutex_t *, int); static void mutex_priority_adjust(struct pthread *, pthread_mutex_t); static void mutex_rescan_owned (struct pthread *, struct pthread *, @@ -515,14 +516,8 @@ TAILQ_INSERT_TAIL(&curthread->mutexq, (*m), m_qe); #endif - } else if (umtx_owner(&(*m)->m_lock) == curthread->tid && - (*m)->m_type != PTHREAD_MUTEX_NORMAL) { - /* - * We don't do deadlock sleep in mutex_self_lock - * if type is normal, instead let umtx_lock sleep - * in kernel. - */ - ret = mutex_self_lock(curthread, *m); + } else if (umtx_owner(&(*m)->m_lock) == curthread->tid) { + ret = mutex_self_lock(curthread, *m, abstime); } else { if (abstime == NULL) { UMTX_LOCK(&(*m)->m_lock, curthread->tid); @@ -611,7 +606,7 @@ /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); } else if ((*m)->m_owner == curthread) { - ret = mutex_self_lock(curthread, *m); + ret = mutex_self_lock(curthread, *m, abstime); /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); @@ -701,7 +696,7 @@ /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); } else if ((*m)->m_owner == curthread) { - ret = mutex_self_lock(curthread, *m); + ret = mutex_self_lock(curthread, *m, abstime); /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); @@ -926,9 +921,10 @@ } static inline int -mutex_self_lock(struct pthread *curthread, pthread_mutex_t m) +mutex_self_lock(struct pthread *curthread, pthread_mutex_t m, + const struct timespec *abstime) { - struct timespec ts; + struct timespec ts1, ts2; int ret; switch (m->m_type) { @@ -946,18 +942,22 @@ * What SS2 define as a 'normal' mutex. Intentionally * deadlock on attempts to get a lock you already own. */ + ret = 0; if (m->m_protocol != PTHREAD_PRIO_NONE) { /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &m->m_lock); - ts.tv_sec = 30; - ts.tv_nsec = 0; + } + if (abstime) { + clock_gettime(CLOCK_REALTIME, &ts1); + TIMESPEC_SUB(&ts2, abstime, &ts1); + __sys_nanosleep(&ts2, NULL); + ret = ETIMEDOUT; + } else { + ts1.tv_sec = 30; + ts1.tv_nsec = 0; for (;;) - __sys_nanosleep(&ts, NULL); - } else { - PANIC("shouldn't be here!\n"); + __sys_nanosleep(&ts1, NULL); } - - ret = 0; break; case PTHREAD_MUTEX_RECURSIVE: