Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SList.cpp
- *
- * Created on: Jun 16, 2018
- * Author: OS1
- */
- #include "PCB.h"
- #include "SList.h"
- #include "SCHEDULE.H"
- //global list
- SleepList* SleepList::sleepingList = NULL;
- //constructor
- SleepList::SleepList(){
- first = NULL;
- last = NULL;
- }
- //destructor
- SleepList::~SleepList(){
- Node* currentNode = this->first; // initialize current node to first
- while (currentNode){
- Node* nextNode = currentNode->next; // get next node
- delete currentNode; // delete current
- currentNode = nextNode; // set current to "old" next
- }
- this->first = NULL;
- this->last = NULL;
- }
- void SleepList::insertFirst(PCB *newPCB, int sleepTime){
- //error
- if(newPCB == NULL)
- return;
- //if there is no first node
- if(first == NULL){
- first = new Node(newPCB, sleepTime);
- last = first;
- return;
- }
- Node *temp = new Node(newPCB, sleepTime);
- //out of memory
- if(temp == NULL)
- return;
- temp->next = first;
- first = temp;
- return;
- }
- void SleepList::insertLast(PCB *newPCB, int sleepTime){
- //error
- if(newPCB == NULL)
- return;
- //if there is no last node
- if(last == NULL){
- last = new Node(newPCB, sleepTime);
- first = last;
- return;
- }
- Node* temp = new Node(newPCB, sleepTime);
- //out of memory
- if(temp == NULL)
- return;
- last->next = temp;
- last = temp;
- }
- void SleepList::insertSorted(PCB *newPCB, int sleepTime){
- //error
- if(newPCB == NULL)
- return;
- //empty list case
- if(first == NULL){
- first = new Node(newPCB, sleepTime);
- last = first;
- return;
- }
- //first edge case
- if(sleepTime <= first->sleepTime){
- insertFirst(newPCB, sleepTime);
- return;
- }
- //second edge case
- if(sleepTime >= last->sleepTime){
- insertLast(newPCB, sleepTime);
- return;
- }
- //'mid' case
- Node *curr, *prev;
- curr = first;
- prev = NULL;
- while(curr != NULL && curr->sleepTime <= sleepTime){
- prev = curr;
- curr = curr->next;
- }
- //some error occured
- if(curr == NULL || prev == NULL){
- return;
- }
- //now curr points to the first node with a greater sleep time
- Node *temp = new Node(newPCB, sleepTime);
- prev->next = temp;
- temp->next = curr;
- }
- void SleepList::decrementAll(){
- if(first == NULL){
- return;
- }
- Node *temp = first;
- while(temp != NULL){
- temp->sleepTime -= 1;
- temp = temp->next;
- }
- }
- void SleepList::simulateSleep() {
- //there are no threads asleep
- if (first == NULL)
- return;
- //we are reducing remaining times to all the sleeping threads
- decrementAll();
- while (first != 0 && first->sleepTime == 0) {
- //we are removing the first element from the list
- Node *temp = first;
- first = first->next;
- //we are taking the PCB and puttiing it in the scheduler
- PCB* wakeUp = temp->pcb;
- wakeUp->state = READY;
- Scheduler::put(wakeUp);
- delete temp;
- }
- //if the list is empty restart the pointers
- if(first == NULL){
- last = NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement