This commit is contained in:
1 parent
a8e01ed69d
commit
c9b7f21be5
11 files changed
+228
-13
No files matched your search
@@ -0,0 +1,36 @@
|
||||
#ifndef THREAD_UTILS_H_INCLUDED
|
||||
#define THREAD_UTILS_H_INCLUDED
|
||||
|
||||
#include "boost/signal.hpp"
|
||||
#include "boost/thread.hpp"
|
||||
|
||||
|
||||
namespace SE
|
||||
{
|
||||
|
||||
struct TFuncToPerform
|
||||
{
|
||||
private:
|
||||
public:
|
||||
|
||||
TFuncToPerform()
|
||||
: LockerPtr(new boost::mutex)
|
||||
{
|
||||
}
|
||||
|
||||
bool Executed;
|
||||
|
||||
std::shared_ptr<boost::mutex> LockerPtr;
|
||||
|
||||
boost::function<void()> Func;
|
||||
};
|
||||
|
||||
void AssertIfInMainThread();
|
||||
void PerformInMainThreadAsync(boost::function<void()> f);
|
||||
|
||||
template<typename RETURNTYPE>
|
||||
RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f);
|
||||
|
||||
} //namespace SE
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
#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;
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace SE
|
||||
|
||||
#endif
|
||||
@@ -25,6 +25,7 @@ This code combines additional routines (such as console/log, exceptions, math ut
|
||||
|
||||
#include "include/Utils/BindableVar.h"
|
||||
#include "include/Utils/SimpleTimer.h"
|
||||
#include "include/Utils/ThreadUtils.h"
|
||||
|
||||
#ifdef TARGET_WIN32
|
||||
#include "WinApi/WinApi.h"
|
||||
|
||||
Reference in new issue
Block a user