Statistics
| Branch: | Revision:

root / include / cscheduler.h @ a3d116e3

History | View | Annotate | Download (6.08 KB)

1
//=========================================================================
2
//  CSCHEDULER.H - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2003
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2003-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21
#ifndef __CSCHEDULER_H
22
#define __CSCHEDULER_H
23

    
24
#include "cobject.h"
25
#include "platdep/timeutil.h"  // for timeval
26
#include "ctaskheap.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
// forward declarations
31
class cSimulation;
32
class cMessage;
33

    
34
/**
35
 * Abstract class to encapsulate event scheduling. See description
36
 * of getNextEvent() for more info.
37
 *
38
 * To switch to your own scheduler class (reasons you'd like to do that
39
 * include real-time simulation, hardware-in-the-loop simulation,
40
 * distributed (federated) simulation, parallel distributed simulation),
41
 * subclass cScheduler, register your new class with
42
 * the Register_Class() macro, then add the following to
43
 * <tt>omnetpp.ini</tt>:
44
 *
45
 * <pre>
46
 * [General]
47
 * scheduler-class="MyClass"
48
 * </pre>
49
 *
50
 * @ingroup EnvirExtensions
51
 * @ingroup Internals
52
 */
53
class SIM_API cScheduler : public cObject
54
{
55
  protected:
56
    cSimulation *sim;
57

    
58
  public:
59
    /**
60
     * Constructor.
61
     */
62
    cScheduler();
63

    
64
    /**
65
     * Destructor.
66
     */
67
    virtual ~cScheduler();
68

    
69
    /**
70
     * Pass cSimulation object to scheduler.
71
     */
72
    virtual void setSimulation(cSimulation *_sim);
73

    
74
    /**
75
     * Called at the beginning of a simulation run.
76
     */
77
    virtual void startRun() = 0;
78

    
79
    /**
80
     * Called at the end of a simulation run.
81
     */
82
    virtual void endRun() = 0;
83

    
84
    /**
85
     * Called every time the user hits the Run button in Tkenv.
86
     * Real-time schedulers (e.g. cRealTimeScheduler) may make use
87
     * this callback to pin current simulation time to current
88
     * wall clock time.
89
     */
90
    virtual void executionResumed()  {}
91

    
92
    /**
93
     * The scheduler function -- it should return the next event
94
     * to be processed. Normally (with sequential execution) it just
95
     * returns msgQueue.peekFirst(). With parallel and/or real-time
96
     * simulation, it is also the scheduler's task to synchronize
97
     * with real time and/or with other partitions.
98
     *
99
     * If there is no more event, it throws cTerminationException.
100
     *
101
     * A NULL return value means that there is no error but execution
102
     * was stopped by the user (e.g. with STOP button on the GUI)
103
     * while getNextEvent() was waiting for external synchronization.
104
     */
105
    virtual cMessage *getNextEvent() = 0;
106
};
107

    
108
/**
109
 * Event scheduler for sequential simulation.
110
 *
111
 * @ingroup Internals
112
 */
113
class SIM_API cSequentialScheduler : public cScheduler
114
{
115
  public:
116
    /**
117
     * Constructor.
118
     */
119
    cSequentialScheduler() {}
120

    
121
    /**
122
     * Called at the beginning of a simulation run.
123
     */
124
    virtual void startRun() {}
125

    
126
    /**
127
     * Called at the end of a simulation run.
128
     */
129
    virtual void endRun() {}
130

    
131
    /**
132
     * Returns the first event in the Future Event Set (that is,
133
     * msgQueue.peekFirst()).
134
     */
135
    virtual cMessage *getNextEvent();
136
};
137

    
138
/**
139
 * Event Scheduler employing EEF (Earliest Endtime First) Scheduling
140
 *
141
 * @ingroup Internals
142
 */
143
class SIM_API cEEFScheduler : public cScheduler
144
{
145
  private:
146
    cTaskHeap independentEventsHeap;
147
  public:
148
    /**
149
     * Constructor.
150
     */
151
    cEEFScheduler();
152

    
153
    virtual ~cEEFScheduler();
154

    
155
    /**
156
     * Called at the beginning of a simulation run.
157
     */
158
    virtual void startRun() {};
159

    
160
    /**
161
     * Called at the end of a simulation run.
162
     */
163
    virtual void endRun() {};
164

    
165
    /**
166
     * Returns the event with the smallest Ending Time in the Set of offloadable events.
167
     */
168
    virtual cMessage *getNextEvent();
169
    /*
170
     * Returns the event with the smallest Ending Time in the Set of offloadable events
171
     * and removes it from the heap. Or, if the heap is empty, returns the first event of the MsgQueue
172
     */
173
    virtual cMessage* removeNextEvent();
174
};
175

    
176

    
177
/**
178
 * Real-time scheduler class. When installed as scheduler using the scheduler-class
179
 * omnetpp.ini entry, it will syncronize simulation execution to real (wall clock)
180
 * time.
181
 *
182
 * Operation: a "base time" is determined when startRun() is called. Later on,
183
 * the scheduler object calls usleep() from getNextEvent() to synchronize the
184
 * simulation time to real time, that is, to wait until
185
 * the current time minus base time becomes equal to the simulation time.
186
 * Should the simulation lag behind real time, this scheduler will try to catch up
187
 * by omitting sleep calls altogether.
188
 *
189
 * Scaling is supported via the realtimescheduler-scaling omnetpp.ini entry.
190
 * For example, if it is set to 2.0, the simulation will try to execute twice
191
 * as fast as real time.
192
 *
193
 * @ingroup Internals
194
 */
195
class SIM_API cRealTimeScheduler : public cScheduler
196
{
197
  protected:
198
    // configuration:
199
    bool doScaling;
200
    double factor;
201

    
202
    // state:
203
    timeval baseTime;
204

    
205
    bool waitUntil(const timeval& targetTime);
206

    
207
  public:
208
    /**
209
     * Constructor.
210
     */
211
    cRealTimeScheduler() : cScheduler()  {}
212

    
213
    /**
214
     * Destructor.
215
     */
216
    virtual ~cRealTimeScheduler() {}
217

    
218
    /**
219
     * Called at the beginning of a simulation run.
220
     */
221
    virtual void startRun();
222

    
223
    /**
224
     * Called at the end of a simulation run.
225
     */
226
    virtual void endRun();
227

    
228
    /**
229
     * Recalculates "base time" from current wall clock time.
230
     */
231
    virtual void executionResumed();
232

    
233
    /**
234
     * Scheduler function -- it comes from cScheduler interface.
235
     * This function synchronizes to real time: it waits (usleep()) until
236
     * the real time reaches the time of the next simulation event.
237
     */
238
    virtual cMessage *getNextEvent();
239
};
240

    
241
NAMESPACE_END
242

    
243

    
244
#endif
245