-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.cpp
More file actions
213 lines (155 loc) · 5.43 KB
/
monitor.cpp
File metadata and controls
213 lines (155 loc) · 5.43 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
<one line to give the library's name and an idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "monitor.h"
#define PROG_NAME "slave"
Monitor::Monitor(uint procNo, const JobList& jobList, const Tids& tids) : App(procNo, jobList),
tids(tids), recvMark(tids.size(), false) {
this->involved = false;
this->lamport = 0;
}
void Monitor::send(Instr instr, int arg, int objNo) {
Msg outMsg = { this->procNo, this->lamport, { instr, arg } };
pvm_initsend(PvmDataDefault);
pvm_pkbyte((char *) &outMsg, sizeof(outMsg), 1);
pvm_send(this->tids[objNo], NORMAL);
if (instr == GET) {
PRINT_VERBOSE(this->procNo, this->lamport, "halt", (int) WAIT_GET);
this->halt |= WAIT_GET;
}
}
void Monitor::receive() {
Msg inMsg;
do {
pvm_recv(-1, NORMAL);
pvm_upkbyte((char *) &inMsg, sizeof(inMsg), 1);
this->msgBuf.push_back(inMsg);
}
while (pvm_probe(-1, NORMAL) > 0);
}
void Monitor::recordState(int init) {
PRINT_VERBOSE(this->procNo, this->lamport, "Recording state");
this->involved = true;
for (uint i = 0; i < this->recvMark.size(); ++i)
this->recvMark[i] = false;
// Oznaczamy, że od "siebie" odebraliśmy już wiadomość
this->recvMark[this->procNo] = true;
this->haltState.instrNo = this->instrNo;
this->haltState.halt = this->halt;
this->haltState.lamport = this->lamport;
this->haltState.obj = this->obj;
this->haltState.reg = this->reg;
this->initializer = init;
for (uint i = 0; i < this->tids.size(); ++i)
if (i != this->procNo)
this->send(MARKER, init, i);
}
void Monitor::sendState() {
PRINT_VERBOSE(this->procNo, this->lamport, "Sending state");
list<Msg>::iterator it;
int msgNum = this->chanState.size();
pvm_initsend(PvmDataDefault);
pvm_pkuint(&this->procNo, 1, 1);
// Zapamiętanie stanu procesu
pvm_pkbyte((char*) &this->haltState, sizeof(this->haltState), 1);
// Zapamiętanie stanu wiadomości
pvm_pkint(&msgNum, 1, 1);
for (it = this->chanState.begin(); it != this->chanState.end(); ++it)
pvm_pkbyte((char *) &(*it), sizeof(Msg), 1);
pvm_send(this->initializer, STATE);
// Po wysłaniu wracamy do normalnej pracy
this->involved = false;
this->chanState.clear();
}
void Monitor::resume(const HaltState& resumeState, list< Msg >& msgSaved) {
// Oddtwarzanie stau systemu
this->instrNo = resumeState.instrNo;
this->halt = (WaitFor) resumeState.halt;
this->lamport = resumeState.lamport;
this->obj = resumeState.obj;
this->reg = resumeState.reg;
// Odtwarzanie stanu wiadomości
this->msgBuf.swap(msgSaved);
PRINT_VERBOSE(this->procNo, this->lamport, "resumed slave");
}
void Monitor::run() {
while (1) {
// runRemote wykonuje wszystko co jest w buforze lokalnym - nie odbiera wiadomości
// Potrzebne do wznawiania - standardowo bufor jest pusty
if (this->msgBuf.size() > 0)
this->runRemote();
if ((!this->done) && (this->halt == WAIT_NONE)) {
this->runLocal(MAX_ITER);
if (pvm_probe(-1, NORMAL) == 0)
continue;
}
this->receive();
}
}
bool Monitor::isAllChanDone() {
for (uint i = 0; i < this->recvMark.size(); ++i)
if (!this->recvMark[i])
return false;
return true;
}
void Monitor::runRemote() {
list<Msg>::iterator it = this->msgBuf.begin();
for (; it != this->msgBuf.end(); it = this->msgBuf.erase(it)) {
this->lamport = std::max(this->lamport, (*it).laport);
switch ((*it).oper.instr) {
case SET:
PRINT_VERBOSE(this->procNo, this->lamport, "SET", (*it).who, (*it).oper.arg);
this->obj = (*it).oper.arg;
break;
case GET:
PRINT_VERBOSE(this->procNo, this->lamport, "GET", (*it).who, (*it).oper.arg);
++this->lamport;
this->send(GET_RESP, this->obj, (*it).who);
break;
case INC:
PRINT_VERBOSE(this->procNo, this->lamport, "INC", (*it).who);
++this->obj;
break;
case ADD:
PRINT_VERBOSE(this->procNo, this->lamport, "ADD", (*it).who, (*it).oper.arg);
this->obj += (*it).oper.arg;
break;
case GET_RESP:
PRINT_VERBOSE(this->procNo, this->lamport, "GET_RESP", (*it).who, (*it).oper.arg);
PRINT_VERBOSE(this->procNo, this->lamport, "unhalt", (int) WAIT_GET);
this->reg = (*it).oper.arg;
this->halt &= ~WAIT_GET;
break;
case MARKER:
PRINT_VERBOSE(this->procNo, this->lamport, "Recive marker", (*it).who);
if (!this->involved)
this->recordState((*it).oper.arg);
if ((*it).who != (uint) -1)
this->recvMark[(*it).who] = true;
if (this->isAllChanDone())
this->sendState();
break;
default:
break;
}
if ((this->halt & WAIT_VAL) && (this->obj == this->reg)) {
PRINT_VERBOSE(this->procNo, this->lamport, "unhalt", (int) WAIT_VAL);
this->halt &= ~WAIT_VAL;
}
if ((this->involved) && ((*it).who != (uint) -1))
if (!this->recvMark[(*it).who])
this->chanState.push_back(*it);
}
}