-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtthreads.cpp
More file actions
352 lines (282 loc) · 7.66 KB
/
Copy pathgtthreads.cpp
File metadata and controls
352 lines (282 loc) · 7.66 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
#include "gtthreads.h"
#include <queue>
#include <list>
#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <ucontext.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define MAX_THREADS 100
#define ESRCH -1
using namespace std;
long quantum;
long total_threads = 0;
list<tcblk*> ready_queue;
list<tcblk*> wait_queue;
list<tcblk*> terminated_queue;
ucontext_t uctx_main;//, uctx_func1, uctx_func2;
ucontext_t wrapper_ctxt;
struct itimerval timer;
sigset_t set;
void internal_exit();
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
void exit_wrapper(void)
{cout << "!!!!!" << endl;
// swapcontext(&wrapper_ctxt, &uctx_main);
cout << "In the wrapper" << endl;
while(1)
{
cout << "Inside the wrapper" << endl;
internal_exit();
}
}
void sigalarm_handler(int sig)
{
cout << "Alarm called" << endl;
tcblk* head = ready_queue.front();
ucontext_t *curr, *next;
if(head->isMain)
curr = &uctx_main;
else
curr = &head->ctxt;
ready_queue.pop_front();
ready_queue.push_back(head);
if(ready_queue.front()->isMain)
{
cout << "Curr = " << head->tid << endl;
cout << "isMain true" << endl;
next = &uctx_main;
}
else
next = &ready_queue.front()->ctxt;
// alarm(2);
setitimer(ITIMER_REAL, &timer, NULL);
cout << "Head id = " << head->tid << endl;
cout << "Before swapping" << endl;
swapcontext(curr, next);//&ready_queue.front()->ctxt);
}
void gtthread_init(long period)
{
if (getcontext(&wrapper_ctxt) == -1)
handle_error("getcontext");
wrapper_ctxt.uc_stack.ss_sp = new char[16384];
wrapper_ctxt.uc_stack.ss_size = 16384;
wrapper_ctxt.uc_link = &uctx_main;
makecontext(&wrapper_ctxt, (void(*)(void))&exit_wrapper, 0);
// The set of signals to be blocked or unblocked
sigfillset(&set);
// Setup the signal handler structure
struct sigaction new_action;
new_action.sa_handler = sigalarm_handler;
new_action.sa_flags = 0;
// Block all the signals when the signal handler is running. This is to ensire that running signal unsafe functions in signals will not ciase any problems
sigfillset(&new_action.sa_mask);
// Setup the strucure to be used by the timer. Intialise it equal to the quantum
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = period/1000;
timer.it_value.tv_usec = (period%1000)*1000;
quantum = period;
tcblk *thread = new tcblk();
thread->tid = total_threads++;
thread->state = READY;
//thread->ctxt = uctx_main;
thread->isMain = 1;
uctx_main.uc_stack.ss_sp = new char[16384];
uctx_main.uc_stack.ss_size = 16384;
thread->ctxt = uctx_main;
ready_queue.push_back(thread);
cout << "Thread created successfully" << endl;
cout << "Current threads are: " << endl;
// Assign the signal handler
sigaction(SIGALRM, &new_action, NULL);
setitimer(ITIMER_REAL, &timer, NULL);
}
int gtthread_create(gtthread_t *thread_t, void* (*start_routine)(void*), void *arg)
{
// Block all the signals because there can be a race condition for the value of 'total_threads'. So, make it kind of atomic
sigprocmask(SIG_BLOCK, &set, NULL);
if(total_threads != MAX_THREADS)
{
tcblk *thread = new tcblk();
*thread_t = total_threads;
(*thread).tid = total_threads++;
(*thread).state = READY;
ready_queue.push_back(thread);
sigprocmask(SIG_UNBLOCK, &set, NULL); // Unblock the signals because rest of the code can't cause any race conditions
cout << "Thread created successfully" << endl;
cout << "Number of threads = " << total_threads << endl;
if (getcontext(&thread->ctxt) == -1)
handle_error("getcontext");
(*thread).ctxt.uc_stack.ss_sp = new char[16384];//thread->c_stack;
(*thread).ctxt.uc_stack.ss_size = 16384;//sizeof(thread->c_stack);
(*thread).ctxt.uc_link = &wrapper_ctxt;
(*thread).isMain = 0;
makecontext(&thread->ctxt, (void(*)())start_routine, 1, arg);
return thread->tid;
}
else
{
sigprocmask(SIG_UNBLOCK, &set, NULL);
cout << "Reached maximum thread limit" << endl;
return -1;
}
}
int gtthread_join(gtthread_t thread, void **status)
{
cout << "Entered join" << endl;
long search_id = (long)thread;
//int returnValue;
if(search_id > total_threads)
return ESRCH;
bool exists = 0;
for (list<tcblk*>::iterator ci = ready_queue.begin(); ci != ready_queue.end(); ++ci)
{
if((*ci)->tid == search_id)
{
exists = 1;
break;
}
}
while(1)
{
int flag = 0;
if(!terminated_queue.empty())
{
for (list<tcblk*>::iterator ci = terminated_queue.begin(); ci != terminated_queue.end(); ++ci)
{
if((*ci)->tid == search_id)
{
cout << "Found tid " << search_id << endl;
cout << "Current size = " << terminated_queue.size() << endl;
flag = 1;
exists = 1;
free((*ci)->ctxt.uc_stack.ss_sp);
int *return_value = new int();;
//*return_value = (*ci)->retval;
*status = (*ci)->retval;
// Check if blocking is really required here or not
sigprocmask(SIG_BLOCK, &set, NULL);
terminated_queue.erase(ci);
sigprocmask(SIG_UNBLOCK, &set, NULL);
break;
}
}
if(!exists)
return ESRCH;
}
if(flag == 1)
break;
gtthread_yield();
}
return 0;
}
void gtthread_exit(void* retval)
{
sigprocmask(SIG_BLOCK, &set, NULL);
int *returnValue;
//if(retval != NULL)
// returnValue = (int*)retval;
//else
// returnValue = NULL;
cout << "Exiting = " << ready_queue.size() << endl;
tcblk* tcb = ready_queue.front();
ready_queue.pop_front();
cout << "Evicted " << tcb->tid << endl;
cout << "Front = " << ready_queue.front()->tid;
cout << "Back = " << ready_queue.back()->tid;
// if(returnValue != NULL)
tcb->retval = retval;
// else
// tcb->retval = 0;
// Check if blocking is really required here or not
terminated_queue.push_back(tcb);
cout << "***Exited***" << endl << endl;
if(ready_queue.front()->isMain != 1)
{
tcb->ctxt.uc_link = &ready_queue.front()->ctxt;
cout << "Normal " << ready_queue.front()->tid << endl << endl;
}
else
{
tcb->ctxt.uc_link = &uctx_main;
cout << "It's main\n" << endl;
}
//free(&tcb->ctxt.uc_stack.ss_sp);
makecontext(&tcb->ctxt, NULL, 0);
setitimer(ITIMER_REAL, &timer, NULL);
//sigprocmask(SIG_UNBLOCK, &set, NULL);
}
int gtthread_equal(gtthread_t t1, gtthread_t t2)
{
return ((t1 == t2)?1:0);
}
gtthread_t gtthread_self()
{
return ready_queue.front()->tid;
}
int gtthread_cancel(gtthread_t tid)
{
for (list<tcblk*>::iterator ci = ready_queue.begin(); ci != ready_queue.end(); ++ci)
{
if((*ci)->tid == tid)
{
cout << "Found cancellation tid " << tid << endl;
tcblk *blk = *ci;
sigprocmask(SIG_BLOCK, &set, NULL);
terminated_queue.push_back(blk);
sigprocmask(SIG_UNBLOCK, &set, NULL);
ready_queue.erase(ci);
return 0;
}
}
return -1;
}
void gtthread_yield()
{
cout << "Yielding!!!!!!" << endl;
setitimer(ITIMER_REAL, &timer, NULL);
sigalarm_handler(0);
}
int gtthread_mutex_init(gtthread_mutex_t *mutex)
{
*mutex = 0;
}
int gtthread_mutex_lock(gtthread_mutex_t *mutex)
{
while(1)
{
sigprocmask(SIG_BLOCK, &set, NULL);
if(*mutex == 0)
{
*mutex = 1;
sigprocmask(SIG_UNBLOCK, &set, NULL);
break;
}
sigprocmask(SIG_UNBLOCK, &set, NULL);
gtthread_yield();
}
return 0;
}
int gtthread_mutex_unlock(gtthread_mutex_t *mutex)
{
*mutex = 0;
}
void internal_exit()
{
sigprocmask(SIG_BLOCK, &set, NULL);
tcblk* tcb = ready_queue.front();
tcb->retval = NULL;
ready_queue.pop_front();
cout << "Evicted " << tcb->tid << endl;
cout << "Front = " << ready_queue.front()->tid;
terminated_queue.push_back(tcb);
setitimer(ITIMER_REAL, &timer, NULL);
if(ready_queue.front()->isMain)
swapcontext(&wrapper_ctxt, &uctx_main);
else
swapcontext(&wrapper_ctxt, &ready_queue.front()->ctxt);
}