Statistics
| Branch: | Revision:

root / include / cscheduler.h @ a3be1d55

History | View | Annotate | Download (5.6 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

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

    
184
class SIM_API cRealTimeScheduler : public cScheduler
185
{
186
  protected:
187
    // configuration:
188
    bool doScaling;
189
    double factor;
190

    
191
    // state:
192
    timeval baseTime;
193

    
194
    bool waitUntil(const timeval& targetTime);
195

    
196
  public:
197
    /**
198
     * Constructor.
199
     */
200
    cRealTimeScheduler() : cScheduler()  {}
201

    
202
    /**
203
     * Destructor.
204
     */
205
    virtual ~cRealTimeScheduler() {}
206

    
207
    /**
208
     * Called at the beginning of a simulation run.
209
     */
210
    virtual void startRun();
211

    
212
    /**
213
     * Called at the end of a simulation run.
214
     */
215
    virtual void endRun();
216

    
217
    /**
218
     * Recalculates "base time" from current wall clock time.
219
     */
220
    virtual void executionResumed();
221

    
222
    /**
223
     * Scheduler function -- it comes from cScheduler interface.
224
     * This function synchronizes to real time: it waits (usleep()) until
225
     * the real time reaches the time of the next simulation event.
226
     */
227
    virtual cMessage *getNextEvent();
228
};
229

    
230
NAMESPACE_END
231

    
232

    
233
#endif
234