-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtthread_sched.c
307 lines (264 loc) · 8.65 KB
/
gtthread_sched.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
/**********************************************************************
gtthread_sched.c.
This file contains the implementation of the scheduling subset of the
gtthreads library. A simple round-robin queue should be used.
**********************************************************************/
/*
Include as needed
*/
#include "gtthread.h"
#include "steque.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
/*
Students should define global variables and helper functions as
they see fit.
*/
steque_t* readyqueue = NULL;
steque_t* terminatequeue = NULL;
sigset_t sigmaskset;
struct itimerval timer;
struct sigaction timerhandler;
static int thread_count = 0;
static ucontext_t scheduler_context;
static ucontext_t main_context;
static void scheduler(int signum) {
gtthread *head, *currentthread;
//all threads complete
if (steque_isempty(readyqueue)) {
exit(0);
}
currentthread = (gtthread* ) steque_front(readyqueue);
if (steque_size(readyqueue) == 1) {
if (currentthread->state == Terminated) {
exit(0);
}
}
else {
if (currentthread->state == Terminated) {
steque_pop(readyqueue);
}
else {
steque_cycle(readyqueue);
}
}
head = (gtthread* ) steque_front(readyqueue);
if (head != currentthread) {
if (currentthread->state == Terminated) {
setcontext(&head->context);
}
else
swapcontext(¤tthread->context, &head->context);
}
}
static void manager() {
raise(SIGPROF);
}
//wrapperfunction runs the thread's function and collects the result, and do the clean up
static void wrapperfunction(void* (*start_routine) (void*), void *arg) {
void *retval;
retval = (*start_routine)(arg);
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
gtthread * head = (gtthread* ) steque_front(readyqueue);
head->retval = retval;
head->state = Terminated;
steque_enqueue(terminatequeue, (steque_item) head);
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
}
/*
The gtthread_init() function does not have a corresponding pthread equivalent.
It must be called from the main thread before any other GTThreads
functions are called. It allows the caller to specify the scheduling
period (quantum in micro second), and may also perform any other
necessary initialization. If period is zero, then thread switching should
occur only on calls to gtthread_yield().
Recall that the initial thread of the program (i.e. the one running
main() ) is a thread like any other. It should have a
gtthread_t that clients can retrieve by calling gtthread_self()
from the initial thread, and they should be able to specify it as an
argument to other GTThreads functions. The only difference in the
initial thread is how it behaves when it executes a return
instruction. You can find details on this difference in the man page
for pthread_create.
*/
void gtthread_init(long period) {
if (readyqueue == NULL && terminatequeue == NULL) {
readyqueue = (steque_t *) malloc (sizeof(steque_t));
terminatequeue = (steque_t *) malloc (sizeof(steque_t));
if (readyqueue == NULL || terminatequeue == NULL) {
perror("Init Failure");
exit(EXIT_FAILURE);
}
if ( getcontext(&scheduler_context) == -1 ) {
perror("getcontext");
exit(EXIT_FAILURE);
}
//establish manager context
scheduler_context.uc_stack.ss_sp = malloc(SIGSTKSZ);
scheduler_context.uc_stack.ss_size = SIGSTKSZ;
scheduler_context.uc_stack.ss_flags = 0;
scheduler_context.uc_link = NULL;
makecontext(&scheduler_context,(void (*) (void))&manager,0);
sigemptyset(&sigmaskset);
sigaddset(&sigmaskset, SIGPROF);
//setup signal handler
timerhandler.sa_handler = &scheduler;
sigemptyset(&timerhandler.sa_mask);
sigaddset(&timerhandler.sa_mask, SIGPROF);
sigaction(SIGPROF, &timerhandler, NULL);
//set up time quantum
timer.it_interval.tv_sec = period/1000000L;
timer.it_interval.tv_usec = period%1000000L;
timer.it_value = timer.it_interval;
//initialize the main thread
gtthread *mainthread = (gtthread*) malloc(sizeof(gtthread));
mainthread->tid = thread_count++;
mainthread->state = Ready;
if ( getcontext(&main_context) == -1) {
perror("getcontext");
exit(EXIT_FAILURE);
}
//main_context.uc_stack.ss_sp = malloc(SIGSTKSZ);
//main_context.uc_stack.ss_size = SIGSTKSZ;
//main_context.uc_stack.ss_flags = 0;
mainthread->context = main_context;
steque_enqueue(readyqueue, (steque_item) mainthread);
setitimer(ITIMER_PROF, &timer, NULL);
}
}
/*
The gtthread_create() function mirrors the pthread_create() function,
only default attributes are always assumed.
*/
int gtthread_create(gtthread_t *thread,
void *(*start_routine)(void *),
void *arg){
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
if (readyqueue != NULL) {
gtthread *newthread = (gtthread*) malloc (sizeof(gtthread));
newthread->tid = thread_count;
newthread->state = Ready;
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
if (getcontext(&newthread->context)==-1) {
perror("getcontext");
exit(EXIT_FAILURE);
}
newthread->context.uc_stack.ss_sp = malloc(SIGSTKSZ);
newthread->context.uc_stack.ss_size = SIGSTKSZ;
newthread->context.uc_stack.ss_flags = 0;
newthread->context.uc_link = &scheduler_context;
if (newthread->context.uc_stack.ss_sp == NULL) {
return -1;
}
makecontext(&newthread->context, (void (*)(void)) wrapperfunction, 2, start_routine, arg);
steque_enqueue(readyqueue, (steque_item) newthread);
*thread = thread_count++;
return 0;
}
else {
return -1;
}
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
}
/*
The gtthread_join() function is analogous to pthread_join.
All gtthreads are joinable.
*/
int gtthread_join(gtthread_t thread, void **status){
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
int queuesize = steque_size(readyqueue);
int i;
gtthread *temp;
int exsitinqueue=0;
for (i=0; i<queuesize; i++) {
temp = (gtthread *)steque_front(readyqueue);
if (temp->tid == thread) {
exsitinqueue = 1;
}
steque_cycle(readyqueue);
}
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
// check terminated queue
while(1) {
int exsitinterminate = 0;
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
if (!steque_isempty(terminatequeue)) {
queuesize = steque_size(terminatequeue);
for (i=0; i<queuesize; i++) {
temp = (gtthread *)steque_front(terminatequeue);
if (temp->tid == thread) {
exsitinqueue=1;
exsitinterminate=1;
if(status!=NULL) *status = temp->retval;
//release the resource
free(temp->context.uc_stack.ss_sp);
steque_pop(terminatequeue);
break;
}
steque_cycle(terminatequeue);
}
}
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
if (exsitinqueue == 0) return -1;
if (exsitinterminate == 1) break;
gtthread_yield();
}
return 0;
}
/*
The gtthread_exit() function is analogous to pthread_exit.
*/
void gtthread_exit(void* retval){
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
gtthread * currentthread = (gtthread *) steque_front (readyqueue);
currentthread->retval = retval;
currentthread->state = Terminated;
steque_enqueue(terminatequeue, (steque_item) currentthread);
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
raise(SIGPROF);
}
/*
The gtthread_yield() function is analogous to pthread_yield, causing
the calling thread to relinquish the cpu and place itself at the
back of the schedule queue.
*/
void gtthread_yield(void){
raise(SIGPROF);
}
/*
The gtthread_yield() function is analogous to pthread_equal,
returning zero if the threads are the same and non-zero otherwise.
*/
int gtthread_equal(gtthread_t t1, gtthread_t t2){
return (t1 == t2 )?1:0;
}
/*
The gtthread_cancel() function is analogous to pthread_cancel,
allowing one thread to terminate another asynchronously.
*/
int gtthread_cancel(gtthread_t thread){
sigprocmask(SIG_BLOCK, &sigmaskset, NULL);
int success =-1;
gtthread *first = (gtthread *)steque_front(readyqueue);
do {
gtthread * temp = (gtthread *)steque_front(readyqueue);
if(temp->tid == thread) {
steque_enqueue(terminatequeue, (steque_item) temp);
temp->state = Terminated;
steque_pop(readyqueue);
success = 0;
if (temp == first) break;
}
steque_cycle(readyqueue);
} while((gtthread *)steque_front(readyqueue) != first);
sigprocmask(SIG_UNBLOCK, &sigmaskset, NULL);
return success;
}
/*
Returns calling thread.
*/
gtthread_t gtthread_self(void){
return ((gtthread *) steque_front(readyqueue))->tid;
}