Statistics
| Branch: | Revision:

root / include / casyncmodule.h @ f857128b

History | View | Annotate | Download (8.89 KB)

1
//==========================================================================
2
//   CASYNCMODULE.H  -  header for
3
//                     Horizon/OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cAsyncModule  : base for asynchronous event handing in simple module objects
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 2009 Georg Kunz
14

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

    
19
#ifndef __CASYNCMODULE_H
20
#define __CASYNCMODULE_H
21

    
22
#include <pthread.h>
23
#include <semaphore.h>
24

    
25
#include "csimplemodule.h"
26
#include "cmessage.h"
27
#include "cnumgen.h"
28

    
29
#ifdef ATOMIC_OPS_DEBUG
30
        #include "catomicopsdebug.h"
31
#else
32
        #include <atomic_ops.h>
33
#endif
34

    
35
/**
36
 * New base class for all modules that handle expanded events for asynchronous
37
 * (parallel) execution. It provides new methods for parallel event handling
38
 * and wrappers of the default API.
39
 *
40
 * @see cSimpleModule
41
 *
42
 * @ingroup Horizon
43
 */
44
class cAsyncMessage;
45

    
46
class SIM_API cAsyncModule : public cSimpleModule, public cNumberGenerator
47
{
48
  private:
49

    
50
    // the two different execution states: either handleMessage is called
51
    // synchronously by the scheduler or asynchronously (to the scheduler)
52
    // by a worker thread
53
    enum ExecutionStates
54
    {
55
        synchronous,
56
        asynchronous
57
    };
58

    
59
    // current state of execution
60
    ExecutionStates executionState;
61

    
62
    // new messages may only be scheduled with timestamps >= this value
63
    simtime_t t_end;
64

    
65
    // priority of the current event
66
    short currentEventPriority;
67

    
68
    // shall zero duration events be executed in parallel
69
    bool parZeroDur;
70

    
71
    // no worker is active inside this module
72
    static const AO_t NOT_BUSY = 0;
73

    
74
    // a worker is active inside this module
75
    static const AO_t BUSY     = 1;
76

    
77
    // flag indicating that a thread is processing an event within this module
78
    AO_t busy;
79

    
80
    // how many messages did a given event send?
81
    unsigned int scheduledMessageCount;
82

    
83
    //
84
    unsigned int executionOrderId;
85

    
86
    // current simulation time within this module
87
    simtime_t now;
88

    
89
    /**
90
     * update meta data needed for event execution
91
     */
92
    void prepareHandleMessage(cMessage* msg);
93

    
94
    /**
95
     * set extended meta data of a message upon sending.
96
     */
97
    void setMessageMetaData(cMessage* msg);
98

    
99
  protected:
100

    
101
  public:
102
    /** @name Constructors and Destructors */
103
    //@{
104
    /**
105
     * Constructor
106
     */
107
    cAsyncModule(const char *name=NULL, cModule *parent=NULL, unsigned stacksize=0);
108

    
109
    /**
110
     * Destructor
111
     */
112
    virtual ~cAsyncModule();
113
    //}@
114

    
115

    
116
    /** @name User-implemented methods for asynchronous event handling. */
117
    //@{
118
    /**
119
     * Returns the duration of the given expanded event. Called by the event
120
     * scheduler to determine the overlapping of expanded events. The user may
121
     * perform any computation on the expanded event (except for deleting it)
122
     * to calculate the event duration.
123
     */
124
    virtual simtime_t getProcessingDelay(cMessage* msg)
125
    {
126
        return 0.0;
127
    }
128

    
129
    /*
130
     * By default non-expanded events are not executed in parallel. The
131
     * option "parallelize-zero-duration-events" globally enables or disables
132
     * parallel execution. This method allows users to overwrite either behavior
133
     * on a per module (and per event) basis. Should return true if parallel
134
     * execution is allowed, false otherwise.
135
     */
136
    virtual bool mayParallelize(cMessage* msg, simtime_t duration) const
137
    {
138
        return duration == SimTime::simTimeZero ? parZeroDur : true;
139
    }
140
    /*
141
     * Initializes the local RNGs according to config options read from ini
142
     */
143
    void initLocalRNGs();
144
    //}@
145

    
146

    
147
    /** @name Support methods for asynchronous event handling. */
148
    //@{
149
    /**
150
     * Returns the duration of the currently processed event.
151
     */
152
    simtime_t getCurrentProcessingDelay() const {
153
        return t_end - simTime();
154
    }
155

    
156
    /*
157
     * Returns true if the module supports parallel event execution.
158
     */
159
    virtual bool isAsyncModule() const {
160
            return true;
161
    }
162
    //}@
163

    
164

    
165
    /** @name Wrapper functions for state keeping inside a module. */
166
    //@{
167
    /**
168
     * INTERNAL: Wrapper for asynchronous message handling.
169
     */
170
    void callHandleAsyncMessage(cMessage* msg);
171

    
172
    /**
173
     * INTERNAL: Wrapper for synchronous message handling.
174
     */
175
    void callHandleMessage(cMessage* msg);
176

    
177
    /*
178
     * INTERNAL: Check if a worker thread is already busy inside this module
179
     * and if so, wait until the worker is done.
180
     */
181
    inline void waitIfBusy() {
182
        while (AO_load_full(&busy) == BUSY) {
183
            __asm__("pause");
184
        }
185
    }
186

    
187
    /*
188
     * INTERNAL: Indicate that a worker is busy inside this module.
189
     */
190
        inline void setBusy() {
191
                AO_store_full(&busy, BUSY);
192
        }
193

    
194
        /**
195
         * INTERNAL: Unset flag to indicate that a worker has finished.
196
         */
197
        inline void unsetBusy() {
198
                AO_store_full(&busy, NOT_BUSY);
199
        }
200
    //}@r
201

    
202

    
203
    /** @name Wrapper functions for the OMNeT++ API */
204
    //@{
205
    /**
206
     * @see cSimpleModule
207
     */
208
    virtual int scheduleAt(simtime_t t, cMessage *msg);
209

    
210
    /**
211
     * @see cSimpleModule
212
     */
213
    virtual int send(cMessage *msg, int gateid);
214

    
215
    /**
216
     * @see cSimpleModule
217
     */
218
    virtual int send(cMessage *msg, const char *gatename, int sn=-1);
219

    
220
    /**
221
     * @see cSimpleModule
222
     */
223
    virtual int send(cMessage *msg, cGate *outputgate);
224

    
225
    /**
226
     * @see cSimpleModule
227
     */
228
    virtual int sendDelayed(cMessage *msg, simtime_t delay, int gateid);
229

    
230
    /**
231
     * @see cSimpleModule
232
     */
233
    virtual int sendDelayed(cMessage *msg, simtime_t delay, const char *gatename, int sn=-1);
234

    
235
    /**
236
     * @see cSimpleModule
237
     */
238
    virtual int sendDelayed(cMessage *msg, simtime_t delay, cGate *outputgate);
239

    
240
    /**
241
     * @see cSimpleModule
242
     */
243
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cModule *mod, int inputgateid);
244

    
245
    /**
246
     * @see cSimpleModule
247
     */
