A thread-safe, FIFO queue of NetDatagrams. More...
#include "datagramQueue.h"
Public Member Functions | |
bool | extract (NetDatagram &result) |
Extracts a datagram from the head of the queue, if one is available. More... | |
int | get_current_queue_size () const |
Returns the current number of things in the queue. More... | |
int | get_max_queue_size () const |
Returns the maximum size the queue is allowed to grow to. More... | |
bool | insert (const NetDatagram &data, bool block=false) |
Inserts the indicated datagram onto the end of the queue, and returns. More... | |
void | set_max_queue_size (int max_size) |
Sets the maximum size the queue is allowed to grow to. More... | |
void | shutdown () |
Marks the queue as shutting down, which will eventually cause all threads blocking on extract() to return false. More... | |
A thread-safe, FIFO queue of NetDatagrams.
This is used by ConnectionWriter for queuing up datagrams for its various threads to write to sockets.
Definition at line 29 of file datagramQueue.h.
bool DatagramQueue::extract | ( | NetDatagram & | result | ) |
Extracts a datagram from the head of the queue, if one is available.
If a datagram is available, this will immediately return; otherwise, it will block until a datagram becomes available. Multiple threads may simultaneously block on extract(); when a datagram is subsequently inserted into the queue, one of the threads will return from extract() with the datagram.
The return value is true if the datagram is successfully extracted, or false if the queue was destroyed while waiting. (In the case of a false return, the thread should not attempt to operate on the queue again.)
Definition at line 99 of file datagramQueue.cxx.
References NetDatagram::clear(), and ConditionVarFullDirect::wait().
int DatagramQueue::get_current_queue_size | ( | ) | const |
Returns the current number of things in the queue.
Definition at line 151 of file datagramQueue.cxx.
Referenced by ConnectionWriter::get_current_queue_size().
int DatagramQueue::get_max_queue_size | ( | ) | const |
Returns the maximum size the queue is allowed to grow to.
See set_max_queue_size().
Definition at line 143 of file datagramQueue.cxx.
Referenced by ConnectionWriter::get_max_queue_size().
bool DatagramQueue::insert | ( | const NetDatagram & | data, |
bool | block = false |
||
) |
Inserts the indicated datagram onto the end of the queue, and returns.
If the queue is empty and any threads are waiting on the queue, this will wake one of them up. Returns true if successful, false if the queue was full.
If block is true, this will not return until successful, waiting until the queue has space available if necessary.
Definition at line 66 of file datagramQueue.cxx.
void DatagramQueue::set_max_queue_size | ( | int | max_size | ) |
Sets the maximum size the queue is allowed to grow to.
This is primarily for a sanity check; this is a limit beyond which we can assume something bad has happened.
It's also a crude check against unfortunate seg faults due to the queue filling up and quietly consuming all available memory.
Definition at line 133 of file datagramQueue.cxx.
Referenced by ConnectionWriter::set_max_queue_size().
void DatagramQueue::shutdown | ( | ) |
Marks the queue as shutting down, which will eventually cause all threads blocking on extract() to return false.
The normal way to delete a DatagramQueue will be to call first shutdown() and then wait for all known threads to terminate. Then it is safe to delete the queue.
Definition at line 47 of file datagramQueue.cxx.
References ConditionVarFullDirect::notify_all().
Referenced by ConnectionWriter::shutdown().