-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
executable file
·74 lines (60 loc) · 1.57 KB
/
thread.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
//File: thread.cpp
//Author: Milos Acimovic
#include "thread.h"
#include "PCB.h"
#include "system.h"
extern void interrupt timer(...);
extern volatile int explicitDispatch;
extern volatile int lockFlag;
extern volatile int whileLockedFlagThreadTimeSliceReachedZero;
Thread::Thread(StackSize stackSize, Time timeSlice){
lock
myPCB = new PCB(stackSize, timeSlice, this);
System::systemPCBManager->put(myPCB);
unlock
}
void Thread::start(){
//Anything else ?
lock
myPCB->prepareStack();
myPCB->state = READY;
System::thereAreReadyThreads++;
Scheduler::put(myPCB);
unlock
}
ID Thread::getId(){
return myPCB->id; //permitted since we declared Thread as friend to PCB
}
ID Thread::getRunningId(){
return PCB::running->id; //permitted since we declared Thread as friend to PCB
}
Thread::~Thread(){
//asm cli; //NO! lockFlag = 0;
waitToComplete();
PCB* temp = myPCB; //myPCB
System::systemPCBManager->remove(System::systemPCBManager->get(getId()));
delete(temp);
//asm sti;
}
void Thread::waitToComplete(){
if(myPCB->state != TERMINATING && myPCB != (PCB*)PCB::running && myPCB->state != NEW){
PCB::running->state = BLOCKED;
myPCB->queueOfThreadsWaitingForThisThread->put((PCB*)PCB::running);
asm cli;
System::thereAreBlockedThreads++;
dispatch();
}
}
Thread* Thread::getThreadById(ID id){
if(System::systemPCBManager->get(id) != NULL){
return System::systemPCBManager->get(id)->myPCB->myThread;
}else{
return NULL;
}
}
void dispatch(){
asm cli;
explicitDispatch = 1;
timer();
asm sti;
}