Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/Function.h>
30#include <AK/NonnullRefPtr.h>
31#include <AK/Optional.h>
32#include <AK/Queue.h>
33#include <LibCore/Event.h>
34#include <LibCore/EventLoop.h>
35#include <LibCore/Object.h>
36#include <LibThread/Lock.h>
37#include <LibThread/Thread.h>
38
39namespace LibThread {
40
41template<typename Result>
42class BackgroundAction;
43
44class BackgroundActionBase {
45 template<typename Result>
46 friend class BackgroundAction;
47
48private:
49 BackgroundActionBase() {}
50
51 static Lockable<Queue<Function<void()>>>& all_actions();
52 static Thread& background_thread();
53};
54
55template<typename Result>
56class BackgroundAction final : public Core::Object
57 , private BackgroundActionBase {
58 C_OBJECT(BackgroundAction);
59
60public:
61 static NonnullRefPtr<BackgroundAction<Result>> create(
62 Function<Result()> action,
63 Function<void(Result)> on_complete = nullptr)
64 {
65 return adopt(*new BackgroundAction(move(action), move(on_complete)));
66 }
67
68 virtual ~BackgroundAction() {}
69
70private:
71 BackgroundAction(Function<Result()> action, Function<void(Result)> on_complete)
72 : Core::Object(&background_thread())
73 , m_action(move(action))
74 , m_on_complete(move(on_complete))
75 {
76 LOCKER(all_actions().lock());
77
78 all_actions().resource().enqueue([this] {
79 m_result = m_action();
80 if (m_on_complete) {
81 Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>([this](auto&) {
82 m_on_complete(m_result.release_value());
83 this->remove_from_parent();
84 }));
85 Core::EventLoop::wake();
86 } else {
87 this->remove_from_parent();
88 }
89 });
90 }
91
92 Function<Result()> m_action;
93 Function<void(Result)> m_on_complete;
94 Optional<Result> m_result;
95};
96
97}