-
Notifications
You must be signed in to change notification settings - Fork 4
/
xthread.hh
279 lines (234 loc) · 7.65 KB
/
xthread.hh
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
273
274
275
276
277
278
279
/*
* FreeGuard: A Faster Secure Heap Allocator
* Copyright (C) 2017 Sam Silvestro, Hongyu Liu, Corey Crosser,
* Zhiqiang Lin, and Tongping Liu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @file xthread.hh: thread-related functions implementation.
* @author Tongping Liu <http://www.cs.utsa.edu/~tongpingliu/>
* @author Sam Silvestro <[email protected]>
*/
#ifndef __XTHREAD_HH__
#define __XTHREAD_HH__
#include <unistd.h>
#include <new>
#include <sys/syscall.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include "hashmap.hh"
#include "hashfuncs.hh"
#include "hashheapallocator.hh"
#include "log.hh"
#include "real.hh"
#include "threadstruct.hh"
#include "xdefines.hh"
#ifdef SSE2RNG
#include "sse2rng.h"
#endif
#ifdef CUSTOMIZED_STACK
extern intptr_t globalStackAddr;
#endif
class xthread {
public:
static xthread& getInstance() {
static char buf[sizeof(xthread)];
static xthread* xthreadObject = new (buf) xthread();
return *xthreadObject;
}
void initialize() {
_aliveThreads = 0;
_threadIndex = 0;
// Initialize the spin_lock
pthread_spin_init(&_spin_lock, PTHREAD_PROCESS_PRIVATE);
thread_t * thread;
// Shared the threads information.
memset(&_threads, 0, sizeof(_threads));
// Initialize all
for(int i = 0; i < MAX_ALIVE_THREADS; i++) {
thread = &_threads[i];
// Those information that are only initialized once.
thread->available = true;
thread->index = i;
}
// Now we will intialize the initial thread
initializeInitialThread();
_xmap.initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, THREAD_MAP_SIZE);
_xmap.insert((void*)pthread_self(), sizeof(void*), 0);
}
void initializeInitialThread(void) {
int tindex = allocThreadIndex();
assert(tindex == 0);
thread_t * thread = getThread(tindex);
// Initial myself, like threadIndex, tid
initializeCurrentThread(thread);
}
// This function is only called in the current thread before the real thread function
void initializeCurrentThread(thread_t * thread) {
SRAND(time(NULL));
thread->tid = syscall(__NR_gettid);
#ifndef CUSTOMIZED_STACK
setThreadIndex(thread->index);
#endif
// Adding the thread's pthreadt.
thread->pthreadt = pthread_self();
}
void reinitialize() {
_aliveThreads = 1;
_threadIndex = 0;
// Reinitialize the spin_lock
pthread_spin_init(&_spin_lock, PTHREAD_PROCESS_PRIVATE);
thread_t * thread;
int currentThreadIndex = getThreadIndex(&thread);
// Initialize all
for(int i = 0; i < MAX_ALIVE_THREADS; i++) {
thread = &_threads[i];
// Reinitialize the current living thread
// (the initiator of the fork)
if(i == currentThreadIndex) {
initializeCurrentThread(thread);
continue;
}
// Those information that are only initialized once.
thread->available = true;
thread->index = i;
}
_xmap.initialize(HashFuncs::hashAddr, HashFuncs::compareAddr, THREAD_MAP_SIZE);
_xmap.insert((void*)pthread_self(), sizeof(void*), currentThreadIndex);
}
/// @ internal function: allocation a thread index when spawning.
/// Since we guarantee that only one thread can be in spawning phase,
/// there is no need to acqurie the lock in this function.
int allocThreadIndex() {
int index = -1;
if(_aliveThreads == MAX_ALIVE_THREADS) {
FATAL("maximum concurrent thread limit reached (%d)", MAX_ALIVE_THREADS);
}
thread_t* thread;
if(_aliveThreads++ == _threadIndex) {
index = _threadIndex++;
thread = getThread(index);
thread->available = false;
} else {
for(int i = 0; i <= _threadIndex; i++) {
thread = getThread(i);
if(thread->available) {
thread->available = false;
index = i;
break;
}
}
}
return index;
}
inline thread_t* getThread(int index) { return &_threads[index]; }
#ifndef CUSTOMIZED_STACK
inline thread_t* getThread() { return &_threads[getThreadIndex()]; }
#endif
int thread_create(pthread_t * tid, const pthread_attr_t * attr, threadFunction * fn, void * arg) {
acquireGlobalLock();
int tindex = allocThreadIndex();
releaseGlobalLock();
// Acquire the thread structure.
thread_t* children = getThread(tindex);
children->startArg = arg;
children->startRoutine = fn;
children->index = tindex;
#ifdef CUSTOMIZED_STACK
pthread_attr_t iattr;
if(attr == NULL) {
pthread_attr_init(&iattr);
} else {
memcpy(&iattr, attr, sizeof(pthread_attr_t));
}
pthread_attr_setstack(&iattr, (void*)(globalStackAddr + (intptr_t)tindex * STACK_SIZE + GUARD_PAGE_SIZE), STACK_SIZE - 2 * GUARD_PAGE_SIZE);
/* Guard pages have already been setted before main()
if(0 != mprotect((void*)(childrenStackStart + STACK_SIZE - GUARD_PAGE_SIZE), GUARD_PAGE_SIZE, PROT_NONE)
|| 0 != mprotect((void*)childrenStackStart, GUARD_PAGE_SIZE, PROT_NONE)) {
fprintf(stderr, "Failed to set guard page for thread#%d\n", tindex);
abort();
}
*/
int result = Real::pthread_create(tid, &iattr, xthread::startThread, (void *)children);
#else
int result = Real::pthread_create(tid, attr, xthread::startThread, (void *)children);
#endif
if(result) {
FATAL("thread_create failure");
}
// Setting up this in the main thread so that
// pthread_join can always find its pthread_t.
// Otherwise, it can fail because the child have set this value.
children->pthreadt = *tid;
acquireGlobalLock();
_xmap.insertIfAbsent((void*)*tid, sizeof(void*), tindex);
releaseGlobalLock();
return result;
}
static void * startThread(void * arg) {
thread_t * current = (thread_t *) arg;
xthread::getInstance().initializeCurrentThread(current);
// Actually run this thread using the function call
void * result = NULL;
try{
result = current->startRoutine(current->startArg);
}
catch (int err){
if(err != PTHREADEXIT_CODE){
throw err;
}
}
return result;
}
int thread_join(pthread_t tid, void ** retval) {
int joinretval;
if((joinretval = Real::pthread_join(tid, retval)) == 0) {
int joinee = -1;
acquireGlobalLock();
if(!_xmap.find((void *)tid, sizeof(void *), &joinee)) {
PRERR("Cannot find joinee index for thread %p", (void *)tid);
} else {
_threads[joinee].available = true;
}
_aliveThreads--;
releaseGlobalLock();
}
return joinretval;
}
void acquireGlobalLock() {
spin_lock();
}
void releaseGlobalLock() {
spin_unlock();
}
private:
inline void spin_lock() {
pthread_spin_lock(&_spin_lock);
}
inline void spin_unlock() {
pthread_spin_unlock(&_spin_lock);
}
pthread_spinlock_t _spin_lock;
// The next available thread index for use by a new thread.
int _threadIndex;
typedef HashMap<void *, int, HeapAllocator> threadHashMap;
threadHashMap _xmap; // The hash map that map the address of pthread_t to thread index.
// Use _threadIndex as the total possible number of alive threads at
// any given time during execution.
int _aliveThreads;
thread_t _threads[MAX_ALIVE_THREADS];
};
#endif