From 61ada33ee16d7f1395037b219482b4359d5a1356 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Thu, 21 Feb 2013 08:35:47 +0000 Subject: [PATCH] IoService included --- include/Utils/ThreadUtils.h | 7 ++--- include/Utils/ThreadUtilsImpl.h | 51 +++++++++++---------------------- src/Utils/ThreadUtils.cpp | 28 +++++++++++------- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/include/Utils/ThreadUtils.h b/include/Utils/ThreadUtils.h index 06ca904..2013977 100644 --- a/include/Utils/ThreadUtils.h +++ b/include/Utils/ThreadUtils.h @@ -12,7 +12,7 @@ namespace SE extern boost::asio::io_service MainThreadIoService; #ifndef UTILS_ENGINE - /* + struct TFuncToPerform { private: @@ -23,13 +23,10 @@ namespace SE { } - bool Executed; - std::shared_ptr LockerPtr; boost::function Func; - };*/ - + }; void AssertIfInMainThread(); void TryUpdateMainThreadId(); diff --git a/include/Utils/ThreadUtilsImpl.h b/include/Utils/ThreadUtilsImpl.h index 9d086a3..45d1823 100644 --- a/include/Utils/ThreadUtilsImpl.h +++ b/include/Utils/ThreadUtilsImpl.h @@ -6,24 +6,11 @@ namespace SE { - - template - struct TCoverFunc - { - public: - typedef void result_type; - - boost::function Func; - - void operator()(RETURNTYPE& rx) - { - rx = Func(); - } - }; template RETURNTYPE PerformInMainThread(boost::function f) { + /* if (boost::this_thread::get_id() == ResourceManager->MainThreadId) { return f(); @@ -48,8 +35,8 @@ namespace SE ServiceLock.unlock(); return result; - } - /* + }*/ + if (boost::this_thread::get_id() == ResourceManager->MainThreadId) { return f(); @@ -58,31 +45,25 @@ namespace SE { RETURNTYPE result; - TCoverFunc cover_f; + boost::mutex serviceLock; - cover_f.Func = f; + boost::function func = + [&result, &f, &serviceLock] () + { + result = f(); + serviceLock.unlock(); + }; - TFuncToPerform funcToPerform; - - funcToPerform.Executed = false; - funcToPerform.Func = boost::bind(cover_f, boost::ref(result)); - funcToPerform.LockerPtr->lock(); - - ResourceManager->FuncListMutex.lock(); - auto itr = ResourceManager->MainThreadSyncFunctionList.insert(ResourceManager->MainThreadSyncFunctionList.end(), funcToPerform); - ResourceManager->FuncListMutex.unlock(); - - itr->LockerPtr->lock(); //wait until lock will be released - itr->LockerPtr->unlock(); - - ResourceManager->FuncListMutex.lock(); - ResourceManager->MainThreadSyncFunctionList.erase(itr); - ResourceManager->FuncListMutex.unlock(); + serviceLock.lock(); + MainThreadIoService.post(func); + serviceLock.lock(); + serviceLock.unlock(); + return result; - }*/ + } } } //namespace SE diff --git a/src/Utils/ThreadUtils.cpp b/src/Utils/ThreadUtils.cpp index 323e9bd..c1392e3 100644 --- a/src/Utils/ThreadUtils.cpp +++ b/src/Utils/ThreadUtils.cpp @@ -24,23 +24,31 @@ namespace SE void PerformInMainThreadAsync(boost::function f) { - /* + if (boost::this_thread::get_id() == ResourceManager->MainThreadId) { f(); } else { - ResourceManager->MainThreadAsyncFunctionArr.push_back(f); - }*/ - if (boost::this_thread::get_id() == ResourceManager->MainThreadId) - { - f(); - } - else - { - MainThreadIoService.post(f); + + boost::mutex serviceLock; + + boost::function func = + [&f, &serviceLock] () + { + f(); + serviceLock.unlock(); + }; + + + serviceLock.lock(); + MainThreadIoService.post(func); + + serviceLock.lock(); + serviceLock.unlock(); } + }