forked from cindywhan/cosc301_proj04
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fifoq2.c
130 lines (113 loc) · 3.09 KB
/
fifoq2.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
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include "fifoq.h"
void fifo_print(struct node *ready) {
// Original code by Professor Sommers, modified by Bria.
printf("***List contents begin***\n");
while (ready != NULL) {
printf("tid: %d\n", ready -> tid);
ready = ready -> next;
}
printf("***List contents end***\n");
}
void fifo_clear(struct node *ready) {
// Code by Professor Sommers from HW3, modified by Sam
while (ready != NULL) {
struct node *tmp = ready;
ready = ready->next;
free(tmp);
}
}
// Code by Bria - pops the head of the list off and returns
// it.
struct node* fifo_pop(struct node **ready) {
struct node* temp = *ready;
if (temp != NULL) {
*ready = (*ready) -> next;
}
return temp;
}
// Code by Bria - pushes an already existing node to the
// back of the list.
void fifo_push(struct node **ready, struct node *thread) {
if (thread == NULL) {
return;
}
thread -> next = NULL;
if (*ready != NULL) {
struct node *temp = *ready;
while (temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = thread;
}
else {
*ready = thread;
}
}
// Code by Bria - frees a thread node.
void node_destroy(struct node *thread) {
free(thread -> thread.uc_stack.ss_sp);
free(thread);
}
// Taken from Bria and adapted by Sam - pops the head of the list off and returns
// it so that the threads can be removed from locks' and semaphores' waiting lists it contains can be run.
struct locknode* locknode_pop(struct locknode **locklist) {
struct locknode* temp = *locklist;
if (temp != NULL) {
*locklist = (*locklist) -> next;
}
return temp;
}
void locknode_push(struct locknode **locklist, struct locknode *lock) {
lock -> next = NULL;
if (*locklist != NULL) {
struct locknode *temp = *locklist;
while (temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = lock;
}
else {
*locklist = lock;
}
}
void locknode_remove(struct locknode **locklist, int lockid) {
if (*locklist != NULL) {
struct locknode *last = *locklist;
struct locknode *next = last->next;
if ((last->lock)->lockid == lockid) {
*locklist = next;
return;
}
while (next != NULL) {
if((next->lock)->lockid == lockid) {
last->next = next->next;
return;
}
last = next;
next = next->next;
}
}
}
struct semnode* semnode_pop(struct semnode **semlist) {
struct semnode* temp = *semlist;
if (temp != NULL) {
*semlist = (*semlist) -> next;
}
return temp;
}
void semnode_push(struct semnode **semlist, struct semnode *sem) {
sem -> next = NULL;
if (*semlist != NULL) {
struct semnode *temp = *semlist;
while (temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = sem;
}
else {
*semlist = sem;
}
}