From a4634497c8ada51755249606d8dc4b4c2761c017 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 1 Apr 2012 12:41:16 -0700 Subject: Creating first pass on LuaTasks. --- Makefile | 2 ++ includes/LuaTask.h | 61 +++++++++++++++++++++++++++++++++++++++++++++ includes/c++11-surrogates.h | 21 ++++++++++++++++ src/LuaTask.cc | 46 ++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 includes/LuaTask.h create mode 100644 includes/c++11-surrogates.h create mode 100644 src/LuaTask.cc diff --git a/Makefile b/Makefile index f12068c..478ff0f 100644 --- a/Makefile +++ b/Makefile @@ -140,6 +140,8 @@ SimpleMustache.cc \ \ BLua.cc \ \ +LuaTask.cc \ +\ BRegex.cc \ ifeq ($(SYSTEM),MINGW32) diff --git a/includes/LuaTask.h b/includes/LuaTask.h new file mode 100644 index 0000000..6dff44e --- /dev/null +++ b/includes/LuaTask.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include +#include + +namespace Balau { + +class LuaTask; +class LuaMainTask; + +class LuaExecCell { + public: + LuaExecCell() : m_detached(false) { } + void detach() { m_detached = true; } + void exec(LuaMainTask * mainTask); + protected: + virtual void run(Lua &) = 0; + private: + Events::Async m_event; + bool m_detached; + friend class LuaTask; +}; + +class LuaExecString : public LuaExecCell { + public: + LuaExecString(const String & str) : m_str(str) { } + private: + virtual void run(Lua &); + String m_str; +}; + +class LuaTask : public Task { + public: + ~LuaTask() { L.weaken(); } + virtual const char * getName() const { return "LuaTask"; } + private: + LuaTask(Lua && __L, LuaExecCell * cell) : L(Move(__L)), m_cell(cell) { } + virtual void Do(); + Lua L; + LuaExecCell * m_cell; + friend class LuaMainTask; +}; + +class LuaMainTask : public Task { + public: + LuaMainTask() : m_stopping(false) { } + ~LuaMainTask() { L.close(); } + void stop(); + virtual const char * getName() const { return "LuaMainTask"; } + private: + void exec(LuaExecCell * cell); + virtual void Do(); + Lua L; + Events::Async m_queueEvent; + Queue m_queue; + bool m_stopping; + friend class LuaExecCell; +}; + +}; diff --git a/includes/c++11-surrogates.h b/includes/c++11-surrogates.h new file mode 100644 index 0000000..4f3a1a0 --- /dev/null +++ b/includes/c++11-surrogates.h @@ -0,0 +1,21 @@ +#pragma once + +namespace Balau { + +template struct RemoveReference { + typedef T type; +}; + +template struct RemoveReference { + typedef T type; +}; + +template struct RemoveReference { + typedef T type; +}; + +template typename RemoveReference::type&& Move(T&& t) { + return static_cast::type&&>(t); +} + +}; diff --git a/src/LuaTask.cc b/src/LuaTask.cc new file mode 100644 index 0000000..754de6f --- /dev/null +++ b/src/LuaTask.cc @@ -0,0 +1,46 @@ +#include "LuaTask.h" +#include "Main.h" +#include "TaskMan.h" + +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(); +} + +void Balau::LuaMainTask::Do() { + while (!m_stopping) { + waitFor(&m_queueEvent); + + LuaExecCell * cell; + while ((cell = m_queue.pop(false))) { + createTask(new LuaTask(L.thread(), cell)); + } + + yield(); + } +} + +void Balau::LuaTask::Do() { + m_cell->run(L); + if (m_cell->m_detached) + delete m_cell; + else + m_cell->m_event.trigger(); +} + +void Balau::LuaExecCell::exec(LuaMainTask * mainTask) { + if (!m_detached) + Task::prepare(&m_event); + mainTask->exec(this); + if (!m_detached) + Task::yield(&m_event); +} + +void Balau::LuaExecString::run(Lua & L) { + L.load(m_str); +} -- cgit v1.2.3