Set worker threads to background mode on Windows if niceness > 0

This commit is contained in:
Marcus Holland-Moritz 2023-06-27 11:15:34 +02:00
parent 3b39b319b4
commit 3dba17ea76

View File

@ -33,6 +33,7 @@
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/portability/PThread.h> #include <folly/portability/PThread.h>
#include <folly/portability/Windows.h>
#include <folly/system/ThreadName.h> #include <folly/system/ThreadName.h>
#include "dwarfs/error.h" #include "dwarfs/error.h"
@ -109,11 +110,11 @@ class basic_worker_group final : public worker_group::impl, private Policy {
} }
for (size_t i = 0; i < num_workers; ++i) { for (size_t i = 0; i < num_workers; ++i) {
workers_.emplace_back([this, /*niceness,*/ group_name, i] { workers_.emplace_back([this, niceness, group_name, i] {
folly::setThreadName(folly::to<std::string>(group_name, i + 1)); folly::setThreadName(folly::to<std::string>(group_name, i + 1));
// TODO: FIXME: this is broken // TODO: FIXME: this is broken
// [[maybe_unused]] auto rv = ::nice(niceness); // [[maybe_unused]] auto rv = ::nice(niceness);
do_work(); do_work(niceness > 0);
}); });
} }
} }
@ -220,7 +221,10 @@ class basic_worker_group final : public worker_group::impl, private Policy {
private: private:
using jobs_t = std::queue<worker_group::job_t>; using jobs_t = std::queue<worker_group::job_t>;
void do_work() { void do_work(bool is_background [[maybe_unused]]) {
#ifdef _WIN32
auto hthr = ::GetCurrentThread();
#endif
for (;;) { for (;;) {
worker_group::job_t job; worker_group::job_t job;
@ -246,7 +250,17 @@ class basic_worker_group final : public worker_group::impl, private Policy {
{ {
typename Policy::task task(this); typename Policy::task task(this);
#ifdef _WIN32
if (is_background) {
::SetThreadPriority(hthr, THREAD_MODE_BACKGROUND_BEGIN);
}
#endif
job(); job();
#ifdef _WIN32
if (is_background) {
::SetThreadPriority(hthr, THREAD_MODE_BACKGROUND_END);
}
#endif
} }
{ {