-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathElevatorLinear.c
231 lines (193 loc) · 5.39 KB
/
ElevatorLinear.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdbool.h>
//======================================================
static TYPE PAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static VTYPE fast CALIGN;
#ifndef CAS
static VTYPE * b CALIGN, x CALIGN, y CALIGN;
#endif // ! CAS
static TYPE PAD2 CALIGN __attribute__(( unused )); // protect further false sharing
#ifdef CAS
#define trylock( x ) Cas( (fast), false, true )
#elif defined( WCasBL )
static inline bool trylock( TYPE id ) { // based on Burns-Lamport algorithm
b[id] = true;
Fence(); // force store before more loads
for ( typeof(id) thr = 0; thr < id; thr += 1 ) {
if ( FASTPATH( b[thr] ) ) {
b[id] = false;
return false ;
} // if
} // for
for ( typeof(id) thr = id + 1; thr < N; thr += 1 ) {
await( ! b[thr] );
} // for
bool leader;
if ( ! fast ) { fast = true; leader = true; }
else leader = false;
b[id] = false;
return leader;
} // WCas
#elif defined( WCasLF )
static inline bool trylock( TYPE id ) { // based on Lamport-Fast algorithm
b[id] = true;
x = id;
Fence(); // force store before more loads
if ( FASTPATH( y != N ) ) {
b[id] = false;
return false;
} // if
y = id;
Fence(); // force store before more loads
if ( FASTPATH( x != id ) ) {
b[id] = false;
Fence(); // OPTIONAL, force store before more loads
for ( typeof(N) k = 0; k < N; k += 1 )
await( ! b[k] );
if ( FASTPATH( y != id ) ) return false;
} // if
bool leader;
if ( ! fast ) { fast = true; leader = true; }
else leader = false;
y = N;
b[id] = false;
return leader;
} // WCas
#else
#error unsupported architecture
#endif // WCas
#define unlock() fast = false
//======================================================
static TYPE PAD3 CALIGN __attribute__(( unused )); // protect further false sharing
typedef struct CALIGN {
VTYPE apply;
#ifdef FLAG
VTYPE flag;
#endif // FLAG
} Flags;
static Flags * flags CALIGN;
#ifndef FLAG
static VTYPE first CALIGN;
#endif // FLAG
static TYPE PAD4 CALIGN __attribute__(( unused )); // protect further false sharing
static void * Worker( void * arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
// _Atomic qualifier not preserved in typeof => VTYPE
VTYPE * applyId = &flags[id].apply; // optimizations
#ifdef FLAG
VTYPE * flagId = &flags[id].flag;
VTYPE * flagN = &flags[N].flag;
#endif // FLAG
#ifdef FAST
typeof(id) cnt = 0, oid = id;
#endif // FAST
NCS_DECL;
typeof(id) thr;
for ( int r = 0; r < RUNS; r += 1 ) {
RTYPE randomThreadChecksum = 0;
for ( entry = 0; stop == 0; entry += 1 ) {
NCS;
*applyId = true; // entry protocol
if ( FASTPATH( trylock( id ) ) ) { // true => leader
#ifndef CAS
Fence(); // force store before more loads
#endif // ! CAS
#ifdef FLAG
await( *flagId || *flagN );
*flagN = false;
#else
await( first == id || first == N );
first = id;
#endif // FLAG
unlock();
} else {
#ifdef FLAG
await( *flagId );
#else
await( first == id );
#endif // FLAG
} // if
#ifdef FLAG
*flagId = false;
#endif // FLAG
randomThreadChecksum += CS( id );
#ifdef CYCLEUP
for ( thr = cycleUp( id, N ); ! flags[thr].apply; thr = cycleUp( thr, N ) );
#else // CYCLEDOWN
#define Mod( a, b, N ) ((a + b) < N ? (a + b) : (a + b - N))
for ( thr = N - 1; ! flags[Mod(id, thr, N)].apply; thr -= 1 );
#endif // CYCLEUP
*applyId = false; // must appear before setting first
#ifdef CYCLEUP
if ( FASTPATH( thr != id ) )
#ifdef FLAG
flags[thr].flag = true; else *flagN = true;
#else // NOFLAG
first = thr; else first = N;
#endif // FLAG
#else // CYCLEDOWN
if ( FASTPATH( thr != 0 ) )
#ifdef FLAG
flags[Mod(id, thr, N)].flag = true; else *flagN = true;
#else // NOFLAG
first = Mod(id, thr, N); else first = N;
#endif // FLAG
#endif // CYCLEUP
#ifdef FAST
id = startpoint( cnt ); // different starting point each experiment
cnt = cycleUp( cnt, NoStartPoints );
applyId = &flags[id].apply; // must reset for new id
#ifdef FLAG
flagId = &flags[id].flag;
flagN = &flags[N].flag;
#endif // FLAG
#endif // FAST
} // for
Fai( sumOfThreadChecksums, randomThreadChecksum );
#ifdef FAST
id = oid;
applyId = &flags[id].apply; // must reset for new id
#ifdef FLAG
flagId = &flags[id].flag;
flagN = &flags[N].flag;
#endif // FLAG
#endif // FAST
entries[r][id] = entry;
Fai( Arrived, 1 );
while ( stop != 0 ) Pause();
Fai( Arrived, -1 );
} // for
return NULL;
} // Worker
void __attribute__((noinline)) ctor() {
flags = Allocator( (N + 1) * sizeof(typeof(flags[0])) );
for ( typeof(N) id = 0; id <= N; id += 1 ) { // initialize shared data
flags[id].apply = false;
#ifdef FLAG
flags[id].flag = false;
#endif // FLAG
} // for
#ifdef FLAG
flags[N].flag = true;
#else
first = N;
#endif // FLAG
#ifndef CAS
b = Allocator( N * sizeof(typeof(b[0])) );
for ( typeof(N) id = 0; id < N; id += 1 ) { // initialize shared data
b[id] = false;
} // for
y = N;
#endif // CAS
unlock();
} // ctor
void __attribute__((noinline)) dtor() {
#ifndef CAS
free( (void *)b );
#endif // ! CAS
free( (void *)flags );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=ElevatorLinear -DWCasLF Harness.c -lpthread -lm -D`hostname` -DCFMT -DCNT=0" //
// End: //