blob: 5918f6b32466327a889fa04e72947fb9f87fd8ae (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/* $OpenBSD: spinlock.c,v 1.2 2020/04/06 00:01:08 pirofti Exp $ */
/* Paul Irofti <paul@irofti.net>, 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;
}
|