Commits

Bart van Strien committed a74672b

Merge in channels from love-experiments

Rewritten love.thread and changed communication
method from weird system before, to channels.

  • Participants
  • Parent commits 4819322
  • Branches minor

Comments (0)

Files changed (42)

platform/unix/exclude

 \./modules/native/tcc/libtcc/stab\.h
 \./libraries/luasocket/libluasocket/wsocket\.*
 \./modules/sound/lullaby/FLACDecoder\.*
-\./modules/thread/sdl/*
-\./modules/thread/win32/*
-\./modules/thread/posix/*
 \./love\.cpp

src/common/Object.h

 	// The reference count.
 	int count;
 }; // Object
-
 } // love
 
 #endif // LOVE_OBJECT_H

src/common/delay.cpp

  **/
 
 #include "delay.h"
+#include <SDL.h>
 
 namespace love
 {
 
 void delay(unsigned int ms)
 {
-#if LOVE_THREADS == LOVE_THREADS_POSIX
-	struct timespec ts1, ts2;
-
-	ts1.tv_sec = ms / 1000;
-	ts1.tv_nsec = (ms % 1000) * 1000000;
-	// FIXME: handle signals
-	nanosleep(&ts1, &ts2);
-#elif LOVE_THREADS == LOVE_THREADS_WIN32
-	Sleep(ms);
-#elif LOVE_THREADS == LOVE_THREADS_SDL
 	SDL_Delay(ms);
-#endif
 }
 
 } // love

src/common/delay.h

 #ifndef DELAY_H_
 #define DELAY_H_
 
