-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedList.cpp
209 lines (160 loc) · 4.55 KB
/
SharedList.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "RealTime.h"
#include<iostream>
#include <thread>
#include <cstdatomic>
#include <functional>
#include <algorithm>
#include<vector>
#include<sstream>
// convert gcc to c0xx
#define thread_local __thread
typedef std::thread Thread;
typedef std::vector<std::thread> ThreadGroup;
typedef std::mutex Mutex;
typedef std::unique_lock<std::mutex> Guard;
typedef std::condition_variable Condition;
#define CACHE_LINE_SIZE 64 // 64 byte cache line on x86 and x86-64
long long threadId() {
std::stringstream ss;
ss << std::this_thread::get_id();
long long id;
ss >> id;
return id;
}
// only 64 bit!
const long tombMask= 0x80000000;
const long pointerMask= 0x7fffffff;
inline bool tombStone(void const * const volatile p) {
return ((long)(p)&tombMask)!=0;
}
template<typename T>
inline void setTombStone(T * volatile & p) {
__sync_or_and_fetch(&p,tombMask);
}
template<typename T>
inline T * asPointer(T * volatile p) {
return (T*)((long)(p)&pointerMask);
}
template<typename T>
class SharedList {
public:
typedef T value_type;
struct Node {
Node() : m_next(0){}
bool valid() const { return !tombStone(m_next); }
void markInvalid() { setTombStone(m_next);}
bool last() const { return 0==asPointer(m_next);}
Node * volatile next() { return asPointer(m_next); }
Node * volatile nextValid() {
return nv();
}
Node const * volatile nextValid() const {
return const_cast<Node*>(this)->nv();
}
Node * volatile nv() {
if (valid()) return m_next;
if (last()) return 0;
return next()->nextValid();
}
Node * volatile m_next;
value_type value;
}; // __attribute__ ((__aligned__(CACHE_LINE_SIZE)));
typedef Node * volatile pointer;
typedef Node const * volatile const_pointer;
SharedList() : head(), collisions(100,0){
// to remove init from timing
alloc();
}
pointer begin() { return head.nextValid();}
const_pointer begin() const { return head.nextValid();}
const_pointer end() const { return 0;}
Node * alloc() {
// return new Node;
static thread_local std::vector<Node> * pool=0;
static thread_local int last=0;
if (0==last) pool = new std::vector<Node>(150000);
return &(*pool)[last++];
}
// insert AFTER p
pointer insert(pointer p, value_type const & value) {
Node * me = alloc();
me->value=value;
// if (head==0 && __sync_bool_compare_and_swap(&head,0,me)) return me;
if (p==0) p=&head;
while (true) {
me->m_next = p->m_next;
if (__sync_bool_compare_and_swap(&(p->m_next),me->m_next,me)) break;
// __sync_add_and_fetch(&(collisions[int(value)]),1);
++collisions[int(value)];
}
return me;
}
// "remove"
pointer remove(pointer p) {
// never delete on the fly! just mark
p->markInvalid();
return p->nextValid();
}
private:
Node head;
public:
typedef int Aint; // __attribute__ ((__aligned__(CACHE_LINE_SIZE)));
std::vector<Aint> collisions;
};
template<typename T>
void dump(SharedList<T> const & list) {
size_t size=0;
typename SharedList<T>::const_pointer p= list.begin();
while(p) {
++size;
std::cout << size << ": " << (*p).value << std::endl;
p = p->nextValid();
}
}
struct Inserter {
static volatile long start;
static volatile long stop;
Inserter(SharedList<float> & ilist, float iid) : list(ilist),id(iid){}
void wait() {
__sync_add_and_fetch(&start,-1);
while(start) nanosleep(0,0);
}
void operator()() {
// wait everybody is ready;
wait();
// strict collision (or no collision)....
SharedList<float>::pointer p=list.begin();
for (int i=0; i<100000; ++i) {
list.insert(list.begin(),id);
// p = list.insert(p,id);
}
__sync_add_and_fetch(&stop,-1);
}
SharedList<float> & list;
float id;
};
volatile long Inserter::start=0;
volatile long Inserter::stop=0;
int main() {
const int NUMTHREADS=10;
Inserter::start=NUMTHREADS+1;
Inserter::stop=NUMTHREADS;
SharedList<float> list;
ThreadGroup threads;
threads.reserve(NUMTHREADS);
for (int i=0; i<NUMTHREADS; ++i) {
threads.push_back(Thread(Inserter(list,float(i))));
}
long long st = rdtsc();
__sync_add_and_fetch(&Inserter::start,-1);
do{}while(Inserter::stop);
long long tot = rdtsc()-st;
std::for_each(threads.begin(),threads.end(),
std::bind(&Thread::join,std::placeholders::_1));
// dump(list);
for (int i=0; i!=NUMTHREADS;++i)
std::cout << list.collisions[i] << " ,";
std::cout << std::endl;
std::cout << "time inserting " << tot <<std::endl;
return 0;
}