/
lists.c
182 lines (154 loc) · 4 KB
/
lists.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
#include <stdlib.h>
#include "kernel.h"
#include "lists.h"
list * timer_list = NULL;
list * wait_list = NULL;
list * ready_list = NULL;
list * temp_list = NULL;
exception init_lists() {
timer_list = create_list();
wait_list = create_list();
ready_list = create_list();
return SUCCESS;
}
/*
* TIMER LIST
*/
/*
* TIMER LIST: INSERTION
* The insertion of an element into the list, pass an integer with the function call. Insertion in the list is
* done so that the element with the lowest value of nTCnt appears first.
*/
void timer_insert(listobj *newObj){
listobj *pTemp = timer_list->pHead;
while(pTemp->pNext != timer_list->pTail){
if(pTemp->pNext->nTCnt >= newObj->nTCnt){
break;
}
pTemp = pTemp->pNext;
}
newObj->pNext = pTemp->pNext;
newObj->pPrevious = pTemp;
pTemp->pNext = newObj;
newObj->pNext->pPrevious = newObj;
}
/*
* TIMER LIST: EXTRACTION
* Extraction is always done from the front, i.e. at the "head-element".
*/
listobj * timer_extract(){
listobj *extObj ;
if(timer_list->pHead->pNext == timer_list->pTail)return NULL;
extObj = timer_list->pHead->pNext;
timer_list->pHead->pNext = extObj->pNext;
return extObj;
}
/*
* WAITING LIST
*/
/*
* WAITING LIST: INSERTION
* A list, where the list is sorted so that the element with the lowest value
* of TCB-> Deadline is first.
*/
void wait_insert(listobj *new_obj){
listobj *newObj = new_obj;
listobj *pTemp = wait_list->pHead;
while(pTemp->pNext != wait_list->pTail){
if(pTemp->pNext->pTask->DeadLine >= newObj->pTask->DeadLine){
break;
}
pTemp = pTemp->pNext;
}
newObj->pNext = pTemp->pNext;
newObj->pPrevious = pTemp;
pTemp->pNext = newObj;
newObj->pNext->pPrevious = newObj;
}
/*
* WAITING LIST: EXTRACTION
* Extraction made by the use of a pointer to the list element, struct l_obj * pBlock
*/
listobj * wait_extract(listobj * pBlock){
if (wait_list->pHead->pNext != wait_list->pTail){
pBlock->pNext->pPrevious = pBlock->pPrevious;
pBlock->pPrevious->pNext = pBlock->pNext;
pBlock->pNext = NULL;
pBlock->pPrevious = NULL;
return pBlock;
}
return FAIL;
}
/*
* READY LIST
*/
/*
* READY LIST: INSERTION
* The element with the lowest value of TCB-> Deadline is first placed first in the list.
*/
void ready_insert(listobj *new_obj){
listobj *newObj = new_obj;
listobj *pTemp = ready_list->pHead;
while(pTemp->pNext != ready_list->pTail){
if(pTemp->pNext->pTask->DeadLine >= newObj->pTask->DeadLine){
break;
}
pTemp = pTemp->pNext;
}
newObj->pNext = pTemp->pNext;
newObj->pPrevious = pTemp;
pTemp->pNext = newObj;
newObj->pNext->pPrevious = newObj;
}
/*
* READY LIST: EXTRACTION
* Extraction is always done from the front, i.e. at the "head-element".
*/
listobj * ready_extract(){
listobj *extObj ;
if(ready_list->pHead->pNext == ready_list->pTail)return NULL;
extObj = ready_list->pHead->pNext;
ready_list->pHead->pNext = extObj->pNext;
return extObj;
}
/*
* CREATES LIST
*
*/
list * create_list(){
temp_list = (list *)malloc (sizeof(list));
if(temp_list == NULL){
return NULL;
}
temp_list->pHead = (listobj *)malloc (sizeof(listobj));
if(temp_list->pHead == NULL) {
free(temp_list);
return NULL;
}
temp_list->pTail = (listobj *)malloc (sizeof(listobj));
if(temp_list->pTail == NULL) {
free(temp_list->pHead);
free(temp_list);
return NULL;
}
temp_list->pHead->pPrevious = NULL;
temp_list->pHead->pNext = temp_list->pTail;
temp_list->pTail->pPrevious = temp_list->pHead;
temp_list->pTail->pNext = NULL;
return temp_list;
}
listobj * create_listobj(int num)
{
listobj * myobj = (listobj *)calloc(1, sizeof(listobj));
TCB * mytcb = malloc(sizeof (TCB));
if (myobj == NULL || mytcb == NULL)
{
free(myobj);
free(mytcb);
return NULL;
}
myobj->nTCnt = num;
mytcb->DeadLine = num;
myobj->pTask = mytcb;
return (myobj);
}