-#include <thread/threads.h>
-
 namespace love
 {
 

src/common/runtime.cpp

 #include "Object.h"
 #include "Reference.h"
 #include "StringMap.h"
-#include "thread/threads.h"
+#include <thread/threads.h>
 
 // STD
 #include <iostream>
 
 static thread::Mutex *gcmutex = 0;
 void *_gcmutex = 0;
-unsigned int _gcthread = 0;
 /**
  * Called when an object is collected. The object is released
  * once in this function, possibly deleting it.
 {
 	if (!gcmutex)
 	{
-		gcmutex = new thread::Mutex();
+		gcmutex = thread::newMutex();
 		_gcmutex = (void *) gcmutex;
 	}
 	Proxy *p = (Proxy *)lua_touserdata(L, 1);
 	if (p->own)
 	{
 		thread::Lock lock(gcmutex);
-		_gcthread = thread::ThreadBase::threadId();
 		t->release();
 	}
 	return 0;
 	return b;
 }
 
+std::string luax_tostring(lua_State *L, int idx)
+{
+	size_t len;
+	const char *str = lua_tolstring(L, idx, &len);
+	return std::string(str, len);
+}
+
 std::string luax_checkstring(lua_State *L, int idx)
 {
 	size_t len;
 
 	// Thread
 	{"Thread", THREAD_THREAD_ID},
+	{"Channel", THREAD_CHANNEL_ID},
 
 	// The modules themselves. Only add abstracted modules here.
 	{"filesystem", MODULE_FILESYSTEM_ID},

src/common/runtime.h

  * @param idx The index on the Lua stack.
  * @return Copy of the string at the specified index.
  **/
+std::string luax_tostring(lua_State *L, int idx);
+
+/**
+ * Converts the value at idx to a std::string. It takes care of the string
+ * size and possible embedded nulls.
+ * @param L The Lua state.
+ * @param idx The index on the Lua stack.
+ * @return Copy of the string at the specified index.
+ **/
 std::string luax_checkstring(lua_State *L, int idx);
 
 /**

src/common/types.h

 
 	// Thread
 	THREAD_THREAD_ID,
+	THREAD_CHANNEL_ID,
 
 	// The modules themselves. Only add abstracted modules here.
 	MODULE_FILESYSTEM_ID,
 
 // Thread.
 const bits THREAD_THREAD_T = (bits(1) << THREAD_THREAD_ID) | OBJECT_T;
+const bits THREAD_CHANNEL_T = (bits(1) << THREAD_CHANNEL_ID) | OBJECT_T;
 
 // Modules.
 const bits MODULE_FILESYSTEM_T = (bits(1) << MODULE_FILESYSTEM_ID) | MODULE_T;

src/modules/audio/openal/Audio.cpp

 
 #include "sound/Decoder.h"
 
+#include <cstdlib>
+
 namespace love
 {
 namespace audio
 	: pool(pool)
 	, finish(false)
 {
+	mutex = thread::newMutex();
 }
 
-void Audio::PoolThread::main()
+Audio::PoolThread::~PoolThread()
+{
+	delete mutex;
+}
+
+
+void Audio::PoolThread::threadFunction()
 {
 	while (true)
 	{
 	finish = true;
 }
 
-
 Audio::Audio() : distanceModel(DISTANCE_INVERSE_CLAMPED)
 {
 	// Passing zero for default device.

src/modules/audio/openal/Audio.h

 	// The Pool.
 	Pool *pool;
 
-	class PoolThread: public thread::ThreadBase
+	class PoolThread: public thread::Threadable
 	{
 	protected:
 		Pool *pool;
 		volatile bool finish;
 
 		// finish lock
-		thread::Mutex mutex;
-
-		virtual void main();
+		thread::Mutex *mutex;
 
 	public:
 		PoolThread(Pool *pool);
+		~PoolThread();
 		void setFinish();
+		void threadFunction();
 	};
 
 	PoolThread *poolThread;

src/modules/audio/openal/Pool.cpp

 	alGenSources(NUM_SOURCES, sources);
 
 	// Create the mutex.
-	mutex = new thread::Mutex();
+	mutex = thread::newMutex();
 
 	if (alGetError() != AL_NO_ERROR)
 		throw love::Exception("Could not generate sources.");

src/modules/event/Event.cpp

 	return m;
 }
 
+Event::Event()
+{
+	mutex = thread::newMutex();
+}
+
 Event::~Event()
 {
+	delete mutex;
 }
 
 void Event::push(Message *msg)

src/modules/event/Event.h

 class Event : public Module
 {
 public:
+	Event();
 	virtual ~Event();
 
 	void push(Message *msg);
 	static bool getConstant(love::keyboard::Keyboard::Key in, const char  *&out);
 
 protected:
-	thread::Mutex mutex;
+	thread::Mutex *mutex;
 	std::queue<Message *> queue;
 	static StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry buttonEntries[];
 	static StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM> buttons;
 	static StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry keyEntries[];
 	static StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM> keys;
-
 }; // Event
 
 } // event

src/modules/image/ImageData.cpp

 
 #include "ImageData.h"
 
+#include <stdio.h>
+
 using love::thread::Lock;
 
 namespace love
 namespace image
 {
 
+ImageData::ImageData()
+{
+	mutex = thread::newMutex();
+}
+
+ImageData::~ImageData()
+{
+	delete mutex;
+}
+
 void *ImageData::getData() const
 {
 	return data;

src/modules/image/ImageData.h

 	// We need to be thread-safe
 	// so we lock when we're accessing our
 	// data
-	Mutex mutex;
+	Mutex *mutex;
 
 public:
 
 		FORMAT_MAX_ENUM
 	};
 
+	ImageData();
+
 	/**
 	 * Destructor.
 	 **/
-	virtual ~ImageData() {};
+	virtual ~ImageData();
 
 	static bool getConstant(const char *in, Format &out);
 	static bool getConstant(Format in, const char  *&out);

src/modules/image/devil/ImageData.cpp

 
 using love::thread::Lock;
 
-static Mutex devilMutex;
+static Mutex *devilMutex = 0;
 
 namespace love
 {
 
 void ImageData::load(Data *data)
 {
+	if (!devilMutex)
+		devilMutex = thread::newMutex();
+
 	Lock lock(devilMutex);
 	ILuint image;
 	ilGenImages(1, &image);
 
 void ImageData::encode(love::filesystem::File *f, ImageData::Format format)
 {
+	if (!devilMutex)
+		devilMutex = thread::newMutex();
+
 	Lock lock1(devilMutex);
 	Lock lock2(mutex);
 

src/modules/image/devil/ImageData.h

 	// Implements ImageData.
 	void encode(love::filesystem::File *f, Format format);
 
+	// We need to be thread-safe
+	// so we lock when we're accessing our
+	// data
+	Mutex *mutex;
+
 }; // ImageData
 
 } // devil

src/modules/thread/Channel.cpp

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "Channel.h"
+#include <map>
+#include <string>
+
+namespace love
+{
+namespace thread
+{
+	static std::map<std::string, Channel*> namedChannels;
+	static Mutex *namedChannelMutex;
+	Channel *Channel::getChannel(const std::string &name)
+	{
+		if (!namedChannelMutex)
+			namedChannelMutex = newMutex();
+		Lock l(namedChannelMutex);
+		if (!namedChannels.count(name))
+			namedChannels[name] = new Channel(name);
+		else
+			namedChannels[name]->retain();
+		return namedChannels[name];
+	}
+
+	Channel::Channel()
+		: named(false)
+	{
+		mutex = newMutex();
+		cond = newConditional();
+	}
+
+	Channel::Channel(const std::string &name)
+		: named(true), name(name)
+	{
+		mutex = newMutex();
+		cond = newConditional();
+	}
+
+	Channel::~Channel()
+	{
+		while (!queue.empty())
+		{
+			queue.front()->release();
+			queue.pop();
+		}
+
+		delete mutex;
+		delete cond;
+		if (named)
+		{
+			Lock l(namedChannelMutex);
+			namedChannels.erase(name);
+		}
+	}
+
+	void Channel::push(Variant *var)
+	{
+		if (!var)
+			return;
+		Lock l(mutex);
+		var->retain();
+		// Keep a reference to ourselves
+		// if we're non-empty and named.
+		if (named && queue.empty())
+			retain();
+		queue.push(var);
+		cond->signal();
+	}
+
+	Variant *Channel::pop()
+	{
+		Lock l(mutex);
+		if (queue.empty())
+			return 0;
+
+		Variant *var = queue.front();
+		queue.pop();
+
+		// Release our reference to ourselves
+		// if we're empty and named.
+		if (named && queue.empty())
+			release();
+		return var;
+	} // NOTE: Returns a retained Variant
+
+	Variant *Channel::demand()
+	{
+		Variant *var;
+		while (!(var = pop()))
+		{
+			mutex->lock();
+			cond->wait(mutex);
+			mutex->unlock();
+		}
+		return var;
+	}
+
+	Variant *Channel::peek()
+	{
+		Lock l(mutex);
+		if (queue.empty())
+			return 0;
+
+		Variant *var = queue.front();
+		var->retain();
+		return var;
+	}
+
+	int Channel::count()
+	{
+		Lock l(mutex);
+		return queue.size();
+	}
+
+	void Channel::clear()
+	{
+		Lock l(mutex);
+		while (!queue.empty())
+		{
+			queue.front()->release();
+			queue.pop();
+		}
+
+		// Once again, release our own
+		// reference if we're named.
+		if (named)
+			release();
+	}
+} // thread
+} // love

src/modules/thread/Channel.h

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_THREAD_CHANNEL_H
+#define LOVE_THREAD_CHANNEL_H
+
+// STL
+#include <queue>
+#include <string>
+
+// LOVE
+#include <common/Variant.h>
+#include <thread/threads.h>
+
+namespace love
+{
+namespace thread
+{
+	class Channel : public love::Object
+	{
+	private:
+		Mutex *mutex;
+		Conditional *cond;
+		std::queue<Variant*> queue;
+		bool named;
+		std::string name;
+		Channel(const std::string &name);
+
+	public:
+		Channel();
+		~Channel();
+		static Channel *getChannel(const std::string &name);
+
+		void push(Variant *var);
+		Variant *pop();
+		Variant *demand();
+		Variant *peek();
+		int count();
+		void clear();
+	}; // Channel
+} // thread
+} // love
+
+#endif // LOVE_THREAD_CHANNEL_H

src/modules/thread/LuaThread.cpp

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "LuaThread.h"
+#include <common/config.h>
+
+#ifdef LOVE_BUILD_STANDALONE
+extern "C" int luaopen_love(lua_State * L);
+#endif // LOVE_BUILD_STANDALONE
+extern "C" int luaopen_love_thread(lua_State *L);
+
+namespace love
+{
+namespace thread
+{
+	LuaThread::LuaThread(const std::string &name, love::Data *code)
+		: name(name), code(code)
+	{
+		code->retain();
+	}
+
+	LuaThread::~LuaThread()
+	{
+		code->release();
+	}
+
+	void LuaThread::threadFunction()
+	{
+		this->retain();
+		lua_State * L = lua_open();
+		luaL_openlibs(L);
+	#ifdef LOVE_BUILD_STANDALONE
+		love::luax_preload(L, luaopen_love, "love");
+		luaopen_love(L);
+	#endif // LOVE_BUILD_STANDALONE
+		luaopen_love_thread(L);
+		if (luaL_loadbuffer(L, (const char*) code->getData(), code->getSize(), name.c_str()) != 0)
+			error = luax_tostring(L, -1);
+		else
+			if (lua_pcall(L, 0, 0, 0) != 0)
+				error = luax_tostring(L, -1);
+		lua_close(L);
+		this->release();
+	}
+
+	const std::string &LuaThread::getError()
+	{
+		return error;
+	}
+} // thread
+} // love

src/modules/thread/LuaThread.h

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_THREAD_LUATHREAD_H
+#define LOVE_THREAD_LUATHREAD_H
+
+// STL
+#include <string>
+
+// LOVE
+#include <common/Object.h>
+#include <common/Data.h>
+#include <thread/threads.h>
+
+namespace love
+{
+namespace thread
+{
+	class LuaThread : public love::Object, public Threadable
+	{
+	private:
+		love::Data *code;
+		std::string name;
+		std::string error;
+
+	public:
+		LuaThread(const std::string &name, love::Data *code);
+		~LuaThread();
+		void threadFunction();
+		const std::string &getError();
+	};
+} // thread
+} // love
+
+#endif // LOVE_THREAD_LUATHREAD_H

src/modules/thread/Thread.cpp

-/**
- * Copyright (c) 2006-2012 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#include "common/config.h"
-
-#include "Thread.h"
-
-#ifdef LOVE_BUILD_STANDALONE
-extern "C" int luaopen_love(lua_State *L);
-#endif // LOVE_BUILD_STANDALONE
-extern "C" int luaopen_love_thread(lua_State *L);
-
-namespace love
-{
-namespace thread
-{
-
-Thread::ThreadThread::ThreadThread(ThreadData *comm)
-	: comm(comm)
-{
-}
-
-void Thread::ThreadThread::main()
-{
-	lua_State *L = lua_open();
-	luaL_openlibs(L);
-#ifdef LOVE_BUILD_STANDALONE
-	love::luax_preload(L, luaopen_love, "love");
-	luaopen_love(L);
-#endif // LOVE_BUILD_STANDALONE
-	luaopen_love_thread(L);
-	{
-		size_t len;
-		const char *name = comm->getName(&len);
-		lua_pushlstring(L, name, len);
-	}
-	luax_convobj(L, lua_gettop(L), "thread", "getThread");
-	lua_getglobal(L, "love");
-	lua_pushvalue(L, -2);
-	lua_setfield(L, -2, "_curthread");
-	if (luaL_dostring(L, comm->getCode()) == 1)
-	{
-		{
-			Lock lock((Mutex *) comm->mutex);
-			Variant *v = new Variant(lua_tostring(L, -1), lua_strlen(L, -1));
-			comm->setValue("error", v);
-			v->release();
-		}
-		((Conditional *) comm->cond)->broadcast();
-	}
-	lua_close(L);
-}
-
-ThreadData::ThreadData(const char *name, size_t len, const char *code, void *mutex, void *cond)
-	: len(len), mutex(mutex), cond(cond)
-{
-	this->name = new char[len+1];
-	memset(this->name, 0, len+1);
-	memcpy(this->name, name, len);
-	if (code)
-	{
-		len = strlen(code);
-		this->code = new char[len+1];
-		memset(this->code, 0, len+1);
-		memcpy(this->code, code, len);
-	}
-	else
-		this->code = 0;
-}
-
-ThreadData::~ThreadData()
-{
-	delete[] name;
-	delete[] code;
-}
-
-const char *ThreadData::getCode()
-{
-	return code;
-}
-
-const char *ThreadData::getName(size_t *len)
-{
-	if (len)
-		 *len = this->len;
-	return name;
-}
-
-Variant *ThreadData::getValue(const std::string &name)
-{
-	if (shared.count(name) == 0)
-		return 0;
-	return shared[name];
-}
-
-void ThreadData::clearValue(const std::string &name)
-{
-	if (shared.count(name) == 0)
-		return;
-	shared[name]->release();
-	shared.erase(name);
-}
-
-void ThreadData::setValue(const std::string &name, Variant *v)
-{
-	if (shared.count(name) != 0)
-		shared[name]->release();
-	v->retain();
-	shared[name] = v;
-}
-
-std::vector<std::string> ThreadData::getKeys()
-{
-	std::vector<std::string> keys;
-	for (std::map<std::string, Variant *>::iterator it = shared.begin(); it != shared.end(); it++)
-	{
-		keys.push_back(it->first);
-	}
-	return keys;
-}
-
-Thread::Thread(love::thread::ThreadModule *module, const std::string &name, love::Data *data)
-	: handle(0), module(module), name(name), isThread(true)
-{
-	module->retain();
-	unsigned int len = data->getSize();
-	this->data = new char[len+1];
-	memset(this->data, 0, len+1);
-	memcpy(this->data, data->getData(), len);
-	mutex = new Mutex();
-	cond = new Conditional();
-	comm = new ThreadData(name.c_str(), name.length(), this->data, mutex, cond);
-}
-
-Thread::Thread(love::thread::ThreadModule *module, const std::string &name)
-	: handle(0), module(module), name(name), data(0), isThread(false)
-{
-	module->retain();
-	mutex = new Mutex();
-	cond = new Conditional();
-	comm = new ThreadData(name.c_str(), name.length(), NULL, mutex, cond);
-}
-
-Thread::~Thread()
-{
-	if (data)
-		delete[] data;
-	delete comm;
-	module->unregister(name);
-	delete mutex;
-	delete cond;
-	module->release();
-}
-
-void Thread::start()
-{
-	if (!handle && isThread)
-	{
-		handle = new ThreadThread(comm);
-		handle->start();
-	}
-}
-
-void Thread::kill()
-{
-	if (handle)
-	{
-		Lock lock((Mutex *) _gcmutex);
-		handle->kill();
-		delete handle;
-		handle = 0;
-	}
-}
-
-void Thread::wait()
-{
-	if (handle)
-	{
-		handle->wait();
-		delete handle;
-		handle = 0;
-	}
-}
-
-void Thread::lock()
-{
-	mutex->lock();
-}
-
-void Thread::unlock()
-{
-	mutex->unlock();
-}
-
-std::string Thread::getName()
-{
-	return name;
-}
-
-Variant *Thread::get(const std::string &name)
-{
-	Variant *v = comm->getValue(name);
-	if (v)
-		v->retain();
-	return v;
-}
-
-std::vector<std::string> Thread::getKeys()
-{
-	return comm->getKeys();
-}
-
-Variant *Thread::demand(const std::string &name)
-{
-	Variant *v = comm->getValue(name);
-	while (!v)
-	{
-		if (comm->getValue("error"))
-			return 0;
-		cond->wait(mutex);
-		v = comm->getValue(name);
-	}
-	v->retain();
-	return v;
-}
-
-void Thread::clear(const std::string &name)
-{
-	comm->clearValue(name);
-}
-
-void Thread::set(const std::string &name, Variant *v)
-{
-	lock(); //this function explicitly locks
-	comm->setValue(name, v); //because we need
-	unlock(); //it to unlock here for the cond
-	cond->broadcast();
-}
-
-ThreadModule::ThreadModule()
-{
-	threads["main"] = new Thread(this, "main");
-}
-
-ThreadModule::~ThreadModule()
-{
-	for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++)
-	{
-		i->second->kill();
-		delete i->second;
-	}
-}
-
-Thread *ThreadModule::newThread(const std::string &name, love::Data *data)
-{
-	if (threads.count(name) != 0)
-		return 0;
-	Thread *t = new Thread(this, name, data);
-	threads[name] = t;
-	return t;
-}
-
-Thread *ThreadModule::getThread(const std::string &name)
-{
-	if (threads.count(name) == 0)
-		return 0;
-	threadlist_t::iterator i = threads.find(name);
-	return i->second;
-}
-
-void ThreadModule::getThreads(Thread **list)
-{
-	int c = 0;
-	for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++, c++)
-	{
-		list[c] = i->second;
-	}
-}
-
-unsigned ThreadModule::getThreadCount() const
-{
-	return threads.size();
-}
-
-void ThreadModule::unregister(const std::string &name)
-{
-	if (threads.count(name) == 0)
-		return;
-	threadlist_t::iterator i = threads.find(name);
-	// FIXME: shouldn't the thread be deleted?
-	threads.erase(i);
-}
-
-const char *ThreadModule::getName() const
-{
-	return "love.thread.sdl";
-}
-
-} // thread
-} // love

src/modules/thread/Thread.h

  * 3. This notice may not be removed or altered from any source distribution.
  **/
 
-#ifndef LOVE_THREAD_SDL_THREAD_H
-#define LOVE_THREAD_SDL_THREAD_H
-
-// STL
-#include <map>
-#include <string>
-#include <vector>
-#include <cstring>
+#ifndef LOVE_THREAD_THREAD_H
+#define LOVE_THREAD_THREAD_H
 
 // LOVE
-#include "filesystem/File.h"
-#include "common/runtime.h"
-#include "common/Module.h"
-#include "common/Variant.h"
-#include "thread/threads.h"
+#include <common/runtime.h>
+#include <common/Object.h>
 
 namespace love
 {
 namespace thread
 {
 
-class ThreadModule;
-
-class ThreadData
+class Thread
 {
 public:
-	ThreadData(const char *name, size_t len, const char *code, void *mutex, void *cond);
-	~ThreadData();
-	const char *getCode();
-	const char *getName(size_t *len = 0);
-	Variant *getValue(const std::string &name);
-	void clearValue(const std::string &name);
-	void setValue(const std::string &name, Variant *v);
-	std::vector<std::string> getKeys();
+	virtual ~Thread() {}
+	virtual bool start() = 0;
+	virtual void wait() = 0;
+	virtual void kill() = 0;
+}; // ThreadObject
 
-	void *mutex;
-	void *cond;
-private:
-	char *code;
-	char *name;
-	std::map<std::string, Variant *> shared;
-	size_t len;
-};
-
-class Thread : public love::Object
-{
-public:
-	Thread(love::thread::ThreadModule *module, const std::string &name, love::Data *data);
-	Thread(love::thread::ThreadModule *module, const std::string &name);
-	virtual ~Thread();
-	void start();
-	void kill();
-	void wait();
-	std::string getName();
-	Variant *get(const std::string &name);
-	std::vector<std::string> getKeys();
-	Variant *demand(const std::string &name);
-	void clear(const std::string &name);
-	void set(const std::string &name, Variant *v);
-	void lock();
-	void unlock();
-private:
-	class ThreadThread: public ThreadBase
-	{
-	private:
-		ThreadData *comm;
-
-	protected:
-		virtual void main();
-
-	public:
-		ThreadThread(ThreadData *comm);
-	};
-
-	ThreadThread *handle;
-
-	ThreadModule *module;
-	ThreadData *comm;
-	std::string name;
-	char *data;
-	Mutex *mutex;
-	Conditional *cond;
-	bool isThread;
-}; // Thread
-
-typedef std::map<std::string, Thread *> threadlist_t;
-
-class ThreadModule : public love::Module
-{
-public:
-	ThreadModule();
-	virtual ~ThreadModule();
-	Thread *newThread(const std::string &name, love::Data *data);
-	void getThreads(Thread **list);
-	Thread *getThread(const std::string &name);
-	unsigned getThreadCount() const;
-	void unregister(const std::string &name);
-	const char *getName() const;
-private:
-	threadlist_t threads;
-}; // ThreadModule
 } // thread
 } // love
 
-#endif // LOVE_THREAD_SDL_THREAD_H
+#endif // LOVE_THREAD_THREAD_H

src/modules/thread/ThreadModule.cpp

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "ThreadModule.h"
+
+namespace love
+{
+namespace thread
+{
+	const char *ThreadModule::getName() const
+	{
+		return "love.thread.sdl";
+	}
+
+	LuaThread *ThreadModule::newThread(const std::string &name, love::Data *data)
+	{
+		LuaThread *lt = new LuaThread(name, data);
+		return lt;
+	}
+
+	Channel *ThreadModule::newChannel()
+	{
+		return new Channel();
+	}
+
+	Channel *ThreadModule::getChannel(const std::string &name)
+	{
+		return Channel::getChannel(name);
+	}
+
+} // thread
+} // love

src/modules/thread/ThreadModule.h

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_THREAD_THREADMODULE_H
+#define LOVE_THREAD_THREADMODULE_H
+
+// STL
+#include <string>
+
+// LOVE
+#include <common/Data.h>
+#include <common/Module.h>
+#include <thread/Thread.h>
+#include <thread/Channel.h>
+#include <thread/LuaThread.h>
+#include <thread/threads.h>
+
+namespace love
+{
+namespace thread
+{
+	class ThreadModule : public love::Module
+	{
+	public:
+		virtual ~ThreadModule() {}
+		LuaThread *newThread(const std::string &name, love::Data *data);
+		Channel *newChannel();
+		Channel *getChannel(const std::string &name);
+
+		const char *getName() const;
+	}; // ThreadModule
+} // thread
+} // love
+
+#endif // LOVE_THREAD_THREADMODULE_H

src/modules/thread/posix/threads.cpp

-/**
- * Copyright (c) 2006-2012 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#include "threads.h"
-#ifdef LOVE_MACOSX
-#include "common/delay.h" // for Mac OS X's fake sem_timedwait
-#endif
-
-namespace love
-{
-namespace thread
-{
-
-Mutex::Mutex()
-{
-	pthread_mutex_init(&mutex, NULL);
-}
-
-Mutex::~Mutex()
-{
-	pthread_mutex_destroy(&mutex);
-}
-
-void Mutex::lock()
-{
-	pthread_mutex_lock(&mutex);
-}
-
-void Mutex::unlock()
-{
-	pthread_mutex_unlock(&mutex);
-}
-
-void *ThreadBase::thread_runner(void *param)
-{
-	ThreadBase *thread = (ThreadBase *)param;
-	thread->main();
-	return NULL;
-}
-
-ThreadBase::ThreadBase()
-	: running(false)
-{
-	pthread_t thread;
-}
-
-ThreadBase::~ThreadBase()
-{
-	if (running)
-	{
-		wait();
-	}
-}
-
-bool ThreadBase::start()
-{
-	if (pthread_create(&thread, NULL, thread_runner, this))
-		return false;
-	return (running = true);
-}
-
-void ThreadBase::wait()
-{
-	pthread_join(thread, NULL);
-	running = false;
-}
-
-void ThreadBase::kill()
-{
-	pthread_kill(thread, 9);
-	running = false;
-}
-
-unsigned int ThreadBase::threadId()
-{
-	return (unsigned int)((size_t)pthread_self());
-}
-
-Semaphore::Semaphore(unsigned int initial_value)
-{
-	sem_init(&sem, 0, initial_value);
-}
-
-Semaphore::~Semaphore()
-{
-	sem_destroy(&sem);
-}
-
-unsigned int Semaphore::value()
-{
-	int val = 0;
-	if (sem_getvalue(&sem, &val))
-		return 0;
-	else
-		return val;
-}
-
-void Semaphore::post()
-{
-	sem_post(&sem);
-}
-
-bool Semaphore::wait(int timeout)
-{
-	if (timeout < 0)
-		return !sem_wait(&sem);
-	else if (timeout == 0)
-		return !sem_trywait(&sem);
-	else
-	{
-#ifdef LOVE_MACOSX
-		// OS X lacks sem_timedwait, so we fake it with a busy loop
-		time_t timer = time(NULL)*1000 + timeout;
-		int retval = 0;
-		do
-		{
-			retval = sem_trywait(&sem);
-			if (retval == 0) break;
-			delay(1);
-		}
-		while (time(NULL)*1000 < timer);
-		return !retval;
-#else
-		struct timespec ts;
-		ts.tv_sec = timeout/1000;
-		ts.tv_nsec = (timeout % 1000) * 1000000;
-		return !sem_timedwait(&sem, &ts);
-#endif
-	}
-}
-
-bool Semaphore::tryWait()
-{
-	return !sem_trywait(&sem);
-}
-
-Conditional::Conditional()
-{
-	pthread_cond_init(&cond, NULL);
-}
-
-Conditional::~Conditional()
-{
-	pthread_cond_destroy(&cond);
-}
-
-void Conditional::signal()
-{
-	pthread_cond_signal(&cond);
-}
-
-void Conditional::broadcast()
-{
-	pthread_cond_broadcast(&cond);
-}
-
-bool Conditional::wait(Mutex *mutex, int timeout)
-{
-	if (timeout < 0)
-		return !pthread_cond_wait(&cond, &mutex->mutex);
-	else
-	{
-		struct timespec ts;
-		int ret;
-
-		ts.tv_sec = timeout / 1000;
-		ts.tv_nsec = (timeout % 1000) * 1000000;
-
-		ret = pthread_cond_timedwait(&cond, &mutex->mutex, &ts);
-		return (ret == 0);
-	}
-}
-} // thread
-} // love
-

src/modules/thread/posix/threads.h

-/**
- * Copyright (c) 2006-2012 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty.  In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- *    claim that you wrote the original software. If you use this software
- *    in a product, an acknowledgment in the product documentation would be
- *    appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
-
-#ifndef LOVE_THREAD_POSIX_THREADS_H
-#define LOVE_THREAD_POSIX_THREADS_H
-
-#include <pthread.h>
-#include <semaphore.h>
-#include <signal.h>
-
-namespace love
-{
-namespace thread
-{
-class Mutex
-{
-public:
-	Mutex();
-	~Mutex();
-
-	void lock();
-	void unlock();
-
-private:
-	pthread_mutex_t mutex;
-	Mutex(const Mutex &mutex) {}
-
-	friend class Conditional;
-};
-
-
-class ThreadBase
-{
-public:
-	ThreadBase();
-	virtual ~ThreadBase();
-
-	bool start();
-	void wait();
-	void kill();
-
-	static unsigned int threadId();
-
-protected:
-	virtual void main() = 0;
-
-private:
-	pthread_t thread;
-	ThreadBase(ThreadBase &thread) {}
-
-	static void *thread_runner(void *param);
-	bool running;
-};
-
-class Semaphore
-{
-public:
-	Semaphore(unsigned int initial_value);
-	~Semaphore();
-
-	unsigned int value();
-	void post();
-	bool wait(int timeout = -1);
-	bool tryWait();
-
-private:
-	Semaphore(const Semaphore &sem) {}
-	sem_t sem;
-};
-
-// Should conditional inherit from mutex?
-class Conditional
-{
-public:
-	Conditional();
-	~Conditional();
-
-	void signal();
-	void broadcast();
-	bool wait(Mutex *mutex, int timeout=-1);
-
-private:
-	pthread_cond_t cond;
-};
-
-} // thread
-} // love
-
-
-#endif // LOVE_THREAD_POSIX_THREADS_H

src/modules/thread/sdl/Thread.cpp

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "Thread.h"
+
+namespace love
+{
+namespace thread
+{
+namespace sdl
+{
+	Thread::Thread(Threadable *t)
+		: t(t), running(false), thread(0)
+	{
+	}
+
+	Thread::~Thread()
+	{
+		if (!running) // Clean up handle
+			wait();
+		/*
+		if (running)
+			wait();
+		FIXME: Needed for proper thread cleanup
+		*/
+	}
+
+	bool Thread::start()
+	{
+		Lock l(mutex);
+		if (running)
+			return false;
+		if (thread) // Clean old handle up
+			SDL_WaitThread(thread, 0);
+		thread = SDL_CreateThread(thread_runner, this);
+		running = (thread != 0);
+		return running;
+	}
+
+	void Thread::wait()
+	{
+		mutex.lock();
+		if (!thread)
+			return;
+		mutex.unlock();
+		SDL_WaitThread(thread, 0);
+		Lock l(mutex);
+		running = false;
+		thread = 0;
+	}
+
+	void Thread::kill()
+	{
+		Lock l(mutex);
+		if (!running)
+			return;
+		SDL_KillThread(thread);
+		SDL_WaitThread(thread, 0);
+		running = false;
+		thread = 0;
+	}
+
+	int Thread::thread_runner(void *data)
+	{
+		Thread *self = (Thread*) data; // some compilers don't like 'this'
+		self->t->threadFunction();
+		Lock l(self->mutex);
+		self->running = false;
+		return 0;
+	}
+} // sdl
+} // thread
+} // love

