Browse Source

Added Thread:isRunning

Alex Szpakowski 12 years ago
parent
commit
8ac288876d

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

@@ -93,9 +93,10 @@ bool LuaThread::start(Variant **args, int nargs)
 	return Threadable::start();
 }
 
-const std::string &LuaThread::getError()
+const std::string &LuaThread::getError() const
 {
 	return error;
 }
+
 } // thread
 } // love

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

@@ -40,7 +40,7 @@ public:
 	LuaThread(const std::string &name, love::Data *code);
 	~LuaThread();
 	void threadFunction();
-	const std::string &getError();
+	const std::string &getError() const;
 
 	bool start(Variant **args, int nargs);
 

+ 3 - 0
src/modules/thread/Thread.h

@@ -33,9 +33,12 @@ namespace thread
 class Thread
 {
 public:
+
 	virtual ~Thread() {}
 	virtual bool start() = 0;
 	virtual void wait() = 0;
+	virtual bool isRunning() = 0;
+
 }; // ThreadObject
 
 } // thread

+ 6 - 0
src/modules/thread/sdl/Thread.cpp

@@ -68,6 +68,12 @@ void Thread::wait()
 	thread = 0;
 }
 
+bool Thread::isRunning()
+{
+	Lock l(mutex);
+	return running;
+}
+
 int Thread::thread_runner(void *data)
 {
 	Thread *self = (Thread *) data; // some compilers don't like 'this'

+ 12 - 6
src/modules/thread/sdl/Thread.h

@@ -36,19 +36,25 @@ namespace sdl
 {
 class Thread : public thread::Thread
 {
+public:
+
+	Thread(Threadable *t);
+	~Thread();
+	bool start();
+	void wait();
+	bool isRunning();
+
 private:
+
 	Threadable *t;
 	bool running;
 	SDL_Thread *thread;
-	static int thread_runner(void *data);
 	Mutex mutex;
 
-public:
-	Thread(Threadable *t);
-	~Thread();
-	bool start();
-	void wait();
+	static int thread_runner(void *data);
+
 }; // Thread
+
 } // sdl
 } // thread
 } // love

+ 5 - 0
src/modules/thread/threads.cpp

@@ -93,5 +93,10 @@ void Threadable::wait()
 	owner->wait();
 }
 
+bool Threadable::isRunning() const
+{
+	return owner->isRunning();
+}
+
 } // thread
 } // love

+ 1 - 0
src/modules/thread/threads.h

@@ -82,6 +82,7 @@ public:
 
 	bool start();
 	void wait();
+	bool isRunning() const;
 
 protected:
 	Thread *owner;

+ 2 - 2
src/modules/thread/wrap_Channel.cpp

@@ -48,7 +48,7 @@ int w_Channel_push(lua_State *L)
 	Channel *c = luax_checkchannel(L, 1);
 	Variant *var = lua_isnoneornil(L, 2) ? 0 : Variant::fromLua(L, 2);
 	if (!var)
-		return luaL_argerror(L, 2, "boolean, number, string, or love type expected");
+		return luaL_argerror(L, 2, "boolean, number, string, love type, or flat table expected");
 	c->push(var);
 	releaseVariant(c, var);
 	return 0;
@@ -59,7 +59,7 @@ int w_Channel_supply(lua_State *L)
 	Channel *c = luax_checkchannel(L, 1);
 	Variant *var = lua_isnoneornil(L, 2) ? 0 : Variant::fromLua(L, 2);
 	if (!var)
-		return luaL_argerror(L, 2, "boolean, number, string, or love type expected");
+		return luaL_argerror(L, 2, "boolean, number, string, love type, or flat table expected");
 	c->supply(var);
 	releaseVariant(c, var);
 	return 0;

+ 8 - 0
src/modules/thread/wrap_LuaThread.cpp

@@ -65,10 +65,18 @@ int w_Thread_getError(lua_State *L)
 	return 1;
 }
 
+int w_Thread_isRunning(lua_State *L)
+{
+	LuaThread *t = luax_checkthread(L, 1);
+	luax_pushboolean(L, t->isRunning());
+	return 1;
+}
+
 static const luaL_Reg type_functions[] = {
 	{ "start", w_Thread_start },
 	{ "wait", w_Thread_wait },
 	{ "getError", w_Thread_getError },
+	{ "isRunning", w_Thread_isRunning },
 	{ 0, 0 }
 };
 

+ 1 - 0
src/modules/thread/wrap_LuaThread.h

@@ -33,6 +33,7 @@ LuaThread *luax_checkthread(lua_State *L, int idx);
 int w_Thread_start(lua_State *L);
 int w_Thread_wait(lua_State *L);
 int w_Thread_getError(lua_State *L);
+int w_Thread_isRunning(lua_State *L);
 
 extern "C" int luaopen_thread(lua_State *L);