summaryrefslogtreecommitdiffstats
path: root/src/OSSupport
diff options
context:
space:
mode:
authorTycho Bickerstaff <work.tycho@gmail.com>2013-12-31 16:48:57 +0100
committerTycho Bickerstaff <work.tycho@gmail.com>2013-12-31 16:48:57 +0100
commitf3736b1eb7bf698518cdb853ee29ee96b9c24a52 (patch)
treeb5a9c4f14e44bc0fd9b88c9d30bc4036c06eff5e /src/OSSupport
parentadded link dependency between WorldStorage and OSSupport (diff)
downloadcuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.gz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.bz2
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.lz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.xz
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.tar.zst
cuberite-f3736b1eb7bf698518cdb853ee29ee96b9c24a52.zip
Diffstat (limited to '')
-rw-r--r--src/OSSupport/Event.h6
-rw-r--r--src/OSSupport/IsThread.h1
-rw-r--r--src/OSSupport/Queue.h110
-rw-r--r--src/OSSupport/Queue.inc4
4 files changed, 95 insertions, 26 deletions
diff --git a/src/OSSupport/Event.h b/src/OSSupport/Event.h
index 71f418c0c..31f32f8c6 100644
--- a/src/OSSupport/Event.h
+++ b/src/OSSupport/Event.h
@@ -20,10 +20,10 @@ class cEvent
{
public:
cEvent(void);
- ~cEvent();
+ virtual ~cEvent();
- void Wait(void);
- void Set (void);
+ virtual void Wait(void);
+ virtual void Set (void);
private:
diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h
index b8784ea33..af4367636 100644
--- a/src/OSSupport/IsThread.h
+++ b/src/OSSupport/IsThread.h
@@ -22,7 +22,6 @@ In the descending class' constructor call the Start() method to start the thread
-
class cIsThread
{
protected:
diff --git a/src/OSSupport/Queue.h b/src/OSSupport/Queue.h
index 838a101e0..eb323b067 100644
--- a/src/OSSupport/Queue.h
+++ b/src/OSSupport/Queue.h
@@ -1,30 +1,104 @@
+
#pragma once
+#include <list>
+
+#include "../OSSupport/Promise.h"
+
+//this empty struct allows function inlining
template<class T>
-class cDeleter
+struct cQueueFuncs
{
public:
static void Delete(T) {};
-}
+ static void Combine(T&, const T) {};
+};
-template<class T, class D = cDelete>
+template<class ItemType, class Funcs = cQueueFuncs<ItemType> >
class cQueue
{
+
+typedef typename std::list<ItemType> ListType;
+//magic typedef to persuade clang that the iterator is a type
+typedef typename ListType::iterator iterator;
public:
- cQueue(int warnsize);
- cQueue(cQueue<T>& queue);
- ~cQueue();
-
- void EnqueueItem(T item);
- bool TryDequeueItem(T& item);
- T DequeueItem();
- void BlockTillEmpty(cEvent CancelationEvent);
- void Clear();
- int Size();
-
+ cQueue() {}
+ ~cQueue() {}
+
+ void EnqueueItem(ItemType a_item)
+ {
+ cCSLock Lock(m_CS);
+ m_contents.push_back(a_item);
+ m_evtAdded.Set();
+ }
+ 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();
+ }
+ bool TryDequeueItem(ItemType& item)
+ {
+ cCSLock Lock(m_CS);
+ if (m_contents.size() == 0) return false;
+ item = m_contents.front();
+ m_contents.pop_front();
+ return true;
+ }
+ ItemType DequeueItem()
+ {
+ cCSLock Lock(m_CS);
+ while (m_contents.size() == 0)
+ {
+ cCSUnlock Unlock(m_CS);
+ m_evtAdded.Wait();
+ }
+ return m_contents.pop_front();
+ }
+ cPromise* BlockTillEmpty() {
+ return new cEmptyQueuePromise(this);
+ }
+ //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();
+ }
+ }
+ size_t Size()
+ {
+ cCSLock Lock(m_CS);
+ return m_contents.size();
+ }
+ bool Remove(ItemType item)
+ {
+ cCSLock Lock(m_CS);
+ m_contents.remove(item);
+ }
+
private:
- int warnsize;
-}
+ ListType m_contents;
+ cCriticalSection m_CS;
+ cEvent m_evtAdded;
-//template classes must be implemented in the header
-#include "Queue.inc"
+ class cEmptyQueuePromise : public cPromise {
+ public:
+ cEmptyQueuePromise(cQueue* a_Queue) : cPromise(), m_Queue(a_Queue) {}
+ virtual bool IsCompleted() {return m_Queue->Size() != 0;}
+ private:
+ cQueue* m_Queue;
+ };
+};
diff --git a/src/OSSupport/Queue.inc b/src/OSSupport/Queue.inc
deleted file mode 100644
index f172e9794..000000000
--- a/src/OSSupport/Queue.inc
+++ /dev/null
@@ -1,4 +0,0 @@
-
-#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-
-