-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_6_spinlock_acq_rel.c
75 lines (64 loc) · 1.24 KB
/
8_6_spinlock_acq_rel.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <sched.h>
#include <stdio.h>
#include <stdbool.h>
#include <pthread.h>
#include <stdint.h>
/**
* Swap to 0 to look at performance difference between ACQ+REL vs
* full barrier.
*/
#define USE_ACQUIRE_RELEASE 1
#if USE_ACQUIRE_RELEASE
static void
spin_lock(volatile bool *lock)
{
while (__atomic_test_and_set(lock, __ATOMIC_ACQUIRE))
{}
}
static void
spin_unlock(volatile bool *lock)
{
__atomic_clear(lock, __ATOMIC_RELEASE);
}
#else
static void
spin_lock(volatile bool *lock)
{
while (! __sync_bool_compare_and_swap(lock, 0, 1))
{}
}
static void
spin_unlock(volatile bool *lock)
{
__sync_bool_compare_and_swap(lock, 1, 0);
}
#endif
static volatile bool start = false;
static uint64_t counter = 0;
static volatile bool lock = false;
void *
thread_f(void *arg)
{
while (! start) {};
for (int i = 0; i < 10000000; ++i) {
spin_lock(&lock);
++counter;
spin_unlock(&lock);
}
return NULL;
}
int
main()
{
pthread_t tid[6];
for (int i = 0; i < 6; ++i)
pthread_create(&tid[i], NULL, thread_f, (void *) i);
clock_t ts1 = clock();
start = true;
for (int i = 0; i < 6; ++i)
pthread_join(tid[i], NULL);
clock_t ts2 = clock();
printf("counter = %llu, time = %lf\n", counter,
((double) (ts2 - ts1)) / CLOCKS_PER_SEC);
return 0;
}