Statistics
| Branch: | Revision:

root / src / sim / clockeddefaultlist.cc @ e26d3d25

History | View | Annotate | Download (3.73 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  CLOCKEDDEFAULTLIST.CC - part of
3
//
4
//                  Horizon/OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//   Member functions of
8
//    cLockedDefaultList : stores a set of cOwnedObjects in a thread-safe manner
9
//
10
//  Author: Georg Kunz
11
//
12
//=========================================================================
13
14
/*--------------------------------------------------------------*
15
  Copyright (C) 1992-2005 Andras Varga
16
  Copyright (C) 2009      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
#include <stdio.h>           // sprintf
23
#include <string.h>          // memcpy
24
#include <assert.h>
25
#include "regmacros.h"
26
#include "errmsg.h"
27
#include "carray.h"
28
#include "cexception.h"
29
#include "clockeddefaultlist.h"
30
#include "cwatch.h"
31
#include "cthreadpool.h"
32
#include "globals.h"
33
#ifdef WITH_PARSIM
34
#include "ccommbuffer.h"
35
#endif
36
37
USING_NAMESPACE
38 93d0d4d3 Simon Tenbusch
//TODO: HORIZON fix me
39
//Register_Class(cLockedDefaultList);
40 01873262 Georg Kunz
41 93d0d4d3 Simon Tenbusch
template<class T>
42
cLockedDefaultList<T>::cLockedDefaultList(const char *name) :
43 01873262 Georg Kunz
  cDefaultList(name)
44
{
45 93d0d4d3 Simon Tenbusch
    lock = new T();
46 01873262 Georg Kunz
}
47
48 93d0d4d3 Simon Tenbusch
template<class T>
49
cLockedDefaultList<T>::cLockedDefaultList(const cLockedDefaultList& list)
50 01873262 Georg Kunz
{
51
    // should never be called due to exception in cDefaultList(list)
52
    copyNotSupported();
53
}
54
55 93d0d4d3 Simon Tenbusch
template<class T>
56
cLockedDefaultList<T>::~cLockedDefaultList()
57 01873262 Georg Kunz
{
58 93d0d4d3 Simon Tenbusch
    delete lock;
59 01873262 Georg Kunz
}
60
61 93d0d4d3 Simon Tenbusch
template<class T>
62
void cLockedDefaultList<T>::doInsert(cOwnedObject *obj)
63 01873262 Georg Kunz
{
64
    /*
65
    TODO: check if this is needed
66
    if (cStaticFlag::isSet())
67
    {
68
        pthread_mutex_init(&mutex, NULL);
69
    }
70
    */
71 93d0d4d3 Simon Tenbusch
    lock->lock();
72 01873262 Georg Kunz
    cDefaultList::doInsert(obj);
73 93d0d4d3 Simon Tenbusch
    lock->unlock();
74 01873262 Georg Kunz
}
75
76 93d0d4d3 Simon Tenbusch
template<class T>
77
void cLockedDefaultList<T>::ownedObjectDeleted(cOwnedObject *obj)
78 01873262 Georg Kunz
{
79 93d0d4d3 Simon Tenbusch
        lock->lock();
80 01873262 Georg Kunz
    cDefaultList::ownedObjectDeleted(obj);
81 93d0d4d3 Simon Tenbusch
    lock->unlock();
82 01873262 Georg Kunz
}
83
84 93d0d4d3 Simon Tenbusch
template<class T>
85
void cLockedDefaultList<T>::yieldOwnership(cOwnedObject *obj, cObject *newowner)
86 01873262 Georg Kunz
{
87 93d0d4d3 Simon Tenbusch
        lock->lock();
88 01873262 Georg Kunz
    cDefaultList::yieldOwnership(obj, newowner);
89 93d0d4d3 Simon Tenbusch
    lock->unlock();
90 01873262 Georg Kunz
}
91
92 93d0d4d3 Simon Tenbusch
template<class T>
93
void cLockedDefaultList<T>::takeAllObjectsFrom(cLockedDefaultList& other)
94 01873262 Georg Kunz
{
95
    while (other.defaultListItems()>0)
96
        take(other.defaultListGet(0));
97
}
98
99 93d0d4d3 Simon Tenbusch
template<class T>
100
cLockedDefaultList<T>& cLockedDefaultList<T>::operator=(const cLockedDefaultList& list)
101 01873262 Georg Kunz
{
102
    copyNotSupported();
103
    return *this;
104
}
105
106 93d0d4d3 Simon Tenbusch
template<class T>
107
std::string cLockedDefaultList<T>::info()
108 01873262 Georg Kunz
{
109 93d0d4d3 Simon Tenbusch
        lock->lock();
110 01873262 Georg Kunz
    std::string str = cDefaultList::info();
111 93d0d4d3 Simon Tenbusch
    lock->unlock();
112 01873262 Georg Kunz
    return str;
113
}
114
115 93d0d4d3 Simon Tenbusch
template<class T>
116
void cLockedDefaultList<T>::forEachChild(cVisitor *v)
117 01873262 Georg Kunz
{
118 93d0d4d3 Simon Tenbusch
        lock->lock();
119 01873262 Georg Kunz
    cDefaultList::forEachChild(v);
120 93d0d4d3 Simon Tenbusch
    lock->unlock();
121 01873262 Georg Kunz
}
122
123 93d0d4d3 Simon Tenbusch
template<class T>
124
void cLockedDefaultList<T>::parsimPack(cCommBuffer *buffer)
125 01873262 Georg Kunz
{
126 93d0d4d3 Simon Tenbusch
        lock->lock();
127 01873262 Georg Kunz
    cDefaultList::parsimPack(buffer);
128 93d0d4d3 Simon Tenbusch
    lock->unlock();
129 01873262 Georg Kunz
}
130
131 93d0d4d3 Simon Tenbusch
template<class T>
132
void cLockedDefaultList<T>::parsimUnpack(cCommBuffer *buffer)
133 01873262 Georg Kunz
{
134 93d0d4d3 Simon Tenbusch
        lock->lock();
135 01873262 Georg Kunz
    cDefaultList::parsimUnpack(buffer);
136 93d0d4d3 Simon Tenbusch
    lock->unlock();
137 01873262 Georg Kunz
}
138
139 93d0d4d3 Simon Tenbusch
template<class T>
140
void cLockedDefaultList<T>::take(cOwnedObject *obj)
141 01873262 Georg Kunz
{
142 93d0d4d3 Simon Tenbusch
        lock->lock();
143 01873262 Georg Kunz
    cDefaultList::take(obj);
144 93d0d4d3 Simon Tenbusch
    lock->unlock();
145 01873262 Georg Kunz
}
146
147 93d0d4d3 Simon Tenbusch
template<class T>
148
void cLockedDefaultList<T>::drop(cOwnedObject *obj)
149 01873262 Georg Kunz
{
150 93d0d4d3 Simon Tenbusch
        lock->lock();
151 01873262 Georg Kunz
    cDefaultList::drop(obj);
152 93d0d4d3 Simon Tenbusch
    lock->unlock();
153 01873262 Georg Kunz
}
154
155 93d0d4d3 Simon Tenbusch
template<class T>
156
cOwnedObject *cLockedDefaultList<T>::defaultListGet(int k)
157 01873262 Georg Kunz
{
158 93d0d4d3 Simon Tenbusch
        lock->lock();
159 01873262 Georg Kunz
    cOwnedObject* obj = cDefaultList::defaultListGet(k);
160 93d0d4d3 Simon Tenbusch
    lock->unlock();
161 01873262 Georg Kunz
    return obj;
162
}
163
164 93d0d4d3 Simon Tenbusch
template<class T>
165
bool cLockedDefaultList<T>::defaultListContains(cOwnedObject *obj)
166 01873262 Georg Kunz
{
167 93d0d4d3 Simon Tenbusch
        lock->lock();
168 01873262 Georg Kunz
    bool result = cDefaultList::defaultListContains(obj);
169 93d0d4d3 Simon Tenbusch
    lock->unlock();
170 01873262 Georg Kunz
    return result;
171
}