summaryrefslogtreecommitdiff
path: root/includes/Threads.h
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2012-04-03 08:46:21 -0700
committerPixel <pixel@nobis-crew.org>2012-04-03 08:46:21 -0700
commit37221dc091725c6fea09181b845308ab8f26c795 (patch)
treef877eb7f70f2f9ec021fd04720d7bd0c937dbab7 /includes/Threads.h
parent7d5f246ae7310055d39ea13ff395d830e3c27a60 (diff)
Reworking a bit the way the queues are working, and thus, the way the LuaTMainTask queue works.
Diffstat (limited to 'includes/Threads.h')
-rw-r--r--includes/Threads.h58
1 files changed, 2 insertions, 56 deletions
diff --git a/includes/Threads.h b/includes/Threads.h
index 46ea365..0dfce1a 100644
--- a/includes/Threads.h
+++ b/includes/Threads.h
@@ -5,8 +5,7 @@
namespace Balau {
-template<class T>
-class Queue;
+class QueueBase;
class Lock {
public:
@@ -18,8 +17,7 @@ class Lock {
Lock(const Lock &) = delete;
Lock & operator=(const Lock &) = delete;
pthread_mutex_t m_lock;
- template<class T>
- friend class Queue;
+ friend class QueueBase;
};
class ScopeLock {
@@ -95,56 +93,4 @@ class GlobalThread : public Thread, public AtStart, public AtExit {
virtual void doExit() { join(); }
};
-template<class T>
-class Queue {
- public:
- Queue() : m_front(NULL), m_back(NULL) { pthread_cond_init(&m_cond, NULL); }
- ~Queue() { while (!isEmpty()) pop(); pthread_cond_destroy(&m_cond); }
- void push(T * t) {
- ScopeLock sl(m_lock);
- Cell * c = new Cell(t);
- c->m_prev = m_back;
- if (m_back)
- m_back->m_next = c;
- else
- m_front = c;
- m_back = c;
- pthread_cond_signal(&m_cond);
- }
- T * pop(bool wait = true) {
- ScopeLock sl(m_lock);
- while (!m_front && wait)
- pthread_cond_wait(&m_cond, &m_lock.m_lock);
- Cell * c = m_front;
- if (!c)
- return NULL;
- m_front = c->m_next;
- if (m_front)
- m_front->m_prev = NULL;
- else
- m_back = NULL;
- T * t = c->m_elem;
- delete c;
- return t;
- }
- bool isEmpty() {
- ScopeLock sl(m_lock);
- return !m_front;
- }
- private:
- Queue(const Queue &) = delete;
- Queue & operator=(const Queue &) = delete;
- Lock m_lock;
- struct Cell {
- Cell(T * elem) : m_next(NULL), m_prev(NULL), m_elem(elem) { }
- Cell(const Cell &) = delete;
- Cell & operator=(const Cell &) = delete;
- Cell * m_next, * m_prev;
- T * m_elem;
- };
- Cell * volatile m_front;
- Cell * volatile m_back;
- pthread_cond_t m_cond;
-};
-
};