-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBarrierTreeCombMCS.c
62 lines (51 loc) · 2 KB
/
BarrierTreeCombMCS.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
// John M. Mellor-Crummey and Michael L. Scott, Algorithms for Scalable Synchronization on Shared-Memory Multiprocessors,
// ACM Transactions on Computer Systems, 9(1), February 1991, Fig. 9, p. 36
typedef struct CALIGN Barrier_node {
TYPE k, count;
VTYPE locksense;
struct Barrier_node * parent;
} Barrier_node;
typedef struct {
Barrier_node * bn;
} Barrier;
static TYPE PAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static Barrier b CALIGN;
static TYPE PAD2 CALIGN __attribute__(( unused )); // protect further false sharing
#define BARRIER_DECL TYPE sense = true;
#define BARRIER_CALL block( &b, p, &sense );
static inline void block_aux( Barrier_node * bn, TYPE sense ) {
if ( Fai( bn->count, 1 ) == bn->k - 1 ) {
if ( bn->parent ) {
block_aux( bn->parent, sense ); // recursion
} // if
bn->count = 0;
bn->locksense = ! bn->locksense;
Fence();
} // if
await( bn->locksense == sense );
} // block_aux
static inline void block( Barrier * b, TYPE p, TYPE * sense ) {
TYPE lsense = *sense; // optimization (compiler probably does it)
block_aux( &b->bn[p], lsense );
*sense = ! lsense;
} // block
#include "BarrierWorker.c"
const TYPE fanin = 2;
void __attribute__((noinline)) ctor() {
worker_ctor();
b.bn = Allocator( sizeof(typeof(b.bn[0])) * N );
b.bn[0] = (Barrier_node){ 1, 0, false, NULL }; // special case for root (no parent)
for ( typeof(N) i = 1; i < N; i += 1 ) {
// add child based on fanin structure (num children per node) of the tree
b.bn[i] = (Barrier_node){ 1, 0, false, &b.bn[(i - 1) / fanin ] };
// update fanin counter for parent (important for trees with a number of nodes that is not a power of fanin)
b.bn[(i - 1) / fanin ].k += 1;
} // for
} // ctor
void __attribute__((noinline)) dtor() {
free( b.bn );
worker_dtor();
} // dtor
// Local Variables: //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=BarrierTreeCombMCS Harness.c -lpthread -lm -D`hostname` -DCFMT" //
// End: //