Project

General

Profile

Statistics
| Branch: | Revision:

root / include / clockedmsgheap.h @ aeae20a1

History | View | Annotate | Download (4.88 KB)

1
//==========================================================================
2
//  CLOCKEDMSGHEAP.H - part of
3
//                     Horizon/OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cLockedMessageHeap : thread safe wrapper around cMessageHeap
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 1992-2005 Andras Varga
14
  Copyright (C) 2010 Georg Kunz
15
  Copyright (C) 2011 Simon Tenbusch
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 __CLOCKEDMSGHEAP_H
22
#define __CLOCKEDMSGHEAP_H
23

    
24
#include "cobject.h"
25
#include "clock.h"
26
#include "cpthreadlock.h"
27
class cMessage;
28

    
29

    
30
/**
31
 * Stores the future event set. The underlying data structure is heap;
32
 * the array used to store the heap expands as needed.
33
 *
34
 * @see Iterator
35
 * @ingroup Internals
36
 */
37
template <class T>
38
class SIM_API cLockedMessageHeap : public cMessageHeap
39
{
40
  private:
41
    cLock* lock;      // the lock
42

    
43
  public:
44
    /** @name Constructors, destructor, assignment */
45
    //@{
46

    
47
    /**
48
     * Copy constructor.
49
     */
50
  cLockedMessageHeap(const cLockedMessageHeap& msgq);
51

    
52
    /**
53
     * Constructor.
54
     */
55
  cLockedMessageHeap(const char *name=NULL, int size=128);
56

    
57
    /**
58
     * Destructor.
59
     */
60
    virtual ~cLockedMessageHeap();
61

    
62
    /**
63
     * Assignment operator. The name member doesn't get copied;
64
     * see cObject's operator=() for more details.
65
     */
66
    cLockedMessageHeap& operator=(const cLockedMessageHeap& msgqueue);
67
    //@}
68

    
69
    /** @name Redefined cObject member functions. */
70
    //@{
71

    
72
    /**
73
     * Creates and returns an exact copy of this object.
74
     * See cObject for more details.
75
     */
76
    virtual cObject *dup();
77

    
78
    /**
79
     * Calls v->visit(this) for each contained object.
80
     * See cObject for more details.
81
     */
82
    virtual void forEachChild(cVisitor *v);
83

    
84
    // no netPack() and netUnpack()
85
    //@}
86

    
87
    /** @name Container functions. */
88
    //@{
89

    
90
    /**
91
     * Insert a new message into the heap.
92
     */
93
    void insert(cMessage *event);
94

    
95
    /**
96
     * Removes and return the first message in the heap (the one
97
     * with the smallest timestamp.) If the heap is empty, it returns NULL.
98
     */
99
    cMessage *removeFirst();
100

    
101
    /**
102
     * Removes and returns the given message in the heap. If the message is
103
     * not in the heap, returns NULL.
104
     */
105
    cMessage *remove(cMessage *event);
106

    
107
    /**
108
     * Returns the mth message in the heap if 0 <= m < length(), and NULL
109
     * otherwise. Note that iteration does not necessarily return messages
110
     * in increasing timestamp (arrivalTime()) order unless you called
111
     * sort() before.
112
     */
113
    cMessage *peek(int m);
114

    
115
    /*
116
     * See peek(1)
117
     */
118
    cMessage *peekFirst();
119

    
120
    /**
121
     * Sorts the contents of the heap. This is only necessary if one wants
122
     * to iterate through in the FES in strict timestamp order.
123
     */
124
    void sort();
125

    
126
    /**
127
     * Deletes all messages in the heap.
128
     */
129
    void clear();
130
    //@}
131
};
132

    
133

    
134
template <class T>
135
cLockedMessageHeap<T>::cLockedMessageHeap(const char *name, int siz) :
136
  cMessageHeap(name, siz)
137
{
138
    lock = new T();
139
}
140

    
141
template <class T>
142
cLockedMessageHeap<T>::cLockedMessageHeap(const cLockedMessageHeap& heap) :
143
  cMessageHeap(heap)
144
{
145
        lock = new T();
146
}
147

    
148
template <class T>
149
cLockedMessageHeap<T>::~cLockedMessageHeap<T>()
150
{
151
    delete lock;
152
}
153

    
154
template <class T>
155
cObject* cLockedMessageHeap<T>::dup()
156
{
157
    lock->lock();
158
    cObject* p = cMessageHeap::dup();
159
    lock->unlock();
160
    return p;
161
}
162

    
163
template <class T>
164
void cLockedMessageHeap<T>::forEachChild(cVisitor *v)
165
{
166
        lock->lock();
167
    cMessageHeap::forEachChild(v);
168
    lock->unlock();
169
}
170

    
171
template <class T>
172
void cLockedMessageHeap<T>::clear()
173
{
174
        lock->lock();
175
    cMessageHeap::clear();
176
    lock->unlock();
177
}
178

    
179
template <class T>
180
cMessage *cLockedMessageHeap<T>::peek(int m)
181
{
182
        lock->lock();
183
    cMessage* msg = cMessageHeap::peek(m);
184
    lock->unlock();
185
    return msg;
186
}
187

    
188
template <class T>
189
cMessage *cLockedMessageHeap<T>::peekFirst()
190
{
191
        lock->lock();
192
    cMessage* msg = cMessageHeap::peekFirst();
193
    lock->unlock();
194
    return msg;
195
}
196

    
197
template <class T>
198
void cLockedMessageHeap<T>::sort()
199
{
200
        lock->lock();
201
    cMessageHeap::sort();
202
    lock->unlock();
203
}
204

    
205
template <class T>
206
void cLockedMessageHeap<T>::insert(cMessage *event)
207
{
208
        lock->lock();
209
    cMessageHeap::insert(event);
210
    lock->unlock();
211
}
212

    
213
template <class T>
214
cMessage *cLockedMessageHeap<T>::removeFirst()
215
{
216
        lock->lock();
217
    cMessage* msg = cMessageHeap::removeFirst();
218
    lock->unlock();
219
    return msg;
220
}
221

    
222
template <class T>
223
cMessage *cLockedMessageHeap<T>::remove(cMessage *event)
224
{
225
        lock->lock();
226
    cMessage* msg = cMessageHeap::remove(event);
227
    lock->unlock();
228
    return msg;
229
}
230
#endif
231