2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-07-04 18:01:01 +02:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-08-25 18:55:56 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
|
#include <AK/Queue.h>
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <LibCore/Event.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
|
#include <LibCore/Object.h>
|
2021-05-22 18:47:42 +02:00
|
|
|
#include <LibThreading/Thread.h>
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-05-22 18:47:42 +02:00
|
|
|
namespace Threading {
|
2019-08-25 18:55:56 +03:00
|
|
|
|
|
|
|
|
template<typename Result>
|
|
|
|
|
class BackgroundAction;
|
|
|
|
|
|
|
|
|
|
class BackgroundActionBase {
|
|
|
|
|
template<typename Result>
|
|
|
|
|
friend class BackgroundAction;
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-18 09:49:51 +02:00
|
|
|
BackgroundActionBase() { }
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-07-04 18:01:01 +02:00
|
|
|
static void enqueue_work(Function<void()>);
|
2019-08-25 18:55:56 +03:00
|
|
|
static Thread& background_thread();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Result>
|
2020-02-02 12:34:39 +01:00
|
|
|
class BackgroundAction final : public Core::Object
|
2019-08-25 18:55:56 +03:00
|
|
|
, private BackgroundActionBase {
|
|
|
|
|
C_OBJECT(BackgroundAction);
|
2020-02-02 12:34:39 +01:00
|
|
|
|
2019-08-25 18:55:56 +03:00
|
|
|
public:
|
2021-07-02 10:34:19 -04:00
|
|
|
void cancel()
|
|
|
|
|
{
|
|
|
|
|
m_cancelled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_cancelled() const
|
|
|
|
|
{
|
|
|
|
|
return m_cancelled;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 09:49:51 +02:00
|
|
|
virtual ~BackgroundAction() { }
|
2019-08-25 18:55:56 +03:00
|
|
|
|
|
|
|
|
private:
|
2021-07-02 10:34:19 -04:00
|
|
|
BackgroundAction(Function<Result(BackgroundAction&)> action, Function<void(Result)> on_complete)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Core::Object(&background_thread())
|
2019-08-25 18:55:56 +03:00
|
|
|
, m_action(move(action))
|
|
|
|
|
, m_on_complete(move(on_complete))
|
|
|
|
|
{
|
2022-01-25 08:54:54 +01:00
|
|
|
enqueue_work([this, origin_event_loop = &Core::EventLoop::current()] {
|
2021-07-02 10:34:19 -04:00
|
|
|
m_result = m_action(*this);
|
2019-08-25 18:55:56 +03:00
|
|
|
if (m_on_complete) {
|
2022-01-25 08:54:54 +01:00
|
|
|
origin_event_loop->deferred_invoke([this] {
|
2021-07-12 11:29:37 +02:00
|
|
|
m_on_complete(m_result.release_value());
|
2021-08-30 10:43:28 +00:00
|
|
|
remove_from_parent();
|
|
|
|
|
});
|
2022-02-11 16:57:10 +01:00
|
|
|
origin_event_loop->wake();
|
2020-02-25 12:05:25 +03:00
|
|
|
} else {
|
|
|
|
|
this->remove_from_parent();
|
|
|
|
|
}
|
2019-08-25 18:55:56 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 10:34:19 -04:00
|
|
|
bool m_cancelled { false };
|
|
|
|
|
Function<Result(BackgroundAction&)> m_action;
|
2019-08-25 18:55:56 +03:00
|
|
|
Function<void(Result)> m_on_complete;
|
|
|
|
|
Optional<Result> m_result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|