-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbakery-test.c
116 lines (95 loc) · 2.92 KB
/
bakery-test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <locks/bakery.h>
bakery_lock_t lock;
pthread_spinlock_t spinlock;
bool use_locks = true;
bool use_pthread_mutex;
volatile int current;
static void *thread_start(void *arg) {
const int threadnum = (long) arg;
struct timespec sleepamt;
if (use_locks) {
if (use_pthread_mutex)
pthread_spin_lock(&spinlock);
else
bakery_lock(&lock, threadnum);
}
printf("thread %d: I acquired it!\n", threadnum);
printf("\tthread %d: these numbers should be in order and contiguous:\n", threadnum);
for (unsigned i=1; i<=10; ++i)
printf("\tthread %d: %d\n", threadnum, i);
current = threadnum;
sleepamt.tv_sec = 0;
sleepamt.tv_nsec = 1000;
for (int i=0; i<10000; ++i) {
assert(current == threadnum);
nanosleep(&sleepamt, NULL);
current = threadnum;
}
if (use_locks) {
if (use_pthread_mutex)
pthread_spin_unlock(&spinlock);
else
bakery_unlock(&lock, threadnum);
}
return 0;
}
int main(int argc, char *argv[]) {
static struct option options[] = {
{ "numthreads", 1, 0, 'n' },
{ "pmutex", 0, 0, 'p' },
{ "unlocked", 0, 0, 'u' },
{ 0, 0, 0, 0 }
};
int numthreads = 1;
int opt;
while ((opt = getopt_long(argc, argv, "n:pu", options, NULL)) != -1) {
switch (opt) {
case 'n':
{
numthreads = strtol(optarg, NULL, 10);
if (errno) {
fprintf(stderr, "Invalid argument for 'numthreads': %s\n", strerror(errno));
exit(EXIT_FAILURE);
} else if (numthreads < 1) {
fprintf(stderr, "Invalid argument for 'numthreads': number must be >=1\n");
exit(EXIT_FAILURE);
}
} break;
case 'p':
use_pthread_mutex = true;
break;
case 'u':
use_locks = false;
break;
case '?':
exit(EXIT_FAILURE);
break;
}
}
printf("running tests on %d threads with %s...\n", numthreads,
use_pthread_mutex ? "pthreads mutexes" : "Lamport's Bakery lock");
pthread_t threads[numthreads-1];
pthread_spin_init(&spinlock, PTHREAD_PROCESS_PRIVATE);
bakery_lock_init(&lock, numthreads);
for (long t=1; t<numthreads; ++t) {
if (pthread_create(&threads[t-1], NULL, &thread_start, (void *) t)) {
perror("pthread_create");
abort();
}
}
thread_start((void *) 0L);
for (long t=0; t<numthreads-1; ++t) {
pthread_join(threads[t], NULL);
}
pthread_spin_destroy(&spinlock);
bakery_lock_destroy(&lock);
return 0;
}