Project

General

Profile

Statistics
| Branch: | Revision:

root / include / casyncmodule.h @ f40e1183

History | View | Annotate | Download (6.1 KB)

1 01873262 Georg Kunz
//==========================================================================
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
47
{
48
  private:
49
50 f40e1183 Georg Kunz
    // 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 01873262 Georg Kunz
59 f40e1183 Georg Kunz
    // current state of execution
60
    ExecutionStates executionState;
61 01873262 Georg Kunz
62
    // new messages may only be scheduled with timestamps >= this value
63
    simtime_t t_end;
64
65
    // shall zero duration events be executed in parallel
66
    bool parZeroDur;
67
68
    // no worker is active inside this module
69
    static const AO_t NOT_BUSY = 0;
70
71
    // a worker is active inside this module
72
    static const AO_t BUSY     = 1;
73
74
    // flag indicating that a thread is processing an event within this module
75
    AO_t busy;
76
77 f40e1183 Georg Kunz
    // how many messages did a given event send?
78
    unsigned int sentMessageCount;
79
80
    /**
81
     * update meta data needed for event execution
82
     */
83
    void prepareHandleMessage(cMessage* msg);
84 01873262 Georg Kunz
85
  protected:
86
    // the central number generator of this module
87
    cNumberGenerator* numGen;
88
89
90
  public:
91
    /** @name Constructors and Destructors */
92
    //@{
93
    /**
94
     * Constructor
95
     */
96
    cAsyncModule(const char *name=NULL, cModule *parent=NULL, unsigned stacksize=0);
97
98
    /**
99
     * Destructor
100
     */
101
    virtual ~cAsyncModule();
102
    //}@
103
104
105
    /** @name User-implemented methods for asynchronous event handling. */
106
    //@{
107
    /**
108
     * Returns the duration of the given expanded event. Called by the event
109
     * scheduler to determine the overlapping of expanded events. The user may
110
     * perform any computation on the expanded event (except for deleting it)
111
     * to calculate the event duration.
112
     */
113
    virtual simtime_t getProcessingDelay(cMessage* msg)
114
    {
115
        return 0.0;
116
    }
117
118
    /*
119
     * By default non-expanded events are not executed in parallel. The
120
     * option "parallelize-zero-duration-events" globally enables or disables
121
     * parallel execution. This method allows users to overwrite either behavior
122
     * on a per module (and per event) basis. Should return true if parallel
123
     * execution is allowed, false otherwise.
124
     */
125
    virtual bool mayParallelize(cMessage* msg, simtime_t duration) const
126
    {
127
        return duration == SimTime::simTimeZero ? parZeroDur : true;
128
    }
129
    //}@
130
131
132
    /** @name Support methods for asynchronous event handling. */
133
    //@{
134
    /**
135
     * Returns the duration of the currently processed event.
136
     */
137
    simtime_t getCurrentProcessingDelay() const {
138
        return t_end - simTime();
139
    }
140
141
    /*
142
     * Returns true if the module supports parallel event execution.
143
     */
144
    virtual bool isAsyncModule() const {
145
            return true;
146
    }
147
    //}@
148
149
150
    /** @name Wrapper functions for state keeping inside a module. */
151
    //@{
152
    /**
153
     * INTERNAL: Wrapper for asynchronous message handling.
154
     */
155
    void callHandleAsyncMessage(cMessage* msg);
156
157
    /**
158
     * INTERNAL: Wrapper for synchronous message handling.
159
     */
160
    void callHandleMessage(cMessage* msg);
161
162
    /*
163
     * INTERNAL: Check if a worker thread is already busy inside this module
164
     * and if so, wait until the worker is done.
165
     */
166
    inline void waitIfBusy() {
167
        while (AO_load_full(&busy) == BUSY) {
168
            __asm__("pause");
169
        }
170
    }
171
172
    /*
173
     * INTERNAL: Indicate that a worker is busy inside this module.
174
     */
175
        inline void setBusy() {
176
                AO_store_full(&busy, BUSY);
177
        }
178
179
        /**
180
         * INTERNAL: Unset flag to indicate that a worker has finished.
181
         */
182
        inline void unsetBusy() {
183
                AO_store_full(&busy, NOT_BUSY);
184
        }
185
    //}@r
186
187
188
    /** @name Wrapper functions for the OMNeT++ API */
189
    //@{
190
    /**
191
     * @see cSimpleModule
192
     */
193
    virtual int scheduleAt(simtime_t t, cMessage *msg);
194
195
    /**
196
     * @see cSimpleModule
197
     */
198
    virtual int send(cMessage *msg, int gateid);
199
200
    /**
201
     * @see cSimpleModule
202
     */
203
    virtual int send(cMessage *msg, const char *gatename, int sn=-1);
204
205
    /**
206
     * @see cSimpleModule
207
     */
208
    virtual int send(cMessage *msg, cGate *outputgate);
209
210
    /**
211
     * @see cSimpleModule
212
     */
213
    virtual int sendDelayed(cMessage *msg, simtime_t delay, int gateid);
214
215
    /**
216
     * @see cSimpleModule
217
     */
218
    virtual int sendDelayed(cMessage *msg, simtime_t delay, const char *gatename, int sn=-1);
219
220
    /**
221
     * @see cSimpleModule
222
     */
223
    virtual int sendDelayed(cMessage *msg, simtime_t delay, cGate *outputgate);
224
225
    /**
226
     * @see cSimpleModule
227
     */
228
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cModule *mod, int inputgateid);
229
230
    /**
231
     * @see cSimpleModule
232
     */
233
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cModule *mod, const char *inputgatename, int sn=-1);
234
235
    /**
236
     * @see cSimpleModule
237
     */
238
    virtual int sendDirect(cMessage *msg, simtime_t delay, simtime_t duration, cGate *inputgate);
239
    //}@
240
};
241
242
#endif /* __CASYNCMODULE_H */