-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxthread.h
230 lines (210 loc) · 4.74 KB
/
xthread.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
#ifndef _THREAD_H
#define _THREAD_H
#include "xthread_config.h"
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <alloca.h>
#include <cstddef>
#include <exception>
#if LONG_BIT == 32
#define LOG2_STACK_AREA_SIZE 18
#else
#define LOG2_STACK_AREA_SIZE 20
#endif
#define STACK_AREA_SIZE (1<<LOG2_STACK_AREA_SIZE)
#define STACK_AREA_MASK ~(STACK_AREA_SIZE-1)
#ifdef STACK_GROWS_UP
#define ThreadInitMainStack() \
do { \
int dummy[1]; \
alloca(STACK_AREA_SIZE-(((uintptr_t) dummy) & ~STACK_AREA_MASK)); \
ThreadGrowMainStack(); \
} while (0)
#else
#define ThreadInitMainStack() \
do { \
int dummy[1]; \
alloca(((uintptr_t) dummy) & ~STACK_AREA_MASK); \
ThreadGrowMainStack(); \
} while (0)
#endif
struct ThreadLocalData;
struct ThreadInfo;
std::size_t threadLocalDataSize();
std::size_t threadInfoSize();
extern std::size_t threadLocalDataSize_;
extern std::size_t threadInfoSize_;
class ThreadAction;
typedef long ThreadID;
class ThreadException : public std::exception {
private:
const char *message;
public:
virtual const char *what() const throw() {
return message;
}
ThreadException(const char *message_init) { message = message_init; }
};
void ThreadError(const char *message);
class Thread {
private:
void *stack_bottom;
std::size_t stack_size;
pthread_t descriptor;
ThreadID thread_num;
public:
Thread();
void Thread0(); // pseudo constructor for main thread
~Thread();
FORCE_INLINE ThreadID id() { return thread_num; }
void* operator new(std::size_t size);
void operator delete(void *memory);
void run(ThreadAction& body);
void run(void (*body)(Thread *));
void wait();
static FORCE_INLINE Thread* current() {
void *stack;
#ifdef __GNUC__
stack = __builtin_frame_address(0);
#else
{ int dummy[1]; stack = dummy; }
#endif
return reinterpret_cast<Thread *>(((uintptr_t) stack) & STACK_AREA_MASK);
}
FORCE_INLINE ThreadLocalData& memory() {
char *base = reinterpret_cast<char *>(this);
return *reinterpret_cast<ThreadLocalData*>(
base + sizeof(Thread)
);
}
FORCE_INLINE ThreadInfo& info() {
char *base = reinterpret_cast<char *>(this);
return *reinterpret_cast<ThreadInfo*>(
base + sizeof(Thread) + threadLocalDataSize_
);
}
static FORCE_INLINE ThreadLocalData& currentMemory() {
return current()->memory();
}
};
class ThreadAction {
public:
virtual void main(Thread *thread) = 0;
};
void ThreadGrowMainStack();
class ConditionVariable;
class Lock {
private:
pthread_mutex_t mutex;
friend class ConditionVariable;
#ifdef DEBUG_THREADS
Thread *owner;
#endif
public:
Lock() {
pthread_mutex_init(&mutex, NULL);
#ifdef DEBUG_THREADS
owner = NULL;
#endif
}
~Lock() {
pthread_mutex_destroy(&mutex);
}
void lock() {
#ifdef DEBUG_THREADS
if (owner == Thread::current())
ThreadError("locking mutex twice");
#endif
pthread_mutex_lock(&mutex);
#ifdef DEBUG_THREADS
owner = Thread::current();
#endif
}
void unlock() {
#ifdef DEBUG_THREADS
if (owner != Thread::current())
ThreadError("unlocking unowned lock");
owner = NULL;
#endif
pthread_mutex_unlock(&mutex);
}
};
class ReadWriteLock {
private:
pthread_rwlock_t rwlock;
public:
ReadWriteLock() {
pthread_rwlock_init(&rwlock, NULL);
}
~ReadWriteLock() {
pthread_rwlock_destroy(&rwlock);
}
void readLock() {
pthread_rwlock_rdlock(&rwlock);
}
void writeLock() {
pthread_rwlock_wrlock(&rwlock);
}
void unlock() {
pthread_rwlock_unlock(&rwlock);
}
};
class ConditionVariable {
private:
pthread_cond_t condition;
Lock *lock;
friend class Semaphore;
ConditionVariable() { }
public:
ConditionVariable(Lock *lock0) {
lock = lock0;
pthread_cond_init(&condition, NULL);
}
~ConditionVariable() {
pthread_cond_destroy(&condition);
}
void wait() {
#ifdef DEBUG_THREADS
if (lock->owner != Thread::current())
ThreadError("waited on condition without locked mutex");
lock->owner = NULL;
#endif
pthread_cond_wait(&condition, &lock->mutex);
#ifdef DEBUG_THREADS
lock->owner = Thread::current();
#endif
}
void signal() {
#ifdef DEBUG_THREADS
if (lock->owner != Thread::current())
ThreadError("signaled condition without locked mutex");
#endif
pthread_cond_signal(&condition);
}
void broadcast() {
#ifdef DEBUG_THREADS
if (lock->owner != Thread::current())
ThreadError("signaled condition without locked mutex");
#endif
pthread_cond_broadcast(&condition);
}
};
class Semaphore {
private:
Lock lock;
ConditionVariable condition;
unsigned count;
unsigned waiting;
public:
Semaphore() {
condition.lock = &lock;
count = 0;
}
Semaphore(unsigned count0) {
count = count0;
}
void wait();
void post();
};
#endif // _THREAD_H