1
0
cuberite-2a/src/OSSupport/Queue.h

152 lines
3.9 KiB
C
Raw Normal View History

2014-01-02 17:43:57 +00:00
// Queue.h
// Implements the cQueue class representing a thread safe queue
2013-12-21 14:43:32 +00:00
#pragma once
2014-01-02 17:43:57 +00:00
/*
2014-01-03 11:22:01 +00:00
Items can be added multiple times to a queue, there are two functions for
adding, EnqueueItem() and EnqueueItemIfNotPresent(). The first one always
enqueues the specified item, the second one checks if the item is already
present and only queues it if it isn't.
2014-01-02 17:43:57 +00:00
Usage:
2014-01-03 11:22:01 +00:00
To create a queue of type T, instantiate a cQueue<T> object. You can also
modify the behavior of the queue when deleting items and when adding items
that are already in the queue by providing a second parameter, a class that
implements the functions Delete() and Combine(). An example is given in
cQueueFuncs and is used as the default behavior.
2014-01-02 17:43:57 +00:00
*/
// this empty struct allows for the callback functions to be inlined
2013-12-21 14:43:32 +00:00
template<class T>
struct cQueueFuncs
2013-12-21 14:43:32 +00:00
{
public:
2014-01-02 17:43:57 +00:00
// Called when an Item is deleted form the queue without being returned
2013-12-21 14:43:32 +00:00
static void Delete(T) {};
2014-01-02 17:43:57 +00:00
// Called when an Item is inserted with EnqueueItemIfNotPresent and
// there is another equal value already inserted
2014-01-03 11:22:01 +00:00
static void Combine(T& a_existing, const T& a_new) {};
};
2013-12-21 14:43:32 +00:00
template<class ItemType, class Funcs = cQueueFuncs<ItemType> >
2013-12-21 14:43:32 +00:00
class cQueue
{
2014-01-02 17:43:57 +00:00
// internal typedef for a List of Items
typedef typename std::list<ItemType> ListType;
2014-01-02 17:43:57 +00:00
// magic typedef to persuade clang that the iterator is a type
typedef typename ListType::iterator iterator;
2013-12-21 14:43:32 +00:00
public:
cQueue() {}
~cQueue() {}
2014-01-02 17:43:57 +00:00
// Enqueues an item to the queue, may block if other threads are accessing
// the queue.
void EnqueueItem(ItemType a_item)
{
cCSLock Lock(m_CS);
m_contents.push_back(a_item);
m_evtAdded.Set();
}
2014-01-02 17:43:57 +00:00
// Enqueues an item to the queue if not already present as determined with
// operator ==. Will block other threads from accessing the queue.
void EnqueueItemIfNotPresent(ItemType a_item)
{
cCSLock Lock(m_CS);
for (iterator itr = m_contents.begin(); itr != m_contents.end(); ++itr)
{
if((*itr) == a_item) {
Funcs funcTable;
funcTable.Combine(*itr,a_item);
return;
}
}
m_contents.push_back(a_item);
m_evtAdded.Set();
}
2014-01-02 17:43:57 +00:00
2014-01-03 17:42:35 +00:00
// Dequeues an Item from the queue if any are present. Returns true if
// successful. Value of item is undefined if Dequeuing was unsuccessful.
bool TryDequeueItem(ItemType& item)
{
cCSLock Lock(m_CS);
2014-01-03 11:22:01 +00:00
if (m_contents.size() == 0)
{
return false;
}
item = m_contents.front();
m_contents.pop_front();
m_evtRemoved.Set();
return true;
}
2014-01-02 17:43:57 +00:00
// Dequeues an Item from the Queue, blocking until an Item is Available.
ItemType DequeueItem()
{
cCSLock Lock(m_CS);
while (m_contents.size() == 0)
{
cCSUnlock Unlock(m_CS);
m_evtAdded.Wait();
}
ItemType item = m_contents.front();
m_contents.pop_front();
m_evtRemoved.Set();
return item;
}
2014-01-02 17:43:57 +00:00
// Blocks Until the queue is Empty, Has a slight race condition which may
// cause it to miss the queue being empty.
void BlockTillEmpty() {
2014-01-02 17:43:57 +00:00
// There is a very slight race condition here if the load completes between the check
// and the wait.
while(!(Size() == 0)){m_evtRemoved.Wait();}
}
2014-01-02 17:43:57 +00:00
// Removes all Items from the Queue, calling Delete on each of them.
// can all be inlined when delete is a noop
void Clear()
{
cCSLock Lock(m_CS);
Funcs funcTable;
while (!m_contents.empty())
{
funcTable.Delete(m_contents.front());
m_contents.pop_front();
}
}
2014-01-02 17:43:57 +00:00
// Returns the Size at time of being called
// Do not use to detirmine weather to call DequeueItem, use TryDequeue instead
size_t Size()
{
cCSLock Lock(m_CS);
return m_contents.size();
}
2014-01-02 17:43:57 +00:00
// Removes an Item from the queue
2014-01-03 16:56:20 +00:00
bool Remove(ItemType a_item)
{
cCSLock Lock(m_CS);
2014-01-03 16:49:14 +00:00
for (iterator itr = m_contents.begin(); itr != m_contents.end(); ++itr)
{
if((*itr) == a_item) {
m_contents.erase(itr);
m_evtRemoved.Set();
return true;
}
}
return false;
}
2013-12-21 14:43:32 +00:00
private:
ListType m_contents;
cCriticalSection m_CS;
cEvent m_evtAdded;
cEvent m_evtRemoved;
};