about summary refs log tree commit diff
path: root/src/thread/pthread_once.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/thread/pthread_once.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.xz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.zip
initial check-in, version 0.5.0 v0.5.0
Diffstat (limited to 'src/thread/pthread_once.c')
-rw-r--r--src/thread/pthread_once.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/thread/pthread_once.c b/src/thread/pthread_once.c
new file mode 100644
index 00000000..72230054
--- /dev/null
+++ b/src/thread/pthread_once.c
@@ -0,0 +1,38 @@
+#include "pthread_impl.h"
+
+static void undo(void *control)
+{
+	a_store(control, 0);
+	__wake(control, 1, 0);
+}
+
+int pthread_once(pthread_once_t *control, void (*init)(void))
+{
+	static int waiters;
+
+	/* Return immediately if init finished before */
+	if (*control == 2) return 0;
+
+	/* Try to enter initializing state. Three possibilities:
+	 *  0 - we're the first or the other cancelled; run init
+	 *  1 - another thread is running init; wait
+	 *  2 - another thread finished running init; just return */
+
+	for (;;) switch (a_swap(control, 1)) {
+	case 0:
+		break;
+	case 1:
+		__wait(control, &waiters, 1, 0);
+		continue;
+	case 2:
+		a_store(control, 2);
+		return 0;
+	}
+
+	pthread_cleanup_push(undo, control);
+	init();
+	pthread_cleanup_pop(0);
+
+	if (waiters) __wake(control, -1, 0);
+	return 0;
+}