src/modules/thread/sdl/Thread.h

+/**
+* Copyright (c) 2006-2012 LOVE Development Team
+*
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+*
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+*
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_THREAD_SDL_THREAD_H
+#define LOVE_THREAD_SDL_THREAD_H
+
+// LOVE
+#include <thread/Thread.h>
+#include "threads.h"
+
+// SDL
+#include <SDL_thread.h>
+
+namespace love
+{
+namespace thread
+{
+namespace sdl
+{
+	class Thread : public thread::Thread
+	{
+	private:
+		Threadable *t;
+		bool running;
+		SDL_Thread *thread;
+		static int thread_runner(void *data);
+		Mutex mutex;
+
+	public:
+		Thread(Threadable *t);
+		~Thread();
+		bool start();
+		void kill();
+		void wait();
+	}; // Thread
+} // sdl
+} // thread
+} // love
+
+#endif // LOVE_THREAD_SDL_THREAD_H

src/modules/thread/sdl/threads.cpp

  **/
 
 #include "threads.h"
+#include "Thread.h"
 
 namespace love
 {
 namespace thread
 {
-
+namespace sdl
+{
 Mutex::Mutex()
 {
 	mutex = SDL_CreateMutex();
 	SDL_mutexV(mutex);
 }
 
-int ThreadBase::thread_runner(void *param)
-{
-	ThreadBase *thread = (ThreadBase *)param;
-	thread->main();
-	return 0;
-}
-
-ThreadBase::ThreadBase()
-	: running(false)
-{
-}
-
-ThreadBase::~ThreadBase()
-{
-	if (running)
-	{
-		wait();
-	}
-}
-
-bool ThreadBase::start()
-{
-	thread = SDL_CreateThread(thread_runner, this);
-	if (thread == NULL)
-		return false;
-	else
-		return (running = true);
-}
-
-void ThreadBase::wait()
-{
-	SDL_WaitThread(thread, NULL);
-	running = false;
-}
-
-void ThreadBase::kill()
-{
-	SDL_KillThread(thread);
-	running = false;
-}
-
-unsigned int ThreadBase::threadId()
-{
-	return (unsigned int)SDL_ThreadID();
-}
-
-Semaphore::Semaphore(unsigned int initial_value)
-{
-	semaphore = SDL_CreateSemaphore(initial_value);
-}
-
-Semaphore::~Semaphore()
-{
-	SDL_DestroySemaphore(semaphore);
-}
-
-unsigned int Semaphore::value()
-{
-	return SDL_SemValue(semaphore);
-}
-
-void Semaphore::post()
-{
-	SDL_SemPost(semaphore);
-}
-
-bool Semaphore::wait(int timeout)
-{
-	if (timeout < 0)
-		return SDL_SemWait(semaphore) ? false : true;
-	else if (timeout == 0)
-		return SDL_SemTryWait(semaphore) ? false : true;
-	else
-	{
-		int ret = SDL_SemWaitTimeout(semaphore, timeout);
-		return (ret == 0);
-	}
-}
-
-bool Semaphore::tryWait()
-{
-	return SDL_SemTryWait(semaphore) ? false : true;
-}
-
 Conditional::Conditional()
 {
 	cond = SDL_CreateCond();
 	SDL_CondBroadcast(cond);
 }
 
-bool Conditional::wait(Mutex *mutex, int timeout)
+bool Conditional::wait(thread::Mutex *_mutex, int timeout)
 {
+	// Yes, I realise this can be dangerous,
+	// however, you're asking for it if you're
+	// mixing thread implementations.
+	Mutex *mutex = (Mutex*) _mutex;
 	if (timeout < 0)
 		return !SDL_CondWait(cond, mutex->mutex);
 	else
 		return (SDL_CondWaitTimeout(cond, mutex->mutex, timeout) == 0);
 }
 
+} // sdl
+
+thread::Mutex *newMutex()
+{
+	return new sdl::Mutex();
+}
+
+thread::Conditional *newConditional()
+{
+	return new sdl::Conditional();
+}
+
+thread::Thread *newThread(Threadable *t)
+{
+	return new sdl::Thread(t);
+}
+
 } // thread
 } // love

