-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.c
320 lines (264 loc) · 7.33 KB
/
threads.c
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
#include "ec440threads.h"
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef PREEMPT
#define PREEMPT 1 /* flag to enable preemption */
#endif
#define MAX_THREADS 128 /* number of threads you support */
#define THREAD_STACK_SIZE (1 << 15) /* size of stack in bytes */
#define QUANTUM (50 * 1000) /* quantum in usec */
typedef struct {
int flag;
} mutex_t;
// usr/include/x86_64-linux-gnu/bits: struct_mutex.h, pthreadtypes.h,
// pthreadtypes-arch.h
typedef union {
pthread_mutex_t mutex;
mutex_t my_mutex;
} my_mutex_t;
typedef struct {
int *waitlist;
unsigned limit;
unsigned count;
} barrier_t;
typedef union {
pthread_barrier_t barrier;
barrier_t my_barrier;
} my_barrier_t;
enum thread_status { TS_EXITED, TS_READY, TS_RUNNING, TS_BLOCKED };
typedef struct thread_control_block {
pthread_t id;
jmp_buf registers;
void *stack;
enum thread_status status;
void *ret_val;
bool has_mutex;
} TCB;
TCB threads[MAX_THREADS];
int current_thread = 0;
int num_threads = 0;
static void scheduler_init();
static void init_handler();
static void lock();
static void unlock();
static TCB *get_new_thread();
static void thread_init(TCB *new_thread);
static void reg_init(TCB *new_thread, void *(*start_routine)(void *),
void *arg);
static void add_thread_to_waitlist(int thread_index, int *waitlist);
static void clear_waitlist(int *waitlist);
void scheduler_init() {
assert(num_threads == 0);
TCB *main_thread = get_new_thread();
thread_init(main_thread);
assert(num_threads == 1);
main_thread->status = TS_RUNNING;
if (PREEMPT)
init_handler();
}
void init_handler() {
struct sigaction sa = {
.sa_handler = schedule,
.sa_flags = SA_NODEFER,
};
sigaction(SIGALRM, &sa, NULL);
ualarm(QUANTUM, QUANTUM);
}
void lock() {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);
}
void unlock() {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
assert(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);
}
TCB *get_new_thread() {
static long i = 0;
int seen = 0;
while (threads[i].status != TS_EXITED || threads[i].stack) {
if (++seen >= MAX_THREADS)
exit(EXIT_FAILURE);
i = (i + 1) % MAX_THREADS;
}
threads[i].id = (pthread_t)i;
return &threads[i];
}
void thread_init(TCB *new_thread) {
new_thread->stack = malloc(THREAD_STACK_SIZE);
assert(new_thread->stack);
new_thread->has_mutex = false;
num_threads++;
}
void reg_init(TCB *new_thread, void *(*start_routine)(void *), void *arg) {
if (setjmp(new_thread->registers))
return;
set_reg(&new_thread->registers, JBL_PC, (unsigned long)start_thunk);
set_reg(&new_thread->registers, JBL_R12, (unsigned long)start_routine);
set_reg(&new_thread->registers, JBL_R13, (unsigned long)arg);
unsigned long *sp = new_thread->stack + THREAD_STACK_SIZE;
set_reg(&new_thread->registers, JBL_RSP, (unsigned long)--sp);
*sp = (unsigned long)pthread_exit;
}
void schedule(int signal) {
if (threads[current_thread].has_mutex)
return;
if (threads[current_thread].status != TS_EXITED) {
if (setjmp(threads[current_thread].registers))
return;
}
if (threads[current_thread].status == TS_RUNNING)
threads[current_thread].status = TS_READY;
int i = current_thread;
do {
i = (i + 1) % MAX_THREADS;
if (i == current_thread)
return;
} while (threads[i].status != TS_READY);
current_thread = i;
threads[current_thread].status = TS_RUNNING;
longjmp(threads[current_thread].registers,
(long)threads[current_thread].id + 1);
}
void add_thread_to_waitlist(int thread_index, int *waitlist) {
assert(waitlist);
for (int i = 0; i < MAX_THREADS; i++) {
if (waitlist[i] == thread_index)
return;
if (waitlist[i] == -1) {
waitlist[i] = thread_index;
return;
}
}
}
void clear_waitlist(int *waitlist) {
assert(waitlist);
for (int i = 0; i < MAX_THREADS; i++) {
if (waitlist[i] == -1)
return;
threads[waitlist[i]].status = TS_READY;
waitlist[i] = -1;
}
}
// Library functions
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
// Create the timer and handler for the scheduler. Create thread 0.
static bool is_first_call = true;
if (is_first_call) {
is_first_call = false;
scheduler_init();
}
if (num_threads >= MAX_THREADS)
exit(EXIT_FAILURE);
TCB *new_thread = get_new_thread();
*thread = (pthread_t)new_thread->id;
thread_init(new_thread);
reg_init(new_thread, start_routine, arg);
new_thread->status = TS_READY;
return 0;
}
void pthread_exit(void *value_ptr) {
threads[current_thread].ret_val = value_ptr;
threads[current_thread].status = TS_EXITED;
schedule(0);
exit(EXIT_SUCCESS);
}
pthread_t pthread_self(void) { return (pthread_t)threads[current_thread].id; }
int pthread_join(pthread_t thread, void **retval) {
if (retval == NULL)
return -1;
int id = (long)thread;
while (threads[id].status != TS_EXITED)
schedule(0);
*retval = threads[id].ret_val;
memset(threads[id].registers, 0, sizeof(jmp_buf));
free(threads[id].stack);
threads[id].stack = NULL;
threads[id].ret_val = NULL;
num_threads--;
return 0;
}
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr) {
my_mutex_t *m = (my_mutex_t *)mutex;
m->my_mutex.flag = 0;
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex) {
my_mutex_t *m = (my_mutex_t *)mutex;
m->my_mutex.flag = 0;
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex) {
lock();
my_mutex_t *m = (my_mutex_t *)mutex;
while (m->my_mutex.flag) {
unlock();
schedule(0);
lock();
}
m->my_mutex.flag = 1;
threads[current_thread].has_mutex = true;
unlock();
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
lock();
my_mutex_t *m = (my_mutex_t *)mutex;
m->my_mutex.flag = 0;
threads[current_thread].has_mutex = false;
unlock();
return 0;
}
int pthread_barrier_init(pthread_barrier_t *restrict barrier,
const pthread_barrierattr_t *attr, unsigned count) {
lock();
if (count == 0) {
unlock();
return EINVAL;
}
my_barrier_t *b = (my_barrier_t *)barrier;
b->my_barrier.limit = count;
b->my_barrier.count = 0;
b->my_barrier.waitlist = calloc(MAX_THREADS, sizeof(int));
assert(b->my_barrier.waitlist);
for (int i = 0; i < MAX_THREADS; i++)
b->my_barrier.waitlist[i] = -1;
unlock();
return 0;
}
int pthread_barrier_destroy(pthread_barrier_t *barrier) {
lock();
my_barrier_t *b = (my_barrier_t *)barrier;
clear_waitlist(b->my_barrier.waitlist);
free(b->my_barrier.waitlist);
memset(&b->my_barrier, 0, sizeof(my_barrier_t));
unlock();
return 0;
}
int pthread_barrier_wait(pthread_barrier_t *barrier) {
lock();
my_barrier_t *b = (my_barrier_t *)barrier;
b->my_barrier.count++;
if (b->my_barrier.count >= b->my_barrier.limit) {
b->my_barrier.count = 0;
clear_waitlist(b->my_barrier.waitlist);
unlock();
return PTHREAD_BARRIER_SERIAL_THREAD;
} else {
threads[current_thread].status = TS_BLOCKED;
add_thread_to_waitlist(current_thread, b->my_barrier.waitlist);
unlock();
schedule(0);
return 0;
}
}