diff --git a/include/system.hpp b/include/system.hpp index 1f22a512..f02b99f8 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -13,6 +13,8 @@ bool isDirectory(const std::string &path); void copyFile(const std::string &srcPath, const std::string &destPath); void createDirectory(const std::string &path); +/** Currently this lies and returns the number of logical cores instead. */ +int getPhysicalCoreCount(); void setThreadName(const std::string &name); /** Opens a URL, also happens to work with PDFs and folders. diff --git a/src/app/Toolbar.cpp b/src/app/Toolbar.cpp index 6c8add4f..4d626618 100644 --- a/src/app/Toolbar.cpp +++ b/src/app/Toolbar.cpp @@ -299,6 +299,35 @@ struct SampleRateItem : MenuItem { }; +struct ThreadCountValueItem : MenuItem { + int threadCount; + ThreadCountValueItem(int threadCount) { + this->threadCount = threadCount; + text = string::f("%d", threadCount); + rightText = CHECKMARK(app()->engine->threadCount == threadCount); + } + void onAction(const event::Action &e) override { + app()->engine->threadCount = threadCount; + } +}; + + +struct ThreadCount : MenuItem { + ThreadCount() { + text = "Thread count"; + } + Menu *createChildMenu() override { + Menu *menu = new Menu; + + int coreCount = system::getPhysicalCoreCount(); + for (int i = 1; i <= coreCount; i++) { + menu->addChild(new ThreadCountValueItem(i)); + } + return menu; + } +}; + + struct FullscreenItem : MenuItem { FullscreenItem() { text = "Fullscreen"; @@ -325,6 +354,7 @@ struct SettingsButton : MenuButton { menu->addChild(new PowerMeterItem); menu->addChild(new LockModulesItem); menu->addChild(new SampleRateItem); + menu->addChild(new ThreadCount); menu->addChild(new FullscreenItem); Slider *zoomSlider = new Slider; diff --git a/src/settings.cpp b/src/settings.cpp index 5c751c03..5933da67 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -68,6 +68,9 @@ static json_t *settingsToJson() { // powerMeter json_object_set_new(rootJ, "powerMeter", json_boolean(powerMeter)); + // threadCount + json_object_set_new(rootJ, "threadCount", json_integer(app()->engine->threadCount)); + // checkVersion json_object_set_new(rootJ, "checkVersion", json_boolean(checkVersion)); @@ -146,6 +149,11 @@ static void settingsFromJson(json_t *rootJ) { if (powerMeterJ) powerMeter = json_boolean_value(powerMeterJ); + // threadCount + json_t *threadCountJ = json_object_get(rootJ, "threadCount"); + if (threadCountJ) + app()->engine->threadCount = json_integer_value(threadCountJ); + // checkVersion json_t *checkVersionJ = json_object_get(rootJ, "checkVersion"); if (checkVersionJ) diff --git a/src/system.cpp b/src/system.cpp index 8554bf71..01961c21 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,4 +1,5 @@ #include "system.hpp" +#include #include #include @@ -77,6 +78,11 @@ void createDirectory(const std::string &path) { #endif } +int getPhysicalCoreCount() { + // TODO Return the physical cores, not logical cores. + return std::thread::hardware_concurrency(); +} + void setThreadName(const std::string &name) { #if defined ARCH_LIN pthread_setname_np(pthread_self(), name.c_str());