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