forked from pabuhr/concurrent-locking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLynch.c
71 lines (63 loc) · 2.31 KB
/
Lynch.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
// Nancy A. Lynch, Distributed Algorithms, Morgan Kaufmann, 1996, Section 10.5.3
// Significant parts of this algorithm are written in prose, and therefore, left to our interpretation with respect to implementation.
static VTYPE *intents, *turns;
static int depth, width, mask;
static inline TYPE min( TYPE a, TYPE b ) { return a < b ? a : b; }
static void *Worker( void *arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
#ifdef FAST
unsigned int cnt = 0, oid = id;
#endif // FAST
unsigned int lid, comp, role, low, high;
for ( int r = 0; r < RUNS; r += 1 ) {
entry = 0;
while ( stop == 0 ) {
for ( TYPE km1 = 0, k = 1; k <= depth; km1 += 1, k += 1 ) { // entry protocol
lid = id >> km1; // local id
comp = (lid >> 1) + (width >> k); // unique position in the tree
role = lid & 1; // left or right descendent
intents[id] = k; // declare intent, current round
turns[comp] = role; // RACE
Fence(); // force store before more loads
low = ((lid) ^ 1) << km1; // lower competition
high = min( low | mask >> (depth - km1), N - 1 ); // higher competition
for ( int i = low; i <= high; i += 1 ) // busy wait
while ( intents[i] >= k && turns[comp] == role ) Pause();
} // for
CriticalSection( id );
intents[id] = 0; // exit protocol
#ifdef FAST
id = startpoint( cnt ); // different starting point each experiment
cnt = cycleUp( cnt, NoStartPoints );
#endif // FAST
entry += 1;
} // while
#ifdef FAST
id = oid;
#endif // FAST
entries[r][id] = entry;
__sync_fetch_and_add( &Arrived, 1 );
while ( stop != 0 ) Pause();
__sync_fetch_and_add( &Arrived, -1 );
} // for
return NULL;
} // Worker
void ctor() {
depth = Clog2( N ); // maximal depth of binary tree
width = 1 << depth; // maximal width of binary tree
mask = width - 1; // 1 bits for masking
intents = Allocator( sizeof(typeof(intents[0])) * N );
for ( int i = 0; i < N; i += 1 ) { // initialize shared data
intents[i] = 0;
} // for
turns = Allocator( sizeof(typeof(turns[0])) * width );
} // ctor
void dtor() {
free( (void *)turns );
free( (void *)intents );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=Lynch Harness.c -lpthread -lm" //
// End: //