From 8ac288876d6746b5840706e448df00622f3153ef Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 16 Jul 2013 00:18:04 -0300 Subject: [PATCH] Added Thread:isRunning --- src/modules/thread/LuaThread.cpp | 3 ++- src/modules/thread/LuaThread.h | 2 +- src/modules/thread/Thread.h | 3 +++ src/modules/thread/sdl/Thread.cpp | 6 ++++++ src/modules/thread/sdl/Thread.h | 20 +++++++++++++------- src/modules/thread/threads.cpp | 5 +++++ src/modules/thread/threads.h | 1 + src/modules/thread/wrap_Channel.cpp | 4 ++-- src/modules/thread/wrap_LuaThread.cpp | 8 ++++++++ src/modules/thread/wrap_LuaThread.h | 1 + 10 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 54fdbe625..77453f5a7 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/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 diff --git a/src/modules/thread/LuaThread.h b/src/modules/thread/LuaThread.h index ba880abf8..b243cc13c 100644 --- a/src/modules/thread/LuaThread.h +++ b/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); diff --git a/src/modules/thread/Thread.h b/src/modules/thread/Thread.h index 61b895fd8..4fa0bf1ca 100644 --- a/src/modules/thread/Thread.h +++ b/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 diff --git a/src/modules/thread/sdl/Thread.cpp b/src/modules/thread/sdl/Thread.cpp index 69356c68b..d9adb5f91 100644 --- a/src/modules/thread/sdl/Thread.cpp +++ b/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' diff --git a/src/modules/thread/sdl/Thread.h b/src/modules/thread/sdl/Thread.h index 5400eb762..a219ca72f 100644 --- a/src/modules/thread/sdl/Thread.h +++ b/src/modules/thread/sdl/Thread.h @@ -36,19 +36,25 @@ namespace sdl { class Thread : public thread::Thread { -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(); + bool isRunning(); + +private: + + Threadable *t; + bool running; + SDL_Thread *thread; + Mutex mutex; + + static int thread_runner(void *data); + }; // Thread + } // sdl } // thread } // love diff --git a/src/modules/thread/threads.cpp b/src/modules/thread/threads.cpp index 60a9bd47c..a333dd631 100644 --- a/src/modules/thread/threads.cpp +++ b/src/modules/thread/threads.cpp @@ -93,5 +93,10 @@ void Threadable::wait() owner->wait(); } +bool Threadable::isRunning() const +{ + return owner->isRunning(); +} + } // thread } // love diff --git a/src/modules/thread/threads.h b/src/modules/thread/threads.h index e42cd3056..eca78e44c 100644 --- a/src/modules/thread/threads.h +++ b/src/modules/thread/threads.h @@ -82,6 +82,7 @@ public: bool start(); void wait(); + bool isRunning() const; protected: Thread *owner; diff --git a/src/modules/thread/wrap_Channel.cpp b/src/modules/thread/wrap_Channel.cpp index e55318c18..b0d632c84 100644 --- a/src/modules/thread/wrap_Channel.cpp +++ b/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; diff --git a/src/modules/thread/wrap_LuaThread.cpp b/src/modules/thread/wrap_LuaThread.cpp index 5f6933d22..98416872e 100644 --- a/src/modules/thread/wrap_LuaThread.cpp +++ b/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 } }; diff --git a/src/modules/thread/wrap_LuaThread.h b/src/modules/thread/wrap_LuaThread.h index 687af3dfd..b53cf2e87 100644 --- a/src/modules/thread/wrap_LuaThread.h +++ b/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);