Browse Source

Made receive destructive, added peek (love.thread)

[email protected] 15 years ago
parent
commit
269b70f30c

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

@@ -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 - 0
src/modules/thread/sdl/Thread.h

@@ -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();

+ 59 - 21
src/modules/thread/sdl/wrap_Thread.cpp

@@ -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;
-			}
+			lua_pushnil(L);
+			return 1;
 		}
-		else
+		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 - 0
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_peek(lua_State *L);
 	int w_Thread_send(lua_State *L);
 
 	int luaopen_thread(lua_State *L);