-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCMachine.cpp
324 lines (242 loc) · 7.68 KB
/
SCMachine.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*-----------------------------------------------------------------------------+
| |
| SCL - Simulation Class Library |
| |
| (c) 1994-98 Marc Diefenbruch, Wolfgang Textor |
| University of Essen, Germany |
| |
+---------------+-------------------+---------------+------------------+-------+
| Module | File | Created | Project | |
+---------------+-------------------+---------------+------------------+-------+
| SCMachine | SCMachine.cpp | 5. Jul 1994 | SCL | |
+---------------+-------------------+---------------+------------------+-------+
| |
| Change Log |
| |
| Nr. Date Description |
| ----- -------- ------------------------------------------------------ |
| 001 |
| 000 05.07.94 Neu angelegt |
| |
+-----------------------------------------------------------------------------*/
/* Lineal
00000000001111111111222222222233333333334444444444555555555566666666667777777777
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
#include <string.h>
#include <math.h>
#include <limits>
#include "SCStream.h"
#include "SCMachine.h"
#include "SCRequest.h"
#include "SCAutomaton.h"
#include "SCScheduler.h"
#include "SCMem.h"
#include "SCDebug.h"
#include "SCTraceControl.h"
#if _SC_NOINLINES
#include "SCMachine.inl.h"
#endif
#if _SC_DMALLOC
#include <dmalloc.h>
#endif
SCMachineList *SCMachine::machineList = NULL;
/*----- Konstruktor / Destruktor -----*/
SCMachine::SCMachine (SCRunnableID pMachineID,
SCDiscipline pDiscipline,
SCNatural pServers,
SCNatural pServices,
const SCDuration *const pSpeeds,
const char *pName,
SCObject *father) :
SCRunnable (pMachineID, // runnableID
true, // sleeping
SC_MACHINE, // object type
father), // parent object
working(false),
servers(pServers),
discipline(pDiscipline),
services(pServices),
name(pName)
{
speeds = new SCDuration[pServices];
assert(speeds);
#ifdef _SC_DMALLOC
memcpy ((char *)speeds, (const char *)pSpeeds, sizeof (SCDuration) * pServices);
#else
memcpy ((void *)speeds, pSpeeds, sizeof (SCDuration) * pServices);
#endif
serviceQueue = new SCRequestSaveList(true, this);
assert(serviceQueue);
lastTime = SCScheduler::GetCurrentTime();
assert (machineList); // allocated in SCMachine::Initialize()
machineList->InsertAfter(this);
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceMachineCreate, this);
}
SCMachine::~SCMachine (void)
{
if (IsTraceOn())
SCTraceControl::LogEvent (scTraceMachineStop, this);
delete serviceQueue;
delete[] speeds;
}
/*----- Member-Funktionen -----*/
void SCMachine::FinishRequests (void)
{
SCDuration epsilon;
SCRequestCons * loop;
SCRequestCons * loopNext;
SCRequest * request;
loop = GetServiceQueue()->Head();
epsilon = SCScheduler::GetEpsilon();
// don't use iterator here because
// list is modified during iteration!
while (loop != NULL)
{
loopNext = loop->Next();
if ((*loop)()->GetRemainingTime() <= epsilon)
{
request = GetServiceQueue()->Remove (loop);
#if _SC_DEBUGOUTPUT
scDebugLog << SCScheduler::GetCurrentTime() << ": " << *this;
scDebugLog << " has finished ";
scDebugLog << *request << std::endl;
#endif
// Wakeup waiting process:
request->GetCaller()->Schedule (kSCScheduleTime,
SCScheduler::GetCurrentTime());
if (IsTraceOn())
{
SCTraceControl::LogEvent (scTraceServiceFinish, this, request);
}
delete request;
}
loop = loopNext;
}
}
void SCMachine::Reschedule (void)
{
SCTime nextTime = DBL_MAX;
SCTime jobTime;
SCRequestIter iter (*GetServiceQueue());
SCRequest * request;
working = false;
for (request = iter++;
request;
request = iter++)
{
working = true;
jobTime = SCScheduler::GetCurrentTime() + request->GetRemainingTime();
if (nextTime > jobTime)
nextTime = jobTime;
}
if (working)
Schedule (kSCScheduleTime, nextTime);
else
Schedule (kSCScheduleWaiting);
}
void SCMachine::Size (SCSize *curSize) const
{
SCRunnable::Size(curSize); // Size of base class.
curSize->size += sizeof(SCBoolean); // working
curSize->historySize += sizeof(SCTime); // Member lastTime
serviceQueue->Size(curSize); // Request Queue.
}
SCBoolean SCMachine::Save (SCMem& saveArea) const
{
// 1. Save base class
SCRunnable::Save (saveArea); // Store base class members.
// 2. Save simple data members
saveArea.HistoryStore(&lastTime, sizeof(SCTime));
saveArea.Store(&working, sizeof(SCBoolean));
// 3. Save Service-Queue (serviceQueue)
assert(serviceQueue);
serviceQueue->Save(saveArea);
return true;
}
SCBoolean SCMachine::Restore (SCMem &saveArea)
{
// 1. Restore base class
SCRunnable::Restore (saveArea);
// 2. Restore simple data members
saveArea.HistoryRestore (&lastTime, sizeof(SCTime));
saveArea.Restore(&working, sizeof(SCBoolean));
// 3. Restore Service Room (serviceRoom)
assert(serviceQueue);
serviceQueue->Restore (saveArea);
return true;
}
SCStream& operator<< (SCStream& pStream, const SCMachine& pData)
{
return pData.Display(pStream); // virtuelle Funktion aufrufen
}
SCStream& SCMachine::Display(SCStream& pStream) const
{
pStream << "machine <" << GetName() << ">";
return SCRunnable::Display(pStream);
}
/* Static Members: */
SCBoolean SCMachine::Initialize (void)
{
// allocate list for machines
machineList = new SCMachineList(false);
assert(machineList);
return true;
}
SCBoolean SCMachine::Finish (void)
{
// delete list for machines
if (machineList)
delete machineList;
return true;
}
SCBoolean SCMachine::StaticSave (SCMem &saveArea)
{
machineList->Save(saveArea);
return true;
}
SCBoolean SCMachine::StaticRestore (SCMem &saveArea)
{
machineList->Restore(saveArea);
return true;
}
void SCMachine::StaticSize (SCSize *pSize)
{
machineList->Size(pSize);
}
void SCMachine::StaticStop (void)
{
SCMachineIter iter(*machineList);
SCMachine * machine;
for (machine = iter++;
machine;
machine = iter++)
{
machine->Cancel();
}
}
#if _SC_VALIDATION_OPTIMIZE
void SCMachine::StaticSetModified (SCBoolean setQueues)
{
SCMachineIter iter(*machineList);
SCMachine * machine;
for (machine = iter++;
machine;
machine = iter++)
{
machine->SetModified(setQueues);
}
}
void SCMachine::StaticUnsetModified (SCBoolean setQueues)
{
SCMachineIter iter(*machineList);
SCMachine * machine;
for (machine = iter++;
machine;
machine = iter++)
{
machine->UnsetModified(setQueues);
}
}
#endif