-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBarrierSenseRev.c
47 lines (39 loc) · 1.48 KB
/
BarrierSenseRev.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
// Debra Hensgen, 1 Raphael Finkel, 1 and Udi Manber, Two Algorithms for Barrier Synchronization International Journal
// of Parallel Programming, Vol. 17, No. 1, 1988 p. 7-8
//
// 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. 8, p. 34
typedef struct {
VTYPE CALIGN flag;
TYPE CALIGN count;
pthread_mutex_t lock;
} Barrier;
static TYPE PAD1 CALIGN __attribute__(( unused )); // protect further false sharing
static Barrier b CALIGN = { .flag = 0, .count = 0, .lock = PTHREAD_MUTEX_INITIALIZER };
static TYPE PAD2 CALIGN __attribute__(( unused )); // protect further false sharing
#define BARRIER_DECL
#define BARRIER_CALL block( &b );
static inline void block( Barrier * b ) {
TYPE negflag = ! b->flag;
pthread_mutex_lock( &b->lock );
b->count += 1;
if ( b->count < N ) {
pthread_mutex_unlock( &b->lock );
await( b->flag == negflag );
} else {
// CALL ACTION CALLBACK BEFORE TRIGGERING BARRIER
b->count = 0;
pthread_mutex_unlock( &b->lock );
b->flag = negflag;
} // if
} // block
#include "BarrierWorker.c"
void __attribute__((noinline)) ctor() {
worker_ctor();
} // ctor
void __attribute__((noinline)) dtor() {
worker_dtor();
} // dtor
// Local Variables: //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=BarrierSenseRev Harness.c -lpthread -lm -D`hostname` -DCFMT" //
// End: //