diff options
author | Pixel <pixel@nobis-crew.org> | 2012-04-03 08:46:21 -0700 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2012-04-03 08:46:21 -0700 |
commit | 37221dc091725c6fea09181b845308ab8f26c795 (patch) | |
tree | f877eb7f70f2f9ec021fd04720d7bd0c937dbab7 /src | |
parent | 7d5f246ae7310055d39ea13ff395d830e3c27a60 (diff) |
Reworking a bit the way the queues are working, and thus, the way the LuaTMainTask queue works.
Diffstat (limited to 'src')
-rw-r--r-- | src/HttpServer.cc | 2 | ||||
-rw-r--r-- | src/LuaTask.cc | 24 | ||||
-rw-r--r-- | src/Main.cc | 2 | ||||
-rw-r--r-- | src/Task.cc | 38 |
4 files changed, 56 insertions, 10 deletions
diff --git a/src/HttpServer.cc b/src/HttpServer.cc index c9cb0ee..9fc48c5 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -594,7 +594,7 @@ typedef Balau::Listener<Balau::HttpWorker> HttpListener; void Balau::HttpServer::start() { AAssert(!m_started, "Don't start an HttpServer twice"); - m_listenerPtr = createTask(new HttpListener(m_port, m_local.to_charp(), this)); + m_listenerPtr = TaskMan::createTask(new HttpListener(m_port, m_local.to_charp(), this)); m_started = true; } diff --git a/src/LuaTask.cc b/src/LuaTask.cc index 9a7e978..acf63b5 100644 --- a/src/LuaTask.cc +++ b/src/LuaTask.cc @@ -2,6 +2,10 @@ #include "Main.h" #include "TaskMan.h" +class LuaTaskDummy : public Balau::LuaExecCell { + virtual void run(Balau::Lua &) { } +}; + Balau::LuaMainTask::LuaMainTask() : m_stopping(false) { L.open_base(); L.open_table(); @@ -12,25 +16,29 @@ Balau::LuaMainTask::LuaMainTask() : m_stopping(false) { L.open_jit(); } +Balau::LuaMainTask::~LuaMainTask() { + L.close(); +} + void Balau::LuaMainTask::exec(LuaExecCell * cell) { m_queue.push(cell); - m_queueEvent.trigger(); } void Balau::LuaMainTask::stop() { Atomic::CmpXChgVal(&m_stopping, true, false); - m_queueEvent.trigger(); + exec(new LuaTaskDummy()); } void Balau::LuaMainTask::Do() { while (!m_stopping) { - waitFor(&m_queueEvent); - LuaExecCell * cell; - while ((cell = m_queue.pop(false))) - createTask(new LuaTask(L.thread(), cell), this); - - yield(); + while ((cell = m_queue.pop(false))) { + if (dynamic_cast<LuaTaskDummy *>(cell)) { + delete cell; + break; + } + TaskMan::createTask(new LuaTask(L.thread(), cell), this); + } } } diff --git a/src/Main.cc b/src/Main.cc index fd4021f..e1eae66 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -60,7 +60,7 @@ int Balau::Main::bootstrap(int _argc, char ** _argv) { try { m_status = RUNNING; - createTask(new MainTask()); + TaskMan::createTask(new MainTask()); r = TaskMan::getDefaultTaskMan()->mainLoop(); m_status = STOPPING; } diff --git a/src/Task.cc b/src/Task.cc index 6278fd5..1486540 100644 --- a/src/Task.cc +++ b/src/Task.cc @@ -234,3 +234,41 @@ void Balau::Task::yield(Events::BaseEvent * evt, bool interruptible) throw (Gene throw EAgain(evt); } } + +void Balau::QueueBase::iPush(void * 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); + m_event.trigger(); +} + +void * Balau::QueueBase::iPop(bool wait) { + ScopeLock sl(m_lock); + while (!m_front) { + if (wait) { + pthread_cond_wait(&m_cond, &m_lock.m_lock); + } else { + Task::prepare(&m_event); + m_lock.leave(); + Task::yield(&m_event); + m_lock.enter(); + } + } + 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; + void * t = c->m_elem; + delete c; + return t; +} |