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