mirror of
https://github.com/darlinghq/darlingserver.git
synced 2024-11-23 12:29:41 +00:00
workers: Add option to make work queues single threaded
This commit is contained in:
parent
97e36ba645
commit
c242e7237e
@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user