-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCTimer.cpp
381 lines (285 loc) · 9.76 KB
/
SCTimer.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*-----------------------------------------------------------------------------+
| |
| SCL - Simulation Class Library |
| |
| (c) 1994-98 Marc Diefenbruch, Wolfgang Textor |
| University of Essen, Germany |
| |
+---------------+-------------------+---------------+-------------------+------+
| Module | File | Created | Project | |
+---------------+-------------------+---------------+-------------------+------+
| SCTimer | SCTimer.cc | 24. Feb 1996 | SCL | |
+---------------+-------------------+---------------+-------------------+------+
| |
| Change Log |
| |
| Nr. Date Description |
| ----- -------- ------------------------------------------------------ |
| 001 |
| 000 --.--.-- Neu angelegt |
| |
+-----------------------------------------------------------------------------*/
/* Lineal
00000000001111111111222222222233333333334444444444555555555566666666667777777777
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
#include "SCStream.h"
#include "SCTimer.h"
#include "SCDataType.h"
#include "SCProcess.h"
#include "SCScheduler.h"
#include "SCTimeEventList.h"
#include "SCTimerType.h"
#include "SCTimerControl.h"
#include "SCTimerListTable.h"
#include "SCMem.h"
#if _SC_DMALLOC
#include <dmalloc.h>
#endif
SCNatural SCTimer::nextTimerID = 1;
SCTimerTable *SCTimer::timerTable = NULL;
#if _SC_NOINLINES
#include "SCTimer.inl.h"
#endif
/*----- Konstruktor -----*/
SCTimer::SCTimer (const SCProcess* pProcess,
const SCTime &pWhen,
const SCTimerType *const pTimerType,
SCDataType *pData,
const SCObject* pParent):
SCObject(SC_TIMER, pParent),
timerID(nextTimerID++),
timerType((SCTimerType *)pTimerType),
when(pWhen),
data(pData),
container(NULL),
tableContainer(NULL),
timeEventContainer(NULL)
{
assert(pProcess != NULL);
processID = pProcess->GetID();
AddTimer(); // insert timer instace
// into hash table of
// timer type for
// SCTimerControl::Locate() speedup
}
SCTimer::SCTimer(SCMem &saveArea, const SCObject *pParent):
SCObject(SC_TIMER, pParent),
timerType(NULL),
data(NULL),
container(NULL),
tableContainer(NULL),
timeEventContainer(NULL)
{
Restore(saveArea);
AddTimer(); // insert timer instace
// into hash table of
// timer type for
// SCTimerControl::Locate() speedup
}
SCTimer::~SCTimer (void)
{
if (data)
{
delete data;
}
RemoveTimer();
}
void SCTimer::AddTimer (void)
{
SCTimerListTable *table;
SCTimerList *list;
SCTimerCons *timerContainer;
SCTimeEventCons *retValue;
// we have a hash table in SCTimerType
// for all timers of this type:
table = timerType->GetTimerListTable();
assert(table);
list = table->Find (processID);
if (!list) // first timer for this timer type?
{
list = new SCTimerList(false); // allocate list for timer type
assert(list);
timerContainer = list->InsertAfter (this); // Containerverwaltung hier
assert(timerContainer); // notwendig da keine Save-Liste!
SetTableContainer(timerContainer);
table->Insert (processID, list); // insert into hash table
}
else // list is present, so other timers
{ // for this timer type have already
// been inserted
timerContainer = list->InsertAfter (this);
assert(timerContainer);
SetTableContainer(timerContainer);
}
retValue = SCTimerControl::GetTimerControl()->AddTimerEvent(this);
SetTimeEventContainer(retValue);
// we have a hash table in SCTimer
// for all living timers:
assert(timerTable); // allocated in SCTimer::Initialize()
if (!timerTable->Insert(timerID, this))
{
std::cerr << std::endl << "Timer Hash Table is full ! " << std::endl << std::endl;
assert (false);
exit(1);
}
}
void SCTimer::RemoveTimer (void)
{
SCTimerListTable *table;
SCTimerList *list;
SCTimerCons *timerContainer;
SCTimeEventCons *vTimeEventContainer;
assert(timerType);
table = timerType->GetTimerListTable();
assert(table);
list = table->Find (processID);
assert(list);
timerContainer = GetTableContainer();
assert(timerContainer);
list->Remove(timerContainer);
vTimeEventContainer = GetTimeEventContainer();
assert(vTimeEventContainer);
SCTimerControl::GetTimerControl()->RemoveTimerEvent(vTimeEventContainer);
assert(timerTable);
timerTable->Remove(timerID);
}
SCStream& operator<< (SCStream& pStream, const SCTimer& pData)
{
pStream << "timer <" << *pData.GetTimerType() << ">(#";
pStream << pData.GetTimerID() << ", time = " << pData.when << ")";
return pStream;
}
SCStream& SCTimer::Display(SCStream& pStream) const
{
return (pStream << *this);
}
SCDataType *SCTimer::RetrieveData (void)
{
SCDataType *retrData = data;
#if _SC_VALIDATION_OPTIMIZE
if (retrData && GetContainer()) // data present and Timer is in List ?
{ // we must adjust the list size!
SCSize dataSize;
SCRunnable *runnable;
SCTimerSaveList *list = (SCTimerSaveList *)GetContainer()->List();
assert (list);
runnable = (SCRunnable *)list->GetParent();
assert(runnable);
retrData->Size(&dataSize);
list->SetCurrentSize(list->GetCurrentSize() - dataSize.size);
list->SetCurrentHistorySize(list->GetCurrentHistorySize() -
dataSize.historySize);
runnable->currentSize -= dataSize.size;
runnable->currentHistorySize -= dataSize.historySize;
#if _SC_VALIDATION_OPTIMIZE >= 2
list->SetModified(kSCQueueCorrupted);
#endif
}
#endif
data = NULL;
return (retrData);
}
void SCTimer::Size(SCSize *curSize) const
{
// system state member:
curSize->size += (sizeof(SCNatural) + // ID of timerType
sizeof(SCProcessID)); // processID
#if _SC_VALIDATION_TIME == 1
curSize->size += sizeof(SCDuration); // remainingTime
#endif
// history state member:
curSize->historySize += (sizeof(SCNatural) + // timerID
sizeof(SCTimerType *) + // timerType
sizeof(SCTime)); // when
if (data)
data->Size(curSize);
}
SCBoolean SCTimer::Save(SCMem& saveArea) const
{
SCNatural key;
#if _SC_VALIDATION_TIME == 1
SCDuration remainingTime;
#endif
saveArea.HistoryStore(&timerID, sizeof(SCNatural));
// nur Zeiger sichern, da statisches Objekt!
saveArea.HistoryStore(&timerType, sizeof(SCTimerType *));
// zusaetzlich ID des Timer-Signals speichern, da
// Signale zustandsrelevant sind:
key = timerType->GetID();
saveArea.Store(&key, sizeof(SCNatural));
saveArea.Store(&processID, sizeof(SCProcessID));
if (data)
data->Save (saveArea);
saveArea.HistoryStore (&when, sizeof(SCTime));
#if _SC_VALIDATION_TIME == 1
remainingTime = when - SCScheduler::GetCurrentTime();
assert(remainingTime >= 0.0);
saveArea.Store(&remainingTime, sizeof(SCDuration));
#endif
return true;
}
SCBoolean SCTimer::Restore(SCMem& saveArea)
{
SCNatural key;
#if _SC_VALIDATION_TIME == 1
SCDuration remainingTime;
#endif
saveArea.HistoryRestore (&timerID, sizeof(SCNatural));
// nur Zeiger restaurieren, da statisches Objekt!
saveArea.HistoryRestore (&timerType, sizeof(SCTimerType *));
saveArea.Restore (&key, sizeof(SCNatural)); // dummy key
saveArea.Restore (&processID, sizeof(SCProcessID));
if (data)
delete data;
data = timerType->NewData();
if (data)
{
data->Restore (saveArea);
}
saveArea.HistoryRestore (&when, sizeof (SCTime));
#if _SC_VALIDATION_TIME == 1
saveArea.Restore (&remainingTime, sizeof (SCDuration));
#endif
return true;
}
SCBoolean SCTimer::Initialize (SCNatural timerTableSize)
{
// allocate table for all timer instances
timerTable = new SCTimerTable(timerTableSize,
false, // delete elems
true, // resolve coll
true, // resize if full
NULL); // parent
assert(timerTable);
return true;
}
SCBoolean SCTimer::Finish (void) // static!
{
// delete table of timers
if (timerTable)
delete timerTable;
return true;
}
void SCTimer::StaticSize(SCSize *curSize)
{
curSize->historySize += sizeof(SCNatural); // Member nextTimerID
}
SCBoolean SCTimer::StaticSave(SCMem &saveArea)
{
saveArea.HistoryStore(&nextTimerID, sizeof(SCNatural));
return true;
}
SCBoolean SCTimer::StaticRestore(SCMem &saveArea)
{
saveArea.HistoryRestore(&nextTimerID, sizeof(SCNatural));
return true;
}
SCBoolean SCTimer::operator==(const SCTimer &second) const
{
return (timerID == second.timerID);
}
SCBoolean SCTimer::operator!=(const SCTimer &second) const
{
return (timerID != second.timerID);
}