Thread now has access to mutex and cond as well, fixes bug where errors wouldn't get picked up

This commit is contained in:
Bart van Strien
2010-09-20 00:56:37 +02:00
parent 8515ee675d
commit fe373075a4
2 changed files with 11 additions and 4 deletions
+7 -3
View File
@@ -49,9 +49,12 @@ namespace sdl
lua_setfield(L, -2, "_curthread"); lua_setfield(L, -2, "_curthread");
if(luaL_dostring(L, comm->getCode()) == 1) if(luaL_dostring(L, comm->getCode()) == 1)
{ {
SDL_mutexP((SDL_mutex*) comm->mutex);
ThreadVariant *v = new ThreadVariant(lua_tostring(L, -1)); ThreadVariant *v = new ThreadVariant(lua_tostring(L, -1));
comm->setValue("error", v); comm->setValue("error", v);
v->release(); v->release();
SDL_mutexV((SDL_mutex*) comm->mutex);
SDL_CondBroadcast((SDL_cond*) comm->cond);
} }
lua_close(L); lua_close(L);
return 0; return 0;
@@ -110,7 +113,8 @@ namespace sdl
} }
} }
ThreadData::ThreadData(const char *name, const char *code) ThreadData::ThreadData(const char *name, const char *code, void *mutex, void *cond)
: mutex(mutex), cond(cond)
{ {
size_t len = strlen(name); size_t len = strlen(name);
this->name = new char[len+1]; this->name = new char[len+1];
@@ -174,18 +178,18 @@ namespace sdl
this->data = new char[len+1]; this->data = new char[len+1];
memset(this->data, 0, len+1); memset(this->data, 0, len+1);
memcpy(this->data, data->getData(), len); memcpy(this->data, data->getData(), len);
comm = new ThreadData(name.c_str(), this->data);
mutex = SDL_CreateMutex(); mutex = SDL_CreateMutex();
cond = SDL_CreateCond(); cond = SDL_CreateCond();
comm = new ThreadData(name.c_str(), this->data, mutex, cond);
} }
Thread::Thread(love::thread::ThreadModule *module, const std::string & name) Thread::Thread(love::thread::ThreadModule *module, const std::string & name)
: handle(0), module(module), name(name), data(0), isThread(false) : handle(0), module(module), name(name), data(0), isThread(false)
{ {
module->retain(); module->retain();
comm = new ThreadData(name.c_str(), NULL);
mutex = SDL_CreateMutex(); mutex = SDL_CreateMutex();
cond = SDL_CreateCond(); cond = SDL_CreateCond();
comm = new ThreadData(name.c_str(), NULL, mutex, cond);
} }
Thread::~Thread() Thread::~Thread()
+4 -1
View File
@@ -79,13 +79,16 @@ namespace sdl
std::map<std::string, ThreadVariant*> shared; std::map<std::string, ThreadVariant*> shared;
public: public:
ThreadData(const char *name, const char *code); ThreadData(const char *name, const char *code, void *mutex, void *cond);
~ThreadData(); ~ThreadData();
const char *getCode(); const char *getCode();
const char *getName(); const char *getName();
ThreadVariant* getValue(const std::string & name); ThreadVariant* getValue(const std::string & name);
void clearValue(const std::string & name); void clearValue(const std::string & name);
void setValue(const std::string & name, ThreadVariant *v); void setValue(const std::string & name, ThreadVariant *v);
void *mutex;
void *cond;
}; };
class Thread : public love::Object class Thread : public love::Object