engine/include/Utils/ThreadUtilsImpl.h

60 lines
1.0 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
{
2013-03-04 19:09:56 +00:00
namespace ST
{
extern boost::condition_variable FunctionFinishedCondition;
extern boost::mutex FunctionMutex;
}
2013-02-03 13:11:16 +00:00
template<typename RETURNTYPE>
RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f)
{
2013-02-21 13:14:46 +00:00
if (boost::this_thread::get_id() == ST::MainThreadId)
2013-02-03 13:11:16 +00:00
{
return f();
}
else
{
RETURNTYPE result;
2013-03-04 19:09:56 +00:00
bool functionCalled = false;
2013-02-21 08:35:47 +00:00
boost::function<void()> func =
2013-03-04 19:09:56 +00:00
[&] ()
2013-02-21 08:35:47 +00:00
{
result = f();
2013-03-04 20:27:47 +00:00
2013-03-04 19:09:56 +00:00
{
2013-03-04 20:27:47 +00:00
boost::mutex::scoped_lock lock(ST::FunctionMutex);
2013-03-04 19:09:56 +00:00
functionCalled = true;
}
2013-03-04 20:27:47 +00:00
ST::FunctionFinishedCondition.notify_one();
2013-02-21 08:35:47 +00:00
};
2013-02-03 13:11:16 +00:00
2013-02-21 13:14:46 +00:00
ST::MainThreadIoService.post(func);
2013-02-03 13:11:16 +00:00
2013-03-04 20:27:47 +00:00
boost::mutex::scoped_lock lock(ST::FunctionMutex);
2013-03-04 19:09:56 +00:00
while (!functionCalled)
{
2013-03-04 20:27:47 +00:00
ST::FunctionFinishedCondition.wait(lock);
2013-03-04 19:09:56 +00:00
}
2013-02-21 08:35:47 +00:00
2013-02-03 13:11:16 +00:00
return result;
2013-02-21 08:35:47 +00:00
}
2013-02-03 13:11:16 +00:00
}
} //namespace SE
#endif