src/modules/thread/sdl/threads.h

 #ifndef LOVE_THREAD_SDL_THREADS_H
 #define LOVE_THREAD_SDL_THREADS_H
 
-#include "SDL.h"
-#include "common/config.h"
+#include <common/config.h>
+#include <thread/threads.h>
+
+#include <SDL_thread.h>
 
 namespace love
 {
 namespace thread
 {
+namespace sdl
+{
+	class Conditional;
 
-class Mutex
-{
-public:
-	Mutex();
-	~Mutex();
+	class Mutex : public thread::Mutex
+	{
+	public:
+		Mutex();
+		~Mutex();
 
-	void lock();
-	void unlock();
-private:
-	SDL_mutex *mutex;
-	Mutex(const Mutex &/* mutex*/) {}
+		void lock();
+		void unlock();
 
-	friend class Conditional;
-};
+	private:
+		SDL_mutex* mutex;
+		Mutex(const Mutex&/* mutex*/) {}
 
+		friend class Conditional;
+	};
 
+	class Conditional : public thread::Conditional
+	{
+	public:
+		Conditional();
+		~Conditional();
 
-class ThreadBase
-{
-public:
-	ThreadBase();
-	virtual ~ThreadBase();
+		void signal();
+		void broadcast();
+		bool wait(thread::Mutex* mutex, int timeout=-1);
 
-	bool start();
-	void wait();
-	void kill(); // FIXME: not supported by SDL (SDL's kill is probably cancel)?
+	private:
+		SDL_cond* cond;
+	};
 
-	static unsigned int threadId();
-
-protected:
-	virtual void main() = 0;
-
-private:
-	SDL_Thread *thread;
-	ThreadBase(ThreadBase &/* thread*/) {}
-	bool running;
-
-	static int thread_runner(void *param);
-};
-
-class Semaphore
-{
-public:
-	Semaphore(unsigned int initial_value);
-	~Semaphore();
-
-	unsigned int value();
-	void post();
-	bool wait(int timeout = -1);
-	bool tryWait();
-
-private:
-	Semaphore(const Semaphore &/* sem*/) {}
-	SDL_sem *semaphore;
-};
-
-// Should conditional inherit from mutex?
-class Conditional
-{
-public:
-	Conditional();
-	~Conditional();
-
-	void signal();
-	void broadcast();
-	bool wait(Mutex *mutex, int timeout=-1);
-
-private:
-	SDL_cond *cond;
-};
-
+} // sdl
 } // thread
 } // love
 
