Commits

Alex Szpakowski  committed 9865d12

Added love.thread.setPriority

  • Participants
  • Parent commits 6bf7373
  • Branches SDL2

Comments (0)

Files changed (8)

File platform/macosx/love-framework.xcodeproj/project.pbxproj

 		FAEC808B1710FEA60057279A /* ImageData.h in Headers */ = {isa = PBXBuildFile; fileRef = FAEC80891710FEA60057279A /* ImageData.h */; };
 		FAEC808E1711E76C0057279A /* CompressedData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAEC808C1711E76C0057279A /* CompressedData.cpp */; };
 		FAEC808F1711E76C0057279A /* CompressedData.h in Headers */ = {isa = PBXBuildFile; fileRef = FAEC808D1711E76C0057279A /* CompressedData.h */; };
+		FAEEB1641782AC7600FE30E6 /* ThreadModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAEEB1621782AC7600FE30E6 /* ThreadModule.cpp */; };
+		FAEEB1651782AC7600FE30E6 /* ThreadModule.h in Headers */ = {isa = PBXBuildFile; fileRef = FAEEB1631782AC7600FE30E6 /* ThreadModule.h */; };
 		FAF272A416E3D44400CC193A /* Channel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAF2729816E3D44400CC193A /* Channel.cpp */; };
 		FAF272A516E3D44400CC193A /* Channel.h in Headers */ = {isa = PBXBuildFile; fileRef = FAF2729916E3D44400CC193A /* Channel.h */; };
 		FAF272A616E3D44400CC193A /* LuaThread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAF2729A16E3D44400CC193A /* LuaThread.cpp */; };
 		FAEC80891710FEA60057279A /* ImageData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageData.h; sourceTree = "<group>"; };
 		FAEC808C1711E76C0057279A /* CompressedData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CompressedData.cpp; sourceTree = "<group>"; };
 		FAEC808D1711E76C0057279A /* CompressedData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompressedData.h; sourceTree = "<group>"; };
+		FAEEB1621782AC7600FE30E6 /* ThreadModule.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadModule.cpp; sourceTree = "<group>"; };
+		FAEEB1631782AC7600FE30E6 /* ThreadModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadModule.h; sourceTree = "<group>"; };
 		FAF2729816E3D44400CC193A /* Channel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Channel.cpp; sourceTree = "<group>"; };
 		FAF2729916E3D44400CC193A /* Channel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Channel.h; sourceTree = "<group>"; };
 		FAF2729A16E3D44400CC193A /* LuaThread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LuaThread.cpp; sourceTree = "<group>"; };
 			children = (
 				FAF272B116E3D46400CC193A /* Thread.cpp */,
 				FAF272B216E3D46400CC193A /* Thread.h */,
+				FAEEB1621782AC7600FE30E6 /* ThreadModule.cpp */,
+				FAEEB1631782AC7600FE30E6 /* ThreadModule.h */,
 				FAF272B316E3D46400CC193A /* threads.cpp */,
 				FAF272B416E3D46400CC193A /* threads.h */,
 			);
 				FAB007981740C87D00A9664D /* wrap_Joystick.h in Headers */,
 				FAC5710117402D1100D147E4 /* BezierCurve.h in Headers */,
 				FAC5710317402D1100D147E4 /* wrap_BezierCurve.h in Headers */,
+				FAEEB1651782AC7600FE30E6 /* ThreadModule.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 				FAB007971740C87D00A9664D /* wrap_Joystick.cpp in Sources */,
 				FAC5710017402D1100D147E4 /* BezierCurve.cpp in Sources */,
 				FAC5710217402D1100D147E4 /* wrap_BezierCurve.cpp in Sources */,
