-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBarrierGen.c
More file actions
50 lines (39 loc) · 1.5 KB
/
BarrierGen.c
File metadata and controls
50 lines (39 loc) · 1.5 KB
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
// This algorithm seems to be in the concurrency folklore (anonymous and transmitted orally), and hence, there is no
// citation.
// generation can overflow, but equality test works. 2^{32/64} + 1 threads must arrive simultaneously for failure.
#include "BarrierCallback.h"
typedef struct {
TYPE CALIGN group;
VTYPE CALIGN count;
VTYPE CALIGN generation;
CBDECL();
} 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
#define BARRIER_CALL block( &b );
static inline bool block( Barrier * b ) {
CBSTART(); // must be first
TYPE mygen = b->generation;
if ( LIKELY( Fai( b->count, 1 ) != b->group - 1 ) ) { // wait ?
await( b->generation != mygen ); // wait for my generation
return false;
} // if
CBEND(); // must appear in safe location
b->count = 0; // release current generation
WO( Fence(); );
b->generation += 1; // start next generation
return true;
} // block
#include "BarrierWorker.c"
void __attribute__((noinline)) ctor() {
worker_ctor();
b = (Barrier){ .group = N, .count = 0, .generation = 0 CBINIT() };
} // ctor
void __attribute__((noinline)) dtor() {
worker_dtor();
} // dtor
// Local Variables: //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=BarrierGen Harness.c -lpthread -lm -D`hostname` -DCFMT" //
// End: //