Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MFXSynchSet.h
Go to the documentation of this file.
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2004-2024 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
18
// missing_desc
19
/****************************************************************************/
20
#pragma once
21
#include <config.h>
22
23
#ifdef HAVE_FOX
24
#include "
fxheader.h
"
25
#endif
26
#include <list>
27
#include <cassert>
28
#include <algorithm>
29
30
//#define DEBUG_LOCKING
31
32
#ifdef DEBUG_LOCKING
33
#include <iostream>
34
#include "
MFXWorkerThread.h
"
35
#endif
36
37
template
<
class
T,
class
Container = std::set<T> >
38
class
MFXSynchSet
{
39
public
:
40
MFXSynchSet
(
const
bool
condition =
true
):
41
#ifdef HAVE_FOX
42
myMutex(true),
43
#endif
44
myCondition
(condition)
45
{}
46
47
// Attention! Removes locking behavior
48
void
unsetCondition
() {
49
myCondition
=
false
;
50
}
51
52
// Attention! Retains the lock
53
Container&
getContainer
() {
54
#ifdef HAVE_FOX
55
if
(
myCondition
) {
56
myMutex.lock();
57
}
58
#endif
59
#ifdef DEBUG_LOCKING
60
if
(debugflag) {
61
std::cout <<
" MFXSynchSet::getContainer thread="
<< MFXWorkerThread::current() <<
"\n"
;
62
}
63
myOwningThread = MFXWorkerThread::current();
64
#endif
65
return
myItems
;
66
}
67
68
void
unlock
() {
69
#ifdef HAVE_FOX
70
if
(
myCondition
) {
71
myMutex.unlock();
72
}
73
#endif
74
#ifdef DEBUG_LOCKING
75
if
(debugflag) {
76
std::cout <<
" MFXSynchSet::unlock thread="
<< MFXWorkerThread::current() <<
"\n"
;
77
}
78
myOwningThread = 0;
79
#endif
80
}
81
82
void
insert
(T what) {
83
#ifdef HAVE_FOX
84
if
(
myCondition
) {
85
myMutex.lock();
86
}
87
#endif
88
myItems
.insert(what);
89
#ifdef HAVE_FOX
90
if
(
myCondition
) {
91
myMutex.unlock();
92
}
93
#endif
94
}
95
96
void
erase
(T what) {
97
#ifdef HAVE_FOX
98
if
(
myCondition
) {
99
myMutex.lock();
100
}
101
#endif
102
myItems
.erase(what);
103
#ifdef HAVE_FOX
104
if
(
myCondition
) {
105
myMutex.unlock();
106
}
107
#endif
108
}
109
110
size_t
count
(T what) {
111
#ifdef HAVE_FOX
112
if
(
myCondition
) {
113
myMutex.lock();
114
}
115
#endif
116
size_t
result =
myItems
.count(what);
117
#ifdef HAVE_FOX
118
if
(
myCondition
) {
119
myMutex.unlock();
120
}
121
#endif
122
return
result;
123
}
124
125
bool
empty
() {
126
#ifdef HAVE_FOX
127
if
(
myCondition
) {
128
myMutex.lock();
129
}
130
#endif
131
const
bool
ret =
myItems
.size() == 0;
132
#ifdef HAVE_FOX
133
if
(
myCondition
) {
134
myMutex.unlock();
135
}
136
#endif
137
return
ret;
138
}
139
140
void
clear
() {
141
#ifdef HAVE_FOX
142
if
(
myCondition
) {
143
myMutex.lock();
144
}
145
#endif
146
myItems
.clear();
147
#ifdef HAVE_FOX
148
if
(
myCondition
) {
149
myMutex.unlock();
150
}
151
#endif
152
}
153
154
size_t
size
()
const
{
155
#ifdef HAVE_FOX
156
if
(
myCondition
) {
157
myMutex.lock();
158
}
159
#endif
160
size_t
res =
myItems
.size();
161
#ifdef HAVE_FOX
162
if
(
myCondition
) {
163
myMutex.unlock();
164
}
165
#endif
166
return
res;
167
}
168
169
bool
contains
(
const
T& item)
const
{
170
#ifdef HAVE_FOX
171
if
(
myCondition
) {
172
myMutex.lock();
173
}
174
#endif
175
bool
res = std::find(
myItems
.begin(),
myItems
.end(), item) !=
myItems
.end();
176
#ifdef HAVE_FOX
177
if
(
myCondition
) {
178
myMutex.unlock();
179
}
180
#endif
181
return
res;
182
}
183
184
bool
isLocked
()
const
{
185
#ifdef HAVE_FOX
186
return
myMutex.locked();
187
#else
188
return
false
;
189
#endif
190
}
191
192
private
:
193
#ifdef HAVE_FOX
194
mutable
FXMutex myMutex;
195
#endif
196
Container
myItems
;
197
bool
myCondition
;
198
199
#ifdef DEBUG_LOCKING
200
mutable
long
long
int
myOwningThread = 0;
201
public
:
202
mutable
bool
debugflag =
false
;
203
#endif
204
205
};
MFXWorkerThread.h
MFXSynchSet
Definition
MFXSynchSet.h:38
MFXSynchSet::unlock
void unlock()
Definition
MFXSynchSet.h:68
MFXSynchSet::size
size_t size() const
Definition
MFXSynchSet.h:154
MFXSynchSet::myCondition
bool myCondition
Definition
MFXSynchSet.h:197
MFXSynchSet::MFXSynchSet
MFXSynchSet(const bool condition=true)
Definition
MFXSynchSet.h:40
MFXSynchSet::isLocked
bool isLocked() const
Definition
MFXSynchSet.h:184
MFXSynchSet::contains
bool contains(const T &item) const
Definition
MFXSynchSet.h:169
MFXSynchSet::myItems
Container myItems
Definition
MFXSynchSet.h:196
MFXSynchSet::clear
void clear()
Definition
MFXSynchSet.h:140
MFXSynchSet::count
size_t count(T what)
Definition
MFXSynchSet.h:110
MFXSynchSet::insert
void insert(T what)
Definition
MFXSynchSet.h:82
MFXSynchSet::getContainer
Container & getContainer()
Definition
MFXSynchSet.h:53
MFXSynchSet::unsetCondition
void unsetCondition()
Definition
MFXSynchSet.h:48
MFXSynchSet::erase
void erase(T what)
Definition
MFXSynchSet.h:96
MFXSynchSet::empty
bool empty()
Definition
MFXSynchSet.h:125
fxheader.h
src
utils
foxtools
MFXSynchSet.h
Generated on Sat Nov 9 2024 00:10:31 for Eclipse SUMO - Simulation of Urban MObility by
1.9.8