forked from mlkazar/lwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.h
More file actions
543 lines (428 loc) · 15.5 KB
/
thread.h
File metadata and controls
543 lines (428 loc) · 15.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
Copyright 2016-2020 Cazamar Systems
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __THREAD_H_ENV__
#define __THREAD_H_ENV__ 1
#include <unistd.h>
#include <stdlib.h>
#include <ucontext.h>
#include <pthread.h>
#include <string>
#include <atomic>
#include "dqueue.h"
#include "osp.h"
/* does a pointer fit in an integer? */
#if defined(__arm__)
#define THREAD_PTR_FITS_IN_INT 1
#define SETCONTEXT(x) xsetcontext(x)
#define GETCONTEXT(x) xgetcontext(x)
#elif defined(__x86_64__)
#define THREAD_PTR_FITS_IN_INT 0
#define SETCONTEXT(x) xsetcontext(x)
#define GETCONTEXT(x) xgetcontext(x)
#endif
class Thread;
class ThreadEntry;
class ThreadDispatcher;
class ThreadMutex;
#include "spinlock.h"
static __inline uint64_t
threadCpuTicks()
{
uint32_t lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return (uint64_t)hi << 32 | lo;
}
class ThreadMon {
public:
typedef void checkProc(void *contextp);
checkProc *_procp;
void *_contextp;
static void init(checkProc *procp, void *contextp) {
_monp->_procp = procp;
_monp->_contextp = contextp;
}
static void check() {
if (_monp && _monp->_procp)
_monp->_procp(_monp->_contextp);
}
ThreadMon() {
_procp = NULL;
_contextp = NULL;
_monp = this;
}
static ThreadMon *_monp;
};
/* use to allow construction of multiple lists of threads */
class ThreadEntry {
public:
ThreadEntry *_dqNextp;
ThreadEntry *_dqPrevp;
Thread *_threadp; /* back ptr */
ThreadEntry() {
_dqNextp = _dqPrevp = NULL;
_threadp = NULL;
}
};
/* one of these per user thread. A thread can only exist in one spot in any collection
* of run queues, unlike Avere Tasks.
*/
class Thread {
friend class ThreadDispatcher;
friend class ThreadMutex;
friend class ThreadMutexDetect;
public:
typedef void (TraceProc)( uint64_t mask,
const char *strp,
uint64_t p0,
uint64_t p1,
uint64_t p2,
uint64_t p3,
uint64_t p4,
uint64_t p5);
typedef void (InitProc) (void *contextp, Thread *threadp);
/* a list of all threads in existence, and a spin lock that
* protects the _allThreads and joinThreads lists, along with the
* joinThreadp pointer.
*/
static dqueue<ThreadEntry> _allThreads;
static dqueue<ThreadEntry> _joinThreads;
static SpinLock _globalThreadLock;
static uint32_t _defaultStackSize;
static int _trackStackUsage;
static TraceProc *_traceProcp; /* someone will init for us */
static void traceProc( uint64_t mask,
const char *p,
uint64_t p0,
uint64_t p1,
uint64_t p2,
uint64_t p3,
uint64_t p4,
uint64_t p5) {
if (Thread::_traceProcp)
Thread::_traceProcp(mask, p, p0, p1, p2, p3, p4, p5);
}
/* for when thread is blocked, or when it is in a run queue, these
* pointers are used.
*/
Thread *_dqNextp;
Thread *_dqPrevp;
/* so we have a list of all threads that exist, so gdb can find them all */
ThreadEntry _allEntry;
/* the context used for stack switching; keep registers and PC when a user thread
* isn't running.
*/
ucontext_t _ctx;
/* EVERYTHING BEFORE THIS POINT IS TRACKED IN GDB, i.e. there's a structure in gdb
* labeled kazar_thread that matches the earlier parts of this structure,
* so that gdb can read the thread and setup a copy of the registers for
* debugging.
*/
std::string _name;
/* timespec for when the thread was created */
struct timespec _createTs;
/* total run time in ticks for this thread */
uint64_t _runTicks;
/* last time this thread was started, for computing run time at next blocking */
uint64_t _lastStartTicks;
/* When we're blocked, the lock clock is space available for the
* locking package to make use of to ensure fairness, by tracking
* how long a thread has been waiting for a lock/resource.
*/
uint32_t _lockClock;
/* list of threads waiting for join */
ThreadEntry _joinEntry;
/* the mutex that we're blocked on, or null if not blocked on a mutex */
ThreadMutex *_blockingMutexp;
/* context used for deadlock detector */
uint32_t _marked;
/* context available for use by task while it is asleep, so anyone waking us up
* knows why we slept. Lets our read/write locks indicate how to wake them up,
*/
uint64_t _sleepContext;
/* set to the current dispatcher when a thread is loaded onto a processor */
ThreadDispatcher *_currentDispatcherp; /* current dispatcher for running thread */
/* certain threads are really pthreads. They only run on a dispatcher that
* runs if the thread sleeps, and the only thread that the dispatcher will
* ever see in its run queue is this thread. These special threads
* have _wiredDispatcherp set to the dispatcher in question, and they
* override their thread's queue function to always put the thread
* in the wiredDispatcher's runQueue. That dispatcher isn't in allDispatchers,
* so normal round robin threads never get queued to it.
*/
ThreadDispatcher *_wiredDispatcherp;
/* pointer to base of stack, and count */
uint32_t _stackSize;
char *_stackp;
private:
/* used by getcontext to differentiate between when the dispatcher calls it to
* store the context, and when the thread is re-woken when the dispatcher reloads
* the context.
*
* Todo: we can get rid of this flag by having getcontext storing
* a return value of 1 in the saved registers, so that a call to
* getcontext that returns 0 means the old context was saved, and
* we should switch the dispatcher to the idle thread to find more
* work, and a return value of 1 would then mean simply return to
* the caller, since the thread was being restarted by the
* dispatcher. In the ARM code at least, it is a one character
* change, but we should wait until we can do the 64 bit x86
* version at the same time.
*/
int _goingToSleep;
/* flag set if exited thread should hang around until joined */
uint8_t _joinable;
/* flag set if we're in the joinThreads queue */
uint8_t _inJoinThreads;
/* non-null if _joiningThreadp called join on us, and we weren't ready; protected
* by globalThreadLock.
*/
Thread *_joiningThreadp;
void *_exitValuep;
uint8_t _exited;
/* Internal C function called by the first activation of a thread
* by makecontext. Note that its signature is defined by the C
* library, and we may have to split a context pointer across two
* integers to get it to fit into ctxStart. This function calls
* the thread's virtual start method.
*/
static void ctxStart(unsigned int p1, unsigned int p2);
public:
Thread(std::string name, uint32_t stackSize=0) {
init(name, stackSize);
}
Thread(uint32_t stackSize=0) {
init("[None]", stackSize);
}
virtual ~Thread();
static void setTraceProc(TraceProc *procp) {
_traceProcp = procp;
}
/* set the flag; once set, the thread can exit, but its state won't get
* freed until the thread is joined.
*/
void setJoinable() {
_joinable = 1;
}
/* this is the main entry point to a thread. The definer of a thread specifies this
* when creating a thread, and it will start here the first time the thread
* is queued.
*/
virtual void *start() = 0;
/* this function is used by primitives to put a thread to sleep.
* The idea is that if you have a task list protected by a spin
* lock, you can hold that spin lock, remove the task from that
* list, and call sleep. Sleep will put your task to sleep and
* then drop the lock.
*
* The goal of this interface is to prevent the race where a
* thread releases a spinlock, making its thread ID visible, and
* then goes to sleep. In the gap between the time the thread is
* visible as sleeping, and the time it actually sleeps, it might
* get queued again (even before its current state is saved),
* causing all sorts of mayhem.
*/
void sleep(SpinLock *lockp);
/* queued to start a task that's been put to sleep, or freshly
* constructed. Can be overridden to splice in a queue that uses
* a dedicated dispatcher.
*/
virtual void queue();
static Thread *getCurrent();
static uint32_t getDefaultStackSize() {
return _defaultStackSize;
}
void exit(void *exitCodep);
void setName(std::string name) {
_name = name;
}
int32_t join(void **ptrpp);
/* provide a way for someone to add reference counts and intercept our
* deletion of the thread. Note that Thread::exit will call this
* on a different thread than the exiting thread (so that we don't free
* the stack we're actively using).
*
* Anyone else who calls this must do so from a different thread than
* the one being released.
*/
virtual void releaseThread() {
assert(Thread::getCurrent() != this);
delete this;
}
virtual void holdThread() {
assert(0 == "must overload hold to use it");
}
static void setTrackStackUsage(int trackStackUsage = 1) {
_trackStackUsage = trackStackUsage;
}
static void displayStackUsage();
private:
/* internal function used in constructing a task */
void init(std::string name, uint32_t stackSize);
void resume();
};
/* this thread provides a context for running the dispatcher, so that when a thread
* blocks, we can run the dispatcher without staying on the same stack.
*/
class ThreadIdle : public Thread {
public:
SpinLock *_userLockToReleasep;
ThreadDispatcher *_disp;
void *start();
ThreadIdle() : Thread("Idle thread") {
_userLockToReleasep = NULL;
}
void setLock(SpinLock *lockp) {
_userLockToReleasep = lockp;
}
SpinLock *getLockAndClear() {
SpinLock *lockp = _userLockToReleasep;
_userLockToReleasep = NULL;
return lockp;
}
};
class ThreadMain : public Thread {
void *start() {
thread_assert(0);
}
public:
/* overridden to place the thread back in the wired dispatcher's run queue */
void queue();
ThreadMain(std::string name) : Thread(name) {
return;
}
};
/* the items in the helper queue are protected by the globalThreadLock */
class ThreadHelper : public Thread {
class ThreadHelperItem {
public:
ThreadHelperItem *_dqNextp;
ThreadHelperItem *_dqPrevp;
Thread *_threadToFreep;
Thread *_threadToQueuep;
ThreadHelperItem() {
_threadToFreep = NULL;
_threadToQueuep = NULL;
_dqNextp = NULL;
_dqPrevp = NULL;
}
};
public:
dqueue<ThreadHelperItem> _items;
uint8_t _running;
ThreadHelper() : Thread ("Thread helper") {
_running = 0;
}
void *start();
/* must be called with globalThreadLock held */
void queueItem(Thread *toQueuep, Thread *toFreep) {
int doStart;
ThreadHelperItem *itemp = new ThreadHelperItem();
itemp->_threadToQueuep = toQueuep;
itemp->_threadToFreep = toFreep;
_items.append(itemp);
if (_running)
doStart = 0;
else {
_running = 1;
doStart = 1;
}
if (doStart) {
queue();
}
}
};
class ThreadDispatcherQueue {
friend class ThreadDispatcher;
friend class Thread;
dqueue<Thread> _queue;
SpinLock _queueLock;
};
class ThreadDispatcher {
friend class Thread;
friend class ThreadDispatcherQueue;
public:
static const long _maxDispatchers=8;
private:
static pthread_once_t _once;
static pthread_key_t _dispatcherKey;
static ThreadDispatcher *_allDispatchers[_maxDispatchers];
static uint16_t _dispatcherCount;
/* queue of pending locks */
ThreadDispatcherQueue _runQueue;
/* manage the pause system; you can call dispatcher pause and then
* perform operations that require all scheduling to stop, like deadlock
* detection. The call resume. More than one pthread can do this at once,
* since _pauseRequests is an integer. This stuff is all protected by the
* runMutex below.
*/
uint32_t _pauseRequests;
uint8_t _paused;
pthread_cond_t _pauseCV;
Thread *_currentThreadp;
int _sleeping;
pthread_cond_t _runCV;
pthread_mutex_t _runMutex;
uint64_t _lastDispatchTicks;
/* other config */
static uint32_t _spinTicks;
/* an idle thread that provides a thread with a stack on which we can run
* the dispatcher.
*/
ThreadIdle _idle;
ThreadHelper _helper;
static void globalInit();
static ThreadDispatcher *currentDispatcher();
static void *dispatcherTop(void *ctx);
public:
/* called to put thread to sleep on current dispatcher, and then dispatch
* more threads.
*/
void sleep(Thread *threadp, SpinLock *lockp);
/* queue this thread on this dispatcher */
void queueThread(Thread *threadp);
/* called to look for work in the run queue, or wait until some shows up */
void dispatch();
/* called to create a bunch of dispatchers and their pthreads */
static void setup(uint16_t ndispatchers, int32_t spinUsec = -1);
int isSleeping() {
int isSleeping;
pthread_mutex_lock(&_runMutex);
isSleeping = _sleeping;
pthread_mutex_unlock(&_runMutex);
return isSleeping;
}
ThreadDispatcher(int special=0);
virtual ~ThreadDispatcher();
void pauseDispatching();
void resumeDispatching();
static uint32_t getCpuCount();
static void pauseAllDispatching();
static void resumeAllDispatching();
static int pausedAllDispatching();
static void pthreadTop(const char *namep = 0);
static bool isLwt();
};
/* lollipop comparison */
int threadClockCmp(uint32_t a, uint32_t b);
/* max # of read locks before we have to grant a write lock */
static const uint32_t threadClockReadWindow = 12;
/* max # of write or exclusive locks before we grant a conflicting lock */
static const uint32_t threadClockWriteWindow = 2;
#endif /* __THREAD_H_ENV__ */