Panda3D
queuedReturn.I
1 // Filename: queuedReturn.I
2 // Created by: drose (25Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: QueuedReturn::set_max_queue_size
18 // Access: Public
19 // Description: Sets the maximum size the queue is allowed to grow
20 // to. This is primarily for a sanity check; this is a
21 // limit beyond which we can assume something bad has
22 // happened.
23 //
24 // It's also a crude check against unfortunate seg
25 // faults due to the queue filling up and quietly
26 // consuming all available memory.
27 ////////////////////////////////////////////////////////////////////
28 template<class Thing>
30 set_max_queue_size(int max_size) {
31  LightMutexHolder holder(_mutex);
32  _max_queue_size = max_size;
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: QueuedReturn::get_max_queue_size
37 // Access: Public
38 // Description: Returns the maximum size the queue is allowed to grow
39 // to. See set_max_queue_size().
40 ////////////////////////////////////////////////////////////////////
41 template<class Thing>
44  return _max_queue_size;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: QueuedReturn::get_current_queue_size
49 // Access: Public
50 // Description: Returns the current number of things in the queue.
51 ////////////////////////////////////////////////////////////////////
52 template<class Thing>
55  LightMutexHolder holder(_mutex);
56  int size = _things.size();
57  return size;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: QueuedReturn::get_overflow_flag
62 // Access: Published
63 // Description: Returns true if the queue has overflowed since the
64 // last call to reset_overflow_flag() (implying that
65 // some elements have been dropped from the queue), or
66 // false otherwise.
67 ////////////////////////////////////////////////////////////////////
68 template<class Thing>
71  return _overflow_flag;
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: QueuedReturn::reset_overflow_flag
76 // Access: Published
77 // Description: Resets the overflow flag so that get_overflow_flag()
78 // will return false until a new overflow occurs.
79 ////////////////////////////////////////////////////////////////////
80 template<class Thing>
83  _overflow_flag = false;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: QueuedReturn::Constructor
88 // Access: Protected
89 // Description:
90 ////////////////////////////////////////////////////////////////////
91 template<class Thing>
93 QueuedReturn() {
94  _available = false;
95  _max_queue_size = get_net_max_response_queue();
96  _overflow_flag = false;
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: QueuedReturn::Destructor
101 // Access: Protected
102 // Description:
103 ////////////////////////////////////////////////////////////////////
104 template<class Thing>
106 ~QueuedReturn() {
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: QueuedReturn::thing_available
111 // Access: Protected
112 // Description: Returns true if a thing is available on the queue;
113 // call get_thing() to extract the thing.
114 ////////////////////////////////////////////////////////////////////
115 template<class Thing>
116 INLINE bool QueuedReturn<Thing>::
117 thing_available() const {
118  return _available;
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: QueuedReturn::get_thing
123 // Access: Protected
124 // Description: If a previous call to thing_available() returned
125 // true, this function will return the thing that has
126 // become available.
127 //
128 // The return value is true if a thing was
129 // successfully returned, or false if there was, in
130 // fact, no thing available. (This may happen if
131 // there are multiple threads accessing the
132 // QueuedReturn).
133 ////////////////////////////////////////////////////////////////////
134 template<class Thing>
136 get_thing(Thing &result) {
137  LightMutexHolder holder(_mutex);
138  if (_things.empty()) {
139  // Huh. Nothing after all.
140  _available = false;
141  return false;
142  }
143 
144  result = _things.front();
145  _things.pop_front();
146  _available = !_things.empty();
147  return true;
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: QueuedReturn::enqueue_thing
152 // Access: Protected
153 // Description: Adds a new thing to the queue for later retrieval.
154 // Returns true if successful, false if the queue is
155 // full (i.e. has reached _max_queue_size).
156 ////////////////////////////////////////////////////////////////////
157 template<class Thing>
159 enqueue_thing(const Thing &thing) {
160  LightMutexHolder holder(_mutex);
161  bool enqueue_ok = ((int)_things.size() < _max_queue_size);
162  if (enqueue_ok) {
163  _things.push_back(thing);
164  } else {
165  _overflow_flag = true;
166  }
167  _available = true;
168 
169  return enqueue_ok;
170 }
171 
172 ////////////////////////////////////////////////////////////////////
173 // Function: QueuedReturn::enqueue_unique_thing
174 // Access: Protected
175 // Description: The same as enqueue_thing(), except the queue is
176 // first checked that it doesn't already have something
177 // like thing. The return value is true if the enqueue
178 // operation was successful, false if the queue was full
179 // or the thing was already on the queue.
180 ////////////////////////////////////////////////////////////////////
181 template<class Thing>
183 enqueue_unique_thing(const Thing &thing) {
184  LightMutexHolder holder(_mutex);
185  bool enqueue_ok = ((int)_things.size() < _max_queue_size);
186  if (enqueue_ok) {
187  if (find(_things.begin(), _things.end(), thing) == _things.end()) {
188  // It wasn't there already; add it now.
189  _things.push_back(thing);
190  } else {
191  // It was already there; return false to indicate this.
192  enqueue_ok = false;
193  }
194 
195  } else {
196  _overflow_flag = true;
197  }
198  _available = true;
199 
200  return enqueue_ok;
201 }
bool get_overflow_flag() const
Returns true if the queue has overflowed since the last call to reset_overflow_flag() (implying that ...
Definition: queuedReturn.I:70
void reset_overflow_flag()
Resets the overflow flag so that get_overflow_flag() will return false until a new overflow occurs...
Definition: queuedReturn.I:82
void set_max_queue_size(int max_size)
Sets the maximum size the queue is allowed to grow to.
Definition: queuedReturn.I:30
Similar to MutexHolder, but for a light mutex.
int get_current_queue_size() const
Returns the current number of things in the queue.
Definition: queuedReturn.I:54
This is the implementation of a family of things that queue up their return values for later retrieva...
Definition: queuedReturn.h:38
int get_max_queue_size() const
Returns the maximum size the queue is allowed to grow to.
Definition: queuedReturn.I:43