Browse Source

manually "shrink wrap" fast path in pthread_once

this change is a workaround for the inability of current compilers to
perform "shrink wrapping" optimizations. in casual testing, it roughly
doubled the performance of pthread_once when called on an
already-finished once control object.
Rich Felker 10 years ago
parent
commit
dc95322e18
1 changed files with 12 additions and 8 deletions
  1. 12 8
      src/thread/pthread_once.c

+ 12 - 8
src/thread/pthread_once.c

@@ -8,15 +8,8 @@ static void undo(void *control)
 		__wake(control, -1, 1);
 		__wake(control, -1, 1);
 }
 }
 
 
-int __pthread_once(pthread_once_t *control, void (*init)(void))
+int __pthread_once_full(pthread_once_t *control, void (*init)(void))
 {
 {
-	/* Return immediately if init finished before, but ensure that
-	 * effects of the init routine are visible to the caller. */
-	if (*control == 2) {
-		a_barrier();
-		return 0;
-	}
-
 	/* Try to enter initializing state. Four possibilities:
 	/* Try to enter initializing state. Four possibilities:
 	 *  0 - we're the first or the other cancelled; run init
 	 *  0 - we're the first or the other cancelled; run init
 	 *  1 - another thread is running init; wait
 	 *  1 - another thread is running init; wait
@@ -43,4 +36,15 @@ int __pthread_once(pthread_once_t *control, void (*init)(void))
 	}
 	}
 }
 }
 
 
+int __pthread_once(pthread_once_t *control, void (*init)(void))
+{
+	/* Return immediately if init finished before, but ensure that
+	 * effects of the init routine are visible to the caller. */
+	if (*control == 2) {
+		a_barrier();
+		return 0;
+	}
+	return __pthread_once_full(control, init);
+}
+
 weak_alias(__pthread_once, pthread_once);
 weak_alias(__pthread_once, pthread_once);