svn commit: r317610 - head/lib/libc/gen

Konstantin Belousov kib at FreeBSD.org
Sun Apr 30 19:32:52 UTC 2017


Author: kib
Date: Sun Apr 30 19:32:51 2017
New Revision: 317610
URL: https://svnweb.freebsd.org/changeset/base/317610

Log:
  Restructure normal (non-error) control flow in sem_close().
  
  Do not retest for the found semaphore after the loop to look it up.
  Instead, handle both cases of last and non-last close simultaneously,
  which allows to consolidate the list unlock and successful return.
  
  Sponsored by:	The FreeBSD Foundation
  MFC after:	1 week

Modified:
  head/lib/libc/gen/sem_new.c

Modified: head/lib/libc/gen/sem_new.c
==============================================================================
--- head/lib/libc/gen/sem_new.c	Sun Apr 30 18:39:31 2017	(r317609)
+++ head/lib/libc/gen/sem_new.c	Sun Apr 30 19:32:51 2017	(r317610)
@@ -41,6 +41,7 @@
 #include <fcntl.h>
 #include <pthread.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -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);


More information about the svn-src-all mailing list