Date: Wed, 5 Jan 2005 02:41:59 GMT From: David Xu <davidxu@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 68289 for review Message-ID: <200501050241.j052fxC7069264@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
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:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200501050241.j052fxC7069264>