Advertisement
Marko35S

Untitled

Jun 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. /*
  2. * SlpList.cpp
  3. *
  4. * Created on: May 22, 2018
  5. * Author: OS1
  6. */
  7.  
  8. #include "SlpList.h"
  9. #include "PCB.h"
  10. #include <STDIO.H>
  11. #include "SCHEDULE.H"
  12. #include "Def.h"
  13.  
  14. SleepList* SleepList::sleepingThreads = new SleepList();
  15.  
  16. /************************************************/
  17. /* Constructor */
  18.  
  19. SleepList::SleepList() {
  20. first = last = 0;
  21. }
  22.  
  23. /************************************************/
  24. /* Methods */
  25.  
  26. SleepList& SleepList::insertFirst(PCB *newPCB) {
  27.  
  28. if (newPCB) {
  29. if (first == last == 0) {
  30. first = new Node(newPCB);
  31. last = first;
  32. first->dt = first->pcb->timeToSleep;
  33. return *this;
  34. }
  35.  
  36. Node *temp = new Node(newPCB);
  37. temp->next = first;
  38. first = temp;
  39. first->dt = first->pcb->timeToSleep;
  40. first->next->dt = first->next->pcb->timeToSleep
  41. - first->pcb->timeToSleep;
  42. }
  43. return *this;
  44.  
  45. }
  46.  
  47. SleepList& SleepList::insertLast(PCB *newPCB) {
  48.  
  49. //printf("Called insertLast");
  50. //exception
  51. if (newPCB == 0)
  52. return *this;
  53.  
  54. if (first == last == 0) {
  55. first->dt = first->pcb->timeToSleep;
  56. first = last = new Node(newPCB);
  57. } else {
  58. Node *prev = last;
  59. last = last->next = new Node(newPCB);
  60. last->dt = last->pcb->timeToSleep - prev->pcb->timeToSleep;
  61. }
  62.  
  63. return *this;
  64. }
  65.  
  66. SleepList& SleepList::insertSorted(PCB *newPCB) {
  67.  
  68. //printf("Called insertSorted");
  69. //exception
  70. if (newPCB == 0)
  71. return *this;
  72.  
  73. Node *temp = new Node(newPCB);
  74.  
  75. //exception
  76. if (temp == 0)
  77. return *this;
  78.  
  79. if (first == last == 0) {
  80. first = last = temp;
  81. first->dt = first->pcb->timeSlice;
  82.  
  83. return *this;
  84. }
  85.  
  86. //if sleep time is smaller then first's then put it at the start
  87. if (temp->pcb->timeToSleep < first->pcb->timeToSleep) {
  88. insertFirst(newPCB);
  89.  
  90. return *this;
  91. }
  92.  
  93. //if its greater put int at the end
  94. if (temp->pcb->timeToSleep > last->pcb->timeToSleep) {
  95. insertLast(newPCB);
  96.  
  97. return *this;
  98. }
  99.  
  100. //put it in so the list remains sorted
  101. Node * curr = first;
  102. Node * prev = 0;
  103.  
  104. //curr je prvi veci od njega
  105. while (curr != 0 && curr->pcb->timeToSleep < temp->pcb->timeToSleep) {
  106. prev = curr;
  107. curr = curr->next;
  108. }
  109. if (!prev) {
  110. //error
  111.  
  112. return *this;
  113. }
  114. if (!curr) {
  115. //error
  116.  
  117. return *this;
  118. }
  119.  
  120. Node* newNode = new Node(newPCB);
  121. prev->next = newNode;
  122. newNode->next = curr;
  123.  
  124. newNode->dt = newPCB->timeToSleep - prev->pcb->timeToSleep;
  125. curr->dt = curr->pcb->timeToSleep - newPCB->timeToSleep;
  126.  
  127. return *this;
  128. }
  129.  
  130. //not sure if this will help with sleep but w/e
  131. void SleepList::decrementAll(int dec) {
  132.  
  133. //lock();
  134.  
  135. Node *temp = first;
  136.  
  137. while (temp != 0) {
  138.  
  139. temp->pcb->timeToSleep -= dec;
  140. temp = temp->next;
  141.  
  142. }
  143.  
  144. //unlock();
  145. }
  146. //if a thread's sleep time is over we out it back in the scheduler
  147. //i should most likely put this code in Timer!!!
  148. void SleepList::simulateSleep() {
  149.  
  150. //lock();
  151. if (first == 0)
  152. return;
  153.  
  154. decrementAll();
  155. first->dt -= 1;
  156.  
  157. while (first != 0 && first->pcb->timeToSleep == 0) {
  158. //here we update the thread's state, put it in the scheduler and then update the first node
  159.  
  160. Node *temp = first;
  161. first = first->next;
  162. PCB* wakeUp = temp->pcb;
  163. wakeUp->state = READY;
  164. Scheduler::put(wakeUp);
  165. delete temp;
  166. temp = first;
  167. }
  168.  
  169. //unlock();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement