Made receive destructive, added peek (love.thread)

This commit is contained in:
bart@bartbes.ath.cx
2010-02-14 10:39:42 +01:00
parent 2dcd11551c
commit 269b70f30c
4 changed files with 78 additions and 22 deletions
+15
View File
@@ -128,6 +128,14 @@ namespace sdl
return shared[name];
}
void ThreadData::clearValue(std::string name)
{
if (shared.count(name) == 0)
return;
shared[name]->release();
shared.erase(name);
}
void ThreadData::setValue(std::string name, ThreadVariant *v)
{
if (shared.count(name) != 0)
@@ -206,6 +214,13 @@ namespace sdl
return v;
}
void Thread::clear(std::string name)
{
lock();
comm->clearValue(name);
unlock();
}
void Thread::send(std::string name, ThreadVariant *v)
{
lock();
+2
View File
@@ -86,6 +86,7 @@ namespace sdl
const char *getCode();
const char *getName();
ThreadVariant* getValue(std::string name);
void clearValue(std::string name);
void setValue(std::string name, ThreadVariant *v);
};
@@ -107,6 +108,7 @@ namespace sdl
void wait();
std::string getName();
ThreadVariant *receive(std::string name);
void clear(std::string name);
void send(std::string name, ThreadVariant *v);
void lock();
void unlock();
+60 -22
View File
@@ -62,30 +62,67 @@ namespace sdl
int w_Thread_receive(lua_State *L)
{
Thread *t = luax_checkthread(L, 1);
ThreadVariant *v = t->receive(luaL_checkstring(L, 2));
if (v)
std::string name = luaL_checkstring(L, 2);
ThreadVariant *v = t->receive(name);
if (!v)
{
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;
}
}
else
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);
std::string name = luaL_checkstring(L, 2);
ThreadVariant *v = t->receive(name);
if (!v)
{
lua_pushnil(L);
return 1;
}
v->retain();
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;
}
@@ -125,6 +162,7 @@ namespace sdl
{ "wait", w_Thread_wait },
{ "getName", w_Thread_getName },
{ "receive", w_Thread_receive },
{ "peek", w_Thread_peek },
{ "send", w_Thread_send },
{ 0, 0 }
};
+1
View File
@@ -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_peek(lua_State *L);
int w_Thread_send(lua_State *L);
int luaopen_thread(lua_State *L);