fix(tray): use the blocking event loop to avoid wasting power (#4457)
This commit is contained in:
parent
62d58ed6e0
commit
f1f9e755a9
4 changed files with 20 additions and 106 deletions
|
|
@ -53,11 +53,6 @@ using namespace std::literals;
|
|||
namespace system_tray {
|
||||
static std::atomic tray_initialized = false;
|
||||
|
||||
// Threading variables for all platforms
|
||||
static std::thread tray_thread;
|
||||
static std::atomic tray_thread_running = false;
|
||||
static std::atomic tray_thread_should_exit = false;
|
||||
|
||||
void tray_open_ui_cb([[maybe_unused]] struct tray_menu *item) {
|
||||
BOOST_LOG(info) << "Opening UI from system tray"sv;
|
||||
launch_ui();
|
||||
|
|
@ -207,16 +202,12 @@ namespace system_tray {
|
|||
|
||||
int process_tray_events() {
|
||||
if (!tray_initialized) {
|
||||
BOOST_LOG(error) << "System tray is not initialized"sv;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Process one iteration of the tray loop with non-blocking mode (0)
|
||||
if (const int result = tray_loop(0); result != 0) {
|
||||
BOOST_LOG(warning) << "System tray loop failed"sv;
|
||||
return result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// Block until an event is processed or tray_quit() is called
|
||||
return tray_loop(1);
|
||||
}
|
||||
|
||||
int end_tray() {
|
||||
|
|
@ -319,63 +310,22 @@ namespace system_tray {
|
|||
// Initialize the tray in this thread
|
||||
if (init_tray() != 0) {
|
||||
BOOST_LOG(error) << "Failed to initialize tray in thread"sv;
|
||||
tray_thread_running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
tray_thread_running = true;
|
||||
|
||||
// Main tray event loop
|
||||
while (!tray_thread_should_exit) {
|
||||
if (process_tray_events() != 0) {
|
||||
BOOST_LOG(warning) << "Tray event processing failed in thread"sv;
|
||||
break;
|
||||
}
|
||||
while (process_tray_events() == 0);
|
||||
|
||||
// Sleep to avoid busy waiting
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
|
||||
// Clean up the tray
|
||||
end_tray();
|
||||
tray_thread_running = false;
|
||||
BOOST_LOG(info) << "System tray thread ended"sv;
|
||||
}
|
||||
|
||||
int init_tray_threaded() {
|
||||
if (tray_thread_running) {
|
||||
BOOST_LOG(warning) << "Tray thread is already running"sv;
|
||||
return 1;
|
||||
}
|
||||
|
||||
tray_thread_should_exit = false;
|
||||
|
||||
try {
|
||||
tray_thread = std::thread(tray_thread_worker);
|
||||
auto tray_thread = std::thread(tray_thread_worker);
|
||||
|
||||
// Wait for the thread to start and initialize
|
||||
const auto start_time = std::chrono::steady_clock::now();
|
||||
while (!tray_thread_running && !tray_thread_should_exit) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
|
||||
// Timeout after 10 seconds
|
||||
if (std::chrono::steady_clock::now() - start_time > std::chrono::seconds(10)) {
|
||||
BOOST_LOG(error) << "Tray thread initialization timeout"sv;
|
||||
tray_thread_should_exit = true;
|
||||
if (tray_thread.joinable()) {
|
||||
tray_thread.join();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!tray_thread_running) {
|
||||
BOOST_LOG(error) << "Tray thread failed to start"sv;
|
||||
if (tray_thread.joinable()) {
|
||||
tray_thread.join();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
// The tray thread doesn't require strong lifetime management.
|
||||
// It will exit asynchronously when tray_exit() is called.
|
||||
tray_thread.detach();
|
||||
|
||||
BOOST_LOG(info) << "System tray thread initialized successfully"sv;
|
||||
return 0;
|
||||
|
|
@ -385,21 +335,5 @@ namespace system_tray {
|
|||
}
|
||||
}
|
||||
|
||||
int end_tray_threaded() {
|
||||
if (!tray_thread_running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BOOST_LOG(info) << "Stopping system tray thread"sv;
|
||||
tray_thread_should_exit = true;
|
||||
|
||||
if (tray_thread.joinable()) {
|
||||
tray_thread.join();
|
||||
}
|
||||
|
||||
BOOST_LOG(info) << "System tray thread stopped"sv;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace system_tray
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue