2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-04-10 12:30:34 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <AK/Forward.h>
|
2020-07-06 18:48:02 -03:00
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <AK/HashMap.h>
|
2020-02-14 22:09:00 -03:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2021-08-30 07:43:28 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2021-08-14 20:50:16 -03:00
|
|
|
#include <AK/Time.h>
|
2019-04-10 12:30:34 -03:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
#include <AK/WeakPtr.h>
|
2021-08-30 07:43:28 -03:00
|
|
|
#include <LibCore/DeferredInvocationContext.h>
|
|
|
|
|
#include <LibCore/Event.h>
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <LibCore/Forward.h>
|
2019-05-28 08:48:06 -03:00
|
|
|
#include <sys/time.h>
|
2020-07-06 18:48:02 -03:00
|
|
|
#include <sys/types.h>
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
class EventLoop {
|
2019-04-10 12:30:34 -03:00
|
|
|
public:
|
2021-05-13 17:42:11 -03:00
|
|
|
enum class MakeInspectable {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-22 18:26:51 -03:00
|
|
|
explicit EventLoop(MakeInspectable = MakeInspectable::No);
|
2020-02-02 08:34:39 -03:00
|
|
|
~EventLoop();
|
2019-04-10 12:30:34 -03:00
|
|
|
|
|
|
|
|
int exec();
|
|
|
|
|
|
2019-06-07 12:13:23 -03:00
|
|
|
enum class WaitMode {
|
2019-05-18 08:39:21 -03:00
|
|
|
WaitForEvents,
|
|
|
|
|
PollForEvents,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// processe events, generally called by exec() in a loop.
|
|
|
|
|
// this should really only be used for integrating with other event loops
|
|
|
|
|
void pump(WaitMode = WaitMode::WaitForEvents);
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
static EventLoop& main();
|
|
|
|
|
static EventLoop& current();
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2019-05-26 12:53:03 -03:00
|
|
|
bool was_exit_requested() const { return m_exit_requested; }
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
|
2019-04-10 12:30:34 -03:00
|
|
|
static bool unregister_timer(int timer_id);
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
static void register_notifier(Badge<Notifier>, Notifier&);
|
|
|
|
|
static void unregister_notifier(Badge<Notifier>, Notifier&);
|
2019-04-10 12:30:34 -03:00
|
|
|
|
|
|
|
|
void quit(int);
|
2019-10-25 02:18:31 -03:00
|
|
|
void unquit();
|
2019-04-10 12:30:34 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void take_pending_events_from(EventLoop& other)
|
2019-04-10 12:30:34 -03:00
|
|
|
{
|
2021-06-12 08:24:45 -03:00
|
|
|
m_queued_events.extend(move(other.m_queued_events));
|
2019-04-10 12:30:34 -03:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 05:20:57 -03:00
|
|
|
static void wake();
|
|
|
|
|
|
2020-07-06 18:48:02 -03:00
|
|
|
static int register_signal(int signo, Function<void(int)> handler);
|
|
|
|
|
static void unregister_signal(int handler_id);
|
|
|
|
|
|
2020-09-07 15:14:42 -03: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 14:59:45 -03:00
|
|
|
static bool has_been_instantiated();
|
|
|
|
|
|
2021-08-30 07:43:28 -03:00
|
|
|
void deferred_invoke(Function<void()> invokee)
|
|
|
|
|
{
|
|
|
|
|
auto context = DeferredInvocationContext::construct();
|
|
|
|
|
post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 12:30:34 -03:00
|
|
|
private:
|
2019-05-18 08:39:21 -03:00
|
|
|
void wait_for_event(WaitMode);
|
2021-08-14 20:50:16 -03:00
|
|
|
Optional<Time> get_next_timer_expiration();
|
2020-07-06 18:48:02 -03:00
|
|
|
static void dispatch_signal(int);
|
|
|
|
|
static void handle_signal(int);
|
2019-04-10 12:30:34 -03:00
|
|
|
|
|
|
|
|
struct QueuedEvent {
|
2020-02-14 18:29:06 -03:00
|
|
|
AK_MAKE_NONCOPYABLE(QueuedEvent);
|
2020-02-14 22:09:00 -03:00
|
|
|
|
2020-02-14 18:29:06 -03:00
|
|
|
public:
|
|
|
|
|
QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
|
|
|
|
|
QueuedEvent(QueuedEvent&&);
|
|
|
|
|
~QueuedEvent();
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
WeakPtr<Object> receiver;
|
|
|
|
|
NonnullOwnPtr<Event> event;
|
2019-04-10 12:30:34 -03:00
|
|
|
};
|
2019-05-18 08:39:21 -03:00
|
|
|
|
2019-04-20 09:02:19 -03:00
|
|
|
Vector<QueuedEvent, 64> m_queued_events;
|
2020-07-06 18:48:02 -03:00
|
|
|
static pid_t s_pid;
|
2019-04-10 12:30:34 -03:00
|
|
|
|
|
|
|
|
bool m_exit_requested { false };
|
|
|
|
|
int m_exit_code { 0 };
|
|
|
|
|
|
2019-07-14 05:20:57 -03:00
|
|
|
static int s_wake_pipe_fds[2];
|
|
|
|
|
|
2020-02-14 22:09:00 -03:00
|
|
|
struct Private;
|
|
|
|
|
NonnullOwnPtr<Private> m_private;
|
2019-04-10 12:30:34 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
2021-08-30 07:43:28 -03:00
|
|
|
inline void deferred_invoke(Function<void()> invokee)
|
|
|
|
|
{
|
|
|
|
|
EventLoop::current().deferred_invoke(move(invokee));
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
}
|