Statistics
| Branch: | Revision:

root / include / cscheduler.h @ aeae20a1

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

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

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

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

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

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

    
152
    virtual ~cEEFScheduler();
153

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

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

    
164
    /**
165
     * Returns the event with the smallest Ending Time in the Set of offloadable events and removes it from the FES.
166
     */
167
    virtual cMessage *getNextEvent();
168
};
169

    
170

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

    
190
class SIM_API cRealTimeScheduler : public cScheduler
191
{
192
  protected:
193
    // configuration:
194
    bool doScaling;
195
    double factor;
196

    
197
    // state:
198
    timeval baseTime;
199

    
200
    bool waitUntil(const timeval& targetTime);
201

    
202
  public:
203
    /**
204
     * Constructor.
205
     */
206
    cRealTimeScheduler() : cScheduler()  {}
207

    
208
    /**
209
     * Destructor.
210
     */
211
    virtual ~cRealTimeScheduler() {}
212

    
213
    /**
214
     * Called at the beginning of a simulation run.
215
     */
216
    virtual void startRun();
217

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

    
223
    /**
224
     * Recalculates "base time" from current wall clock time.
225
     */
226
    virtual void executionResumed();
227

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

    
236
NAMESPACE_END
237

    
238

    
239
#endif
240