From 8330d43435f37955b5a44318bc10652bac376e68 Mon Sep 17 00:00:00 2001 From: mtm Date: Wed, 18 Feb 2004 15:22:52 +0000 Subject: o Style o Instead of checking both the passed in pointer and its value for NULL, only check the latter. Any caller that passes in a NULL pointer is obviously wrong. --- lib/libthr/thread/thr_mutex_prioceiling.c | 58 +++++++++++++++---------------- lib/libthr/thread/thr_mutex_protocol.c | 29 ++++++---------- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/lib/libthr/thread/thr_mutex_prioceiling.c b/lib/libthr/thread/thr_mutex_prioceiling.c index 7701378..c7396a4 100644 --- a/lib/libthr/thread/thr_mutex_prioceiling.c +++ b/lib/libthr/thread/thr_mutex_prioceiling.c @@ -47,9 +47,7 @@ _pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) { int ret = 0; - if ((mattr == NULL) || (*mattr == NULL)) - ret = EINVAL; - else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT) + if (*mattr == NULL) ret = EINVAL; else *prioceiling = (*mattr)->m_ceiling; @@ -62,12 +60,13 @@ _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) { int ret = 0; - if ((mattr == NULL) || (*mattr == NULL)) - ret = EINVAL; - else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT) + if (*mattr == NULL) ret = EINVAL; - else + else if (prioceiling <= PTHREAD_MAX_PRIORITY && + prioceiling >= PTHREAD_MIN_PRIORITY) (*mattr)->m_ceiling = prioceiling; + else + ret = EINVAL; return (ret); } @@ -76,16 +75,11 @@ int _pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *prioceiling) { - int ret; - - if ((mutex == NULL) || (*mutex == NULL)) - ret = EINVAL; - else if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT) - ret = EINVAL; + if (*mutex == NULL) + return (EINVAL); else - ret = (*mutex)->m_prio; - - return (ret); + *prioceiling = (*mutex)->m_prio; + return (0); } int @@ -94,20 +88,26 @@ _pthread_mutex_setprioceiling(pthread_mutex_t *mutex, { int ret = 0; - if ((mutex == NULL) || (*mutex == NULL)) - ret = EINVAL; - else if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT) - ret = EINVAL; - else { - /* Lock the mutex: */ - if ((ret = pthread_mutex_lock(mutex)) == 0) { - /* Return the old ceiling and set the new ceiling: */ - *old_ceiling = (*mutex)->m_prio; - (*mutex)->m_prio = prioceiling; + if (*mutex == NULL) + return (EINVAL); + else if (prioceiling > PTHREAD_MAX_PRIORITY || + prioceiling < PTHREAD_MIN_PRIORITY) + return (EINVAL); + + /* + * Because of the use of pthread_mutex_unlock(), the + * priority ceiling of a mutex cannot be changed + * while the mutex is held by another thread. It also, + * means that the the thread trying to change the + * priority ceiling must adhere to prio protection rules. + */ + if ((ret = pthread_mutex_lock(mutex)) == 0) { + /* Return the old ceiling and set the new ceiling: */ + *old_ceiling = (*mutex)->m_prio; + (*mutex)->m_prio = prioceiling; - /* Unlock the mutex: */ - ret = pthread_mutex_unlock(mutex); - } + /* Unlock the mutex: */ + ret = pthread_mutex_unlock(mutex); } return(ret); } diff --git a/lib/libthr/thread/thr_mutex_protocol.c b/lib/libthr/thread/thr_mutex_protocol.c index f7be5a6..56c8622 100644 --- a/lib/libthr/thread/thr_mutex_protocol.c +++ b/lib/libthr/thread/thr_mutex_protocol.c @@ -43,28 +43,19 @@ __weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol); int _pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) { - int ret = 0; - - if ((mattr == NULL) || (*mattr == NULL)) - ret = EINVAL; - else - *protocol = (*mattr)->m_protocol; - - return(ret); + if (*mattr == NULL) + return (EINVAL); + *protocol = (*mattr)->m_protocol; + return(0); } int _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) { - int ret = 0; - - if ((mattr == NULL) || (*mattr == NULL) || - (protocol < PTHREAD_PRIO_NONE) || (protocol > PTHREAD_PRIO_PROTECT)) - ret = EINVAL; - else { - (*mattr)->m_protocol = protocol; - (*mattr)->m_ceiling = PTHREAD_MAX_PRIORITY; - } - return(ret); + if (*mattr == NULL || protocol < PTHREAD_PRIO_NONE || + protocol > PTHREAD_PRIO_PROTECT) + return (EINVAL); + (*mattr)->m_protocol = protocol; + (*mattr)->m_ceiling = PTHREAD_MAX_PRIORITY; + return(0); } - -- cgit v1.1