-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyThread.c
More file actions
341 lines (313 loc) · 6.73 KB
/
Copy pathmyThread.c
File metadata and controls
341 lines (313 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
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
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#define max_threads 10
#define ssize 60000
struct mythread{
int s_id;
ucontext_t *context;
};
struct queue{
struct queue* next;
struct mythread *thread;
}*head,*tail;
struct thread_status{
int s_id;
struct thread_status * next;
};
struct sigaction act;
ucontext_t Main;
struct itimerval it;
struct itimerval timer;
struct mythread * current_thread;
struct thread_status * running,*waiting;
int id,count;
void killThread();
void delete_running(int t_id,struct thread_status * head);
void initTimer(struct itimerval it);
void schedule(void);
int mythread_self(){
return current_thread->s_id;
}
void block(){
sigfillset(&act.sa_mask);
sigprocmask(SIG_BLOCK,&act.sa_mask,NULL);
}
void unblock(){
sigprocmask(SIG_UNBLOCK,&act.sa_mask,NULL);
sigemptyset(&act.sa_mask);
}
void init(struct mythread * newThread){
newThread->context=(ucontext_t *)malloc(sizeof(ucontext_t));
getcontext(newThread->context);
ucontext_t *temp;
getcontext(temp);
temp->uc_stack.ss_sp=malloc(ssize);
temp->uc_stack.ss_size=ssize;
temp->uc_stack.ss_flags=0;
makecontext(temp,killThread,0);
newThread->context->uc_link=temp;
newThread->context->uc_stack.ss_sp=malloc(ssize);
newThread->context->uc_stack.ss_size=ssize;
newThread->context->uc_stack.ss_flags=0;
}
void addRunning(int newId){
struct thread_status * temp=(struct thread_status*)malloc(sizeof(struct thread_status));
temp->next=running;
temp->s_id=newId;
running=temp;
return ;
}
int enqueue(struct mythread *newThread,int flag){
block();
if(head==NULL){
initTimer(timer);
struct mythread *temp=(struct mythread*)malloc(sizeof(struct mythread));
init(temp);
temp->s_id=-1;
head=(struct queue*)malloc(sizeof(struct queue));
tail=(struct queue*)malloc(sizeof(struct queue));
head->thread=temp;
tail->next=NULL;
head->next=tail;
tail->thread=newThread;
newThread->s_id=id;
id++;
count++;
addRunning(newThread->s_id);
current_thread=head->thread;
unblock();
return 1;
}
else{
if(count==max_threads){
unblock();
return -1;
}
else{
struct queue*temp=(struct queue*)malloc(sizeof(struct queue));
temp->next=NULL;
temp->thread=newThread;
if(flag){
newThread->s_id=id;
id++;
count++;
addRunning(newThread->s_id);
}
tail->next=temp;
tail=tail->next;
if(flag){
unblock();
return 0;
}
}
}
}
int thread_create(struct mythread * newThread, void (*fun)(), void *arg){
init(newThread);
makecontext(newThread->context,fun,1,arg);
int ret=enqueue(newThread,1);
return ret;
}
void initTimer(struct itimerval it){
struct sigaction act, oact;
act.sa_handler=(void*)&schedule;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGPROF, &act, &oact);
it.it_interval.tv_sec=0;
it.it_interval.tv_usec=1000;
it.it_value.tv_sec=0;
it.it_value.tv_usec =5000;
setitimer(ITIMER_PROF, &it, NULL);
}
void killThread(){
block();
ucontext_t temp;
if(head->next->next==NULL){
current_thread=NULL;
delete_running(head->thread->s_id,running);
it.it_interval.tv_sec=0;
it.it_interval.tv_usec=0;
it.it_value.tv_sec=0;
it.it_value.tv_usec=0;
fflush(stdout);
count--;
ucontext_t * main1;
main1=head->next->thread->context;
free(head->next);
free(head);
head=NULL;
setitimer(ITIMER_PROF,&it,NULL);
unblock();
swapcontext(&temp,main1);
return ;
}
else
{
current_thread=head->next->thread;
delete_running(head->thread->s_id,running);
struct queue * temp1=head;
head=head->next;
free(temp1);
count--;
unblock();
fflush(stdout);
swapcontext(&temp,current_thread->context);
}
}
void delete_running(int t_id,struct thread_status * head){
struct thread_status * temp=head;
if(temp->s_id==t_id){
running=temp->next;
free(temp);
}
else{
while(temp->next!=NULL){
if(temp->next->s_id==t_id){
struct thread_status * temp2=temp->next;
temp->next=temp->next->next;
free(temp2);
return ;
}
temp=temp->next;
}
}
}
void schedule(void){
block();
if(current_thread==NULL){
current_thread=head->thread;
unblock();
swapcontext(&Main,current_thread->context);
}
else{
if(head->next==NULL){
it.it_interval.tv_sec=0;
it.it_interval.tv_usec=0;
it.it_value.tv_sec=0;
it.it_value.tv_usec =0;
struct queue * temp=head;
head=head->next;
unblock();
setitimer(ITIMER_PROF, &it, NULL);
setcontext(temp->thread->context);
}
else{
current_thread=head->next->thread;
printf("%d scheduling thread ",current_thread->s_id);
struct mythread * t=head->thread;
struct queue * temp=head;
enqueue(t,0);
head=head->next;
free(temp);
unblock();
swapcontext(t->context,current_thread->context);
}
}
}
void thread_wait(struct mythread * newThread){
while(if_running(newThread->s_id,running)){
schedule();
}
}
int if_running(int t_id,struct thread_status * newThread){
struct thread_status * temp=newThread;
while(temp!=NULL){
if(temp->s_id==t_id){
return 1;
}
temp=temp->next;
}
return 0;
}
struct mythread_mutex{
int lock;
int owner;
struct thread_status * head,*tail;
};
void mythread_mutex_init(struct mythread_mutex * mutex){
mutex->lock=0;
mutex->owner=-1;
}
int add_to_waiting(struct mythread_mutex * mutex,int s_id){
if(mutex->head==NULL){
mutex->head=(struct thread_status *)malloc(sizeof(struct thread_status));
mutex->tail=mutex->head;
mutex->head->s_id=s_id;
mutex->head->next=NULL;
mutex->tail->next=NULL;
}
else{
struct thread_status * temp=(struct thread_status *)malloc(sizeof(struct thread_status));
temp->next=NULL;
temp->s_id=s_id;
mutex->tail->next=temp;
mutex->tail=temp;
}
}
int mythread_mutex_lock(struct mythread_mutex * mutex){
block();
if(mutex->lock==0){
mutex->lock=1;
mutex->owner=mythread_self();
mutex->head=(struct thread_status *)malloc(sizeof(struct thread_status));
mutex->tail=mutex->head;
mutex->head->s_id=mythread_self();
mutex->head->next=NULL;
mutex->tail->next=NULL;
unblock();
return 0;
}
else if(mutex->owner==mythread_self()){
unblock();
return -1;
}
else{
add_to_waiting(mutex,mythread_self());
while(1){
if(mutex->owner!=mythread_self()){
schedule();
block();
}
else{
break;
}
}
unblock();
}
}
int mythread_mutex_unlock(struct mythread_mutex * mutex){
block();
if(mutex->owner==mythread_self()){
mutex->lock=0;
if(mutex->head==NULL){
unblock();
return 0;
}
else{
struct thread_status * temp=mutex->head;
mutex->head=mutex->head->next;
free(temp);
if(mutex->head==NULL){
unblock();
return 0;
}
else{
mutex->lock=1;
mutex->owner=mutex->head->s_id;
unblock();
return 0;
}
}
}
else{
unblock();
return -1;
}
}