Statistics
| Branch: | Revision:

root / include / cscheduler.h @ 7f251033

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

    
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.peekFirst()).
133
     */
134
    virtual cMessage *getNextEvent();
135
};
136

    
137

    
138
/**
139
 * Real-time scheduler class. When installed as scheduler using the scheduler-class
140
 * omnetpp.ini entry, it will syncronize simulation execution to real (wall clock)
141
 * time.
142
 *
143
 * Operation: a "base time" is determined when startRun() is called. Later on,
144
 * the scheduler object calls usleep() from getNextEvent() to synchronize the
145
 * simulation time to real time, that is, to wait until
146
 * the current time minus base time becomes equal to the simulation time.
147
 * Should the simulation lag behind real time, this scheduler will try to catch up
148
 * by omitting sleep calls altogether.
149
 *
150
 * Scaling is supported via the realtimescheduler-scaling omnetpp.ini entry.
151
 * For example, if it is set to 2.0, the simulation will try to execute twice
152
 * as fast as real time.
153
 *
154
 * @ingroup Internals
155
 */
156
class SIM_API cRealTimeScheduler : public cScheduler
157
{
158
  protected:
159
    // configuration:
160
    bool doScaling;
161
    double factor;
162

    
163
    // state:
164
    timeval baseTime;
165

    
166
    bool waitUntil(const timeval& targetTime);
167

    
168
  public:
169
    /**
170
     * Constructor.
171
     */
172
    cRealTimeScheduler() : cScheduler()  {}
173

    
174
    /**
175
     * Destructor.
176
     */
177
    virtual ~cRealTimeScheduler() {}
178

    
179
    /**
180
     * Called at the beginning of a simulation run.
181
     */
182
    virtual void startRun();
183

    
184
    /**
185
     * Called at the end of a simulation run.
186
     */
187
    virtual void endRun();
188

    
189
    /**
190
     * Recalculates "base time" from current wall clock time.
191
     */
192
    virtual void executionResumed();
193

    
194
    /**
195
     * Scheduler function -- it comes from cScheduler interface.
196
     * This function synchronizes to real time: it waits (usleep()) until
197
     * the real time reaches the time of the next simulation event.
198
     */
199
    virtual cMessage *getNextEvent();
200
};
201

    
202
NAMESPACE_END
203

    
204

    
205
#endif
206