2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-25 15:55:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/Queue.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <LibCore/Event.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
#include <LibCore/Object.h>
|
2021-05-22 16:47:42 +00:00
|
|
|
#include <LibThreading/Lock.h>
|
|
|
|
#include <LibThreading/Thread.h>
|
2019-08-25 15:55:56 +00:00
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
namespace Threading {
|
2019-08-25 15:55:56 +00:00
|
|
|
|
|
|
|
template<typename Result>
|
|
|
|
class BackgroundAction;
|
|
|
|
|
|
|
|
class BackgroundActionBase {
|
|
|
|
template<typename Result>
|
|
|
|
friend class BackgroundAction;
|
|
|
|
|
|
|
|
private:
|
2020-09-18 07:49:51 +00:00
|
|
|
BackgroundActionBase() { }
|
2019-08-25 15:55:56 +00:00
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
static Lockable<Queue<Function<void()>>>& all_actions();
|
2019-08-25 15:55:56 +00:00
|
|
|
static Thread& background_thread();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Result>
|
2020-02-02 11:34:39 +00:00
|
|
|
class BackgroundAction final : public Core::Object
|
2019-08-25 15:55:56 +00:00
|
|
|
, private BackgroundActionBase {
|
|
|
|
C_OBJECT(BackgroundAction);
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2019-08-25 15:55:56 +00:00
|
|
|
public:
|
|
|
|
static NonnullRefPtr<BackgroundAction<Result>> create(
|
|
|
|
Function<Result()> action,
|
2020-02-02 11:34:39 +00:00
|
|
|
Function<void(Result)> on_complete = nullptr)
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
2021-04-23 14:46:57 +00:00
|
|
|
return adopt_ref(*new BackgroundAction(move(action), move(on_complete)));
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 07:49:51 +00:00
|
|
|
virtual ~BackgroundAction() { }
|
2019-08-25 15:55:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
BackgroundAction(Function<Result()> action, Function<void(Result)> on_complete)
|
2020-02-02 11:34:39 +00:00
|
|
|
: Core::Object(&background_thread())
|
2019-08-25 15:55:56 +00:00
|
|
|
, m_action(move(action))
|
|
|
|
, m_on_complete(move(on_complete))
|
|
|
|
{
|
2021-05-10 08:21:35 +00:00
|
|
|
Locker locker(all_actions().lock());
|
2019-08-25 15:55:56 +00:00
|
|
|
|
|
|
|
all_actions().resource().enqueue([this] {
|
|
|
|
m_result = m_action();
|
|
|
|
if (m_on_complete) {
|
2020-02-24 20:18:35 +00:00
|
|
|
Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>([this](auto&) {
|
2019-08-25 15:55:56 +00:00
|
|
|
m_on_complete(m_result.release_value());
|
2020-02-25 09:05:25 +00:00
|
|
|
this->remove_from_parent();
|
2019-08-25 15:55:56 +00:00
|
|
|
}));
|
2020-02-24 20:18:35 +00:00
|
|
|
Core::EventLoop::wake();
|
2020-02-25 09:05:25 +00:00
|
|
|
} else {
|
|
|
|
this->remove_from_parent();
|
|
|
|
}
|
2019-08-25 15:55:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Function<Result()> m_action;
|
|
|
|
Function<void(Result)> m_on_complete;
|
|
|
|
Optional<Result> m_result;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|