mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 22:15:06 +00:00
LibThreading: Add ability to cancel ongoing BackgroundActions
Handlers of the BackgroundAction are responsible for checking if the action has been cancelled and returning early.
This commit is contained in:
parent
4a3958c8ae
commit
00f93b2545
4 changed files with 18 additions and 7 deletions
|
@ -61,7 +61,7 @@ private:
|
|||
void ThreadStackWidget::refresh()
|
||||
{
|
||||
Threading::BackgroundAction<Vector<Symbolication::Symbol>>::create(
|
||||
[pid = m_pid, tid = m_tid] {
|
||||
[pid = m_pid, tid = m_tid](auto&) {
|
||||
return Symbolication::symbolicate_thread(pid, tid);
|
||||
},
|
||||
|
||||
|
|
|
@ -587,7 +587,7 @@ bool FileSystemModel::fetch_thumbnail_for(const Node& node)
|
|||
auto weak_this = make_weak_ptr();
|
||||
|
||||
Threading::BackgroundAction<RefPtr<Gfx::Bitmap>>::create(
|
||||
[path] {
|
||||
[path](auto&) {
|
||||
return render_thumbnail(path);
|
||||
},
|
||||
|
||||
|
|
|
@ -39,16 +39,26 @@ class BackgroundAction final : public Core::Object
|
|||
|
||||
public:
|
||||
static NonnullRefPtr<BackgroundAction<Result>> create(
|
||||
Function<Result()> action,
|
||||
Function<Result(BackgroundAction&)> action,
|
||||
Function<void(Result)> 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<Result()> action, Function<void(Result)> on_complete)
|
||||
BackgroundAction(Function<Result(BackgroundAction&)> action, Function<void(Result)> 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<Core::DeferredInvocationEvent>([this](auto&) {
|
||||
m_on_complete(m_result.release_value());
|
||||
|
@ -69,7 +79,8 @@ private:
|
|||
});
|
||||
}
|
||||
|
||||
Function<Result()> m_action;
|
||||
bool m_cancelled { false };
|
||||
Function<Result(BackgroundAction&)> m_action;
|
||||
Function<void(Result)> m_on_complete;
|
||||
Optional<Result> m_result;
|
||||
};
|
||||
|
|
|
@ -689,7 +689,7 @@ bool Compositor::set_wallpaper_mode(const String& mode)
|
|||
bool Compositor::set_wallpaper(const String& path, Function<void(bool)>&& callback)
|
||||
{
|
||||
Threading::BackgroundAction<RefPtr<Gfx::Bitmap>>::create(
|
||||
[path] {
|
||||
[path](auto&) {
|
||||
return Gfx::Bitmap::load_from_file(path);
|
||||
},
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue