From cc4d737924180fb34659e58a2414786ce5198133 Mon Sep 17 00:00:00 2001 From: "bart@bartbes.ath.cx" Date: Sun, 14 Feb 2010 11:16:56 +0100 Subject: [PATCH] Added demand (love.thread) --- src/modules/thread/sdl/Thread.cpp | 16 ++++++++++++ src/modules/thread/sdl/Thread.h | 2 ++ src/modules/thread/sdl/wrap_Thread.cpp | 35 ++++++++++++++++++++++++++ src/modules/thread/sdl/wrap_Thread.h | 1 + 4 files changed, 54 insertions(+) diff --git a/src/modules/thread/sdl/Thread.cpp b/src/modules/thread/sdl/Thread.cpp index b58f7a3cd..69e9e38e3 100644 --- a/src/modules/thread/sdl/Thread.cpp +++ b/src/modules/thread/sdl/Thread.cpp @@ -154,6 +154,7 @@ namespace sdl memcpy(this->data, data->getData(), len); comm = new ThreadData(name.c_str(), this->data); mutex = SDL_CreateMutex(); + cond = SDL_CreateCond(); } Thread::~Thread() @@ -164,6 +165,7 @@ namespace sdl SDL_KillThread(handle); reg->unregister(name); SDL_DestroyMutex(mutex); + SDL_DestroyCond(cond); reg->release(); } @@ -214,6 +216,19 @@ namespace sdl return v; } + ThreadVariant *Thread::demand(std::string name) + { + lock(); + ThreadVariant *v = comm->getValue(name); + while (!v) + { + SDL_CondWait(cond, mutex); + v = comm->getValue(name); + } + unlock(); + return v; + } + void Thread::clear(std::string name) { lock(); @@ -226,6 +241,7 @@ namespace sdl lock(); comm->setValue(name, v); unlock(); + SDL_CondBroadcast(cond); } ThreadModule::~ThreadModule() diff --git a/src/modules/thread/sdl/Thread.h b/src/modules/thread/sdl/Thread.h index bf6e6dc7a..46190e93b 100644 --- a/src/modules/thread/sdl/Thread.h +++ b/src/modules/thread/sdl/Thread.h @@ -99,6 +99,7 @@ namespace sdl std::string name; char *data; SDL_mutex *mutex; + SDL_cond *cond; public: Thread(ThreadModuleRegistrar *reg, std::string name, love::Data *data); @@ -108,6 +109,7 @@ namespace sdl void wait(); std::string getName(); ThreadVariant *receive(std::string name); + ThreadVariant *demand(std::string name); void clear(std::string name); void send(std::string name, ThreadVariant *v); void lock(); diff --git a/src/modules/thread/sdl/wrap_Thread.cpp b/src/modules/thread/sdl/wrap_Thread.cpp index da9fe80ec..db89163b8 100644 --- a/src/modules/thread/sdl/wrap_Thread.cpp +++ b/src/modules/thread/sdl/wrap_Thread.cpp @@ -93,6 +93,40 @@ namespace sdl return 1; } + int w_Thread_demand(lua_State *L) + { + Thread *t = luax_checkthread(L, 1); + std::string name = luaL_checkstring(L, 2); + ThreadVariant *v = t->demand(name); + if (!v) + { + lua_pushnil(L); + return 1; + } + v->retain(); + t->clear(name); + switch(v->type) + { + case BOOLEAN: + lua_pushboolean(L, v->data.boolean); + break; + case NUMBER: + lua_pushnumber(L, v->data.number); + break; + case STRING: + lua_pushstring(L, v->data.string); + break; + case USERDATA: //FIXME: full userdata + lua_pushlightuserdata(L, v->data.userdata); + break; + default: + lua_pushnil(L); + break; + } + v->release(); + return 1; + } + int w_Thread_peek(lua_State *L) { Thread *t = luax_checkthread(L, 1); @@ -162,6 +196,7 @@ namespace sdl { "wait", w_Thread_wait }, { "getName", w_Thread_getName }, { "receive", w_Thread_receive }, + { "demand", w_Thread_demand }, { "peek", w_Thread_peek }, { "send", w_Thread_send }, { 0, 0 } diff --git a/src/modules/thread/sdl/wrap_Thread.h b/src/modules/thread/sdl/wrap_Thread.h index 018d2e284..b88cf829e 100644 --- a/src/modules/thread/sdl/wrap_Thread.h +++ b/src/modules/thread/sdl/wrap_Thread.h @@ -37,6 +37,7 @@ namespace sdl int w_Thread_wait(lua_State *L); int w_Thread_getName(lua_State *L); int w_Thread_receive(lua_State *L); + int w_Thread_demand(lua_State *L); int w_Thread_peek(lua_State *L); int w_Thread_send(lua_State *L);