Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SlpList.cpp
- *
- * Created on: May 22, 2018
- * Author: OS1
- */
- #include "SlpList.h"
- #include "PCB.h"
- #include <STDIO.H>
- #include "SCHEDULE.H"
- #include "Def.h"
- SleepList* SleepList::sleepingThreads = new SleepList();
- /************************************************/
- /* Constructor */
- SleepList::SleepList() {
- first = last = 0;
- }
- /************************************************/
- /* Methods */
- SleepList& SleepList::insertFirst(PCB *newPCB) {
- if (newPCB) {
- if (first == last == 0) {
- first = new Node(newPCB);
- last = first;
- first->dt = first->pcb->timeToSleep;
- return *this;
- }
- Node *temp = new Node(newPCB);
- temp->next = first;
- first = temp;
- first->dt = first->pcb->timeToSleep;
- first->next->dt = first->next->pcb->timeToSleep
- - first->pcb->timeToSleep;
- }
- return *this;
- }
- SleepList& SleepList::insertLast(PCB *newPCB) {
- //printf("Called insertLast");
- //exception
- if (newPCB == 0)
- return *this;
- if (first == last == 0) {
- first->dt = first->pcb->timeToSleep;
- first = last = new Node(newPCB);
- } else {
- Node *prev = last;
- last = last->next = new Node(newPCB);
- last->dt = last->pcb->timeToSleep - prev->pcb->timeToSleep;
- }
- return *this;
- }
- SleepList& SleepList::insertSorted(PCB *newPCB) {
- //printf("Called insertSorted");
- //exception
- if (newPCB == 0)
- return *this;
- Node *temp = new Node(newPCB);
- //exception
- if (temp == 0)
- return *this;
- if (first == last == 0) {
- first = last = temp;
- first->dt = first->pcb->timeSlice;
- return *this;
- }
- //if sleep time is smaller then first's then put it at the start
- if (temp->pcb->timeToSleep < first->pcb->timeToSleep) {
- insertFirst(newPCB);
- return *this;
- }
- //if its greater put int at the end
- if (temp->pcb->timeToSleep > last->pcb->timeToSleep) {
- insertLast(newPCB);
- return *this;
- }
- //put it in so the list remains sorted
- Node * curr = first;
- Node * prev = 0;
- //curr je prvi veci od njega
- while (curr != 0 && curr->pcb->timeToSleep < temp->pcb->timeToSleep) {
- prev = curr;
- curr = curr->next;
- }
- if (!prev) {
- //error
- return *this;
- }
- if (!curr) {
- //error
- return *this;
- }
- Node* newNode = new Node(newPCB);
- prev->next = newNode;
- newNode->next = curr;
- newNode->dt = newPCB->timeToSleep - prev->pcb->timeToSleep;
- curr->dt = curr->pcb->timeToSleep - newPCB->timeToSleep;
- return *this;
- }
- //not sure if this will help with sleep but w/e
- void SleepList::decrementAll(int dec) {
- //lock();
- Node *temp = first;
- while (temp != 0) {
- temp->pcb->timeToSleep -= dec;
- temp = temp->next;
- }
- //unlock();
- }
- //if a thread's sleep time is over we out it back in the scheduler
- //i should most likely put this code in Timer!!!
- void SleepList::simulateSleep() {
- //lock();
- if (first == 0)
- return;
- decrementAll();
- first->dt -= 1;
- while (first != 0 && first->pcb->timeToSleep == 0) {
- //here we update the thread's state, put it in the scheduler and then update the first node
- Node *temp = first;
- first = first->next;
- PCB* wakeUp = temp->pcb;
- wakeUp->state = READY;
- Scheduler::put(wakeUp);
- delete temp;
- temp = first;
- }
- //unlock();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement