Statistics
| Branch: | Revision:

root / include / cscheduler.h @ master

History | View | Annotate | Download (5.83 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
NAMESPACE_BEGIN
28

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

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

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

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

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

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

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

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

    
91
    /**
92
     * Returns the next Event to be processed from the FES according to the Scheduling policy.
93
     * The Event is removed from the FES.
94
     * Never returns NULL
95
     * throws cTerminationException(eENDEDOK), when no Event is left to return (the simulation is done)
96
     * This should only be called by doOneEvent() in the Simulation
97
     */
98
    virtual cMessage *getNextEvent() = 0;
99
};
100

    
101
/**
102
 * Event scheduler for sequential simulation.
103
 *
104
 * @ingroup Internals
105
 */
106
class SIM_API cSequentialScheduler : public cScheduler
107
{
108
  public:
109
    /**
110
     * Constructor.
111
     */
112
    cSequentialScheduler() {}
113

    
114
    /**
115
     * Called at the beginning of a simulation run.
116
     */
117
    virtual void startRun() {}
118

    
119
    /**
120
     * Called at the end of a simulation run.
121
     */
122
    virtual void endRun() {}
123

    
124
    /**
125
     * Returns the first event in the Future Event Set (that is,
126
     * msgQueue.removeFirst()).
127
     */
128
    virtual cMessage *getNextEvent();
129
};
130

    
131
/**
132
 * Event Scheduler employing EEF (Earliest Endtime First) Scheduling
133
 *
134
 * @ingroup Internals
135
 */
136
class SIM_API cEEFScheduler : public cScheduler
137
{
138
  private:
139
    cTaskHeap independentEventsHeap;
140
  public:
141
    /**
142
     * Constructor.
143
     */
144
    cEEFScheduler();
145

    
146
    virtual ~cEEFScheduler();
147

    
148
    /**
149
     * Called at the beginning of a simulation run.
150
     */
151
    virtual void startRun() {};
152

    
153
    /**
154
     * Called at the end of a simulation run.
155
     */
156
    virtual void endRun() {};
157

    
158
    /**
159
     * Returns the event with the smallest Ending Time in the Set of offloadable events and removes it from the FES.
160
     */
161
    virtual cMessage *getNextEvent();
162

    
163
    /**
164
     * Updates the IES by filling it up with newly independent events from the FES.
165
     */
166
    void updateIES();
167
    /*
168
     * Returns the first Event to be scheduled according to EEF
169
     */
170
    cMessage* getFirstEvent();
171
};
172

    
173

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

    
193
class SIM_API cRealTimeScheduler : public cScheduler
194
{
195
  protected:
196
    // configuration:
197
    bool doScaling;
198
    double factor;
199

    
200
    // state:
201
    timeval baseTime;
202

    
203
    bool waitUntil(const timeval& targetTime);
204

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

    
211
    /**
212
     * Destructor.
213
     */
214
    virtual ~cRealTimeScheduler() {}
215

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

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

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

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

    
239
NAMESPACE_END
240

    
241

    
242
#endif
243