From 1b594d9ed18229524135d1aa7a5022a61f303caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 12 Oct 2022 14:13:20 +0800 Subject: [PATCH] Thread: add common thread abstraction layer, and make threading optional --- CMakeLists.txt | 38 +++++--- README.md | 3 +- src/common/init.c | 90 +++++-------------- src/common/settings.c | 16 ++-- src/common/thread.h | 43 +++++++++ src/detection/datetime.c | 10 +-- src/detection/displayserver/linux/wayland.c | 8 +- src/detection/gtk.c | 19 ++-- src/detection/internal.h | 10 +-- src/detection/media/media_linux.c | 2 +- src/detection/qt.c | 10 +-- src/detection/temps/temps_linux.c | 14 +-- .../terminalshell/terminalshell_linux.c | 10 +-- src/detection/title.c | 10 +-- src/detection/vulkan.c | 11 ++- 15 files changed, 151 insertions(+), 143 deletions(-) create mode 100644 src/common/thread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 018d3375e..41aaabaaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,17 @@ if(MSYS) enable_language(CXX) endif() +############################# +# Compile time dependencies # +############################# + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads) + +find_package(PkgConfig REQUIRED) + +include(CheckIncludeFile) + ##################### # Configure options # ##################### @@ -52,21 +63,11 @@ cmake_dependent_option(ENABLE_OSMESA "Enable osmesa" ON "LINUX OR BSD" OFF) cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR BSD OR MSYS" OFF) cmake_dependent_option(ENABLE_LIBCJSON "Enable libcjson" ON "LINUX OR MSYS" OFF) cmake_dependent_option(ENABLE_FREETYPE "Enable freetype" ON "ANDROID" OFF) +cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND AND NOT ANDROID" OFF) option(BUILD_TESTS "Build tests" OFF) # Also create test executables option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by github actions for release builds -############################# -# Compile time dependencies # -############################# - -set(THREADS_PREFER_PTHREAD_FLAG ON) -find_package(Threads REQUIRED) - -find_package(PkgConfig REQUIRED) - -include(CheckIncludeFile) - #################### # Compiler options # #################### @@ -76,6 +77,15 @@ if(NOT CMAKE_BUILD_TYPE) endif() message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") +if(ENABLE_THREADS) + if(CMAKE_USE_PTHREADS_INIT) + message(STATUS "Threads type: pthread") + else() + message(STATUS "Threads type: Win32 thread") + endif() +else() + message(STATUS "Threads type: disabled") +endif() set(CMAKE_C_STANDARD 11) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wconversion") @@ -482,6 +492,11 @@ ff_lib_enable(OPENCL OpenCL) ff_lib_enable(LIBCJSON libcjson) ff_lib_enable(FREETYPE freetype2) +if(ENABLE_THREADS) + target_compile_definitions(libfastfetch PRIVATE FF_HAVE_THREADS) + target_link_libraries(libfastfetch PRIVATE Threads::Threads) +endif() + if(APPLE) target_link_libraries(libfastfetch PRIVATE "-framework CoreFoundation" @@ -511,7 +526,6 @@ target_include_directories(libfastfetch target_link_libraries(libfastfetch PRIVATE ${CMAKE_DL_LIBS} - PRIVATE Threads::Threads ) ###################### diff --git a/README.md b/README.md index f100921bb..b5282ea46 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,10 @@ There are some premade config files in [`presets`](presets), including the ones ## Dependencies -Fastfetch dynamically loads needed libraries if they are available. Therefore its only hard dependencies are `libc` (any implementation of the c standard library), `libdl` and `libpthread`. They are all shipped with [`glibc`](https://www.gnu.org/software/libc/), which is already installed on most linux distributions, so you probably don't have to worry about it. +Fastfetch dynamically loads needed libraries if they are available. Therefore its only hard dependencies are `libc` (any implementation of the c standard library), `libdl`. They are all shipped with [`glibc`](https://www.gnu.org/software/libc/), which is already installed on most linux distributions, so you probably don't have to worry about it. The following libraries are used if present at runtime: +* [`libpthread`](https://man7.org/linux/man-pages/man7/pthreads.7.html): For multithreading support, which may improve performance * [`libpci`](https://github.com/pciutils/pciutils): GPU output. * [`libvulkan`](https://www.vulkan.org/): Vulkan module & fallback for GPU output. * [`libxcb-randr`](https://xcb.freedesktop.org/), diff --git a/src/common/init.c b/src/common/init.c index be5dd73c1..f9001f12f 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -1,6 +1,7 @@ #include "fastfetch.h" #include "common/caching.h" #include "common/parsing.h" +#include "common/thread.h" #include "detection/qt.h" #include "detection/gtk.h" #include "detection/displayserver/displayserver.h" @@ -9,7 +10,6 @@ #include #include #include -#include static bool strbufEqualsAdapter(const void* first, const void* second) { @@ -259,87 +259,39 @@ void ffInitInstance(FFinstance* instance) defaultConfig(instance); } -#if !defined(__ANDROID__) && !defined(_WIN32) && !defined(__MSYS__) +#ifdef FF_HAVE_THREADS -static void* connectDisplayServerThreadMain(void* instance) -{ - ffConnectDisplayServer((FFinstance*)instance); - return NULL; -} +FF_THREAD_ENTRY_DECL_WRAPPER(ffConnectDisplayServer, FFinstance*) -#if !defined(__APPLE__) +#if !(defined(__APPLE__) || defined(__MSYS__) || defined(_WIN32)) -static void* detectPlasmaThreadMain(void* instance) -{ - ffDetectQt((FFinstance*)instance); - return NULL; -} +#define FF_DETECT_QT_GTK 1 -static void* detectGTK2ThreadMain(void* instance) -{ - ffDetectGTK2((FFinstance*)instance); - return NULL; -} +FF_THREAD_ENTRY_DECL_WRAPPER(ffDetectQt, FFinstance*) +FF_THREAD_ENTRY_DECL_WRAPPER(ffDetectGTK2, FFinstance*) +FF_THREAD_ENTRY_DECL_WRAPPER(ffDetectGTK3, FFinstance*) +FF_THREAD_ENTRY_DECL_WRAPPER(ffDetectGTK4, FFinstance*) -static void* detectGTK3ThreadMain(void* instance) -{ - ffDetectGTK3((FFinstance*)instance); - return NULL; -} +#endif //!(defined(__APPLE__) || defined(__MSYS__) || defined(_WIN32)) -static void* detectGTK4ThreadMain(void* instance) -{ - ffDetectGTK4((FFinstance*)instance); - return NULL; -} - -static 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; -} +#endif //FF_HAVE_THREADS void startDetectionThreads(FFinstance* instance) { - pthread_t startThreadsThread; - pthread_create(&startThreadsThread, NULL, startThreadsThreadMain, instance); - pthread_detach(startThreadsThread); -} + #ifdef FF_HAVE_THREADS + ffThreadCreateAndDetach(ffConnectDisplayServerThreadMain, instance); -#else // !__APPLE__ -void startDetectionThreads(FFinstance* instance) -{ - pthread_t startThreadsThread; - pthread_create(&startThreadsThread, NULL, connectDisplayServerThreadMain, instance); - pthread_detach(startThreadsThread); -} -#endif // __APPLE__ + #ifdef FF_DETECT_QT_GTK + ffThreadCreateAndDetach(ffDetectQtThreadMain, instance); + ffThreadCreateAndDetach(ffDetectGTK2ThreadMain, instance); + ffThreadCreateAndDetach(ffDetectGTK3ThreadMain, instance); + ffThreadCreateAndDetach(ffDetectGTK4ThreadMain, instance); + #endif -#else // !__ANDROID__ -void startDetectionThreads(FFinstance* instance) -{ + #else FF_UNUSED(instance); + #endif } -#endif // __ANDROID__ static volatile bool ffDisableLinewrap = true; static volatile bool ffHideCursor = true; diff --git a/src/common/settings.c b/src/common/settings.c index 1b8a08c5d..6f5a5cfcb 100644 --- a/src/common/settings.c +++ b/src/common/settings.c @@ -2,8 +2,8 @@ #include "common/settings.h" #include "common/library.h" #include "common/io.h" +#include "common/thread.h" -#include #include typedef enum FFInitState @@ -15,18 +15,18 @@ typedef enum FFInitState #define FF_LIBRARY_DATA_LOAD_INIT(dataObject, userLibraryName, ...) \ static dataObject data; \ - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \ + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; \ static FFInitState initState = FF_INITSTATE_UNINITIALIZED; \ - pthread_mutex_lock(&mutex); \ + ffThreadMutexLock(&mutex); \ if(initState != FF_INITSTATE_UNINITIALIZED) {\ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return initState == FF_INITSTATE_SUCCESSFUL ? &data : NULL; \ } \ initState = FF_INITSTATE_SUCCESSFUL; \ void* libraryHandle = ffLibraryLoad(&userLibraryName, __VA_ARGS__, NULL); \ if(libraryHandle == NULL) { \ initState = FF_INITSTATE_FAILED; \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return NULL; \ } \ @@ -35,20 +35,20 @@ typedef enum FFInitState if(data.ff ## symbolName == NULL) { \ dlclose(libraryHandle); \ initState = FF_INITSTATE_FAILED; \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return NULL; \ } #define FF_LIBRARY_DATA_LOAD_RETURN \ initState = FF_INITSTATE_SUCCESSFUL; \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return &data; #define FF_LIBRARY_DATA_LOAD_ERROR \ { \ dlclose(libraryHandle); \ initState = FF_INITSTATE_FAILED; \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return NULL; \ } diff --git a/src/common/thread.h b/src/common/thread.h new file mode 100644 index 000000000..3c80f84fd --- /dev/null +++ b/src/common/thread.h @@ -0,0 +1,43 @@ +#pragma once + +#ifndef FF_INCLUDED_common_thread +#define FF_INCLUDED_common_thread + +#include "fastfetch.h" + +#ifdef FF_HAVE_THREADS + #if defined(_WIN32) + #include + #include + #include // Win32 isn't available on MSYS2 + #define FF_THREAD_MUTEX_INITIALIZER SRWLOCK_INIT + typedef SRWLOCK FFThreadMutex; + static inline void ffThreadMutexLock(FFThreadMutex* mutex) { AcquireSRWLockExclusive(mutex); } + static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { ReleaseSRWLockExclusive(mutex); } + static inline void ffThreadCreateAndDetach(__stdcall unsigned (* func)(void*), void* data) { + uintptr_t newThread = _beginthreadex(func, 0, data, NULL, 0, NULL); + if(newThread != 0) + CloseHandle((HANDLE)newThread); + } + #define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) static __stdcall unsigned fn ## ThreadMain (void* data) { fn((paramType)data); return 0; } + #else + #include + #define FF_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + typedef pthread_mutex_t FFThreadMutex; + static inline void ffThreadMutexLock(FFThreadMutex* mutex) { pthread_mutex_lock(mutex); } + static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { pthread_mutex_unlock(mutex); } + static inline void ffThreadCreateAndDetach(void* (* func)(void*), void* data) { + pthread_t newThread; + if(pthread_create(&newThread, NULL, func, data) == 0) + pthread_detach(newThread); + } + #define FF_THREAD_ENTRY_DECL_WRAPPER(fn, paramType) static void* fn ## ThreadMain (void* data) { fn((paramType)data); return NULL; } + #endif +#else //FF_HAVE_THREADS + #define FF_THREAD_MUTEX_INITIALIZER 0 + typedef char FFThreadMutex; + static inline void ffThreadMutexLock(FFThreadMutex* mutex) { FF_UNUSED(mutex) } + static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { FF_UNUSED(mutex) } +#endif //FF_HAVE_THREADS + +#endif diff --git a/src/detection/datetime.c b/src/detection/datetime.c index 9c5ebf414..20b8ec367 100644 --- a/src/detection/datetime.c +++ b/src/detection/datetime.c @@ -1,21 +1,21 @@ #include "fastfetch.h" #include "detection/datetime.h" +#include "common/thread.h" #include -#include const FFDateTimeResult* ffDetectDateTime(const FFinstance* instance) { FF_UNUSED(instance); //We may need it later for additional configuration static FFDateTimeResult result; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if (init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -68,6 +68,6 @@ const FFDateTimeResult* ffDetectDateTime(const FFinstance* instance) ffStrbufInitA(&result.secondPretty, FASTFETCH_STRBUF_DEFAULT_ALLOC); result.secondPretty.length = (uint32_t) strftime(result.secondPretty.chars, ffStrbufGetFree(&result.secondPretty), "%S", tm); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/displayserver/linux/wayland.c b/src/detection/displayserver/linux/wayland.c index 39743282c..166ec25b2 100644 --- a/src/detection/displayserver/linux/wayland.c +++ b/src/detection/displayserver/linux/wayland.c @@ -6,7 +6,7 @@ #ifdef FF_HAVE_WAYLAND #include "common/library.h" #include "common/io.h" -#include +#include "common/thread.h" #include #include @@ -56,12 +56,12 @@ static void waylandOutputModeListener(void* data, struct wl_output* output, uint if(!(flags & WL_OUTPUT_MODE_CURRENT) || width <= 0 || height <= 0) return; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&mutex); + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; + ffThreadMutexLock(&mutex); FFResolutionResult* result = ffListAdd(wldata->results); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); result->width = (uint32_t) width; result->height = (uint32_t) height; diff --git a/src/detection/gtk.c b/src/detection/gtk.c index 3855f7990..ad24c9c4e 100644 --- a/src/detection/gtk.c +++ b/src/detection/gtk.c @@ -1,11 +1,10 @@ #include "fastfetch.h" #include "detection/gtk.h" #include "common/properties.h" +#include "common/thread.h" #include "common/settings.h" #include "detection/displayserver/displayserver.h" -#include - static inline bool allPropertiesSet(FFGTKResult* result) { return @@ -34,7 +33,7 @@ static inline void applyGTKSettings(FFGTKResult* result, const char* themeName, static void detectGTKFromSettings(const FFinstance* instance, FFGTKResult* result) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static const char* themeName = NULL; static const char* iconsName = NULL; @@ -44,11 +43,11 @@ static void detectGTKFromSettings(const FFinstance* instance, FFGTKResult* resul static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); applyGTKSettings(result, themeName, iconsName, fontName, cursorTheme, cursorSize); return; } @@ -90,7 +89,7 @@ static void detectGTKFromSettings(const FFinstance* instance, FFGTKResult* resul cursorSize = ffSettingsGet(instance, "/org/gnome/desktop/interface/cursor-size", "org.gnome.desktop.interface", NULL, "cursor-size", FF_VARIANT_TYPE_INT).intValue; } - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); applyGTKSettings(result, themeName, iconsName, fontName, cursorTheme, cursorSize); } @@ -168,12 +167,12 @@ static void detectGTK(const FFinstance* instance, const char* version, FFGTKResu } #define FF_DETECT_GTK_IMPL(version) \ - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \ + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; \ static FFGTKResult result; \ static bool init = false; \ - pthread_mutex_lock(&mutex); \ + ffThreadMutexLock(&mutex); \ if(init){ \ - pthread_mutex_unlock(&mutex);\ + ffThreadMutexUnlock(&mutex);\ return &result; \ } \ init = true; \ @@ -183,7 +182,7 @@ static void detectGTK(const FFinstance* instance, const char* version, FFGTKResu ffStrbufInit(&result.cursor); \ ffStrbufInit(&result.cursorSize); \ detectGTK(instance, #version, &result); \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return &result; const FFGTKResult* ffDetectGTK2(const FFinstance* instance) diff --git a/src/detection/internal.h b/src/detection/internal.h index f4dc67cbb..11b081f7e 100644 --- a/src/detection/internal.h +++ b/src/detection/internal.h @@ -3,21 +3,21 @@ #ifndef FF_INCLUDED_detection_internal #define FF_INCLUDED_detection_internal -#include "pthread.h" +#include "common/thread.h" #define FF_DETECTION_INTERNAL_GUARD(ResultType, ...) \ - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \ + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; \ static ResultType result; \ static bool init = false; \ - pthread_mutex_lock(&mutex); \ + ffThreadMutexLock(&mutex); \ if(init) \ { \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return &result; \ } \ init = true; \ __VA_ARGS__; \ - pthread_mutex_unlock(&mutex); \ + ffThreadMutexUnlock(&mutex); \ return &result; \ #endif diff --git a/src/detection/media/media_linux.c b/src/detection/media/media_linux.c index 77ecbc3a9..60b407b99 100644 --- a/src/detection/media/media_linux.c +++ b/src/detection/media/media_linux.c @@ -1,8 +1,8 @@ #include "fastfetch.h" #include "detection/media/media.h" +#include "common/thread.h" #include -#include #define FF_DBUS_MPRIS_PREFIX "org.mpris.MediaPlayer2." #define FF_DBUS_TIMEOUT_MILLISECONDS 35 diff --git a/src/detection/qt.c b/src/detection/qt.c index 5bd9b2403..6a7794c8e 100644 --- a/src/detection/qt.c +++ b/src/detection/qt.c @@ -1,11 +1,11 @@ #include "fastfetch.h" #include "detection/qt.h" #include "common/properties.h" +#include "common/thread.h" #include "detection/displayserver/displayserver.h" #include #include -#include static inline bool allValuesSet(const FFQtResult* result) { @@ -135,12 +135,12 @@ const FFQtResult* ffDetectQt(const FFinstance* instance) { static FFQtResult result; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -157,6 +157,6 @@ const FFQtResult* ffDetectQt(const FFinstance* instance) else if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "LXQt") == 0) detectLXQt(instance, &result); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/temps/temps_linux.c b/src/detection/temps/temps_linux.c index 5fad783d5..cb342a9f8 100644 --- a/src/detection/temps/temps_linux.c +++ b/src/detection/temps/temps_linux.c @@ -1,9 +1,9 @@ #include "fastfetch.h" #include "common/io.h" +#include "common/thread.h" #include "temps_linux.h" #include -#include #include static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value) @@ -38,13 +38,13 @@ static bool parseHwmonDir(FFstrbuf* dir, FFTempValue* value) const FFTempsResult* ffDetectTemps(const FFinstance* instance) { static FFTempsResult result; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -52,7 +52,7 @@ const FFTempsResult* ffDetectTemps(const FFinstance* instance) if(!instance->config.allowSlowOperations) { ffListInitA(&result.values, sizeof(FFTempValue), 0); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } @@ -68,7 +68,7 @@ const FFTempsResult* ffDetectTemps(const FFinstance* instance) if(dirp == NULL) { ffStrbufDestroy(&baseDir); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } @@ -97,6 +97,6 @@ const FFTempsResult* ffDetectTemps(const FFinstance* instance) closedir(dirp); ffStrbufDestroy(&baseDir); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index b5f68c58f..534c343c7 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -3,13 +3,13 @@ #include "common/io.h" #include "common/parsing.h" #include "common/processing.h" +#include "common/thread.h" #include "terminalshell.h" #include #include #include #include -#include #ifdef __APPLE__ #include @@ -318,13 +318,13 @@ const FFTerminalShellResult* { FF_UNUSED(instance); - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static FFTerminalShellResult result; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -361,6 +361,6 @@ const FFTerminalShellResult* else ffStrbufInitCopy(&result.terminalPrettyName, &result.terminalProcessName); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/title.c b/src/detection/title.c index b4394be15..50482977f 100644 --- a/src/detection/title.c +++ b/src/detection/title.c @@ -1,9 +1,9 @@ #include "fastfetch.h" #include "detection/title.h" +#include "common/thread.h" #include #include -#include #include #ifndef HOST_NAME_MAX @@ -37,12 +37,12 @@ const FFTitleResult* ffDetectTitle(const FFinstance* instance) { static FFTitleResult result; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -60,6 +60,6 @@ const FFTitleResult* ffDetectTitle(const FFinstance* instance) if(result.fqdn.length == 0) ffStrbufAppend(&result.fqdn, &result.hostname); - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/vulkan.c b/src/detection/vulkan.c index 1ff96e2c9..9c348b74b 100644 --- a/src/detection/vulkan.c +++ b/src/detection/vulkan.c @@ -1,9 +1,8 @@ #include "fastfetch.h" +#include "common/thread.h" #include "detection/vulkan.h" #include "detection/gpu/gpu.h" -#include - #ifdef FF_HAVE_VULKAN #include "common/library.h" #include "common/io.h" @@ -217,13 +216,13 @@ static const char* detectVulkan(const FFinstance* instance, FFVulkanResult* resu const FFVulkanResult* ffDetectVulkan(const FFinstance* instance) { static FFVulkanResult result; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static FFThreadMutex mutex = FF_THREAD_MUTEX_INITIALIZER; static bool init = false; - pthread_mutex_lock(&mutex); + ffThreadMutexLock(&mutex); if(init) { - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; } init = true; @@ -240,6 +239,6 @@ const FFVulkanResult* ffDetectVulkan(const FFinstance* instance) result.error = "fastfetch was compiled without vulkan support"; #endif - pthread_mutex_unlock(&mutex); + ffThreadMutexUnlock(&mutex); return &result; }