2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-02 13:52:38 +00:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
|
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-04-10 15:30:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-07-06 21:48:02 +00:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/HashMap.h>
|
2020-02-15 01:09:00 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2021-08-30 10:43:28 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2021-08-14 23:50:16 +00:00
|
|
|
#include <AK/Time.h>
|
2019-04-10 15:30:34 +00:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
2021-08-30 10:43:28 +00:00
|
|
|
#include <LibCore/DeferredInvocationContext.h>
|
|
|
|
#include <LibCore/Event.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <LibCore/Forward.h>
|
2022-01-02 13:52:38 +00:00
|
|
|
#include <LibThreading/MutexProtected.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <sys/time.h>
|
2020-07-06 21:48:02 +00:00
|
|
|
#include <sys/types.h>
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2022-01-25 07:49:43 +00:00
|
|
|
extern Threading::MutexProtected<EventLoop*> s_main_event_loop;
|
2022-01-02 13:52:38 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class EventLoop {
|
2019-04-10 15:30:34 +00:00
|
|
|
public:
|
2021-05-13 20:42:11 +00:00
|
|
|
enum class MakeInspectable {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2022-02-11 15:57:10 +00:00
|
|
|
enum class ShouldWake {
|
|
|
|
No,
|
|
|
|
Yes
|
|
|
|
};
|
|
|
|
|
2021-05-22 21:26:51 +00:00
|
|
|
explicit EventLoop(MakeInspectable = MakeInspectable::No);
|
2020-02-02 11:34:39 +00:00
|
|
|
~EventLoop();
|
2022-01-02 14:14:25 +00:00
|
|
|
static void initialize_wake_pipes();
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
int exec();
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class WaitMode {
|
2019-05-18 11:39:21 +00:00
|
|
|
WaitForEvents,
|
|
|
|
PollForEvents,
|
|
|
|
};
|
|
|
|
|
|
|
|
// processe events, generally called by exec() in a loop.
|
|
|
|
// this should really only be used for integrating with other event loops
|
2022-01-05 23:55:48 +00:00
|
|
|
size_t pump(WaitMode = WaitMode::WaitForEvents);
|
2019-05-18 11:39:21 +00:00
|
|
|
|
2021-09-25 17:32:14 +00:00
|
|
|
void spin_until(Function<bool()>);
|
|
|
|
|
2022-02-11 15:57:10 +00:00
|
|
|
void post_event(Object& receiver, NonnullOwnPtr<Event>&&, ShouldWake = ShouldWake::No);
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2022-01-02 13:52:38 +00:00
|
|
|
template<typename Callback>
|
|
|
|
static decltype(auto) with_main_locked(Callback callback)
|
|
|
|
{
|
|
|
|
return s_main_event_loop.with_locked([&callback](auto*& event_loop) {
|
|
|
|
VERIFY(event_loop != nullptr);
|
|
|
|
return callback(event_loop);
|
|
|
|
});
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
static EventLoop& current();
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2019-05-26 15:53:03 +00:00
|
|
|
bool was_exit_requested() const { return m_exit_requested; }
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
|
2019-04-10 15:30:34 +00:00
|
|
|
static bool unregister_timer(int timer_id);
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
static void register_notifier(Badge<Notifier>, Notifier&);
|
|
|
|
static void unregister_notifier(Badge<Notifier>, Notifier&);
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
void quit(int);
|
2019-10-25 05:18:31 +00:00
|
|
|
void unquit();
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void take_pending_events_from(EventLoop& other)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2021-06-12 11:24:45 +00:00
|
|
|
m_queued_events.extend(move(other.m_queued_events));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 15:57:10 +00:00
|
|
|
static void wake_current();
|
|
|
|
void wake();
|
2019-07-14 08:20:57 +00:00
|
|
|
|
2020-07-06 21:48:02 +00:00
|
|
|
static int register_signal(int signo, Function<void(int)> handler);
|
|
|
|
static void unregister_signal(int handler_id);
|
|
|
|
|
2020-09-07 18:14:42 +00:00
|
|
|
// Note: Boost uses Parent/Child/Prepare, but we don't really have anything
|
|
|
|
// interesting to do in the parent or before forking.
|
|
|
|
enum class ForkEvent {
|
|
|
|
Child,
|
|
|
|
};
|
|
|
|
static void notify_forked(ForkEvent);
|
|
|
|
|
2021-08-25 17:59:45 +00:00
|
|
|
static bool has_been_instantiated();
|
|
|
|
|
2021-08-30 10:43:28 +00:00
|
|
|
void deferred_invoke(Function<void()> invokee)
|
|
|
|
{
|
|
|
|
auto context = DeferredInvocationContext::construct();
|
|
|
|
post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
private:
|
2019-05-18 11:39:21 +00:00
|
|
|
void wait_for_event(WaitMode);
|
2021-08-14 23:50:16 +00:00
|
|
|
Optional<Time> get_next_timer_expiration();
|
2020-07-06 21:48:02 +00:00
|
|
|
static void dispatch_signal(int);
|
|
|
|
static void handle_signal(int);
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
struct QueuedEvent {
|
2020-02-14 21:29:06 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(QueuedEvent);
|
2020-02-15 01:09:00 +00:00
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
public:
|
|
|
|
QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
|
|
|
|
QueuedEvent(QueuedEvent&&);
|
|
|
|
~QueuedEvent();
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
WeakPtr<Object> receiver;
|
|
|
|
NonnullOwnPtr<Event> event;
|
2019-04-10 15:30:34 +00:00
|
|
|
};
|
2019-05-18 11:39:21 +00:00
|
|
|
|
2019-04-20 12:02:19 +00:00
|
|
|
Vector<QueuedEvent, 64> m_queued_events;
|
2020-07-06 21:48:02 +00:00
|
|
|
static pid_t s_pid;
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
bool m_exit_requested { false };
|
|
|
|
int m_exit_code { 0 };
|
|
|
|
|
2022-01-02 13:52:38 +00:00
|
|
|
static thread_local int s_wake_pipe_fds[2];
|
2022-01-02 14:14:25 +00:00
|
|
|
static thread_local bool s_wake_pipe_initialized;
|
2019-07-14 08:20:57 +00:00
|
|
|
|
2022-02-11 15:57:10 +00:00
|
|
|
// The wake pipe of this event loop needs to be accessible from other threads.
|
|
|
|
int (*m_wake_pipe_fds)[2];
|
|
|
|
|
2020-02-15 01:09:00 +00:00
|
|
|
struct Private;
|
|
|
|
NonnullOwnPtr<Private> m_private;
|
2019-04-10 15:30:34 +00:00
|
|
|
};
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2021-08-30 10:43:28 +00:00
|
|
|
inline void deferred_invoke(Function<void()> invokee)
|
|
|
|
{
|
|
|
|
EventLoop::current().deferred_invoke(move(invokee));
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|