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 /****************************************************************************/
16 // missing_desc
17 /****************************************************************************/
18 #ifndef FXSynchQue_h
19 #define FXSynchQue_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #ifdef HAVE_FOX
28 #include <fx.h>
29 #endif
30 #include <list>
31 #include <cassert>
32 
33 template<class T, class Container = std::list<T> >
34 class FXSynchQue {
35 public:
36  FXSynchQue(const bool condition = true): myCondition(condition) {}
37 
38  T top() {
39  assert(myItems.size() != 0);
40 #ifdef HAVE_FOX
41  if (myCondition) {
42  myMutex.lock();
43  }
44 #endif
45  T ret = myItems.front();
46 #ifdef HAVE_FOX
47  if (myCondition) {
48  myMutex.unlock();
49  }
50 #endif
51  return ret;
52  }
53 
54  void pop() {
55 #ifdef HAVE_FOX
56  if (myCondition) {
57  myMutex.lock();
58  }
59 #endif
60  myItems.erase(myItems.begin());
61 #ifdef HAVE_FOX
62  if (myCondition) {
63  myMutex.unlock();
64  }
65 #endif
66  }
67 
68  // Attention! Removes locking behavior
69  void unsetCondition() {
70  myCondition = false;
71  }
72 
73  // Attention! Retains the lock
74  Container& getContainer() {
75 #ifdef HAVE_FOX
76  if (myCondition) {
77  myMutex.lock();
78  }
79 #endif
80  return myItems;
81  }
82 
83  void unlock() {
84 #ifdef HAVE_FOX
85  if (myCondition) {
86  myMutex.unlock();
87  }
88 #endif
89  }
90 
91  void push_back(T what) {
92 #ifdef HAVE_FOX
93  if (myCondition) {
94  myMutex.lock();
95  }
96 #endif
97  myItems.push_back(what);
98 #ifdef HAVE_FOX
99  if (myCondition) {
100  myMutex.unlock();
101  }
102 #endif
103  }
104 
105  bool empty() {
106 #ifdef HAVE_FOX
107  if (myCondition) {
108  myMutex.lock();
109  }
110 #endif
111  const bool ret = myItems.size() == 0;
112 #ifdef HAVE_FOX
113  if (myCondition) {
114  myMutex.unlock();
115  }
116 #endif
117  return ret;
118  }
119 
120  void clear() {
121 #ifdef HAVE_FOX
122  if (myCondition) {
123  myMutex.lock();
124  }
125 #endif
126  myItems.clear();
127 #ifdef HAVE_FOX
128  if (myCondition) {
129  myMutex.unlock();
130  }
131 #endif
132  }
133 
134  size_t size() const {
135 #ifdef HAVE_FOX
136  if (myCondition) {
137  myMutex.lock();
138  }
139 #endif
140  size_t res = myItems.size();
141 #ifdef HAVE_FOX
142  if (myCondition) {
143  myMutex.unlock();
144  }
145 #endif
146  return res;
147  }
148 
149 private:
150 #ifdef HAVE_FOX
151  mutable FXMutex myMutex;
152 #endif
153  Container myItems;
155 };
156 
157 
158 #endif
159 
160 /****************************************************************************/
bool empty()
Definition: FXSynchQue.h:105
bool myCondition
Definition: FXSynchQue.h:154
Container & getContainer()
Definition: FXSynchQue.h:74
void push_back(T what)
Definition: FXSynchQue.h:91
void unsetCondition()
Definition: FXSynchQue.h:69
Container myItems
Definition: FXSynchQue.h:153
FXSynchQue(const bool condition=true)
Definition: FXSynchQue.h:36
void unlock()
Definition: FXSynchQue.h:83
void clear()
Definition: FXSynchQue.h:120
void pop()
Definition: FXSynchQue.h:54
size_t size() const
Definition: FXSynchQue.h:134