From 4890b5c1594c7c7b253e144c69171365a885158a Mon Sep 17 00:00:00 2001 From: Philip Guenthe Date: Tue, 28 Feb 2012 02:41:57 +0000 Subject: Our default mutex type is PTHREAD_MUTEX_ERRORCHECK, for which trying to unlock an uninitialized mutex is required to return EPERM, so add the necessary checks. For recursive mutexes, return an error from pthread_mutex_lock() if the count would overflow. problem observed in glib testing by aja@ --- lib/librthread/rthread_sync.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'lib/librthread') diff --git a/lib/librthread/rthread_sync.c b/lib/librthread/rthread_sync.c index 2d5b5afe587..c5d74f131c6 100644 --- a/lib/librthread/rthread_sync.c +++ b/lib/librthread/rthread_sync.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rthread_sync.c,v 1.32 2012/02/23 07:58:25 guenther Exp $ */ +/* $OpenBSD: rthread_sync.c,v 1.33 2012/02/28 02:41:56 guenther Exp $ */ /* * Copyright (c) 2004,2005 Ted Unangst * Copyright (c) 2012 Philip Guenther @@ -128,6 +128,10 @@ _rthread_mutex_lock(pthread_mutex_t *mutexp, int trywait, _spinlock(&mutex->lock); return (ETIMEDOUT); } + if (mutex->count == INT_MAX) { + _spinunlock(&mutex->lock); + return (EAGAIN); + } } else if (trywait) { /* try failed */ _spinunlock(&mutex->lock); @@ -183,6 +187,11 @@ pthread_mutex_unlock(pthread_mutex_t *mutexp) _rthread_debug(5, "%p: mutex_unlock %p\n", (void *)self, (void *)mutex); +#if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_ERRORCHECK + if (mutex == NULL) + return (EPERM); +#endif + if (mutex->owner != self) return (EPERM); @@ -264,6 +273,11 @@ pthread_cond_timedwait(pthread_cond_t *condp, pthread_mutex_t *mutexp, _rthread_debug(5, "%p: cond_timed %p,%p\n", (void *)self, (void *)cond, (void *)mutex); +#if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_ERRORCHECK + if (mutex == NULL) + return (EPERM); +#endif + if (mutex->owner != self) return (EPERM); if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || @@ -403,6 +417,11 @@ pthread_cond_wait(pthread_cond_t *condp, pthread_mutex_t *mutexp) _rthread_debug(5, "%p: cond_timed %p,%p\n", (void *)self, (void *)cond, (void *)mutex); +#if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_ERRORCHECK + if (mutex == NULL) + return (EPERM); +#endif + if (mutex->owner != self) return (EPERM); -- cgit v1.2.3