Skip to content

Commit e2ffc2d

Browse files
authored
refactor: use GCancellable for timer cancellation (#202)
* refactor: use GCancellable for timer cancellation - Replace std::atomic<bool> stop_timer_ with GCancellable* cancellable_ - Add cancellable_wait helper function for polling cancellation with timeout - Refactor timer_worker loop to use cancellable_wait instead of sleep loop - Include necessary <gio/gio.h> header - Adjust Qt header order in main.cpp for compilation error This change improves cancellation responsiveness and reduces CPU usage by leveraging GLib's cancellation mechanism. Bug: https://pms.uniontech.com/bug-view-339241.html * chore: Update version to 7.0.36 As title. Log: Update version to 7.0.36
1 parent e16153e commit e2ffc2d

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

debian/changelog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
deepin-anything (7.0.36) unstable; urgency=medium
2+
3+
* fix: Fix sw64 gcc not support pie by default
4+
* refactor: use GCancellable for timer cancellation
5+
6+
-- wangrong <wangrong@uniontech.com> Tue, 06 Jan 2026 13:20:26 +0800
7+
18
deepin-anything (7.0.35) unstable; urgency=medium
29

310
* refactor: Refactor and improve configuration loading and path handling

src/daemon/include/core/base_event_handler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <filesystem>
1010
#include <glib.h>
11+
#include <gio/gio.h>
1112

1213
#include "common/anything_fwd.hpp"
1314
#include "common/fs_event.h"
@@ -88,7 +89,7 @@ class base_event_handler
8889
std::vector<std::string> pending_paths_;
8990
std::vector<anything::index_job> jobs_;
9091
anything::thread_pool pool_;
91-
std::atomic<bool> stop_timer_;
92+
GCancellable *cancellable_;
9293
std::mutex jobs_mtx_;
9394
std::mutex pending_mtx_;
9495
std::thread timer_;

src/daemon/src/core/base_event_handler.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ base_event_handler::base_event_handler(const event_handler_config &config)
1717
index_manager_(config_.persistent_index_dir, config_.volatile_index_dir, config_.file_type_mapping),
1818
batch_size_(200),
1919
pool_(1),
20-
stop_timer_(false),
20+
cancellable_(g_cancellable_new()),
2121
delay_mode_(true/*index_manager_.indexed()*/),
2222
index_dirty_(false),
2323
volatile_index_dirty_(false),
@@ -34,10 +34,11 @@ base_event_handler::base_event_handler(const event_handler_config &config)
3434
}
3535

3636
base_event_handler::~base_event_handler() {
37+
g_object_unref(cancellable_);
3738
}
3839

3940
void base_event_handler::terminate_processing() {
40-
stop_timer_ = true;
41+
g_cancellable_cancel(cancellable_);
4142
stop_scan_directory_ = true;
4243

4344
if (timer_.joinable()) {
@@ -276,19 +277,54 @@ void base_event_handler::jobs_push(std::string src,
276277
}
277278
}
278279

280+
// Returns: TRUE if the cancellable was cancelled, FALSE if it timed out.
281+
static gboolean
282+
cancellable_wait(GCancellable *cancellable, int cancellable_fd, gint timeout_ms)
283+
{
284+
if (g_cancellable_is_cancelled(cancellable)) {
285+
return TRUE;
286+
}
287+
288+
if (cancellable_fd == -1) {
289+
if (timeout_ms >= 0) {
290+
g_usleep(timeout_ms * 1000);
291+
}
292+
return g_cancellable_is_cancelled(cancellable);
293+
}
294+
295+
GPollFD pfd;
296+
pfd.fd = cancellable_fd;
297+
pfd.events = G_IO_IN;
298+
pfd.revents = 0;
299+
300+
int ret = g_poll(&pfd, 1, timeout_ms);
301+
302+
if (ret > 0) {
303+
// cancel event
304+
if (pfd.revents & G_IO_IN) {
305+
return TRUE;
306+
}
307+
} else if (ret == 0) {
308+
// timeout
309+
;
310+
} else {
311+
// error
312+
spdlog::warn("g_poll() returned an error: %s", g_strerror(errno));
313+
if (timeout_ms >= 0) {
314+
g_usleep(timeout_ms * 1000);
315+
}
316+
}
317+
318+
return g_cancellable_is_cancelled(cancellable);
319+
}
320+
279321
void base_event_handler::timer_worker(int64_t interval) {
280322
// When pending_batch_size is small, CPU usage is low, but total indexing time is longer.
281323
// When pending_batch_size is large, CPU usage is high, but total indexing time is shorter.
282324
constexpr std::size_t pending_batch_size = 20000;
283-
while(!stop_timer_) {
284-
// {
285-
// std::lock_guard<std::mutex> lock(jobs_mtx_);
286-
// if (!jobs_.empty()) {
287-
// eat_jobs(jobs_, std::min(batch_size_, jobs_.size()));
288-
// }
289-
// }
290-
// std::this_thread::sleep_for(std::chrono::milliseconds(interval));
325+
int cancellable_fd = g_cancellable_get_fd(cancellable_);
291326

327+
while(!cancellable_wait(cancellable_, cancellable_fd, (gint)interval)) {
292328
bool idle = false;
293329
{
294330
std::lock_guard<std::mutex> lock(jobs_mtx_);
@@ -379,8 +415,10 @@ void base_event_handler::timer_worker(int64_t interval) {
379415
set_index_invalid_and_restart();
380416
}
381417
}
418+
}
382419

383-
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
420+
if (cancellable_fd != -1) {
421+
g_cancellable_release_fd(cancellable_);
384422
}
385423
}
386424

src/daemon/src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
#include <unistd.h>
77
#include <pwd.h>
8-
#include <QCoreApplication>
9-
#include <QTimer>
108

119
#include "anything.hpp"
1210
#include "core/config.h"
1311

12+
#include <QTimer>
13+
#include <QCoreApplication>
14+
1415
using namespace anything;
1516

1617
// 判断 uid 是否可登录

0 commit comments

Comments
 (0)