forked from xxlelementlxx/Thread-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cc
More file actions
285 lines (247 loc) · 6.73 KB
/
thread.cc
File metadata and controls
285 lines (247 loc) · 6.73 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
#include <cstdlib>
#include <ucontext.h>
#include <deque>
#include <iterator>
#include <map>
#include <iostream>
#include "interrupt.h"
#include "thread.h"
using namespace std;
struct Thread {
unsigned int id;
char* stack;
ucontext_t* ucontext_ptr;
bool finished;
};
struct Lock {
Thread* owner;
deque<Thread*>* blocked_threads;
};
typedef map<unsigned int, deque<Thread*>*>::const_iterator condition_iterator;
typedef map<unsigned int, Lock*>::const_iterator lock_iterator;
typedef void (*thread_startfunc_t)(void *);
static Thread* current;
static ucontext_t* scheduler;
static deque<Thread*> ready;
static map<unsigned int, deque<Thread*>*> condition_map;
static map<unsigned int, Lock*> lock_map;
static bool init = false;
static int id = 0;
void delete_current_thread() {
delete current->stack;
current->ucontext_ptr->uc_stack.ss_sp = NULL;
current->ucontext_ptr->uc_stack.ss_size = 0;
current->ucontext_ptr->uc_stack.ss_flags = 0;
current->ucontext_ptr->uc_link = NULL;
delete current->ucontext_ptr;
delete current;
current = NULL;
}
int thread_libinit(thread_startfunc_t func, void *arg) {
if (init) return -1;
init = true;
if (thread_create(func, arg) < 0)
return -1;
Thread* first = ready.front();
ready.pop_front();
current = first;
try {
scheduler = new ucontext_t;
} catch (std::bad_alloc b) {
delete scheduler;
return -1;
}
getcontext(scheduler);
//switch to current thread
interrupt_disable();
swapcontext(scheduler, first->ucontext_ptr);
while (ready.size() > 0) {
if (current->finished == true)
delete_current_thread();
Thread* next = ready.front();
ready.pop_front();
current = next;
swapcontext(scheduler, current->ucontext_ptr);
}
if (current != NULL) {
delete_current_thread();
}
//When there are no runnable threads in the system (e.g. all threads have
//finished, or all threads are deadlocked)
cout << "Thread library exiting.\n";
exit(0);
}
static int exec_func(thread_startfunc_t func, void *arg) {
interrupt_enable();
func(arg);
interrupt_disable();
current->finished = true;
swapcontext(current->ucontext_ptr, scheduler);
return 0;
}
int thread_create(thread_startfunc_t func, void *arg) {
if (!init) return -1;
interrupt_disable();
Thread* t;
try {
t = new Thread;
t->ucontext_ptr = new ucontext_t;
getcontext(t->ucontext_ptr);
t->stack = new char [STACK_SIZE];
t->ucontext_ptr->uc_stack.ss_sp = t->stack;
t->ucontext_ptr->uc_stack.ss_size = STACK_SIZE;
t->ucontext_ptr->uc_stack.ss_flags = 0;
t->ucontext_ptr->uc_link = NULL;
makecontext(t->ucontext_ptr, (void (*)())exec_func, 2, func, arg);
t->id = id;
id++;
t->finished = false;
ready.push_back(t);
}
catch (std::bad_alloc b) {
delete t->ucontext_ptr;
delete t->stack;
delete t;
interrupt_enable();
return -1;
}
interrupt_enable();
return 0;
}
int thread_yield(void) {
if (!init) return -1;
interrupt_disable();
ready.push_back(current);
swapcontext(current->ucontext_ptr, scheduler);
interrupt_enable();
return 0;
}
int thread_lock(unsigned int lock) {
if (!init) return -1;
interrupt_disable();
lock_iterator lock_iter = lock_map.find(lock);
Lock* l;
if (lock_iter == lock_map.end()) { //New Lock
try {
l = new Lock;
l->owner = current;
l->blocked_threads = new deque<Thread*>;
}
catch (std::bad_alloc b) {
delete l->blocked_threads;
delete l;
interrupt_enable();
return -1;
}
lock_map.insert(make_pair(lock, l));
}
else { //Lock Found
l = (*lock_iter).second;
if (l->owner == NULL) //Lock is free, get it
l->owner = current;
else {
if (l->owner->id == current->id) {
//Calling lock when holding it = error
interrupt_enable();
return -1;
}
else { //Lock is owned by another Thread, get it line
l->blocked_threads->push_back(current);
swapcontext(current->ucontext_ptr, scheduler);
}
}
}
interrupt_enable();
return 0;
}
int unlock_without_interrupts(unsigned int lock) {
lock_iterator lock_iter = lock_map.find(lock);
Lock* l;
if (lock_iter == lock_map.end()) // Lock Not Found, error
return -1;
else { //Lock Found
l = (*lock_iter).second;
if (l->owner == NULL) //Lock Free, error
return -1;
else { //Lock owned
if (l->owner->id == current->id) { //Current thread owns the lock
if (l->blocked_threads->size() > 0) {
l->owner = l->blocked_threads->front();
l->blocked_threads->pop_front();
ready.push_back(l->owner);
}
else
l->owner = NULL;
}
else //Not the owner, error
return -1;
}
}
return 0;
}
int thread_unlock(unsigned int lock) {
if (!init) return -1;
interrupt_disable();
int result = unlock_without_interrupts(lock);
interrupt_enable();
return result;
}
int thread_wait(unsigned int lock, unsigned int cond) {
if (!init) return -1;
interrupt_disable();
if (unlock_without_interrupts(lock) == 0) {
condition_iterator cond_iter = condition_map.find(cond);
if (cond_iter == condition_map.end()) { //New Condition variable
deque<Thread*>* waiting_threads;
try {
waiting_threads = new deque<Thread*>;
}
catch (std::bad_alloc b) {
delete waiting_threads;
interrupt_enable();
return -1;
}
waiting_threads->push_back(current);
condition_map.insert(make_pair(cond, waiting_threads));
}
else //Found Condition variable
(*cond_iter).second->push_back(current);
swapcontext(current->ucontext_ptr, scheduler);
interrupt_enable();
return thread_lock(lock);
}
interrupt_enable();
return -1;
}
int thread_signal(unsigned int lock, unsigned int cond) {
if (!init) return -1;
interrupt_disable();
condition_iterator cond_iter = condition_map.find(cond);
if (cond_iter != condition_map.end()) { //Condition Found
deque<Thread*>* c = (*cond_iter).second;
if (!c->empty()) { //Threads waiting
Thread* t = c->front();
c->pop_front();
ready.push_back(t);
}
}
//Condition not found/not holding the lock => not an error
interrupt_enable();
return 0;
}
int thread_broadcast(unsigned int lock, unsigned int cond) {
if (!init) return -1;
interrupt_disable();
condition_iterator cond_iter = condition_map.find(cond);
if (cond_iter != condition_map.end()) { //Condition Found
deque<Thread*>* c = (*cond_iter).second;
while (c->size() > 0) { //Threads waiting
Thread* t = c->front();
c->pop_front();
ready.push_back(t);
}
}
//Condition not found/not holding the lock => not an error
interrupt_enable();
return 0;
}