workers: Add option to make work queues single threaded

This commit is contained in:
Ariel Abreu 2022-03-02 23:50:00 -05:00
parent 97e36ba645
commit c242e7237e
No known key found for this signature in database
GPG Key ID: D67AE16CCEA85B70
2 changed files with 19 additions and 9 deletions

View File

@ -6,7 +6,13 @@
namespace DarlingServer {
namespace Config {
// NOTE: you should not rely on these values being `constexpr`;
// in the future, there may be a way to change them during startup.
constexpr std::string_view defaultMldrPath = LIBEXEC_PATH "/usr/libexec/darling/mldr";
// this would actually probably be better as a workqueue construction parameter
constexpr bool singleThreadedWorkQueue = false;
};
};

View File

@ -30,6 +30,8 @@
#include <chrono>
#include <optional>
#include <darlingserver/config.hpp>
namespace DarlingServer {
template<class WorkItem>
class WorkQueue {
@ -43,19 +45,21 @@ namespace DarlingServer {
bool _dying = false;
static constexpr unsigned int minimumThreads() {
return 2;
return (Config::singleThreadedWorkQueue) ? 1 : 2;
};
static unsigned int maximumThreads() {
unsigned int hw = std::thread::hardware_concurrency();
if (hw > 0 && hw > minimumThreads()) {
// we used to take the main thread into account by doing `hw - 1`,
// but we no longer do that because the main thread is going to be sleeping most of the time.
// therefore, it makes sense to create as many worker threads as there are CPUs/cores.
return hw;
} else {
return minimumThreads();
if (!Config::singleThreadedWorkQueue) {
unsigned int hw = std::thread::hardware_concurrency();
if (hw > 0 && hw > minimumThreads()) {
// we used to take the main thread into account by doing `hw - 1`,
// but we no longer do that because the main thread is going to be sleeping most of the time.
// therefore, it makes sense to create as many worker threads as there are CPUs/cores.
return hw;
}
}
return minimumThreads();
};
static constexpr std::chrono::seconds temporaryWorkerWaitPeriod() {