From 7676a313c5b53a46d9d4b88cf92b794e935a9417 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Mon, 25 Jul 2022 15:01:11 +0200 Subject: [PATCH] Refactor: move threading functions to init --- CMakeLists.txt | 1 - src/common/init.c | 74 ++++++++++++++++++++++++++++++++++++++++++ src/common/threading.c | 72 ---------------------------------------- src/fastfetch.c | 8 +---- src/fastfetch.h | 6 ++-- src/flashfetch.c | 5 +-- 6 files changed, 78 insertions(+), 88 deletions(-) delete mode 100644 src/common/threading.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4695b1b1b..92c67f1cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,7 +138,6 @@ add_library(libfastfetch OBJECT src/util/FFlist.c src/util/FFvaluestore.c src/common/init.c - src/common/threading.c src/common/io.c src/common/printing.c src/common/caching.c diff --git a/src/common/init.c b/src/common/init.c index a7fa5e4a2..22dcb336b 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -5,6 +5,7 @@ #include #include #include +#include static bool strbufEqualsAdapter(const void* first, const void* second) { @@ -137,6 +138,7 @@ static void defaultConfig(FFinstance* instance) instance->config.escapeBedrock = true; instance->config.glType = FF_GL_TYPE_AUTO; instance->config.pipe = false; + instance->config.multithreading = true; initModuleArg(&instance->config.os); initModuleArg(&instance->config.host); @@ -219,6 +221,75 @@ void ffInitInstance(FFinstance* instance) defaultConfig(instance); } +static inline void* detectPlasmaThreadMain(void* instance) +{ + ffDetectQt((FFinstance*)instance); + return NULL; +} + +static inline void* detectGTK2ThreadMain(void* instance) +{ + ffDetectGTK2((FFinstance*)instance); + return NULL; +} + +static inline void* detectGTK3ThreadMain(void* instance) +{ + ffDetectGTK3((FFinstance*)instance); + return NULL; +} + +static inline void* detectGTK4ThreadMain(void* instance) +{ + ffDetectGTK4((FFinstance*)instance); + return NULL; +} + +static inline void* connectDisplayServerThreadMain(void* instance) +{ + ffConnectDisplayServer((FFinstance*)instance); + return NULL; +} + +static inline void* startThreadsThreadMain(void* instance) +{ + pthread_t dsThread; + pthread_create(&dsThread, NULL, connectDisplayServerThreadMain, instance); + pthread_detach(dsThread); + + pthread_t gtk2Thread; + pthread_create(>k2Thread, NULL, detectGTK2ThreadMain, instance); + pthread_detach(gtk2Thread); + + pthread_t gtk3Thread; + pthread_create(>k3Thread, NULL, detectGTK3ThreadMain, instance); + pthread_detach(gtk3Thread); + + pthread_t gtk4Thread; + pthread_create(>k4Thread, NULL, detectGTK4ThreadMain, instance); + pthread_detach(gtk4Thread); + + pthread_t plasmaThread; + pthread_create(&plasmaThread, NULL, detectPlasmaThreadMain, instance); + pthread_detach(plasmaThread); + + return NULL; +} + +void startDetectionThreads(FFinstance* instance) +{ + //Android needs none of the things that are detected here + //And using gsettings sometimes hangs the program in android for some unknown reason, + //and since we don't need it we just never call it. + #ifdef __ANDROID__ + return; + #endif + + pthread_t startThreadsThread; + pthread_create(&startThreadsThread, NULL, startThreadsThreadMain, instance); + pthread_detach(startThreadsThread); +} + static volatile bool ffDisableLinewrap = true; static volatile bool ffHideCursor = true; @@ -240,6 +311,9 @@ static void exitSignalHandler(int signal) void ffStart(FFinstance* instance) { + if(instance->config.multithreading) + startDetectionThreads(instance); + ffDisableLinewrap = instance->config.disableLinewrap && !instance->config.pipe; ffHideCursor = instance->config.hideCursor && !instance->config.pipe; diff --git a/src/common/threading.c b/src/common/threading.c deleted file mode 100644 index 636885c2a..000000000 --- a/src/common/threading.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "fastfetch.h" - -#include - -static inline void* detectPlasmaThreadMain(void* instance) -{ - ffDetectQt((FFinstance*)instance); - return NULL; -} - -static inline void* detectGTK2ThreadMain(void* instance) -{ - ffDetectGTK2((FFinstance*)instance); - return NULL; -} - -static inline void* detectGTK3ThreadMain(void* instance) -{ - ffDetectGTK3((FFinstance*)instance); - return NULL; -} - -static inline void* detectGTK4ThreadMain(void* instance) -{ - ffDetectGTK4((FFinstance*)instance); - return NULL; -} - -static inline void* connectDisplayServerThreadMain(void* instance) -{ - ffConnectDisplayServer((FFinstance*)instance); - return NULL; -} - -static inline void* startThreadsThreadMain(void* instance) -{ - pthread_t dsThread; - pthread_create(&dsThread, NULL, connectDisplayServerThreadMain, instance); - pthread_detach(dsThread); - - pthread_t gtk2Thread; - pthread_create(>k2Thread, NULL, detectGTK2ThreadMain, instance); - pthread_detach(gtk2Thread); - - pthread_t gtk3Thread; - pthread_create(>k3Thread, NULL, detectGTK3ThreadMain, instance); - pthread_detach(gtk3Thread); - - pthread_t gtk4Thread; - pthread_create(>k4Thread, NULL, detectGTK4ThreadMain, instance); - pthread_detach(gtk4Thread); - - pthread_t plasmaThread; - pthread_create(&plasmaThread, NULL, detectPlasmaThreadMain, instance); - pthread_detach(plasmaThread); - - return NULL; -} - -void ffStartDetectionThreads(FFinstance* instance) -{ - //Android needs none of the things that are detected here - //And using gsettings sometimes hangs the program in android for some unknown reason, - //and since we don't need it we just never call it. - #ifdef __ANDROID__ - return; - #endif - - pthread_t startThreadsThread; - pthread_create(&startThreadsThread, NULL, startThreadsThreadMain, instance); - pthread_detach(startThreadsThread); -} diff --git a/src/fastfetch.c b/src/fastfetch.c index ee70e1b74..170a0107b 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -12,7 +12,6 @@ typedef struct FFdata { FFvaluestore valuestore; FFstrbuf structure; - bool multithreading; bool loadUserConfig; } FFdata; @@ -752,7 +751,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con else if(strcasecmp(key, "--load-config") == 0) optionParseConfigFile(instance, data, key, value); else if(strcasecmp(key, "--multithreading") == 0) - data->multithreading = optionParseBoolean(value); + instance->config.multithreading = optionParseBoolean(value); else if(strcasecmp(key, "--allow-slow-operations") == 0) instance->config.allowSlowOperations = optionParseBoolean(value); else if(strcasecmp(key, "--escape-bedrock") == 0) @@ -1374,17 +1373,12 @@ int main(int argc, const char** argv) FFdata data; ffValuestoreInit(&data.valuestore); ffStrbufInitA(&data.structure, 256); - data.multithreading = true; data.loadUserConfig = true; parseConfigFileSystem(&instance, &data); parseConfigFileUser(&instance, &data); parseArguments(&instance, &data, argc, argv); - //Start detection threads - if(data.multithreading) - ffStartDetectionThreads(&instance); - //If we don't have a custom structure, use the default one if(data.structure.length == 0) ffStrbufSetS(&data.structure, FASTFETCH_DATATEXT_STRUCTURE); diff --git a/src/fastfetch.h b/src/fastfetch.h index 49179f31c..fcf3c98d5 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -77,6 +77,7 @@ typedef struct FFconfig bool escapeBedrock; FFGLType glType; bool pipe; //disables logo and all escape sequences + bool multithreading; FFModuleArgs os; FFModuleArgs host; @@ -311,7 +312,7 @@ typedef struct FFDateTimeResult } FFDateTimeResult; ////////////////////// -// Common functions // +// Init functions // ////////////////////// //common/init.c @@ -321,9 +322,6 @@ void ffFinish(FFinstance* instance); void ffListFeatures(); -//common/threading.c -void ffStartDetectionThreads(FFinstance* instance); - //////////////////// // Logo functions // //////////////////// diff --git a/src/flashfetch.c b/src/flashfetch.c index 91d91a391..d08c59799 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -10,10 +10,7 @@ int main(int argc, char** argv) //Modify instance.config here - //Multithreading --> better performance - ffStartDetectionThreads(&instance); - - //Does things like disabling line wrap + //Does things like starting detection threads, disabling line wrap, etc ffStart(&instance); //Printing