boost::asio for resource manager threads

This commit is contained in:
Vladislav Khorev 2013-02-20 05:10:38 +00:00
parent 06652bfa0d
commit a68e798a89
7 changed files with 48 additions and 10 deletions

View File

@ -55,10 +55,10 @@ public:
boost::thread::id MainThreadId; boost::thread::id MainThreadId;
std::vector<boost::function<void()>> MainThreadAsyncFunctionArr; //std::vector<boost::function<void()>> MainThreadAsyncFunctionArr;
boost::mutex FuncListMutex; //boost::mutex FuncListMutex;
std::list<TFuncToPerform> MainThreadSyncFunctionList; //std::list<TFuncToPerform> MainThreadSyncFunctionList;
TTextureListClass TexList; TTextureListClass TexList;
TModelManager ModelManager; TModelManager ModelManager;

View File

@ -1,13 +1,18 @@
#ifndef THREAD_UTILS_H_INCLUDED #ifndef THREAD_UTILS_H_INCLUDED
#define THREAD_UTILS_H_INCLUDED #define THREAD_UTILS_H_INCLUDED
#include "boost/asio.hpp"
#include "boost/signal.hpp" #include "boost/signal.hpp"
#include "boost/thread.hpp" #include "boost/thread.hpp"
namespace SE namespace SE
{ {
extern boost::asio::io_service MainThreadIoService;
#ifndef UTILS_ENGINE #ifndef UTILS_ENGINE
/*
struct TFuncToPerform struct TFuncToPerform
{ {
private: private:
@ -23,7 +28,7 @@ namespace SE
std::shared_ptr<boost::mutex> LockerPtr; std::shared_ptr<boost::mutex> LockerPtr;
boost::function<void()> Func; boost::function<void()> Func;
}; };*/
void AssertIfInMainThread(); void AssertIfInMainThread();

View File

@ -24,7 +24,21 @@ namespace SE
template<typename RETURNTYPE> template<typename RETURNTYPE>
RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f) RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f)
{ {
if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{
return f();
}
else
{
RETURNTYPE result;
boost::function<void()> cover_f = [&result, f]() { result = f(); };
MainThreadIoService.post(cover_f);
return result;
}
/*
if (boost::this_thread::get_id() == ResourceManager->MainThreadId) if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{ {
return f(); return f();
@ -57,7 +71,7 @@ namespace SE
return result; return result;
} }*/
} }
} //namespace SE } //namespace SE

View File

@ -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 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); void GetWindowWidthHeight(int& width, int& height);
int GetWindowWidth(); int GetWindowWidth();
int GetWindowHeight(); int GetWindowHeight();*/
} //namespace SE } //namespace SE

View File

@ -25,12 +25,12 @@ TResourceManager* ResourceManager;
void TResourceManager::Update(cardinal timer) void TResourceManager::Update(cardinal timer)
{ {
FuncListMutex.lock(); //FuncListMutex.lock();
SoundManager.Update(timer); SoundManager.Update(timer);
GUIManager.Update(timer); GUIManager.Update(timer);
/*
if (MainThreadAsyncFunctionArr.size() != 0) if (MainThreadAsyncFunctionArr.size() != 0)
{ {
MainThreadAsyncFunctionArr[0](); MainThreadAsyncFunctionArr[0]();
@ -52,7 +52,9 @@ void TResourceManager::Update(cardinal timer)
itr->LockerPtr->unlock(); itr->LockerPtr->unlock();
} }
FuncListMutex.unlock(); FuncListMutex.unlock();*/
MainThreadIoService.run();
MainThreadIoService.reset();
} }
TResourceManager::~TResourceManager() TResourceManager::~TResourceManager()

View File

@ -3,6 +3,8 @@
namespace SE namespace SE
{ {
boost::asio::io_service MainThreadIoService;
#ifndef UTILS_ENGINE #ifndef UTILS_ENGINE
void AssertIfInMainThread() void AssertIfInMainThread()
{ {
@ -22,6 +24,7 @@ namespace SE
void PerformInMainThreadAsync(boost::function<void()> f) void PerformInMainThreadAsync(boost::function<void()> f)
{ {
/*
if (boost::this_thread::get_id() == ResourceManager->MainThreadId) if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{ {
f(); f();
@ -29,7 +32,17 @@ namespace SE
else else
{ {
ResourceManager->MainThreadAsyncFunctionArr.push_back(f); ResourceManager->MainThreadAsyncFunctionArr.push_back(f);
}*/
if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{
f();
} }
else
{
MainThreadIoService.dispatch(f);
}
} }
#endif #endif

View File

@ -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 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) void GetWindowWidthHeight(HWND hwnd, int& width, int& height)
{ {
RECT rc; RECT rc;
@ -32,5 +34,5 @@ int GetWindowHeight(HWND hwnd)
return rc.bottom; return rc.bottom;
} }
*/
} //namespace SE } //namespace SE