-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHesselinkBWBakery.c
117 lines (96 loc) · 3.34 KB
/
HesselinkBWBakery.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
117
// Hesselink, W.H. Correctness and Concurrent Complexity of the Black-White Bakery Algorithm. Formal Aspects of
// Computing, Figure 1, 28, 325-341 (2016). https://doi-org.proxy.lib.uwaterloo.ca/10.1007/s00165-016-0364-4
#include <stdbool.h>
static TYPE PAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static VTYPE color CALIGN;
static VTYPE * cho CALIGN, * pair CALIGN;
static TYPE PAD2 CALIGN __attribute__(( unused )); // protect further false sharing
#define fn( n, mcol ) (1 + (n % 2 == mcol ? n / 2 : 0))
#define guardA( n, mcol, lev, p, thr ) (n / 2 == 0 || n % 2 != mcol || lev < n / 2 || (p <= thr && lev <= n / 2))
#define guardB( n, mcol ) ((n / 2 == 0 || n % 2 == mcol))
static void * Worker( void * arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
TYPE mcol, lev, v;
#ifdef FAST
unsigned int cnt = 0, oid = id;
#endif // FAST
NCS_DECL;
typeof(&cho[0]) mycho = &cho[id]; // optimization
typeof(&pair[0]) mypair = &pair[id];
for ( int r = 0; r < RUNS; r += 1 ) {
RTYPE randomThreadChecksum = 0;
for ( entry = 0; stop == 0; entry += 1 ) {
NCS;
*mycho = true;
Fence();
mcol = color;
lev = 1;
for ( typeof(N) thr = 0; thr < N; thr += 1 ) {
// Macros fn has multiple reads of "n", which must only be read once, so variable pair[thr] is copied
// into local variable read "v" preventing multiple reads.
if ( thr != id ) {
v = pair[thr];
lev = MAX( lev, fn( v, mcol ) );
} // if
} // for
*mypair = 2 * lev + mcol;
WO( Fence(); );
*mycho = false;
Fence();
for ( typeof(N) thr = 0; thr < N; thr += 1 ) {
if ( thr != id ) {
typeof(&cho[0]) othercho = &cho[thr]; // optimization
await( ! *othercho );
WO( Fence(); );
// Macros guardA and guardB have multiple reads of "n", but each read occurs in an independent
// disjunction, where reading different values for each disjunction does not affect correctness.
typeof(&pair[0]) otherpair = &pair[thr]; // optimization
if ( *otherpair % 2 == mcol )
await( guardA( *otherpair, mcol, lev, id, thr ) );
else
await( guardB( *otherpair, mcol ) || color != mcol );
} // if
} // for
WO( Fence(); );
randomThreadChecksum += CS( id );
WO( Fence(); );
color = 1 - mcol;
WO( Fence(); );
*mypair = mcol;
WO( Fence(); );
#ifdef FAST
id = startpoint( cnt ); // different starting point each experiment
mycho = &cho[id]; // optimization
mypair = &pair[id];
cnt = cycleUp( cnt, NoStartPoints );
#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() {
color = 0;
cho = Allocator( sizeof(typeof(cho[0])) * N );
pair = Allocator( sizeof(typeof(pair[0])) * N );
for ( typeof(N) i = 0; i < N; i += 1 ) { // initialize shared data
cho[i] = false;
pair[i] = 0;
} // for
} // ctor
void __attribute__((noinline)) dtor() {
free( (void *)pair );
free( (void *)cho );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=HesselinkBWBakery Harness.c -lpthread -lm -D`hostname` -DCFMT -DCNT=0" //
// End: //