Eclipse SUMO - Simulation of Urban MObility
FXSynchSet.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2004-2022 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 #ifndef FXSynchSet_h
21 #define FXSynchSet_h
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 "FXWorkerThread.h"
36 #endif
37 
38 template<class T, class Container = std::set<T> >
39 class FXSynchSet {
40 public:
41  FXSynchSet(const bool condition = true):
42 #ifdef HAVE_FOX
43  myMutex(true),
44 #endif
45  myCondition(condition)
46  {}
47 
48  // Attention! Removes locking behavior
49  void unsetCondition() {
50  myCondition = false;
51  }
52 
53  // Attention! Retains the lock
54  Container& getContainer() {
55 #ifdef HAVE_FOX
56  if (myCondition) {
57  myMutex.lock();
58  }
59 #endif
60 #ifdef DEBUG_LOCKING
61  if (debugflag) {
62  std::cout << " FXSynchSet::getContainer thread=" << FXWorkerThread::current() << "\n";
63  }
64  myOwningThread = FXWorkerThread::current();
65 #endif
66  return myItems;
67  }
68 
69  void unlock() {
70 #ifdef HAVE_FOX
71  if (myCondition) {
72  myMutex.unlock();
73  }
74 #endif
75 #ifdef DEBUG_LOCKING
76  if (debugflag) {
77  std::cout << " FXSynchSet::unlock thread=" << FXWorkerThread::current() << "\n";
78  }
79  myOwningThread = 0;
80 #endif
81  }
82 
83  void insert(T what) {
84 #ifdef HAVE_FOX
85  if (myCondition) {
86  myMutex.lock();
87  }
88 #endif
89  myItems.insert(what);
90 #ifdef HAVE_FOX
91  if (myCondition) {
92  myMutex.unlock();
93  }
94 #endif
95  }
96 
97  bool empty() {
98 #ifdef HAVE_FOX
99  if (myCondition) {
100  myMutex.lock();
101  }
102 #endif
103  const bool ret = myItems.size() == 0;
104 #ifdef HAVE_FOX
105  if (myCondition) {
106  myMutex.unlock();
107  }
108 #endif
109  return ret;
110  }
111 
112  void clear() {
113 #ifdef HAVE_FOX
114  if (myCondition) {
115  myMutex.lock();
116  }
117 #endif
118  myItems.clear();
119 #ifdef HAVE_FOX
120  if (myCondition) {
121  myMutex.unlock();
122  }
123 #endif
124  }
125 
126  size_t size() const {
127 #ifdef HAVE_FOX
128  if (myCondition) {
129  myMutex.lock();
130  }
131 #endif
132  size_t res = myItems.size();
133 #ifdef HAVE_FOX
134  if (myCondition) {
135  myMutex.unlock();
136  }
137 #endif
138  return res;
139  }
140 
141  bool contains(const T& item) const {
142 #ifdef HAVE_FOX
143  if (myCondition) {
144  myMutex.lock();
145  }
146 #endif
147  bool res = std::find(myItems.begin(), myItems.end(), item) != myItems.end();
148 #ifdef HAVE_FOX
149  if (myCondition) {
150  myMutex.unlock();
151  }
152 #endif
153  return res;
154  }
155 
156  bool isLocked() const {
157 #ifdef HAVE_FOX
158  return myMutex.locked();
159 #else
160  return false;
161 #endif
162  }
163 
164 private:
165 #ifdef HAVE_FOX
166  mutable FXMutex myMutex;
167 #endif
168  Container myItems;
170 
171 #ifdef DEBUG_LOCKING
172  mutable long long int myOwningThread = 0;
173 public:
174  mutable bool debugflag = false;
175 #endif
176 
177 };
178 
179 
180 #endif
Container & getContainer()
Definition: FXSynchSet.h:54
void unsetCondition()
Definition: FXSynchSet.h:49
Container myItems
Definition: FXSynchSet.h:168
void insert(T what)
Definition: FXSynchSet.h:83
bool empty()
Definition: FXSynchSet.h:97
FXSynchSet(const bool condition=true)
Definition: FXSynchSet.h:41
bool contains(const T &item) const
Definition: FXSynchSet.h:141
void unlock()
Definition: FXSynchSet.h:69
size_t size() const
Definition: FXSynchSet.h:126
void clear()
Definition: FXSynchSet.h:112
bool myCondition
Definition: FXSynchSet.h:169
bool isLocked() const
Definition: FXSynchSet.h:156