forked from pabuhr/concurrent-locking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZhang2T.c
116 lines (108 loc) · 2.7 KB
/
Zhang2T.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
static volatile TYPE **x CALIGN, **c CALIGN;
int lN;
static void *Worker( void *arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
#ifdef FAST
unsigned int cnt = 0, oid = id;
#endif // FAST
int high, j, l, rival;
for ( int r = 0; r < RUNS; r += 1 ) {
entry = 0;
while ( stop == 0 ) {
j = 0;
l = id;
high = Clog2( N ); // maximal depth of binary tree
while ( j < high ) {
if ( l % 2 == 0 ) {
x[j][l] = id;
c[j][id] = 1;
Fence(); // force store before more loads
rival = x[j][l + 1];
if ( rival != -1 ) {
c[j][rival] = 0;
Fence(); // force store before more loads
while( c[j][id] != 0 ) Pause();
}
} else {
x[j][l] = id;
Fence(); // force store before more loads
yy:
rival = x[j][l - 1];
if ( rival != -1 ) {
c[j][rival] = 0;
Fence(); // force store before more loads
while ( c[j][id] != 0 ) Pause();
c[j][id] = 1;
Fence(); // force store before more loads
goto yy;
} // if
} // if
l /= 2;
j += 1;
}
CriticalSection( id );
j = high;
//int pow2 = pow( Degree, high );
int pow2 = 1 << high;
while ( j > 0 ) {
j -= 1;
pow2 /= Degree;
l = id / pow2;
x[j][l] = -1;
int temp = (l % 2 == 0) ? l + 1 : l - 1;
Fence(); // force store before more loads
rival = x[j][temp];
if ( rival != -1 ) {
c[j][rival] = 0;
}
} // while
#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() {
Degree = 2;
lN = N;
if ( N % 2 == 1 ) lN += 1;
x = Allocator( sizeof(typeof(x[0])) * lN );
for ( int i = 0; i < lN; i += 1 ) {
x[i] = Allocator( sizeof(typeof(x[0][0])) * lN );
} // for
c = Allocator( sizeof(typeof(c[0])) * lN );
for ( int i = 0; i < lN; i += 1 ) {
c[i] = Allocator( sizeof(typeof(c[0][0])) * lN );
} // for
for ( int i = 0; i < lN; i += 1 ) { // initialize shared data
for ( int j = 0; j < lN; j += 1 ) {
x[i][j] = -1;
c[i][j] = 0;
} // for
} // for
} // ctor
void dtor() {
for ( int i = 0; i < N; i += 1 ) {
free( (void *)c[i] );
} // for
for ( int i = 0; i < lN; i += 1 ) {
free( (void *)x[i] );
} // for
free( (void *)c );
free( (void *)x );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=Zhang2T Harness.c -lpthread -lm" //
// End: //