summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/spinlock/spinlock.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/lib/libpthread/spinlock/spinlock.c')
-rw-r--r--regress/lib/libpthread/spinlock/spinlock.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/regress/lib/libpthread/spinlock/spinlock.c b/regress/lib/libpthread/spinlock/spinlock.c
new file mode 100644
index 00000000000..dadfe14d1f0
--- /dev/null
+++ b/regress/lib/libpthread/spinlock/spinlock.c
@@ -0,0 +1,50 @@
+/* $OpenBSD: spinlock.c,v 1.1 2012/05/03 09:07:17 pirofti Exp $ */
+/* Paul Irofti <pirofti@openbsd.org>, 2012. Public Domain. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <unistd.h>
+
+#include "test.h"
+
+void *
+foo(void *arg)
+{
+ int rc = 0;
+ pthread_spinlock_t l = (pthread_spinlock_t)arg;
+ rc = pthread_spin_trylock(&l);
+ if (rc != 0 && rc != EBUSY) {
+ PANIC("pthread_trylock returned %d", rc);
+ }
+ if (rc == 0) {
+ CHECKr(pthread_spin_unlock(&l));
+ }
+ CHECKr(pthread_spin_lock(&l));
+ CHECKr(pthread_spin_unlock(&l));
+ return NULL;
+}
+
+int main()
+{
+ int i;
+ pthread_t thr[10];
+ pthread_spinlock_t l;
+
+ _CHECK(pthread_spin_init(&l, PTHREAD_PROCESS_SHARED), == ENOTSUP,
+ strerror(_x));
+
+ CHECKr(pthread_spin_init(&l, PTHREAD_PROCESS_PRIVATE));
+ for (i = 0; i < 10; i++) {
+ printf("Thread %d started\n", i);
+ CHECKr(pthread_create(&thr[i], NULL, foo, (void *)l));
+ }
+ for (i = 0; i < 10; i++) {
+ CHECKr(pthread_join(thr[i], NULL));
+ }
+ CHECKr(pthread_spin_destroy(&l));
+
+ SUCCEED;
+}