svn commit: r185468 - head/lib/libc/stdlib

Jason Evans jasone at FreeBSD.org
Sat Nov 29 21:55:25 PST 2008


Author: jasone
Date: Sun Nov 30 05:55:24 2008
New Revision: 185468
URL: http://svn.freebsd.org/changeset/base/185468

Log:
  Do not spin when trying to lock on a single-CPU system.
  
  Reported by:	davidxu

Modified:
  head/lib/libc/stdlib/malloc.c

Modified: head/lib/libc/stdlib/malloc.c
==============================================================================
--- head/lib/libc/stdlib/malloc.c	Sun Nov 30 05:45:18 2008	(r185467)
+++ head/lib/libc/stdlib/malloc.c	Sun Nov 30 05:55:24 2008	(r185468)
@@ -1262,18 +1262,20 @@ malloc_spin_lock(pthread_mutex_t *lock)
 
 	if (__isthreaded) {
 		if (_pthread_mutex_trylock(lock) != 0) {
-			unsigned i;
-			volatile unsigned j;
+			/* Exponentially back off if there are multiple CPUs. */
+			if (ncpus > 1) {
+				unsigned i;
+				volatile unsigned j;
+
+				for (i = 1; i <= SPIN_LIMIT_2POW; i++) {
+					for (j = 0; j < (1U << i); j++) {
+						ret++;
+						CPU_SPINWAIT;
+					}
 
-			/* Exponentially back off. */
-			for (i = 1; i <= SPIN_LIMIT_2POW; i++) {
-				for (j = 0; j < (1U << i); j++) {
-					ret++;
-					CPU_SPINWAIT;
+					if (_pthread_mutex_trylock(lock) == 0)
+						return (ret);
 				}
-
-				if (_pthread_mutex_trylock(lock) == 0)
-					return (ret);
 			}
 
 			/*


More information about the svn-src-head mailing list