From e3d383dcc0e8bb03c78c139264c81131e4b80df5 Mon Sep 17 00:00:00 2001 From: kib Date: Sun, 7 May 2017 07:54:21 +0000 Subject: MFC r317610: Restructure normal (non-error) control flow in sem_close(). --- lib/libc/gen/sem_new.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'lib/libc') diff --git a/lib/libc/gen/sem_new.c b/lib/libc/gen/sem_new.c index e95b290..3e1f5ea 100644 --- a/lib/libc/gen/sem_new.c +++ b/lib/libc/gen/sem_new.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -260,6 +261,7 @@ int _sem_close(sem_t *sem) { struct sem_nameinfo *ni; + bool last; if (sem_check_validity(sem) != 0) return (-1); @@ -274,21 +276,17 @@ _sem_close(sem_t *sem) _pthread_mutex_lock(&sem_llock); LIST_FOREACH(ni, &sem_list, next) { if (sem == ni->sem) { - if (--ni->open_count > 0) { - _pthread_mutex_unlock(&sem_llock); - return (0); + last = --ni->open_count == 0; + if (last) + LIST_REMOVE(ni, next); + _pthread_mutex_unlock(&sem_llock); + if (last) { + munmap(sem, sizeof(*sem)); + free(ni); } - break; + return (0); } } - - if (ni != NULL) { - LIST_REMOVE(ni, next); - _pthread_mutex_unlock(&sem_llock); - munmap(sem, sizeof(*sem)); - free(ni); - return (0); - } _pthread_mutex_unlock(&sem_llock); errno = EINVAL; return (-1); -- cgit v1.1