summaryrefslogtreecommitdiffstats
path: root/lib/libthr/thread/thr_mutex.c
diff options
context:
space:
mode:
authordavidxu <davidxu@FreeBSD.org>2010-09-01 03:11:21 +0000
committerdavidxu <davidxu@FreeBSD.org>2010-09-01 03:11:21 +0000
commit5f00b957aee7c4cc6138734e4b4dc582cf4d10f9 (patch)
tree121f43c8c92789442e82ae22a69ea70be4586b0a /lib/libthr/thread/thr_mutex.c
parent4dcb50723aa0d3f4264ee1d9ca3baf70b4adc2de (diff)
downloadFreeBSD-src-5f00b957aee7c4cc6138734e4b4dc582cf4d10f9.zip
FreeBSD-src-5f00b957aee7c4cc6138734e4b4dc582cf4d10f9.tar.gz
Change atfork lock from mutex to rwlock, also make mutexes used by malloc()
module private type, when private type mutex is locked/unlocked, thread critical region is entered or leaved. These changes makes fork() async-signal safe which required by POSIX. Note that user's atfork handler still needs to be async-signal safe, but it is not problem of libthr, it is user's responsiblity.
Diffstat (limited to 'lib/libthr/thread/thr_mutex.c')
-rw-r--r--lib/libthr/thread/thr_mutex.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c
index 5b69952..fd8a342 100644
--- a/lib/libthr/thread/thr_mutex.c
+++ b/lib/libthr/thread/thr_mutex.c
@@ -224,8 +224,12 @@ _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
.m_ceiling = 0
};
static const struct pthread_mutex_attr *pattr = &attr;
+ int ret;
- return mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb);
+ ret = mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb);
+ if (ret == 0)
+ (*mutex)->m_private = 1;
+ return (ret);
}
void
@@ -319,13 +323,16 @@ mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
id = TID(curthread);
m = *mutex;
+ if (m->m_private)
+ THR_CRITICAL_ENTER(curthread);
ret = _thr_umutex_trylock(&m->m_lock, id);
if (ret == 0) {
ENQUEUE_MUTEX(curthread, m);
} else if (m->m_owner == curthread) {
ret = mutex_self_trylock(m);
} /* else {} */
-
+ if (ret && m->m_private)
+ THR_CRITICAL_LEAVE(curthread);
return (ret);
}
@@ -417,13 +424,19 @@ static inline int
mutex_lock_common(struct pthread *curthread, struct pthread_mutex *m,
const struct timespec *abstime)
{
+ int ret;
+ if (m->m_private)
+ THR_CRITICAL_ENTER(curthread);
if (_thr_umutex_trylock2(&m->m_lock, TID(curthread)) == 0) {
ENQUEUE_MUTEX(curthread, m);
- return (0);
+ ret = 0;
+ } else {
+ ret = mutex_lock_sleep(curthread, m, abstime);
}
-
- return (mutex_lock_sleep(curthread, m, abstime));
+ if (ret && m->m_private)
+ THR_CRITICAL_LEAVE(curthread);
+ return (ret);
}
int
@@ -625,6 +638,8 @@ mutex_unlock_common(pthread_mutex_t *mutex)
MUTEX_INIT_LINK(m);
_thr_umutex_unlock(&m->m_lock, id);
}
+ if (m->m_private)
+ THR_CRITICAL_LEAVE(curthread);
return (0);
}
@@ -660,6 +675,9 @@ _mutex_cv_unlock(pthread_mutex_t *mutex, int *count)
}
MUTEX_INIT_LINK(m);
_thr_umutex_unlock(&m->m_lock, TID(curthread));
+
+ if (m->m_private)
+ THR_CRITICAL_LEAVE(curthread);
return (0);
}
OpenPOWER on IntegriCloud