From 8292b55ae61b9d38ffeb215623bd4372c4dfe70c Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 18:38:36 -0600 Subject: [PATCH 1/7] Request exit even if it's not running --- main.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index bae1aab..d23f7b1 100644 --- a/main.cpp +++ b/main.cpp @@ -218,10 +218,16 @@ extern "C" { rtld_fini, stack_end ); + exitThread = true; if(stateThreadRunning){ _DEBUG("Waiting for thread to exit"); - exitThread = true; - stateThread.join(); + if(stateThread.joinable()){ + stateThread.join(); + }else{ + while(stateThreadRunning){ + usleep(1000); + } + } } return res; } From 84413a4103970b267a73ed76e7021719d8f8e595 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 18:48:19 -0600 Subject: [PATCH 2/7] Flatten logic --- main.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index d23f7b1..5b73e16 100644 --- a/main.cpp +++ b/main.cpp @@ -219,14 +219,12 @@ extern "C" { stack_end ); exitThread = true; - if(stateThreadRunning){ - _DEBUG("Waiting for thread to exit"); + _DEBUG("Waiting for thread to exit"); + while(stateThreadRunning){ if(stateThread.joinable()){ stateThread.join(); }else{ - while(stateThreadRunning){ - usleep(1000); - } + usleep(1000); } } return res; From 81f0d3c98ebc5edab2c25bd769d021f6e6cc7c5d Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 19:26:28 -0600 Subject: [PATCH 3/7] Use condition variable instead --- main.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 5b73e16..ca0c327 100644 --- a/main.cpp +++ b/main.cpp @@ -10,12 +10,15 @@ #include #include #include +#include #define forever for(;;) namespace { static int(*func_open)(const char*, int, mode_t) = nullptr; static std::mutex logMutex; + std::condition_variable stateThreadCondition; + std::mutex stateThreadConditionMutex; static std::thread stateThread; static std::atomic stateThreadRunning = false; static bool exitThread = false; @@ -74,6 +77,7 @@ namespace { #define _WARN(...) _PRINTF(LOG_WARNING, __VA_ARGS__) #define _INFO(...) _PRINTF(LOG_INFO, __VA_ARGS__) #define _CRIT(...) _PRINTF(LOG_CRIT, __VA_ARGS__) + #define _UNUSED(x) inline std::string trim(std::string& str){ str.erase(str.find_last_not_of(' ') + 1); @@ -112,7 +116,12 @@ namespace { _WARN("Unknown power state call: %s", data.c_str()); } } - stateThreadRunning = false; + { + std::lock_guard lock(stateThreadConditionMutex); + _UNUSED(lock); + stateThreadRunning = false; + } + stateThreadCondition.notify_one(); } int __open(const char* pathname, int flags){ @@ -220,11 +229,12 @@ extern "C" { ); exitThread = true; _DEBUG("Waiting for thread to exit"); - while(stateThreadRunning){ - if(stateThread.joinable()){ - stateThread.join(); - }else{ - usleep(1000); + { + std::unique_lock lock(stateThreadConditionMutex); + if(!stateThreadCondition.wait_for(lock, std::chrono::seconds(5), []{ + return !stateThreadRunning; + })){ + _WARN("Timeout waiting for state thread to exit"); } } return res; From 646d6d6488c1426d3c58a7e444a62431146f4155 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 20:39:40 -0600 Subject: [PATCH 4/7] Remove unused include --- main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/main.cpp b/main.cpp index ca0c327..fabd418 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include From 9df092fdf0b4a319b4e4815cc4a7268377a273df Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 20:45:23 -0600 Subject: [PATCH 5/7] Use maybe_unused --- main.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index fabd418..96b25ad 100644 --- a/main.cpp +++ b/main.cpp @@ -76,7 +76,6 @@ namespace { #define _WARN(...) _PRINTF(LOG_WARNING, __VA_ARGS__) #define _INFO(...) _PRINTF(LOG_INFO, __VA_ARGS__) #define _CRIT(...) _PRINTF(LOG_CRIT, __VA_ARGS__) - #define _UNUSED(x) inline std::string trim(std::string& str){ str.erase(str.find_last_not_of(' ') + 1); @@ -116,8 +115,7 @@ namespace { } } { - std::lock_guard lock(stateThreadConditionMutex); - _UNUSED(lock); + [[maybe_unused]] std::lock_guard lock(stateThreadConditionMutex); stateThreadRunning = false; } stateThreadCondition.notify_one(); From e8e21a2b470c032a058f01d6714da7513f886224 Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 20:59:38 -0600 Subject: [PATCH 6/7] code review feedback --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 96b25ad..4839899 100644 --- a/main.cpp +++ b/main.cpp @@ -117,8 +117,8 @@ namespace { { [[maybe_unused]] std::lock_guard lock(stateThreadConditionMutex); stateThreadRunning = false; + stateThreadCondition.notify_one(); } - stateThreadCondition.notify_one(); } int __open(const char* pathname, int flags){ From 02506c16029ab10fd8320ac84726fd659d38cbad Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 10 Oct 2024 22:34:11 -0600 Subject: [PATCH 7/7] Bump to 1.0.3 --- package | 4 ++-- sysfs_preload.pro | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package b/package index e5a3e48..6d4e165 100644 --- a/package +++ b/package @@ -1,8 +1,8 @@ pkgnames=(sysfs_preload) pkgdesc="A simple preload that forces any calls to /sys/power/state to use systemd instead." url=https://github.com/Eeems-Org/sysfs_preload -pkgver=1.0.2-1 -timestamp=2024-06-22T05:19Z +pkgver=1.0.3-1 +timestamp=2024-10-11T04:33:10Z section=util maintainer="Eeems " license=MIT diff --git a/sysfs_preload.pro b/sysfs_preload.pro index 07b40c0..36f4131 100644 --- a/sysfs_preload.pro +++ b/sysfs_preload.pro @@ -2,7 +2,7 @@ TARGET = sysfs_preload TEMPLATE = lib QMAKE_RPATHDIR += /lib /usr/lib /opt/lib /opt/usr/lib -VERSION = 1.0.2 +VERSION = 1.0.3 CONFIG += hide_symbols CONFIG += c++17