summaryrefslogtreecommitdiff
path: root/includes/BLua.h
diff options
context:
space:
mode:
authorNicolas Noble <pixel@nobis-crew.org>2013-08-07 15:07:19 -0700
committerNicolas Noble <pixel@nobis-crew.org>2013-08-07 15:07:19 -0700
commit06c4d6a7bca960a163cf45d37cc5f42684d6840d (patch)
tree0e3e8e7edaa3f20b9d81b7a51f8e950dafe88421 /includes/BLua.h
parente1f011cbce057e0d4452f8c91329afa1ac3a80a3 (diff)
Deferring object collection in Lua because yielding from the garbage collector is a very bad idea.
Diffstat (limited to 'includes/BLua.h')
-rw-r--r--includes/BLua.h21
1 files changed, 19 insertions, 2 deletions
diff --git a/includes/BLua.h b/includes/BLua.h
index ccf7bee..354fb25 100644
--- a/includes/BLua.h
+++ b/includes/BLua.h
@@ -10,6 +10,7 @@ extern "C" {
#include <Exceptions.h>
#include <Handle.h>
#include <Task.h>
+#include <StacklessTask.h>
namespace Balau {
@@ -17,8 +18,9 @@ class Lua;
class LuaObjectBase {
public:
- virtual void destroy() { }
+ virtual void destroy() = 0;
void detach() { m_detached = true; }
+ virtual Task * spawnCollector() = 0;
protected:
bool isDetached() { return m_detached; }
private:
@@ -26,11 +28,26 @@ class LuaObjectBase {
};
template<class T>
+class DeferredCollector : public StacklessTask {
+ public:
+ DeferredCollector(T * obj) : m_obj(obj) { }
+ virtual const char * getName() const override { return "DeferredCollector"; }
+ virtual void Do() override {
+ StacklessBegin();
+ StacklessOperation(delete m_obj);
+ StacklessEnd();
+ }
+ private:
+ T * m_obj;
+};
+
+template<class T>
class LuaObject : public LuaObjectBase {
public:
LuaObject(T * obj) : m_obj(obj) { }
- virtual void destroy() { if (!isDetached() && m_obj) delete m_obj; detach(); }
+ virtual void destroy() override { if (!isDetached() && m_obj) delete m_obj; detach(); }
T * getObj() { return m_obj; }
+ virtual Task * spawnCollector() override { return isDetached() ? NULL : new DeferredCollector<T>(m_obj); }
private:
T * m_obj;
};