From 4dd473489ef973f5d3a1b52dd87370ef00097810 Mon Sep 17 00:00:00 2001 From: deischen Date: Thu, 23 Feb 2006 21:34:08 +0000 Subject: Eliminate a race condition in timed waits (cv, mutex, and sleeps). MFC Candidate. PR: 93592 --- lib/libpthread/thread/thr_mutex.c | 46 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'lib/libpthread/thread/thr_mutex.c') diff --git a/lib/libpthread/thread/thr_mutex.c b/lib/libpthread/thread/thr_mutex.c index e095ac5..0f56069 100644 --- a/lib/libpthread/thread/thr_mutex.c +++ b/lib/libpthread/thread/thr_mutex.c @@ -551,14 +551,6 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); } else { - /* Set the wakeup time: */ - if (abstime) { - curthread->wakeup_time.tv_sec = - abstime->tv_sec; - curthread->wakeup_time.tv_nsec = - abstime->tv_nsec; - } - /* * Join the queue of threads waiting to lock * the mutex and save a pointer to the mutex. @@ -572,6 +564,14 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, * be able to safely set the state. */ THR_SCHED_LOCK(curthread, curthread); + /* Set the wakeup time: */ + if (abstime) { + curthread->wakeup_time.tv_sec = + abstime->tv_sec; + curthread->wakeup_time.tv_nsec = + abstime->tv_nsec; + } + THR_SET_STATE(curthread, PS_MUTEX_WAIT); THR_SCHED_UNLOCK(curthread, curthread); @@ -633,14 +633,6 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); } else { - /* Set the wakeup time: */ - if (abstime) { - curthread->wakeup_time.tv_sec = - abstime->tv_sec; - curthread->wakeup_time.tv_nsec = - abstime->tv_nsec; - } - /* * Join the queue of threads waiting to lock * the mutex and save a pointer to the mutex. @@ -659,6 +651,13 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, mutex_priority_adjust(curthread, *m); THR_SCHED_LOCK(curthread, curthread); + /* Set the wakeup time: */ + if (abstime) { + curthread->wakeup_time.tv_sec = + abstime->tv_sec; + curthread->wakeup_time.tv_nsec = + abstime->tv_nsec; + } THR_SET_STATE(curthread, PS_MUTEX_WAIT); THR_SCHED_UNLOCK(curthread, curthread); @@ -730,14 +729,6 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, /* Unlock the mutex structure: */ THR_LOCK_RELEASE(curthread, &(*m)->m_lock); } else { - /* Set the wakeup time: */ - if (abstime) { - curthread->wakeup_time.tv_sec = - abstime->tv_sec; - curthread->wakeup_time.tv_nsec = - abstime->tv_nsec; - } - /* * Join the queue of threads waiting to lock * the mutex and save a pointer to the mutex. @@ -756,6 +747,13 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *m, */ THR_SCHED_LOCK(curthread, curthread); + /* Set the wakeup time: */ + if (abstime) { + curthread->wakeup_time.tv_sec = + abstime->tv_sec; + curthread->wakeup_time.tv_nsec = + abstime->tv_nsec; + } THR_SET_STATE(curthread, PS_MUTEX_WAIT); THR_SCHED_UNLOCK(curthread, curthread); -- cgit v1.1