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-2023 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
37template<class T, class Container = std::set<T> >
39public:
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
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 bool empty() {
97#ifdef HAVE_FOX
98 if (myCondition) {
99 myMutex.lock();
100 }
101#endif
102 const bool ret = myItems.size() == 0;
103#ifdef HAVE_FOX
104 if (myCondition) {
105 myMutex.unlock();
106 }
107#endif
108 return ret;
109 }
110
111 void clear() {
112#ifdef HAVE_FOX
113 if (myCondition) {
114 myMutex.lock();
115 }
116#endif
117 myItems.clear();
118#ifdef HAVE_FOX
119 if (myCondition) {
120 myMutex.unlock();
121 }
122#endif
123 }
124
125 size_t size() const {
126#ifdef HAVE_FOX
127 if (myCondition) {
128 myMutex.lock();
129 }
130#endif
131 size_t res = myItems.size();
132#ifdef HAVE_FOX
133 if (myCondition) {
134 myMutex.unlock();
135 }
136#endif
137 return res;
138 }
139
140 bool contains(const T& item) const {
141#ifdef HAVE_FOX
142 if (myCondition) {
143 myMutex.lock();
144 }
145#endif
146 bool res = std::find(myItems.begin(), myItems.end(), item) != myItems.end();
147#ifdef HAVE_FOX
148 if (myCondition) {
149 myMutex.unlock();
150 }
151#endif
152 return res;
153 }
154
155 bool isLocked() const {
156#ifdef HAVE_FOX
157 return myMutex.locked();
158#else
159 return false;
160#endif
161 }
162
163private:
164#ifdef HAVE_FOX
165 mutable FXMutex myMutex;
166#endif
167 Container myItems;
169
170#ifdef DEBUG_LOCKING
171 mutable long long int myOwningThread = 0;
172public:
173 mutable bool debugflag = false;
174#endif
175
176};
void unlock()
Definition MFXSynchSet.h:68
size_t size() const
MFXSynchSet(const bool condition=true)
Definition MFXSynchSet.h:40
bool isLocked() const
bool contains(const T &item) const
Container myItems
void insert(T what)
Definition MFXSynchSet.h:82
Container & getContainer()
Definition MFXSynchSet.h:53
void unsetCondition()
Definition MFXSynchSet.h:48
bool empty()
Definition MFXSynchSet.h:96