-
-#endif // LOVE_THREAD_SDL_THREADS_H
+#endif /* LOVE_THREAD_SDL_THREADS_H */

src/modules/thread/threads.cpp

 
 #include "threads.h"
 
-#if LOVE_THREADS == LOVE_THREADS_POSIX
-#  include "posix/threads.cpp"
-#elif LOVE_THREADS == LOVE_THREADS_WIN32
-#  include "win32/threads.cpp"
-#elif LOVE_THREADS == LOVE_THREADS_SDL
-#  include "sdl/threads.cpp"
-#endif
-
 namespace love
 {
 namespace thread
 {
 
-const char *threadAPI()
+Lock::Lock(Mutex *m)
+	: mutex(m)
 {
-#if LOVE_THREADS == LOVE_THREADS_POSIX
-	return "posix";
-#elif LOVE_THREADS == LOVE_THREADS_WIN32
-	return "win32";
-#elif LOVE_THREADS == LOVE_THREADS_SDL
-	return "sdl";
-#endif
+	mutex->lock();
+}
+
+Lock::Lock(Mutex &m)
+	: mutex(&m)
+{
+	mutex->lock();
+}
+
+Lock::~Lock()
+{
+	mutex->unlock();
+}
+
+Threadable::Threadable()
+{
+	owner = newThread(this);
+}
+
+Threadable::~Threadable()
+{
+	delete owner;
+}