Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MFXSynchQue.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
/****************************************************************************/
19
// missing_desc
20
/****************************************************************************/
21
#pragma once
22
#include <config.h>
23
24
#ifdef HAVE_FOX
25
#include "
fxheader.h
"
26
#endif
27
#include <list>
28
#include <cassert>
29
#include <algorithm>
30
31
//#define DEBUG_LOCKING
32
33
#ifdef DEBUG_LOCKING
34
#include <iostream>
35
#include "
MFXWorkerThread.h
"
36
#endif
37
38
template
<
class
T,
class
Container = std::list<T> >
39
class
MFXSynchQue
{
40
public
:
41
MFXSynchQue
(
const
bool
condition =
true
):
42
#ifdef HAVE_FOX
43
myMutex(true),
44
#endif
45
myCondition
(condition)
46
{}
47
48
T
top
() {
49
assert(
myItems
.size() != 0);
50
#ifdef HAVE_FOX
51
if
(
myCondition
) {
52
myMutex.lock();
53
}
54
#endif
55
T ret =
myItems
.front();
56
#ifdef HAVE_FOX
57
if
(
myCondition
) {
58
myMutex.unlock();
59
}
60
#endif
61
return
ret;
62
}
63
64
void
pop
() {
65
#ifdef HAVE_FOX
66
if
(
myCondition
) {
67
myMutex.lock();
68
}
69
#endif
70
myItems
.erase(
myItems
.begin());
71
#ifdef HAVE_FOX
72
if
(
myCondition
) {
73
myMutex.unlock();
74
}
75
#endif
76
}
77
78
// Attention! Removes locking behavior
79
void
unsetCondition
() {
80
myCondition
=
false
;
81
}
82
83
// Attention! Retains the lock
84
Container&
getContainer
() {
85
#ifdef HAVE_FOX
86
if
(
myCondition
) {
87
myMutex.lock();
88
}
89
#endif
90
#ifdef DEBUG_LOCKING
91
if
(debugflag) {
92
std::cout <<
" MFXSynchQue::getContainer thread="
<< MFXWorkerThread::current() <<
"\n"
;
93
}
94
myOwningThread = MFXWorkerThread::current();
95
#endif
96
return
myItems
;
97
}
98
99
void
unlock
() {
100
#ifdef HAVE_FOX
101
if
(
myCondition
) {
102
myMutex.unlock();
103
}
104
#endif
105
#ifdef DEBUG_LOCKING
106
if
(debugflag) {
107
std::cout <<
" MFXSynchQue::unlock thread="
<< MFXWorkerThread::current() <<
"\n"
;
108
}
109
myOwningThread = 0;
110
#endif
111
}
112
113
void
push_back
(T what) {
114
#ifdef HAVE_FOX
115
if
(
myCondition
) {
116
myMutex.lock();
117
}
118
#endif
119
myItems
.push_back(what);
120
#ifdef HAVE_FOX
121
if
(
myCondition
) {
122
myMutex.unlock();
123
}
124
#endif
125
}
126
127
bool
empty
() {
128
#ifdef HAVE_FOX
129
if
(
myCondition
) {
130
myMutex.lock();
131
}
132
#endif
133
const
bool
ret =
myItems
.size() == 0;
134
#ifdef HAVE_FOX
135
if
(
myCondition
) {
136
myMutex.unlock();
137
}
138
#endif
139
return
ret;
140
}
141
142
void
clear
() {
143
#ifdef HAVE_FOX
144
if
(
myCondition
) {
145
myMutex.lock();
146
}
147
#endif
148
myItems
.clear();
149
#ifdef HAVE_FOX
150
if
(
myCondition
) {
151
myMutex.unlock();
152
}
153
#endif
154
}
155
156
size_t
size
()
const
{
157
#ifdef HAVE_FOX
158
if
(
myCondition
) {
159
myMutex.lock();
160
}
161
#endif
162
size_t
res =
myItems
.size();
163
#ifdef HAVE_FOX
164
if
(
myCondition
) {
165
myMutex.unlock();
166
}
167
#endif
168
return
res;
169
}
170
171
bool
contains
(
const
T& item)
const
{
172
#ifdef HAVE_FOX
173
if
(
myCondition
) {
174
myMutex.lock();
175
}
176
#endif
177
bool
res = std::find(
myItems
.begin(),
myItems
.end(), item) !=
myItems
.end();
178
#ifdef HAVE_FOX
179
if
(
myCondition
) {
180
myMutex.unlock();
181
}
182
#endif
183
return
res;
184
}
185
186
bool
isLocked
()
const
{
187
#ifdef HAVE_FOX
188
return
myMutex.locked();
189
#else
190
return
false
;
191
#endif
192
}
193
194
private
:
195
#ifdef HAVE_FOX
196
mutable
FXMutex myMutex;
197
#endif
198
Container
myItems
;
199
bool
myCondition
;
200
201
#ifdef DEBUG_LOCKING
202
mutable
long
long
int
myOwningThread = 0;
203
public
:
204
mutable
bool
debugflag =
false
;
205
#endif
206
207
};
MFXWorkerThread.h
MFXSynchQue
Definition
MFXSynchQue.h:39
MFXSynchQue::getContainer
Container & getContainer()
Definition
MFXSynchQue.h:84
MFXSynchQue::myCondition
bool myCondition
Definition
MFXSynchQue.h:199
MFXSynchQue::MFXSynchQue
MFXSynchQue(const bool condition=true)
Definition
MFXSynchQue.h:41
MFXSynchQue::myItems
Container myItems
Definition
MFXSynchQue.h:198
MFXSynchQue::pop
void pop()
Definition
MFXSynchQue.h:64
MFXSynchQue::unlock
void unlock()
Definition
MFXSynchQue.h:99
MFXSynchQue::empty
bool empty()
Definition
MFXSynchQue.h:127
MFXSynchQue::unsetCondition
void unsetCondition()
Definition
MFXSynchQue.h:79
MFXSynchQue::clear
void clear()
Definition
MFXSynchQue.h:142
MFXSynchQue::contains
bool contains(const T &item) const
Definition
MFXSynchQue.h:171
MFXSynchQue::push_back
void push_back(T what)
Definition
MFXSynchQue.h:113
MFXSynchQue::size
size_t size() const
Definition
MFXSynchQue.h:156
MFXSynchQue::isLocked
bool isLocked() const
Definition
MFXSynchQue.h:186
MFXSynchQue::top
T top()
Definition
MFXSynchQue.h:48
fxheader.h
src
utils
foxtools
MFXSynchQue.h
Generated on Sun Dec 22 2024 00:15:39 for Eclipse SUMO - Simulation of Urban MObility by
1.9.8