Browse Source

getThreads works now (love.thread)

[email protected] 15 years ago
parent
commit
75f651c84b
2 changed files with 17 additions and 6 deletions
  1. 4 5
      src/modules/thread/sdl/Thread.cpp
  2. 13 1
      src/modules/thread/sdl/wrap_Thread.cpp

+ 4 - 5
src/modules/thread/sdl/Thread.cpp

@@ -304,17 +304,16 @@ namespace sdl
 		return i->second;
 	}
 
-	Thread **ThreadModule::getThreads()  //THIS FUNCTION IS BROKEN
-	//DO NOT USE IT
-	//IT EVEN CONTAINS MEMORY LEAKS!!
+	Thread **ThreadModule::getThreads()
 	{
-		Thread **list = new Thread*[threads.size()];
+		Thread **list = new Thread*[threads.size()+1];
 		int c = 0;
 		for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++, c++)
 		{
 			list[c] = i->second;
 		}
-		return 0;
+		list[threads.size()] = 0;
+		return list;
 	}
 
 	void ThreadModule::unregister(std::string name)

+ 13 - 1
src/modules/thread/sdl/wrap_Thread.cpp

@@ -260,7 +260,18 @@ namespace sdl
 
 	int w_getThreads(lua_State *L)
 	{
-		return 0;
+		Thread **list = instance->getThreads();
+		lua_newtable(L);
+		for (int i = 0; list[i] != 0; i++)
+		{
+			luax_newtype(L, "Thread", THREAD_THREAD_T, (void*) list[i]);
+			list[i]->lock();
+			list[i]->retain();
+			list[i]->unlock();
+			lua_setfield(L, -2, list[i]->getName().c_str());
+		}
+		delete[] list;
+		return 1;
 	}
 
 	int w_getThread(lua_State *L)
@@ -284,6 +295,7 @@ namespace sdl
 	static const luaL_Reg module_functions[] = {
 		{ "newThread", w_newThread },
 		{ "getThread", w_getThread },
+		{ "getThreads", w_getThreads },
 		{ 0, 0 }
 	};