Explorar o código

- Adding the job manager

Panagiotis Christopoulos Charitos %!s(int64=14) %!d(string=hai) anos
pai
achega
6efda7acde

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1 - 1
build/debug/Makefile


+ 0 - 1
src/Core/App.cpp

@@ -153,7 +153,6 @@ void App::initWindow()
 
 	glContext = SDL_GL_CreateContext(windowId);
 
-
 	// the icon
 	iconImage = SDL_LoadBMP("gfx/icon.bmp");
 	if(iconImage == NULL)

+ 2 - 1
src/Core/App.h

@@ -64,9 +64,10 @@ class App
 		GETTER_R(uint, windowH, getWindowHeight)
 		GETTER_R(boost::filesystem::path, settingsPath, getSettingsPath)
 		GETTER_R(boost::filesystem::path, cachePath, getCachePath)
+		GETTER_RW(SDL_WindowID, windowId, getWindowId)
 		/// @}
 
-	public: /// @todo
+	private:
 		uint windowW; ///< The main window width
 		uint windowH; ///< The main window height
 		boost::filesystem::path settingsPath; ///< The path that holds the configuration

+ 5 - 5
src/Core/AsyncLoader.cpp

@@ -1,4 +1,3 @@
-#include <SDL/SDL.h>
 #include "AsyncLoader.h"
 #include "Logger.h"
 #include "App.h"
@@ -34,10 +33,11 @@ void AsyncLoader::load(const char* filename, LoadCallback loadCallback, void* st
 //======================================================================================================================
 void AsyncLoader::workingFunc()
 {
-	/*SDL_GLContext glContext;
-	glContext = SDL_GL_CreateContext(AppSingleton::getInstance().windowId);
-
-	ERROR(SDL_GetError() << " " << glContext);*/
+	glContext = SDL_GL_CreateContext(AppSingleton::getInstance().getWindowId());
+	if(SDL_GL_MakeCurrent(AppSingleton::getInstance().getWindowId(), glContext) != 0)
+	{
+		throw EXCEPTION("Cannot select GL context");
+	}
 
 
 	while(1)

+ 3 - 0
src/Core/AsyncLoader.h

@@ -5,6 +5,7 @@
 #include <string>
 #include <boost/thread/thread.hpp>
 #include <boost/thread/mutex.hpp>
+#include <SDL/SDL.h>
 
 
 /// Asynchronous loader
@@ -56,6 +57,8 @@ class AsyncLoader
 			bool ok; ///< True if the loading was successful
 		};
 
+		SDL_GLContext glContext; ///< For OpenGL stuff
+
 		std::list<Request> requests;
 		std::list<Response> responses;
 		boost::mutex mutexReq; ///< Protect the requests container

+ 61 - 0
src/Core/JobManager.cpp

@@ -0,0 +1,61 @@
+#include "JobManager.h"
+
+
+//======================================================================================================================
+// assignNewJob                                                                                                        =
+//======================================================================================================================
+void WorkerThread::assignNewJob(JobCallback job_, void* jobParams_)
+{
+	boost::mutex::scoped_lock lock(mutex);
+	job = job_;
+	jobParams = jobParams_;
+
+	lock.unlock();
+	condVar.notify_one();
+}
+
+
+//======================================================================================================================
+// workingFunc                                                                                                         =
+//======================================================================================================================
+void WorkerThread::workingFunc()
+{
+	while(1)
+	{
+		// Wait for something
+		{
+			boost::mutex::scoped_lock lock(mutex);
+			while(job == NULL)
+			{
+				condVar.wait(lock);
+			}
+		}
+
+		// Exec
+		job(id, jobParams);
+
+		// Nullify
+		{
+			boost::mutex::scoped_lock lock(mutex);
+			job = NULL;
+		}
+
+		barrier->wait();
+	}
+}
+
+
+//======================================================================================================================
+// Constructor                                                                                                         =
+//======================================================================================================================
+JobManager::JobManager(uint threadsNum):
+	barrier(threadsNum + 1)
+{
+	for(uint i = 0; i < threadsNum; i++)
+	{
+		workers.push_back(new WorkerThread(i, &barrier));
+	}
+}
+
+
+

+ 85 - 0
src/Core/JobManager.h

@@ -0,0 +1,85 @@
+#ifndef JOB_MANAGER_H
+#define JOB_MANAGER_H
+
+#include <boost/thread.hpp>
+#include <boost/ptr_container/ptr_vector.hpp>
+
+
+/// @todo
+class WorkerThread
+{
+	public:
+		typedef void (*JobCallback)(uint jobId, void*);
+
+		/// Constructor
+		WorkerThread(int id, boost::barrier* barrier);
+
+		/// Assign new job to the thread
+		void assignNewJob(JobCallback job, void* jobParams);
+
+	private:
+		uint id; ///< An ID
+		boost::thread thread; ///< Runs the workingFunc
+		boost::mutex mutex; ///< Protect the WorkerThread::job
+		boost::condition_variable condVar; ///< To wake up the thread
+		boost::barrier* barrier;
+		JobCallback job; ///< Its NULL if there are no pending jobs
+		void* jobParams;
+
+		/// Start thread
+		void start();
+
+		/// Thread loop
+		void workingFunc();
+};
+
+
+inline WorkerThread::WorkerThread(int id_, boost::barrier* barrier_):
+	id(id_),
+	barrier(barrier_),
+	job(NULL)
+{
+	start();
+}
+
+
+inline void WorkerThread::start()
+{
+	thread = boost::thread(&WorkerThread::workingFunc, this);
+}
+
+
+/// The job manager
+class JobManager
+{
+	public:
+		/// Constructor
+		JobManager(uint threadsNum);
+
+		/// Assign a job to a working thread
+		void assignNewJob(uint threadId, WorkerThread::JobCallback job, void* jobParams);
+
+		/// Wait for all jobs to finish
+		void waitForAllJobsToFinish();
+
+		uint getThreadsNum() const {return workers.size();}
+
+	private:
+		boost::ptr_vector<WorkerThread> workers;
+		boost::barrier barrier;
+};
+
+
+inline void JobManager::assignNewJob(uint threadId, WorkerThread::JobCallback job, void* jobParams)
+{
+	workers[threadId].assignNewJob(job, jobParams);
+}
+
+
+inline void JobManager::waitForAllJobsToFinish()
+{
+	barrier.wait();
+}
+
+
+#endif

+ 3 - 2
src/Resources/Core/RsrcAsyncLoadingReqsHandler.h

@@ -29,8 +29,9 @@ class RsrcAsyncLoadingReqsHandler
 		void sendNewLoadingRequest(const char* filename, Type** objToLoad);
 		
 		/// Serve the finished requests. This should be called once every loop of the main loop
-		/// @param maxTime The max time to spend serving finished requests. If for example there are many that need more
-		/// time than the max the method will return. The pending requests will be served when it will be called again.
+		/// @param maxTime The max time to spend serving finished requests. If for example there are many that need
+		/// more time than the max the method will return. The pending requests will be served when it will be called
+		/// again.
 		/// In ms
 		void postProcessFinishedRequests(uint maxTime);
 	

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio