/
queue.cpp
123 lines (114 loc) · 1.77 KB
/
queue.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdlib.h>
#include "pcb.h"
#include "queue.h"
#include "kernel.h"
#include <iostream.h>
Element::Element(PCB *_pcb)
{
pcb = _pcb;
next = 0;
}
Queue::Queue()
{
lock();
first = 0;
last = 0;
size = 0;
unlock();
}
Queue::~Queue()
{
lock();
Element* temp = first;
while(temp)
{
delete temp;
first = first->next;
temp = first;
}
first = 0;
last = 0;
unlock();
}
int Queue::getSize() const
{
return size;
}
void Queue::put(PCB *_pcb)
{
lock();
//last = first ? last->next : first = new Element(_pcb);
if(first == 0) first = last = new Element(_pcb);
else last = last->next = new Element(_pcb);
size++;
unlock();
}
PCB* Queue::get()
{
lock();
if(first == 0)
{
unlock();
return 0;
}
Element *temp = first;
PCB *pcb = temp->pcb;
first = first->next;
if(first == 0) last = 0;
delete temp;
size--;
unlock();
return pcb;
}
Thread* Queue::findThread(int ID)
{
lock();
Element *temp = first;
if(first == 0)
{
unlock();
return 0;
}
while(temp->pcb->myThreadID != ID && temp)
{
cout << "MyThreadID je " << temp->pcb->myThreadID << " a ID trazeni je " << ID << endl;
temp = temp->next;
}
if(temp == 0)
{
unlock();
return 0;
}
else
{
unlock();
return temp->pcb->myThread;
}
}
void Queue::findAndDelete(PCB* _pcb)
{
lock();
Element *temp = first;
Element *temp_last = 0;
if(first == 0)
{
unlock();
return;
}
while(temp->pcb != _pcb)
{
temp_last = temp;
temp = temp->next;
}
if(temp_last != 0) temp_last->next = temp->next;
else first = first->next;
if(temp == last) last = temp_last;
if(first == 0) last = 0;
size--;
delete temp;
unlock();
}
Element* Queue::getFirst()
{
return first;
}