Browse Source

Threads can now start with arguments, which will be in ... inside of the thread

Bart van Strien 12 years ago
parent
commit
7e50691b6c

+ 33 - 1
src/modules/thread/LuaThread.cpp

@@ -33,6 +33,8 @@ namespace thread
 LuaThread::LuaThread(const std::string &name, love::Data *code)
 LuaThread::LuaThread(const std::string &name, love::Data *code)
 	: code(code)
 	: code(code)
 	, name(name)
 	, name(name)
+	, args(0)
+	, nargs(0)
 {
 {
 	code->retain();
 	code->retain();
 }
 }
@@ -40,6 +42,11 @@ LuaThread::LuaThread(const std::string &name, love::Data *code)
 LuaThread::~LuaThread()
 LuaThread::~LuaThread()
 {
 {
 	code->release();
 	code->release();
+
+	// No args should still exist at this point,
+	// but you never know.
+	for (int i = 0; i < nargs; ++i)
+		args[i]->release();
 }
 }
 
 
 void LuaThread::threadFunction()
 void LuaThread::threadFunction()
@@ -55,12 +62,37 @@ void LuaThread::threadFunction()
 	if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
 	if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
 		error = luax_tostring(L, -1);
 		error = luax_tostring(L, -1);
 	else
 	else
-		if (lua_pcall(L, 0, 0, 0) != 0)
+	{
+		int pushedargs = nargs;
+		for (int i = 0; i < nargs; ++i)
+		{
+			args[i]->toLua(L);
+			args[i]->release();
+		}
+		// Set both args and nargs to nil,
+		// prevents the deconstructor from
+		// accessing it again.
+		nargs = 0;
+		args = 0;
+
+		if (lua_pcall(L, pushedargs, 0, 0) != 0)
 			error = luax_tostring(L, -1);
 			error = luax_tostring(L, -1);
+	}
 	lua_close(L);
 	lua_close(L);
 	this->release();
 	this->release();
 }
 }
 
 
+bool LuaThread::start(Variant **args, int nargs)
+{
+	for (int i = 0; i < this->nargs; ++i)
+		this->args[i]->release();
+
+	this->args = args;
+	this->nargs = nargs;
+
+	return Threadable::start();
+}
+
 const std::string &LuaThread::getError()
 const std::string &LuaThread::getError()
 {
 {
 	return error;
 	return error;

+ 7 - 1
src/modules/thread/LuaThread.h

@@ -25,8 +25,9 @@
 #include <string>
 #include <string>
 
 
 // LOVE
 // LOVE
-#include <common/Object.h>
 #include <common/Data.h>
 #include <common/Data.h>
+#include <common/Object.h>
+#include <common/Variant.h>
 #include <thread/threads.h>
 #include <thread/threads.h>
 
 
 namespace love
 namespace love
@@ -41,10 +42,15 @@ public:
 	void threadFunction();
 	void threadFunction();
 	const std::string &getError();
 	const std::string &getError();
 
 
+	bool start(Variant **args, int nargs);
+
 private:
 private:
 	love::Data *code;
 	love::Data *code;
 	std::string name;
 	std::string name;
 	std::string error;
 	std::string error;
+
+	Variant **args;
+	int nargs;
 };
 };
 } // thread
 } // thread
 } // love
 } // love

+ 11 - 1
src/modules/thread/wrap_LuaThread.cpp

@@ -33,7 +33,17 @@ LuaThread *luax_checkthread(lua_State *L, int idx)
 int w_Thread_start(lua_State *L)
 int w_Thread_start(lua_State *L)
 {
 {
 	LuaThread *t = luax_checkthread(L, 1);
 	LuaThread *t = luax_checkthread(L, 1);
-	luax_pushboolean(L, t->start());
+	int nargs = lua_gettop(L) - 1;
+	Variant **args = 0;
+
+	if (nargs > 0)
+	{
+		args = new Variant*[nargs];
+		for (int i = 0; i < nargs; ++i)
+			args[i] = Variant::fromLua(L, i+2);
+	}
+
+	luax_pushboolean(L, t->start(args, nargs));
 	return 1;
 	return 1;
 }
 }