-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcb.cpp
187 lines (148 loc) · 3.73 KB
/
pcb.cpp
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
/*
* PCB.cpp
*
* Created on: Aug 20, 2018
* Author: OS1
*/
#include "PCB.h"
#include "symbols.h"
#include <dos.h>
#include "SCHEDULE.H"
#include "thread.h"
#include "System.h"
volatile PCB::PCBlist *PCB::head=NULL, *PCB::tail=NULL;
volatile int PCB::lastID=0;
void PCB::addToPCBlist() {
PCBlist* node = new PCBlist (this);
if (PCB::head == NULL) {
PCB::head=node;
PCB::tail=node;
} else {
PCB::tail->next=node;
PCB::tail=node;
}
}
PCB* PCB::getPCBbyID(ID id) {
for (PCBlist* cur = (PCBlist*)PCB::head; cur != NULL; cur=cur->next) {
if(cur->elem->myID == id) return cur->elem;
}
return NULL;
}
void PCB::removeFromPCBlist() {
PCBlist *cur= (PCBlist*) head, *prev=NULL;
while (cur->elem != this) {
prev=cur;
cur=cur->next;
}
if (prev == NULL) {
//deleting at head postion
head=head->next;
if (head == NULL) tail=NULL;
cur->elem=NULL;
cur->next=NULL;
delete cur;
}
else if (cur->next == NULL) {
//deleting last element
tail=prev;
tail->next=NULL;
cur->elem=NULL;
delete cur;
}
else {
//deleting middle element
prev->next=cur->next;
cur->next=NULL;
cur->elem = NULL;
delete cur;
}
}
void PCB::wrapper() {
System::runningUserThread->myThread->run();
System::exit_thread();
}
PCB::PCB(StackSize stacksize, Time timeSlice, Thread *thr) {
this->state=NEW;
if (stacksize > maxStackSize) stacksize = maxStackSize;
size=stacksize/sizeof(unsigned);
myThread=thr;
stack = new unsigned [size];
quantum=timeSlice;
myID=++lastID;
addToPCBlist();
thr->pcbID=myID;
this->WaitingThreadsHead=NULL;
this->WaitingThreadsTail=NULL;
}
/*
PCB::PCB(StackSize stacksize, Time timeSlice, void (*f)()) {
fun=f;
size=stacksize;
quantum=timeSlice;
stack = new unsigned [size];
myID=++lastID;
addToPCBlist();
this->state=NEW;
}
*/
PCB::~PCB() {
removeFromPCBlist();
delete [] stack;
stack = NULL;
this->myThread=NULL;
stack = NULL;
}
void PCB::createContext() {
if (this->state == PCB::NEW) {
this->state = PCB::READY;
#ifndef BCC_BLOCK_IGNORE
this->stack[this->size-1]=FP_SEG(this->myThread);
this->stack[this->size-2]=FP_OFF(this->myThread);
//skipping -3 -4 (callback)
this->stack[this->size-5]=0x200; //I bit = 1
this->stack[this->size-6]=FP_SEG(Thread::threadWrapper);
this->stack[this->size-7]=FP_OFF(Thread::threadWrapper);
this->s_s=FP_SEG(this->stack + (this->size-16 ));
this->s_p=FP_OFF(this->stack + (this->size-16));
this->b_p=FP_OFF(this->stack + (this->size-16));
if (this != System::idleThread) {
Scheduler::put(this);
System::number_of_user_threads++;
}
#endif
}
}
void PCB::pcb_waitToComplete(){
if (this->state == PCB::FINISHED) {
return;
}
PCB::WaitingThreads* nw = new PCB::WaitingThreads((PCB*)System::runningUserThread);
if (this->WaitingThreadsHead == NULL) {
this->WaitingThreadsHead=this->WaitingThreadsTail=nw;
} else {
this->WaitingThreadsTail->next=nw;
this->WaitingThreadsTail=nw;
}
System::runningUserThread->state=PCB::BLOCKED;
System::demanded_context_switch=TRUE; // context switch requested
// dispatch () ;
}
void PCB::deblockAllThreads(){
System::softLock();
if (this->WaitingThreadsHead != NULL) {
//there are some blocked threads
PCB::WaitingThreads* cur = this->WaitingThreadsHead;
while (cur != NULL) {
//deblocking threads
WaitingThreads* old = cur;
cur->mypcb->state = PCB::READY;
Scheduler::put(cur->mypcb);
cur->mypcb = NULL;
cur=cur->next;
//mozda syscall pa ne treba okruziti sa lock/unlock
delete old;
}
this->WaitingThreadsHead = this->WaitingThreadsTail = NULL;
}
System::softUnlock();
}