engine/include/Utils/ThreadUtilsImpl.h

90 lines
1.9 KiB
C
Raw Normal View History

2013-02-03 13:11:16 +00:00
#ifndef THREAD_UTILS_IMPL_H_INCLUDED
#define THREAD_UTILS_IMPL_H_INCLUDED
#include "include/Engine.h"
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>
RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f)
{
if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{
return f();
}
else
{
RETURNTYPE result;
2013-02-20 17:41:51 +00:00
boost::mutex ServiceLock;
2013-02-20 17:41:51 +00:00
ServiceLock.lock();
boost::function<void()> cover_f = [&result, &ServiceLock, f]()
{
result = f();
ServiceLock.unlock();
};
MainThreadIoService.post(cover_f);
2013-02-20 17:41:51 +00:00
ServiceLock.lock();
ServiceLock.unlock();
return result;
}
/*
2013-02-03 13:11:16 +00:00
if (boost::this_thread::get_id() == ResourceManager->MainThreadId)
{
return f();
}
else
{
RETURNTYPE result;
TCoverFunc<RETURNTYPE> cover_f;
cover_f.Func = f;
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();
return result;
}*/
2013-02-03 13:11:16 +00:00
}
} //namespace SE
#endif