-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCB.cpp
executable file
·61 lines (43 loc) · 1.46 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
//File: PCB.cpp
//Author: Milos Acimovic
#include "PCB.h"
#include "system.h"
ID PCB::positionalId = 0;
volatile PCB* PCB::running = NULL;
extern volatile int lockFlag;
extern volatile int whileLockedFlagThreadTimeSliceReachedZero;
PCB::PCB(StackSize stacksize, Time timeslice, Thread* thread){
stack = new unsigned [sizeOfStack = stacksize];
timeSlice = timeslice;
remainingTimeOnCPU = timeslice;
myThread = thread;
queueOfThreadsWaitingForThisThread = new Queue();
id = positionalId++;
state = NEW;
}
PCB::~PCB(){
if(queueOfThreadsWaitingForThisThread != NULL)delete queueOfThreadsWaitingForThisThread;
if(stack != NULL) delete[] stack;
}
void PCB::prepareStack(){
stack[sizeOfStack - 1] = 0x200; //PSW with interrupts enabled
stack[sizeOfStack - 2] = FP_SEG(&PCB::wrapper); //CS of the start of wrapper function
stack[sizeOfStack - 3] = FP_OFF(&PCB::wrapper); //IP of the start of wrapper function
//Registers ax, bx, cx, dx, es, ds, si, di, bp
ss = FP_SEG(stack + sizeOfStack - 12);
sp = FP_OFF(stack + sizeOfStack - 12);
bp = sp;
}
void PCB::wrapper(){
running->myThread->run();
asm cli;
while(running->queueOfThreadsWaitingForThisThread->size() > 0){
PCB* temp = running->queueOfThreadsWaitingForThisThread->get();
temp->state = READY;
System::thereAreBlockedThreads--;
System::thereAreReadyThreads++;
Scheduler::put(temp);
}
running->state = TERMINATING;
dispatch();
}