Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/foxtools/MFXSynchQue.h
193716 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2004-2026 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
/****************************************************************************/
14
/// @file MFXSynchQue.h
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @date 2004-03-19
18
///
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
};
208
209