Statistics
| Branch: | Revision:

root / include / clockedmsgheap.h @ b781545c

History | View | Annotate | Download (4.2 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
  Copyright (C) 2012 Georg Kunz
17

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

    
22
#ifndef __CLOCKEDMSGHEAP_H
23
#define __CLOCKEDMSGHEAP_H
24

    
25
#include "cobject.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
    T lock;      // the lock
42

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

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

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

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

    
71
    /** @name Redefined cObject member functions. */
72
    //@{
73

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

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

    
97
    // no netPack() and netUnpack()
98
    //@}
99

    
100
    /** @name Container functions. */
101
    //@{
102

    
103
    /**
104
     * Insert a new message into the heap.
105
     */
106
    inline void insert(cMessage *event)
107
    {
108
            lock.lock();
109
        cMessageHeap::insert(event);
110
        lock.unlock();
111
    }
112

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

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

    
137
    /**
138
     * Returns the mth message in the heap if 0 <= m < length(), and NULL
139
     * otherwise. Note that iteration does not necessarily return messages
140
     * in increasing timestamp (arrivalTime()) order unless you called
141
     * sort() before.
142
     */
143
    inline cMessage *peek(int m)
144
    {
145
            lock.lock();
146
        cMessage* msg = cMessageHeap::peek(m);
147
        lock.unlock();
148
        return msg;
149
    }
150

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

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

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

    
185
#endif
186