+				FAEEB1641782AC7600FE30E6 /* ThreadModule.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

File src/modules/thread/ThreadModule.cpp

 {
 namespace thread
 {
-const char *ThreadModule::getName() const
-{
-	return "love.thread.sdl";
-}
 
 LuaThread *ThreadModule::newThread(const std::string &name, love::Data *data)
 {
 {
 	return Channel::getChannel(name);
 }
+
+bool ThreadModule::getConstant(const char *in, Priority &out)
+{
+	return priorities.find(in, out);
+}
+
+bool ThreadModule::getConstant(Priority in, const char *&out)
+{
+	return priorities.find(in, out);
+}
+
+StringMap<ThreadModule::Priority, ThreadModule::PRIORITY_MAX_ENUM>::Entry ThreadModule::priorityEntries[] =
+{
+	{"low", ThreadModule::PRIORITY_LOW},
+	{"normal", ThreadModule::PRIORITY_NORMAL},
+	{"high", ThreadModule::PRIORITY_HIGH},
+};
+
+StringMap<ThreadModule::Priority, ThreadModule::PRIORITY_MAX_ENUM> ThreadModule::priorities(ThreadModule::priorityEntries, sizeof(ThreadModule::priorityEntries));
+
 } // thread
 } // love

File src/modules/thread/ThreadModule.h

 #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>
+#include "common/Data.h"
+#include "common/Module.h"
+#include "common/StringMap.h"
+
+#include "Thread.h"
+#include "Channel.h"
+#include "LuaThread.h"
+#include "threads.h"
 
 namespace love
 {
 class ThreadModule : public love::Module
 {
 public:
+
+	// Thread priority levels.
+	enum Priority
+	{
+		PRIORITY_LOW,
+		PRIORITY_NORMAL,
+		PRIORITY_HIGH,
+		PRIORITY_MAX_ENUM
+	};
+
 	virtual ~ThreadModule() {}
-	LuaThread *newThread(const std::string &name, love::Data *data);
-	Channel *newChannel();
-	Channel *getChannel(const std::string &name);
+	virtual LuaThread *newThread(const std::string &name, love::Data *data);
+	virtual Channel *newChannel();
+	virtual Channel *getChannel(const std::string &name);
 
-	const char *getName() const;
+	virtual bool setPriority(Priority priority) = 0;
+
+	static bool getConstant(const char *in, Priority &out);
+	static bool getConstant(Priority in, const char *&out);
+
+private:
+
+	static StringMap<Priority, PRIORITY_MAX_ENUM>::Entry priorityEntries[];
+	static StringMap<Priority, PRIORITY_MAX_ENUM> priorities;
+
 }; // ThreadModule
 } // thread
 } // love

File src/modules/thread/sdl/Thread.cpp

 	Lock l(mutex);
 	if (!running)
 		return;
-//	SDL_KillThread(thread); // FIXME?
 	SDL_WaitThread(thread, 0);
 	running = false;
 	thread = 0;

File src/modules/thread/sdl/ThreadModule.cpp

+/**
+ * Copyright (c) 2006-2013 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.
+ **/
+
+// LOVE
+#include "ThreadModule.h"
+
+// SDL
+#include <SDL_thread.h>
+
+namespace love
+{
+namespace thread
+{
+namespace sdl
+{
+
+bool ThreadModule::setPriority(Priority priority)
+{
+	SDL_ThreadPriority sdlpriority;
+
+	switch (priority)
+	{
+	case ThreadModule::PRIORITY_LOW:
+		sdlpriority = SDL_THREAD_PRIORITY_LOW;
+		break;
+	case ThreadModule::PRIORITY_NORMAL:
+	default:
+		sdlpriority = SDL_THREAD_PRIORITY_NORMAL;
+		break;
+	case ThreadModule::PRIORITY_HIGH:
+		sdlpriority = SDL_THREAD_PRIORITY_HIGH;
+		break;
+	}
+
+	return SDL_SetThreadPriority(sdlpriority) >= 0;
+}
+
+const char *ThreadModule::getName() const
+{
+	return "love.thread.sdl";
+}
+
+} // sdl
+} // thread
+} // love

File src/modules/thread/sdl/ThreadModule.h

+/**
+ * Copyright (c) 2006-2013 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_THREADMODULE_H
+#define LOVE_THREAD_SDL_THREADMODULE_H
+
+#include "thread/ThreadModule.h"
+
+namespace love
+{
+namespace thread
+{
+namespace sdl
+{
+
+class ThreadModule : public love::thread::ThreadModule
+{
+public:
+
+	virtual bool setPriority(Priority priority);
+
+	// Implements Module.
+	virtual const char *getName() const;
+
+private:
+
+}; // ThreadModule
+
+} // sdl
+} // thread
+} // love
+
+#endif // LOVE_THREAD_SDL_THREADMODULE_H

File src/modules/thread/wrap_ThreadModule.cpp

 * 3. This notice may not be removed or altered from any source distribution.
 **/
 
+// LOVE
 #include "wrap_ThreadModule.h"
 #include "wrap_LuaThread.h"
 #include "wrap_Channel.h"
-#include <filesystem/File.h>
-#include <filesystem/FileData.h>
+#include "thread/sdl/ThreadModule.h"
+
+#include "filesystem/File.h"
+#include "filesystem/FileData.h"
 
 namespace love
 {
 namespace thread
 {
+
 static ThreadModule *instance = 0;
 
 int w_newThread(lua_State *L)
 	return 1;
 }
 
+int w_setPriority(lua_State *L)
+{
+	const char *str = luaL_checkstring(L, 1);
+	ThreadModule::Priority priority;
+
+	if (!ThreadModule::getConstant(str, priority))
+		return luaL_error(L, "Invalid thread priority: %s", str);
+
+	luax_pushboolean(L, instance->setPriority(priority));
+	return 1;
+}
+
 // List of functions to wrap.
 static const luaL_Reg module_functions[] = {
 	{ "newThread", w_newThread },
 	{ "newChannel", w_newChannel },
 	{ "getChannel", w_getChannel },
+	{ "setPriority", w_setPriority },
 	{ 0, 0 }
 };
 
 	{
 		try
 		{
-			instance = new ThreadModule();
+			instance = new love::thread::sdl::ThreadModule();
 		}
 		catch (Exception & e)
 		{
 
 	return luax_register_module(L, w);
 }
-}
-}
+
+} // thread
+} // love

File src/modules/thread/wrap_ThreadModule.h

 #define LOVE_THREAD_WRAP_THREADMODULE_H
 
 // LOVE
-#include "ThreadModule.h"
+#include "common/config.h"
+#include "common/runtime.h"
 
 namespace love
 {
 int w_newThread(lua_State *L);
 int w_newChannel(lua_State *L);
 int w_getChannel(lua_State *L);
+int w_setPriority(lua_State *L);
 
 extern "C" LOVE_EXPORT int luaopen_love_thread(lua_State * L);