-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmythread.c
344 lines (290 loc) · 9 KB
/
mythread.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "mythread.h"
#include <ucontext.h>
#include <stdlib.h>
#include <stdio.h>
#define THREADSTACK 8192
typedef struct node {
void *object;
struct node * next;
} Node;
typedef struct queue {
Node * front;
Node * rear;
int count;
} Queue;
typedef struct MyThreadStruct {
int tid;
int child_tid_join;
int join_all;
ucontext_t context;
Queue child_queue;
struct MyThreadStruct *parent;
} Thread;
typedef struct MySemaphoreStruct {
int value;
Queue semephore_queue;
} Semaphore;
void * dequeue_object(Queue *);
int global_tid = 0; //-1= no thread; 0= Unix thread; >0= child threads
void * queue_object_value; // For refering objects to queue functions
Thread * running_thread; // Master thread which is in running state
ucontext_t unix_context; // Context of the Unix process set during init
Queue ready_queue = {NULL, NULL,0};
Queue blocked_queue = {NULL, NULL,0};
void equeue_object(Queue * queue_value) {
Node * temp = (Node *) calloc(1,sizeof(Node));
queue_value->count++;
temp->object = queue_object_value;
if (queue_value->front == NULL) {
queue_value->front = temp;
queue_value->rear = temp;
return;
}
queue_value->rear->next = temp;
queue_value->rear = temp;
}
void * dequeue_object(Queue *queue_value) {
if (queue_value->count == 0) {
return NULL;
}
void * object = queue_value->front->object;
if (queue_value->count == 1) {
free(queue_value->front);
queue_value->rear = NULL;
queue_value->front = NULL;
} else {
Node * temp = queue_value->front;
queue_value->front = queue_value->front->next;
free(temp);
}
queue_value->count--;
return object;
}
void remove_node(Queue * queue_value) {
if (queue_value->count == 0) {
return;
}
if (queue_value->front->object == queue_object_value) {
(void)dequeue_object(queue_value);
return;
}
Node * temp_1 = queue_value->front;
Node * temp_2 = queue_value->front->next;
while (temp_2 != NULL) {
if (temp_2->object == queue_object_value) {
if (queue_value->rear == temp_2) {
queue_value->rear = temp_1;
}
queue_value->count--;
temp_1->next = temp_2->next;
free(temp_2);
return;
}
temp_1 = temp_2;
temp_2 = temp_2->next;
}
}
int search_queue(Queue *queue_value) {
Node * temp = queue_value->front;
while (temp != NULL) {
if (temp->object == queue_object_value) {
return 1;
}
temp = temp->next;
}
return 0;
}
MyThread MyThreadCreate(void(*start_funct)(void *), void *args) {
Thread * child = (Thread *) calloc(1, sizeof(Thread));
if (child == NULL) {
printf("THREADCREATE: No Memory\n");
return NULL;
}
ucontext_t *temp_context = &child->context;
getcontext(temp_context);
temp_context->uc_link = &running_thread->context;
temp_context->uc_stack.ss_sp= malloc(THREADSTACK);
temp_context->uc_stack.ss_size=THREADSTACK;
temp_context->uc_stack.ss_flags=0;
makecontext(temp_context, (void (*)(void))start_funct, 1, args);
child->tid = global_tid;
global_tid++;
child->parent = running_thread;
queue_object_value = child;
// Put newly created thread in ready queue
equeue_object(&ready_queue);
// Make this newly created thread a child of thread which is currently running
equeue_object(&running_thread->child_queue);
return (MyThread)child;
}
void MyThreadYield(void) {
// Yield running thread by enqueuing it in ready queue
queue_object_value = running_thread;
equeue_object(&ready_queue);
// putting thread from ready queue in to running state
if (ready_queue.count > 0) {
Thread * temp_running = running_thread;
running_thread = (Thread *)dequeue_object(&ready_queue);
swapcontext(&temp_running->context, &running_thread->context);
}
else{
setcontext(&unix_context);
}
}
int MyThreadJoin(MyThread thread) {
Thread * child = (Thread *) thread;
queue_object_value = child;
int ret = search_queue(&running_thread->child_queue);
// If thread in parameter is running thread's child then
if (ret == 1) {
// Put child thread id in parents datastructure for record to block on it during exit
running_thread->child_tid_join = child->tid;
// Enqueue to blocked queue
queue_object_value = running_thread;
equeue_object(&blocked_queue);
// putting thread from ready queue in to running state
if (ready_queue.count > 0) {
Thread * temp_running = running_thread;
running_thread = (Thread *)dequeue_object(&ready_queue);
swapcontext(&temp_running->context, &running_thread->context);
} else {
setcontext(&unix_context);
}
} else {
return -1;
}
return 0;
}
void MyThreadJoinAll(void) {
// Thread has to have child to set join all
if (running_thread->child_queue.count > 0) {
// Set join all flag
running_thread->join_all = 1;
// Enqueue to blocked queue
queue_object_value = running_thread;
equeue_object(&blocked_queue);
// putting thread from ready queue in to running state
if (ready_queue.count > 0) {
Thread * temp_running = running_thread;
running_thread = (Thread *)dequeue_object(&ready_queue);
swapcontext(&temp_running->context, &running_thread->context);
} else {
setcontext(&unix_context);
}
}
}
// Start: Help sort from following for MyThreadExit
// https://github.com/aneesh87/User-Level-Thread-Library
void MyThreadExit(void) {
Thread * rths_parent = running_thread->parent;
// Check whether running threads parent is live?
if (rths_parent != NULL) {
queue_object_value = running_thread;
remove_node(&rths_parent->child_queue);
queue_object_value = rths_parent;
// Look for running threads parent in blocked queue, if it exists
if (search_queue(&blocked_queue)) {
// Check whether join all flag is set for this parent
if (rths_parent->join_all){
// if join all set, check whether all children have exited
if(rths_parent->child_queue.count == 0) {
queue_object_value = running_thread;
remove_node(&blocked_queue);
queue_object_value = rths_parent;
equeue_object(&ready_queue);
rths_parent->join_all = 0;
}
} // Otherwise, check for simple join based on thread id match
else {
if (rths_parent->child_tid_join == running_thread->tid) {
queue_object_value = running_thread;
remove_node(&blocked_queue);
queue_object_value = rths_parent;
equeue_object(&ready_queue);
// -1 thread id indicates that no thread is attached
rths_parent->child_tid_join = -1;
}
}
}
}
if (ready_queue.count > 0) {
running_thread = (Thread *)dequeue_object(&ready_queue);;
setcontext(&running_thread->context);
} else {
setcontext(&unix_context);
}
}
// End
MySemaphore MySemaphoreInit(int initialValue) {
if (initialValue < 0) {
printf("SEMERROR: Initial Value less than zero\n");
return NULL;
}
Semaphore * temp_sem = (Semaphore *)calloc(1, sizeof(Semaphore));
temp_sem->value = initialValue;
return (MySemaphore) temp_sem;
}
void MySemaphoreSignal(MySemaphore sem) {
if (sem == NULL) {
printf("SEMERROR: Signal Sem NULL\n");
return;
}
Semaphore * temp_sem = (Semaphore *)sem;
temp_sem->value++;
if (temp_sem->value <= 0) {
Thread * temp_thread = (Thread *)dequeue_object(&temp_sem->semephore_queue);
queue_object_value = temp_thread;
equeue_object(&ready_queue);
}
}
void MySemaphoreWait(MySemaphore sem) {
if (sem == NULL) {
printf("SEMERROR: Wait Sem NULL\n");
return;
}
Semaphore * temp_sem = (Semaphore *)sem;
temp_sem->value--;
if (temp_sem->value < 0) {
queue_object_value = running_thread;
equeue_object(&temp_sem->semephore_queue);
// putting thread from ready queue in to running state
if (ready_queue.count > 0) {
Thread * temp_running = running_thread;
running_thread = (Thread *)dequeue_object(&ready_queue);
swapcontext(&temp_running->context, &running_thread->context);
}else{
setcontext(&unix_context);
}
}
}
int MySemaphoreDestroy(MySemaphore sem) {
if (sem == NULL) {
printf("SEMERROR: Destroy Sem NULL\n");
return -1;
}
Semaphore * temp_sem = (Semaphore *)sem;
if (temp_sem->semephore_queue.count > 0) {
printf("SEMQUEUE: Not Empty\n");
return -1;
}
free(temp_sem);
return 0;
}
void MyThreadInit(void(*start_funct)(void *), void *args) {
ucontext_t *temp_context;
running_thread = (Thread *) calloc(1, sizeof(Thread));
// Set thread id
running_thread->tid = global_tid;
global_tid++;
temp_context = &running_thread->context;
getcontext(&unix_context);
getcontext(temp_context);
// Assigning parameters to temp_conext for runnung_thread
temp_context->uc_link = NULL;
temp_context->uc_stack.ss_sp=malloc(THREADSTACK);
temp_context->uc_stack.ss_size=THREADSTACK;
temp_context->uc_stack.ss_flags=0;
makecontext(temp_context, (void (*)(void))start_funct, 1, args);
// Activating context
swapcontext(&unix_context, temp_context);
}