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