-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_lib.c
369 lines (340 loc) · 8.93 KB
/
t_lib.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* t_lib.c
*
* Authors: Sophia Freaney and Connor Onweller
*
* Purpose: Implements function to initialize thread library,
* create/terminate threads, and use semaphores. Struct
* descriptions are in t_lib.h, and extended function
* descriptions are in ud_thread.c
*
*/
#include "ud_thread.h"
#include <string.h>
tcb *running;
tcb *ready;
struct mboxList *box_list;
// relinqueshes CPU to thread on running queue, moves new thread to running,
// moves old running to end of ready
void t_yield() {
if (ready != NULL) {
tcb *old, *new, *tmp;
old = running;
new = ready;
// Goes to end of ready queue and adds currently running thread, which will be
// deactivated at end of function. Also dequeues the new thread to be run from
// the head of the ready thread.
tmp = ready;
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = old;
ready = ready->next;
new->next = NULL;
running = new;
swapcontext(old->thread_context, new->thread_context);
}
}
// sets up first thread (main) sets up running and ready queues
void t_init() {
tcb *tmp = malloc(sizeof(tcb));
tmp->thread_context = (ucontext_t *)malloc(sizeof(ucontext_t));
tmp->next = NULL;
tmp->thread_id = 0;
tmp->thread_priority = 0;
getcontext(tmp->thread_context); /* let tmp be the context of main() */
running = tmp;
ready = NULL;
box_list = NULL;
}
int t_create(void (*fct)(int), int id, int pri) {
/* printf("created %d\n", id); */
size_t sz = 0x10000;
ucontext_t *uc;
uc = (ucontext_t *)malloc(sizeof(ucontext_t));
getcontext(uc);
uc->uc_stack.ss_sp = malloc(sz); /* new statement */
uc->uc_stack.ss_size = sz;
uc->uc_stack.ss_flags = 0;
uc->uc_link = running->thread_context;
makecontext(uc, (void (*)(void))fct, 1, id);
tcb *new_tcb = malloc(sizeof(tcb));
new_tcb->thread_context = uc;
new_tcb->thread_id = id;
new_tcb->thread_priority = pri;
new_tcb->next = NULL;
tcb *tmp = ready;
if (tmp == NULL)
ready = new_tcb;
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = new_tcb; // add to end of ready list
}
mbox *mb;
mbox_create(&mb);
if (box_list == NULL) {
box_list = malloc(sizeof (struct mboxList));
box_list->mbox = mb;
box_list->tid = id;
box_list->next = NULL;
}
else {
struct mboxList *cur_box = box_list;
while (cur_box->next !=NULL) {
cur_box = cur_box->next;
}
cur_box->next = malloc(sizeof(struct mboxList));
cur_box->next->mbox = mb;
cur_box->next->tid = id;
}
}
// frees all threads
void t_shutdown(void) {
struct mbox *mb = NULL;
mbox_destroy(&mb);
if (ready != NULL) {
tcb *tmp = ready;
while (tmp->next != NULL) {
tcb *last = tmp;
tmp = tmp->next;
tcb_free(last); // free contex
free(last); // free thread
}
free(tmp);
}
/* free(running->thread_context); */
tcb_free(running); // free context
free(running); // free thread
}
// removes and frees running thread
void t_terminate(void) {
running->next = NULL; // make sure its the only one in the list
tcb_free(running); // free the context
free(running); // free the thread
running = ready; // update running to a ready thread
ready = ready->next; // remove last head of ready
running->next = NULL; // only one running
setcontext(running->thread_context);
}
// Frees the thread's stack and context
void tcb_free(tcb *thread) {
free(thread->thread_context->uc_stack.ss_sp);
free(thread->thread_context);
}
////////////////////////////////// SEMAPHORES /////////////////////////////////
int sem_init(sem_t **sp, unsigned int sem_count) {
*sp = malloc(sizeof(sem_t));
(*sp)->count = sem_count;
(*sp)->q = NULL;
return sem_count;
}
void sem_wait(sem_t *s) {
while (s->count < 1) {
tcb *old, *new;
old = running;
new = ready;
running = new;
ready = ready->next;
// Enqueues previously running thread to semaphore queue
tcb *tmp = s->q;
if (tmp == NULL) {
s->q = old;
} else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = old;
}
new->next = NULL;
// Activates new thread
swapcontext(old->thread_context, new->thread_context);
}
s->count--;
}
void sem_signal(sem_t *s) {
s->count++;
if (s->q != NULL) {
// Dequeues a thread from the waiting queue, and adds it to the ready queue
tcb *tmp = ready;
tcb *tmp2 = s->q;
s->q = s->q->next; // Removes thread from semaphore queue
tmp2->next = NULL;
if (tmp == NULL) {
ready = tmp2;
} else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = tmp2; // add thread to ready queue
}
}
}
void sem_destroy(sem_t **s)
{
free(*s);
}
/////////////////////////////// MESSAGING /////////////////////////////////////
int mbox_create(mbox **mb) {
*mb = malloc(sizeof(mbox));
/* (*mb)->msg = malloc(sizeof(struct messageNode)); */
(*mb)->msg = NULL;
sem_init(&((*mb)->mbox_sem), 1);
}
void mbox_destroy_helper(mbox *mb) {
struct messageNode *tmp_node, *last_node;
tmp_node = mb->msg;
if (tmp_node != NULL) {
while (tmp_node->next != NULL) {
last_node = tmp_node;
tmp_node = tmp_node->next;
free(last_node->message);
free(last_node);
}
free(tmp_node->message);
free(tmp_node);
}
free(mb->mbox_sem);
free(mb);
}
void mbox_destroy(mbox **mb) {
struct mboxList *tmp_list, *last_list;
tmp_list = box_list;
if (tmp_list != NULL) {
while (tmp_list->next != NULL) {
last_list = tmp_list;
tmp_list = tmp_list->next;
mbox_destroy_helper(last_list->mbox);
free(last_list);
}
mbox_destroy_helper(tmp_list->mbox); //needs to be expanded
free(tmp_list);
}
if ((*mb) != NULL) {
mbox_destroy_helper(*mb);
}
}
// going to assume that sender and receiver are both the running thread's id
void mbox_deposit(mbox *mb, char *msg, int len) {
struct messageNode *newMsg = malloc(sizeof(struct messageNode));
newMsg->sender = running->thread_id;
newMsg->receiver = running->thread_id; //PROBABLY NEED TO CHANGE THIS LATER
newMsg->next = NULL;
newMsg->message = malloc(sizeof(char*)*(1+strlen(msg)));
strcpy(newMsg->message, msg);
newMsg->len = len;
sem_wait(mb->mbox_sem);
struct messageNode *cur = mb->msg;
if (cur == NULL) {
mb->msg = newMsg;
}
else {
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newMsg;
}
sem_signal(mb->mbox_sem);
}
void mbox_depositV2(mbox *mb, char *msg, int len, int tid) {
struct messageNode *newMsg = malloc(sizeof(struct messageNode));
newMsg->sender = running->thread_id;
newMsg->receiver = tid; //PROBABLY NEED TO CHANGE THIS LATER
newMsg->next = NULL;
newMsg->message = malloc(sizeof(char*)*(1+strlen(msg)));
strcpy(newMsg->message, msg);
newMsg->len = len;
sem_wait(mb->mbox_sem);
struct messageNode *cur = mb->msg;
if (cur == NULL) {
mb->msg = newMsg;
}
else {
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newMsg;
}
sem_signal(mb->mbox_sem);
}
void mbox_withdraw(mbox *mb, char *msg, int *len) {
if (mb->msg == NULL)
return;
sem_wait(mb->mbox_sem);
strcpy(msg, mb->msg->message);
*len = mb->msg->len;
struct messageNode *tmp = mb->msg;
mb->msg = mb->msg->next;
free(tmp->message);
free(tmp);
sem_signal(mb->mbox_sem);
}
void mbox_withdrawV2(mbox *mb, char *msg, int *len, int *tid) {
if (mb->msg == NULL)
return;
sem_wait(mb->mbox_sem);
strcpy(msg, mb->msg->message);
*tid = mb->msg->sender;
*len = mb->msg->len;
struct messageNode *tmp = mb->msg;
mb->msg = mb->msg->next;
free(tmp->message);
free(tmp);
sem_signal(mb->mbox_sem);
}
void mbox_withdrawV3(mbox *mb, char *msg, int *len, int tid) {
if (mb->msg == NULL)
return;
sem_wait(mb->mbox_sem);
struct messageNode *curmsg = mb->msg;
struct messageNode *prev = NULL;
while (curmsg != NULL) {
if (curmsg->sender == tid){
strcpy(msg, curmsg->message);
*len = curmsg->len;
struct messageNode *tmp = curmsg;
curmsg = curmsg->next;
if (prev !=NULL) {
prev->next = curmsg;
}
/* free(tmp->message); */
/* free(tmp); */
break;
}
/* printf("%s\n", curmsg->message); */
prev = curmsg;
curmsg = curmsg->next;
}
sem_signal(mb->mbox_sem);
}
void send(int tid, char *msg, int len) {
mbox box;
struct mboxList *tmp = box_list;
while (tmp != NULL) {
if (tmp->tid == tid) {
mbox_depositV2(tmp->mbox, msg, len, tid);
return;
}
tmp = tmp->next;
}
printf("Error: no possible receivers\n");
}
void receive(int *tid, char *msg, int *len) {
mbox box;
struct mboxList *tmp = box_list;
while (tmp != NULL) {
/* printf("current box is %d\n", tmp->tid); */
if (tmp->tid == running->thread_id) {
if (*tid == 0)
mbox_withdrawV2(tmp->mbox, msg, len, tid);
else
mbox_withdrawV3(tmp->mbox, msg, len, *tid);
/* mbox_withdrawV2(tmp->mbox, msg, len, tid); */
return;
}
tmp = tmp->next;
}
/* printf("msg = %s\n", msg); */
printf("Error: no possible receivers\n");
}