248
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cModule *mod, const char *inputgatename, int sn=-1);
249

    
250
    /**
251
     * @see cSimpleModule
252
     */
253
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cGate *inputgate);
254
    //}@
255

    
256

    
257
    /*
258
     *
259
     * Wrapper methods for numberGenerator Method calls
260
     *
261
     */
262
/*
263
    inline double uniform(double a, double b, int rng=0) {
264
        return numGen->uniform(a,b,rng);
265
    }
266

267
    inline double exponential(double mean, int rng=0) {
268
        return numGen->exponential(mean, rng);
269
    }
270

271
    inline double normal(double mean, double stddev, int rng=0) {
272
        return numGen->normal(mean,stddev, rng);
273
    }
274

275
    inline double truncnormal(double mean, double stddev, int rng=0) {
276
        return numGen->truncnormal(mean,stddev, rng);
277
    }
278

279
    inline double gamma_d(double alpha, double theta, int rng=0) {
280
        return numGen->gamma_d(alpha, theta, rng);
281
    }
282

283
    inline double beta(double alpha1, double alpha2, int rng=0) {
284
        return numGen->beta(alpha1,alpha2, rng);
285
    }
286

287
    inline double erlang_k(unsigned int k, double mean, int rng=0) {
288
        return numGen->erlang_k(k, mean, rng);
289
    }
290

291
    inline double chi_square(unsigned int k, int rng=0) {
292
        return numGen->chi_square(k, rng);
293
    }
294

295
    inline double student_t(unsigned int i, int rng=0) {
296
        return numGen->student_t(i, rng);
297
    }
298

299
    inline double cauchy(double a, double b, int rng=0) {
300
        return numGen->cauchy(a,b, rng);
301
    }
302

303
    inline double triang(double a, double b, double c, int rng=0) {
304
        return numGen->triang(a,b,c, rng);
305
    }
306

307
    inline double lognormal(double m, double w, int rng=0) {
308
        return numGen->lognormal(m,w, rng);
309
    }
310

311
    inline double weibull(double a, double b, int rng=0) {
312
        return numGen->weibull(a,b, rng);
313
    }
314

315
    inline double pareto_shifted(double a, double b, double c, int rng=0) {
316
        return numGen->pareto_shifted(a,b,c, rng);
317
    }
318

319
    inline int intuniform(int a, int b, int rng=0) {
320
        return numGen->intuniform(a,b, rng);
321
    }
322

323
    inline int bernoulli(double p, int rng=0) {
324
        return numGen->bernoulli(p, rng);
325
    }
326

327
    inline int binomial(int n, double p, int rng=0) {
328
        return numGen->binomial(n,p, rng);
329
    }
330

331
    inline int geometric(double p, int rng=0) {
332
        return numGen->geometric(p, rng);
333
    }
334

335
    inline int negbinomial(int n, double p, int rng=0) {
336
        return numGen->negbinomial(n,p, rng);
337
    }
338

339
    inline int poisson(double lambda, int rng=0) {
340
        return numGen->poisson(lambda, rng);
341
    }
342

343
    inline long intrand(long r) {
344
        return numGen->intrand(r);
345
    }
346

347
    inline double dblrand() {
348
        return numGen->dblrand();
349
    }
350
*/
351
};
352

    
353
#endif /* __CASYNCMODULE_H */