Revision b781545c include/clockedmsgheap.h

View differences:

include/clockedmsgheap.h
13 13
  Copyright (C) 1992-2005 Andras Varga
14 14
  Copyright (C) 2010 Georg Kunz
15 15
  Copyright (C) 2011 Simon Tenbusch
16
  Copyright (C) 2012 Georg Kunz
16 17

  
17 18
  This file is distributed WITHOUT ANY WARRANTY. See the file
18 19
  `license' for details on this and other legal matters.
......
22 23
#define __CLOCKEDMSGHEAP_H
23 24

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

  
......
38 38
class SIM_API cLockedMessageHeap : public cMessageHeap
39 39
{
40 40
  private:
41
    cLock* lock;      // the lock
41
    T lock;      // the lock
42 42

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

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

  
52
    /**
53 48
     * Constructor.
54 49
     */
55
  cLockedMessageHeap(const char *name=NULL, int size=128);
50
    cLockedMessageHeap(const char *name=NULL, int size=128) :
51
      cMessageHeap(name, size),
52
      lock()
53
    {
54
    }
56 55

  
57 56
    /**
58
     * Destructor.
57
     * Copy constructor.
59 58
     */
60
    virtual ~cLockedMessageHeap();
59
    cLockedMessageHeap(const cLockedMessageHeap& heap) :
60
      cMessageHeap(heap)
61
    {
62
    }
61 63

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

  
69 71
    /** @name Redefined cObject member functions. */
......
73 75
     * Creates and returns an exact copy of this object.
74 76
     * See cObject for more details.
75 77
     */
76
    virtual cObject *dup();
78
    virtual cObject *dup()
79
    {
80
        lock.lock();
81
        cObject* p = cMessageHeap::dup();
82
        lock.unlock();
83
        return p;
84
    }
77 85

  
78 86
    /**
79 87
     * Calls v->visit(this) for each contained object.
80 88
     * See cObject for more details.
81 89
     */
82
    virtual void forEachChild(cVisitor *v);
90
    inline void forEachChild(cVisitor *v)
91
    {
92
    	lock.lock();
93
        cMessageHeap::forEachChild(v);
94
        lock.unlock();
95
    }
83 96

  
84 97
    // no netPack() and netUnpack()
85 98
    //@}
......
90 103
    /**
91 104
     * Insert a new message into the heap.
92 105
     */
93
    void insert(cMessage *event);
106
    inline void insert(cMessage *event)
107
    {
108
    	lock.lock();
109
        cMessageHeap::insert(event);
110
        lock.unlock();
111
    }
94 112

  
95 113
    /**
96 114
     * Removes and return the first message in the heap (the one
97 115
     * with the smallest timestamp.) If the heap is empty, it returns NULL.
98 116
     */
99
    cMessage *removeFirst();
117
    inline cMessage *removeFirst()
118
    {
119
    	lock.lock();
120
        cMessage* msg = cMessageHeap::removeFirst();
121
        lock.unlock();
122
        return msg;
123
    }
100 124

  
101 125
    /**
102 126
     * Removes and returns the given message in the heap. If the message is
103 127
     * not in the heap, returns NULL.
104 128
     */
105
    cMessage *remove(cMessage *event);
129
    inline cMessage *remove(cMessage *event)
130
    {
131
    	lock.lock();
132
        cMessage* msg = cMessageHeap::remove(event);
133
        lock.unlock();
134
        return msg;
135
    }
106 136

  
107 137
    /**
108 138
     * Returns the mth message in the heap if 0 <= m < length(), and NULL
......
110 140
     * in increasing timestamp (arrivalTime()) order unless you called
111 141
     * sort() before.
112 142
     */
113
    cMessage *peek(int m);
143
    inline cMessage *peek(int m)
144
    {
145
    	lock.lock();
146
        cMessage* msg = cMessageHeap::peek(m);
147
        lock.unlock();
148
        return msg;
149
    }
114 150

  
115 151
    /*
116 152
     * See peek(1)
117 153
     */
118
    cMessage *peekFirst();
154
    inline cMessage *peekFirst()
155
    {
156
    	lock.lock();
157
        cMessage* msg = cMessageHeap::peekFirst();
158
        lock.unlock();
159
        return msg;
160
    }
119 161

  
120 162
    /**
121 163
     * Sorts the contents of the heap. This is only necessary if one wants
122 164
     * to iterate through in the FES in strict timestamp order.
123 165
     */
124
    void sort();
166
    inline void sort()
167
    {
168
    	lock.lock();
169
        cMessageHeap::sort();
170
        lock.unlock();
171
    }
125 172

  
126 173
    /**
127 174
     * Deletes all messages in the heap.
128 175
     */
129
    void clear();
176
    inline void clear()
177
    {
178
    	lock.lock();
179
        cMessageHeap::clear();
180
        lock.unlock();
181
    }
130 182
    //@}
131 183
};
132 184

  
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 185
#endif
231 186

  

Also available in: Unified diff