From 00f93b254547c6a63e58cfc46591f6db07cc6765 Mon Sep 17 00:00:00 2001 From: Spencer Dixon Date: Fri, 2 Jul 2021 10:34:19 -0400 Subject: [PATCH] LibThreading: Add ability to cancel ongoing BackgroundActions Handlers of the BackgroundAction are responsible for checking if the action has been cancelled and returning early. --- .../SystemMonitor/ThreadStackWidget.cpp | 2 +- Userland/Libraries/LibGUI/FileSystemModel.cpp | 2 +- .../Libraries/LibThreading/BackgroundAction.h | 19 +++++++++++++++---- Userland/Services/WindowServer/Compositor.cpp | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp b/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp index 23999e4f0e..a108faa749 100644 --- a/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp +++ b/Userland/Applications/SystemMonitor/ThreadStackWidget.cpp @@ -61,7 +61,7 @@ private: void ThreadStackWidget::refresh() { Threading::BackgroundAction>::create( - [pid = m_pid, tid = m_tid] { + [pid = m_pid, tid = m_tid](auto&) { return Symbolication::symbolicate_thread(pid, tid); }, diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index af46539059..06d1b0c97d 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -587,7 +587,7 @@ bool FileSystemModel::fetch_thumbnail_for(const Node& node) auto weak_this = make_weak_ptr(); Threading::BackgroundAction>::create( - [path] { + [path](auto&) { return render_thumbnail(path); }, diff --git a/Userland/Libraries/LibThreading/BackgroundAction.h b/Userland/Libraries/LibThreading/BackgroundAction.h index 9c40873a3c..2ac77d5758 100644 --- a/Userland/Libraries/LibThreading/BackgroundAction.h +++ b/Userland/Libraries/LibThreading/BackgroundAction.h @@ -39,16 +39,26 @@ class BackgroundAction final : public Core::Object public: static NonnullRefPtr> create( - Function action, + Function action, Function on_complete = nullptr) { return adopt_ref(*new BackgroundAction(move(action), move(on_complete))); } + void cancel() + { + m_cancelled = true; + } + + bool is_cancelled() const + { + return m_cancelled; + } + virtual ~BackgroundAction() { } private: - BackgroundAction(Function action, Function on_complete) + BackgroundAction(Function action, Function on_complete) : Core::Object(&background_thread()) , m_action(move(action)) , m_on_complete(move(on_complete)) @@ -56,7 +66,7 @@ private: Locker locker(all_actions().lock()); all_actions().resource().enqueue([this] { - m_result = m_action(); + m_result = m_action(*this); if (m_on_complete) { Core::EventLoop::current().post_event(*this, make([this](auto&) { m_on_complete(m_result.release_value()); @@ -69,7 +79,8 @@ private: }); } - Function m_action; + bool m_cancelled { false }; + Function m_action; Function m_on_complete; Optional m_result; }; diff --git a/Userland/Services/WindowServer/Compositor.cpp b/Userland/Services/WindowServer/Compositor.cpp index 5ab367ff80..f851d64a88 100644 --- a/Userland/Services/WindowServer/Compositor.cpp +++ b/Userland/Services/WindowServer/Compositor.cpp @@ -689,7 +689,7 @@ bool Compositor::set_wallpaper_mode(const String& mode) bool Compositor::set_wallpaper(const String& path, Function&& callback) { Threading::BackgroundAction>::create( - [path] { + [path](auto&) { return Gfx::Bitmap::load_from_file(path); },