Set idle priority for navmesh generation thread

Support Linux, Windows, FreeBSD.
pull/593/head
elsid 4 years ago
parent 0a6ef9c1bd
commit ab8d1c02d4
No known key found for this signature in database
GPG Key ID: D27B8E8D10A2896B

@ -87,7 +87,7 @@ add_component_dir (esmterrain
) )
add_component_dir (misc add_component_dir (misc
constants utf8stream stringops resourcehelpers rng messageformatparser weakcache constants utf8stream stringops resourcehelpers rng messageformatparser weakcache thread
) )
add_component_dir (debug add_component_dir (debug

@ -4,6 +4,7 @@
#include "settings.hpp" #include "settings.hpp"
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/misc/thread.hpp>
#include <osg/Stats> #include <osg/Stats>
@ -135,6 +136,7 @@ namespace DetourNavigator
void AsyncNavMeshUpdater::process() noexcept void AsyncNavMeshUpdater::process() noexcept
{ {
Log(Debug::Debug) << "Start process navigator jobs by thread=" << std::this_thread::get_id(); Log(Debug::Debug) << "Start process navigator jobs by thread=" << std::this_thread::get_id();
Misc::setCurrentThreadIdlePriority();
while (!mShouldStop) while (!mShouldStop)
{ {
try try

@ -0,0 +1,73 @@
#include "thread.hpp"
#include <components/debug/debuglog.hpp>
#include <cstring>
#include <thread>
#ifdef __linux__
#include <pthread.h>
#include <sched.h>
namespace Misc
{
void setCurrentThreadIdlePriority()
{
sched_param param;
param.sched_priority = 0;
if (pthread_setschedparam(pthread_self(), SCHED_IDLE, &param) == 0)
Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
else
Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": " << std::strerror(errno);
}
}
#elif defined(WIN32)
#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
namespace Misc
{
void setCurrentThreadIdlePriority()
{
if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST))
Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
else
Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": " << GetLastError();
}
}
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/rtprio.h>
namespace Misc
{
void setCurrentThreadIdlePriority()
{
rtprio prio;
prio.type = RTP_PRIO_IDLE;
prio.prio = RTP_PRIO_MAX;
if (rtprio_thread(RTP_SET, 0, &prio) == 0)
Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id();
else
Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": " << std::strerror(errno);
}
}
#else
namespace Misc
{
void setCurrentThreadIdlePriority()
{
Log(Debug::Warning) << "Idle thread priority is not supported on this system";
}
}
#endif

@ -0,0 +1,11 @@
#ifndef OPENMW_COMPONENTS_MISC_THREAD_H
#define OPENMW_COMPONENTS_MISC_THREAD_H
#include <thread>
namespace Misc
{
void setCurrentThreadIdlePriority();
}
#endif
Loading…
Cancel
Save