From a68e798a891361abe074a95e05960dd54cc45840 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Wed, 20 Feb 2013 05:10:38 +0000 Subject: [PATCH] boost::asio for resource manager threads --- include/SalmonEngineInterface.h | 6 +++--- include/Utils/ThreadUtils.h | 7 ++++++- include/Utils/ThreadUtilsImpl.h | 16 +++++++++++++++- include/Utils/WinApi/WinApi.h | 4 +++- src/SalmonEngineInterface.cpp | 8 +++++--- src/Utils/ThreadUtils.cpp | 13 +++++++++++++ src/Utils/WinApi/WinApi.cpp | 4 +++- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/include/SalmonEngineInterface.h b/include/SalmonEngineInterface.h index fd49bac..b77c955 100644 --- a/include/SalmonEngineInterface.h +++ b/include/SalmonEngineInterface.h @@ -55,10 +55,10 @@ public: boost::thread::id MainThreadId; - std::vector> MainThreadAsyncFunctionArr; + //std::vector> MainThreadAsyncFunctionArr; - boost::mutex FuncListMutex; - std::list MainThreadSyncFunctionList; + //boost::mutex FuncListMutex; + //std::list MainThreadSyncFunctionList; TTextureListClass TexList; TModelManager ModelManager; diff --git a/include/Utils/ThreadUtils.h b/include/Utils/ThreadUtils.h index eb9f321..06ca904 100644 --- a/include/Utils/ThreadUtils.h +++ b/include/Utils/ThreadUtils.h @@ -1,13 +1,18 @@ #ifndef THREAD_UTILS_H_INCLUDED #define THREAD_UTILS_H_INCLUDED +#include "boost/asio.hpp" #include "boost/signal.hpp" #include "boost/thread.hpp" namespace SE { + + extern boost::asio::io_service MainThreadIoService; + #ifndef UTILS_ENGINE + /* struct TFuncToPerform { private: @@ -23,7 +28,7 @@ namespace SE std::shared_ptr LockerPtr; boost::function Func; - }; + };*/ void AssertIfInMainThread(); diff --git a/include/Utils/ThreadUtilsImpl.h b/include/Utils/ThreadUtilsImpl.h index 43bf316..6c6d11a 100644 --- a/include/Utils/ThreadUtilsImpl.h +++ b/include/Utils/ThreadUtilsImpl.h @@ -24,7 +24,21 @@ namespace SE template RETURNTYPE PerformInMainThread(boost::function f) { + if (boost::this_thread::get_id() == ResourceManager->MainThreadId) + { + return f(); + } + else + { + RETURNTYPE result; + boost::function cover_f = [&result, f]() { result = f(); }; + + MainThreadIoService.post(cover_f); + + return result; + } + /* if (boost::this_thread::get_id() == ResourceManager->MainThreadId) { return f(); @@ -57,7 +71,7 @@ namespace SE return result; - } + }*/ } } //namespace SE diff --git a/include/Utils/WinApi/WinApi.h b/include/Utils/WinApi/WinApi.h index 0f81a7a..bb96fdb 100644 --- a/include/Utils/WinApi/WinApi.h +++ b/include/Utils/WinApi/WinApi.h @@ -15,10 +15,12 @@ Here goes all functions that are platform-specific When I make iOS/Mac/Linux port, I will make same API pair h/cpp */ + //Not in use, wow + /* void GetWindowWidthHeight(int& width, int& height); int GetWindowWidth(); -int GetWindowHeight(); +int GetWindowHeight();*/ } //namespace SE \ No newline at end of file diff --git a/src/SalmonEngineInterface.cpp b/src/SalmonEngineInterface.cpp index 6a57f2c..4fa88ea 100644 --- a/src/SalmonEngineInterface.cpp +++ b/src/SalmonEngineInterface.cpp @@ -25,12 +25,12 @@ TResourceManager* ResourceManager; void TResourceManager::Update(cardinal timer) { - FuncListMutex.lock(); + //FuncListMutex.lock(); SoundManager.Update(timer); GUIManager.Update(timer); - + /* if (MainThreadAsyncFunctionArr.size() != 0) { MainThreadAsyncFunctionArr[0](); @@ -52,7 +52,9 @@ void TResourceManager::Update(cardinal timer) itr->LockerPtr->unlock(); } - FuncListMutex.unlock(); + FuncListMutex.unlock();*/ + MainThreadIoService.run(); + MainThreadIoService.reset(); } TResourceManager::~TResourceManager() diff --git a/src/Utils/ThreadUtils.cpp b/src/Utils/ThreadUtils.cpp index 52894d3..260c7b1 100644 --- a/src/Utils/ThreadUtils.cpp +++ b/src/Utils/ThreadUtils.cpp @@ -3,6 +3,8 @@ namespace SE { + boost::asio::io_service MainThreadIoService; + #ifndef UTILS_ENGINE void AssertIfInMainThread() { @@ -22,6 +24,7 @@ namespace SE void PerformInMainThreadAsync(boost::function f) { + /* if (boost::this_thread::get_id() == ResourceManager->MainThreadId) { f(); @@ -29,7 +32,17 @@ namespace SE else { ResourceManager->MainThreadAsyncFunctionArr.push_back(f); + }*/ + if (boost::this_thread::get_id() == ResourceManager->MainThreadId) + { + f(); } + else + { + MainThreadIoService.dispatch(f); + } + + } #endif diff --git a/src/Utils/WinApi/WinApi.cpp b/src/Utils/WinApi/WinApi.cpp index 63a3a22..c2db88f 100644 --- a/src/Utils/WinApi/WinApi.cpp +++ b/src/Utils/WinApi/WinApi.cpp @@ -8,6 +8,8 @@ Here goes all functions that are platform-specific When I make iOS/Mac/Linux port, I will make same API pair h/cpp */ + //Not in use, wow + /* void GetWindowWidthHeight(HWND hwnd, int& width, int& height) { RECT rc; @@ -32,5 +34,5 @@ int GetWindowHeight(HWND hwnd) return rc.bottom; } - +*/ } //namespace SE \ No newline at end of file