Project

General

Profile

Statistics
| Branch: | Revision:

root / include / clockedmsgheap.h @ fbe00e73

History | View | Annotate | Download (4.2 KB)

1 01873262 Georg Kunz
//==========================================================================
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 93d0d4d3 Simon Tenbusch
  Copyright (C) 2011 Simon Tenbusch
16 b781545c Georg Kunz
  Copyright (C) 2012 Georg Kunz
17 01873262 Georg Kunz

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 93d0d4d3 Simon Tenbusch
#include "cpthreadlock.h"
27 01873262 Georg Kunz
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 93d0d4d3 Simon Tenbusch
template <class T>
38 01873262 Georg Kunz
class SIM_API cLockedMessageHeap : public cMessageHeap
39
{
40
  private:
41 b781545c Georg Kunz
    T lock;      // the lock
42 01873262 Georg Kunz
43
  public:
44
    /** @name Constructors, destructor, assignment */
45
    //@{
46
47
    /**
48
     * Constructor.
49
     */
50 b781545c Georg Kunz
    cLockedMessageHeap(const char *name=NULL, int size=128) :
51
      cMessageHeap(name, size),
52
      lock()
53
    {
54
    }
55 01873262 Georg Kunz
56
    /**
57 b781545c Georg Kunz
     * Copy constructor.
58 01873262 Georg Kunz
     */
59 b781545c Georg Kunz
    cLockedMessageHeap(const cLockedMessageHeap& heap) :
60
      cMessageHeap(heap)
61
    {
62
    }
63 01873262 Georg Kunz
64
    /**
65
     * Assignment operator. The name member doesn't get copied;
66
     * see cObject's operator=() for more details.
67
     */
68 b781545c Georg Kunz
    //cLockedMessageHeap& operator=(const cLockedMessageHeap& msgqueue);
69 01873262 Georg Kunz
    //@}
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 b781545c Georg Kunz
    virtual cObject *dup()
79
    {
80
        lock.lock();
81
        cObject* p = cMessageHeap::dup();
82
        lock.unlock();
83
        return p;
84
    }
85 01873262 Georg Kunz
86
    /**
87
     * Calls v->visit(this) for each contained object.
88
     * See cObject for more details.
89
     */
90 b781545c Georg Kunz
    inline void forEachChild(cVisitor *v)
91
    {
92
            lock.lock();
93
        cMessageHeap::forEachChild(v);
94
        lock.unlock();
95
    }
96 01873262 Georg Kunz
97
    // no netPack() and netUnpack()
98
    //@}
99
100
    /** @name Container functions. */
101
    //@{
102
103
    /**
104
     * Insert a new message into the heap.
105
     */
106 b781545c Georg Kunz
    inline void insert(cMessage *event)
107
    {
108
            lock.lock();
109
        cMessageHeap::insert(event);
110
        lock.unlock();
111
    }
112 01873262 Georg Kunz
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 b781545c Georg Kunz
    inline cMessage *removeFirst()
118
    {
119
            lock.lock();
120
        cMessage* msg = cMessageHeap::removeFirst();
121
        lock.unlock();
122
        return msg;
123
    }
124 01873262 Georg Kunz
125
    /**
126
     * Removes and returns the given message in the heap. If the message is
127
     * not in the heap, returns NULL.
128
     */
129 b781545c Georg Kunz
    inline cMessage *remove(cMessage *event)
130
    {
131
            lock.lock();
132
        cMessage* msg = cMessageHeap::remove(event);
133
        lock.unlock();
134
        return msg;
135
    }
136 01873262 Georg Kunz
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 b781545c Georg Kunz
    inline cMessage *peek(int m)
144
    {
145
            lock.lock();
146
        cMessage* msg = cMessageHeap::peek(m);
147
        lock.unlock();
148
        return msg;
149
    }
150 01873262 Georg Kunz
151
    /*
152
     * See peek(1)
153
     */
154 b781545c Georg Kunz
    inline cMessage *peekFirst()
155
    {
156
            lock.lock();
157
        cMessage* msg = cMessageHeap::peekFirst();
158
        lock.unlock();
159
        return msg;
160
    }
161 01873262 Georg Kunz
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 b781545c Georg Kunz
    inline void sort()
167
    {
168
            lock.lock();
169
        cMessageHeap::sort();
170
        lock.unlock();
171
    }
172 01873262 Georg Kunz
173
    /**
174
     * Deletes all messages in the heap.
175
     */
176 b781545c Georg Kunz
    inline void clear()
177
    {
178
            lock.lock();
179
        cMessageHeap::clear();
180
        lock.unlock();
181
    }
182 01873262 Georg Kunz
    //@}
183
};
184
185
#endif