-
Notifications
You must be signed in to change notification settings - Fork 2
/
thread.h
272 lines (213 loc) · 6.1 KB
/
thread.h
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#pragma once
#include <tr1/functional>
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <time.h>
#include "types.h"
using namespace std;
using namespace tr1;
using namespace placeholders; //for bind
// http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html
#ifdef SINGLE_THREAD
//#define CAS(var, old, new) (((var) == (old)) ? ((var) = (new), true) : false )
//#define INCR(var) (++(var))
//#define PLUS(var, val) ((var) += (val))
template<class A, class B, class C> bool CAS(A & var, const B & old, const C & newval){
if(var == old){
var = newval;
return true;
}
return false;
}
template<class A> A INCR(A & var){
return ++var;
}
template<class A, class B> A PLUS(A & var, const B & val){
return (var += val);
}
#else
//#define CAS(var, old, new) __sync_bool_compare_and_swap(&(var), old, new)
//#define INCR(var) __sync_add_and_fetch(&(var), 1)
//#define PLUS(var, val) __sync_add_and_fetch(&(var), val)
template<class A, class B, class C> bool CAS(A & var, const B & old, const C & newval){
return __sync_bool_compare_and_swap(& var, old, newval);
}
template<class A> A INCR(A & var){
return __sync_add_and_fetch(& var, 1);
}
template<class A, class B> A PLUS(A & var, const B & val){
return __sync_add_and_fetch(& var, val);
}
#endif
class Thread {
pthread_t thread;
bool destruct;
function<void()> func;
static void * runner(void * blah){
Thread * t = (Thread *)blah;
t->func();
return NULL;
}
static void nullfunc(){ assert(false && "How did Thread::nullfunc get called?"); }
public:
Thread() : destruct(false), func(nullfunc) { }
Thread(function<void()> fn) : destruct(false), func(nullfunc) { (*this)(fn); }
//act as a move constructor, no copy constructor
Thread(Thread & o) { *this = o; }
Thread & operator = (Thread & o) {
assert(destruct == false); //don't overwrite a thread
thread = o.thread;
destruct = o.destruct;
func = o.func;
//set the other object to no longer own the thread
o.thread = pthread_t();
o.destruct = false;
o.func = nullfunc;
return *this;
}
int operator()(function<void()> fn){
assert(destruct == false);
func = fn;
destruct = true;
return pthread_create(&thread, NULL, (void* (*)(void*)) &Thread::runner, this);
}
int detach(){ assert(destruct == true); return pthread_detach(thread); }
int join() { assert(destruct == true); destruct = false; return pthread_join(thread, NULL); }
int cancel(){ assert(destruct == true); return pthread_cancel(thread); }
~Thread(){
if(destruct){
cancel();
join();
}
}
};
class SpinLock {
int taken;
public:
SpinLock() : taken(0) { }
void lock() { while(!trylock()) ; }
#ifdef SINGLE_THREAD
bool trylock(){ return true; }
void unlock() { };
#else
bool trylock(){ return !__sync_lock_test_and_set(&taken, 1); }
void unlock() { __sync_lock_release(&taken); }
#endif
};
//object wrapper around pthread rwlock
class RWLock {
pthread_rwlock_t rwlock;
public:
RWLock(bool preferwriter = true){
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
#ifdef PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
if(preferwriter)
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
#endif
pthread_rwlock_init(&rwlock, &attr);
pthread_rwlockattr_destroy(&attr);
}
~RWLock(){ pthread_rwlock_destroy(&rwlock); }
int rdlock() { return pthread_rwlock_rdlock(&rwlock); }
int tryrdlock() { return pthread_rwlock_tryrdlock(&rwlock); }
int wrlock() { return pthread_rwlock_wrlock(&rwlock); }
int trywrlock() { return pthread_rwlock_trywrlock(&rwlock); }
int unlock() { return pthread_rwlock_unlock(&rwlock); }
};
class Mutex {
protected:
pthread_mutex_t mutex;
public:
Mutex() { pthread_mutex_init(&mutex, NULL); }
~Mutex() { pthread_mutex_destroy(&mutex); }
int lock() { return pthread_mutex_lock(&mutex); }
int trylock() { return pthread_mutex_trylock(&mutex); }
int unlock() { return pthread_mutex_unlock(&mutex); }
};
class CondVar : public Mutex {
protected:
pthread_cond_t cond;
public:
CondVar() { Mutex(); pthread_cond_init(&cond, NULL);}
~CondVar(){ pthread_cond_destroy(&cond); }
int broadcast(){ return pthread_cond_broadcast(&cond); }
int signal() { return pthread_cond_signal(&cond); }
int wait(){ return pthread_cond_wait(&cond, &mutex); }
int timedwait(double timeout){
timespec t;
t.tv_sec = (int)timeout;
t.tv_nsec = (long int)((timeout - (int)timeout) * 1000000000);
return pthread_cond_timedwait(&cond, &mutex, &t);
}
};
//*
class Barrier {
static const uint32_t waitexit = (1<<30);
CondVar cond;
volatile uint32_t numthreads, //number of threads to wait for
counter; //number of threads currently waiting
//not copyable
Barrier(const Barrier & b) { }
Barrier operator=(const Barrier & b);
public:
Barrier(int n = 1) : numthreads(n), counter(0) { }
void reset(int n){
cond.lock();
assert(counter == 0);
numthreads = n;
cond.unlock();
}
~Barrier(){
cond.lock();
//wait for threads to exit
while(counter > waitexit)
cond.wait();
cond.unlock();
}
bool wait(){
cond.lock();
//wait for threads to exit
while(counter > waitexit)
cond.wait();
bool flip = (++counter == numthreads);
if(flip){
counter--;
if(numthreads > 1)
counter += waitexit;
cond.broadcast();
}else{
while(counter < waitexit)
cond.wait();
counter--;
if(counter == waitexit){
counter = 0;
cond.broadcast();
}
}
cond.unlock();
return flip;
}
};
/*/
//use the pthread Barrier implementation
//the destructor below seems to have a deadlock that I can't explain but that doesn't affect the custom version above
class Barrier {
pthread_barrier_t barrier;
//not copyable
Barrier(const Barrier & b) { }
Barrier operator=(const Barrier & b);
public:
Barrier(int n = 1) { pthread_barrier_init(&barrier, NULL, n); }
~Barrier() { pthread_barrier_destroy(&barrier); }
void reset(int n){
pthread_barrier_destroy(&barrier);
pthread_barrier_init(&barrier, NULL, n);
}
bool wait(){
return !(pthread_barrier_wait(&barrier) == 0);
}
};
//*/