-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLycklamaBuhr.c
115 lines (97 loc) · 3.47 KB
/
LycklamaBuhr.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
// Edward A. Lycklama and Vassos Hadzilacos, A First-Come-First-Served Mutual-Exclusion Algorithm with Small
// Communication Variables, TOPLAS, 13(4), 1991, Fig. 4, p. 569
// Replaced pairs of bits by the values 0, 1, 2, respectively, and cycle through these values using modulo arithmetic.
static TYPE PAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static VTYPE * c CALIGN, * v CALIGN, * intents CALIGN, * turn CALIGN;
static TYPE PAD2 CALIGN __attribute__(( unused )); // protect further false sharing
static void * Worker( void * arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
#ifdef FAST
unsigned int cnt = 0, oid = id;
#endif // FAST
NCS_DECL;
TYPE copy[N];
typeof(N) j;
typeof(&c[0]) otherc;
typeof(&v[0]) otherv;
typeof(&intents[0]) otherintent;
for ( int r = 0; r < RUNS; r += 1 ) {
RTYPE randomThreadChecksum = 0;
// Here because of FAST reset
typeof(&c[0]) myc = &c[id]; // optimization
typeof(&v[0]) myv = &v[id];
typeof(&intents[0]) myintent = &intents[id];
for ( entry = 0; stop == 0; entry += 1 ) {
NCS;
*myc = 1; // stage 1, establish FCFS
Fence(); // force store before more loads
for ( j = 0; j < N; j += 1 ) // copy turn values
copy[j] = turn[j];
// turn[id] = cycleUp( turn[id], 4 ); // advance my turn
turn[id] = cycleUp( turn[id], 3 ); // advance my turn
*myv = 1;
*myc = 0;
Fence(); // force store before more loads
for ( j = 0; j < N; j += 1 ) {
otherc = &c[j]; // optimization
otherv = &v[j]; // optimization
while ( *otherc != 0 || (*otherv != 0 && copy[j] == turn[j]) ) Pause();
} // for
L: *myintent = 1; // B-L
Fence(); // force store before more loads
for ( j = 0; j < id; j += 1 ) // stage 2, high priority search
if ( intents[j] != 0 ) {
*myintent = 0;
Fence(); // force store before more loads
otherintent = &intents[j]; // optimization
while ( *otherintent != 0 ) Pause();
goto L;
} // if
for ( j = id + 1; j < N; j += 1 ) { // stage 3, low priority search
otherintent = &intents[j]; // optimization
while ( *otherintent != 0 ) Pause();
} // for
WO( Fence(); );
randomThreadChecksum += CS( id );
WO( Fence(); );
*myv = *myintent = 0; // exit protocol
WO( Fence(); );
#ifdef FAST
id = startpoint( cnt ); // different starting point each experiment
cnt = cycleUp( cnt, NoStartPoints );
myc = &c[id]; // must reset
myv = &v[id];
myintent = &intents[id];
#endif // FAST
} // for
Fai( sumOfThreadChecksums, randomThreadChecksum );
#ifdef FAST
id = oid;
#endif // FAST
entries[r][id] = entry;
Fai( Arrived, 1 );
while ( stop != 0 ) Pause();
Fai( Arrived, -1 );
} // for
return NULL;
} // Worker
void __attribute__((noinline)) ctor() {
c = Allocator( sizeof(typeof(c[0])) * N );
v = Allocator( sizeof(typeof(v[0])) * N );
intents = Allocator( sizeof(typeof(intents[0])) * N );
turn = Allocator( sizeof(typeof(turn[0])) * N );
for ( typeof(N) i = 0; i < N; i += 1 ) {
c[i] = v[i] = intents[i] = turn[i] = 0;
} // for
} // ctor
void __attribute__((noinline)) dtor() {
free( (void *)turn );
free( (void *)intents );
free( (void *)v );
free( (void *)c );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=LycklamaBuhr Harness.c -lpthread -lm -D`hostname` -DCFMT -DCNT=0" //
// End: //