IoService included

This commit is contained in:
Vladislav Khorev 2013-02-21 08:35:47 +00:00
parent 407ee3868f
commit 61ada33ee1
3 changed files with 36 additions and 50 deletions

View File

@ -12,7 +12,7 @@ namespace SE
extern boost::asio::io_service MainThreadIoService; extern boost::asio::io_service MainThreadIoService;
#ifndef UTILS_ENGINE #ifndef UTILS_ENGINE
/*
struct TFuncToPerform struct TFuncToPerform
{ {
private: private:
@ -23,13 +23,10 @@ namespace SE
{ {
} }
bool Executed;
std::shared_ptr<boost::mutex> LockerPtr; std::shared_ptr<boost::mutex> LockerPtr;
boost::function<void()> Func; boost::function<void()> Func;
};*/ };
void AssertIfInMainThread(); void AssertIfInMainThread();
void TryUpdateMainThreadId(); void TryUpdateMainThreadId();

View File

@ -7,23 +7,10 @@
namespace SE namespace SE
{ {
template<typename RETURNTYPE>
struct TCoverFunc
{
public:
typedef void result_type;
boost::function<RETURNTYPE()> Func;
void operator()(RETURNTYPE& rx)
{
rx = Func();
}
};
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) if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{ {
return f(); return f();
@ -48,8 +35,8 @@ namespace SE
ServiceLock.unlock(); ServiceLock.unlock();
return result; return result;
} }*/
/*
if (boost::this_thread::get_id() == ResourceManager->MainThreadId) if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{ {
return f(); return f();
@ -58,31 +45,25 @@ namespace SE
{ {
RETURNTYPE result; RETURNTYPE result;
TCoverFunc<RETURNTYPE> cover_f; boost::mutex serviceLock;
cover_f.Func = f; boost::function<void()> func =
[&result, &f, &serviceLock] ()
{
result = f();
serviceLock.unlock();
};
TFuncToPerform funcToPerform; serviceLock.lock();
funcToPerform.Executed = false; MainThreadIoService.post(func);
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();
serviceLock.unlock();
return result; return result;
}*/ }
} }
} //namespace SE } //namespace SE

View File

@ -24,25 +24,33 @@ 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();
} }
else else
{ {
ResourceManager->MainThreadAsyncFunctionArr.push_back(f);
}*/ boost::mutex serviceLock;
if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{ boost::function<void()> func =
f(); [&f, &serviceLock] ()
} {
else f();
{ serviceLock.unlock();
MainThreadIoService.post(f); };
serviceLock.lock();
MainThreadIoService.post(func);
serviceLock.lock();
serviceLock.unlock();
} }
} }
#endif #endif