2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-02 16:52:38 +03:00
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <malu.bertsch@gmail.com>
|
2022-02-26 19:09:45 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2022-01-21 15:23:57 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-02-15 00:29:06 +03:00
|
|
|
#include <AK/Badge.h>
|
2021-01-24 17:28:26 +03:00
|
|
|
#include <AK/Debug.h>
|
2021-04-14 11:29:33 +03:00
|
|
|
#include <AK/Format.h>
|
2020-01-05 02:28:42 +03:00
|
|
|
#include <AK/IDAllocator.h>
|
2019-08-17 12:35:09 +03:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-03-10 13:31:37 +03:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2021-01-08 22:09:39 +03:00
|
|
|
#include <AK/Singleton.h>
|
2020-07-07 00:48:02 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <AK/Time.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-15 00:29:06 +03:00
|
|
|
#include <LibCore/LocalServer.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
#include <LibCore/Object.h>
|
2021-07-09 12:14:57 +03:00
|
|
|
#include <LibThreading/Mutex.h>
|
2022-01-02 16:52:38 +03:00
|
|
|
#include <LibThreading/MutexProtected.h>
|
2019-06-22 22:21:57 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2020-07-07 00:48:02 +03:00
|
|
|
#include <signal.h>
|
2019-06-22 22:21:57 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2019-04-10 18:30:34 +03:00
|
|
|
|
2022-01-23 16:47:10 +03:00
|
|
|
#ifdef __serenity__
|
|
|
|
extern bool s_global_initializers_ran;
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
namespace Core {
|
|
|
|
|
2021-05-13 23:42:11 +03:00
|
|
|
class InspectorServerConnection;
|
|
|
|
|
|
|
|
[[maybe_unused]] static bool connect_to_inspector_server();
|
2020-01-03 22:27:48 +03:00
|
|
|
|
2020-02-15 04:09:00 +03:00
|
|
|
struct EventLoopTimer {
|
|
|
|
int timer_id { 0 };
|
2021-08-15 02:50:16 +03:00
|
|
|
Time interval;
|
|
|
|
Time fire_time;
|
2020-02-15 04:09:00 +03:00
|
|
|
bool should_reload { false };
|
|
|
|
TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
|
|
|
|
WeakPtr<Object> owner;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void reload(Time const& now);
|
|
|
|
bool has_expired(Time const& now) const;
|
2020-02-15 04:09:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EventLoop::Private {
|
2021-07-09 12:14:57 +03:00
|
|
|
Threading::Mutex lock;
|
2020-02-15 04:09:00 +03:00
|
|
|
};
|
|
|
|
|
2022-01-02 16:52:38 +03:00
|
|
|
// The main event loop is global to the program, so it may be accessed from multiple threads.
|
2022-01-25 10:49:43 +03:00
|
|
|
Threading::MutexProtected<EventLoop*> s_main_event_loop;
|
2022-01-02 16:52:38 +03:00
|
|
|
static Threading::MutexProtected<NeverDestroyed<IDAllocator>> s_id_allocator;
|
|
|
|
static Threading::MutexProtected<RefPtr<InspectorServerConnection>> s_inspector_server_connection;
|
2022-01-07 02:45:27 +03:00
|
|
|
|
2022-01-02 16:52:38 +03:00
|
|
|
// Each thread has its own event loop stack, its own timers, notifiers and a wake pipe.
|
|
|
|
static thread_local Vector<EventLoop&>* s_event_loop_stack;
|
|
|
|
static thread_local HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
|
|
|
|
static thread_local HashTable<Notifier*>* s_notifiers;
|
|
|
|
thread_local int EventLoop::s_wake_pipe_fds[2];
|
2022-01-02 17:14:25 +03:00
|
|
|
thread_local bool EventLoop::s_wake_pipe_initialized { false };
|
|
|
|
|
|
|
|
void EventLoop::initialize_wake_pipes()
|
|
|
|
{
|
|
|
|
if (!s_wake_pipe_initialized) {
|
|
|
|
#if defined(SOCK_NONBLOCK)
|
|
|
|
int rc = pipe2(s_wake_pipe_fds, O_CLOEXEC);
|
|
|
|
#else
|
|
|
|
int rc = pipe(s_wake_pipe_fds);
|
|
|
|
fcntl(s_wake_pipe_fds[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(s_wake_pipe_fds[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
s_wake_pipe_initialized = true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 18:30:34 +03:00
|
|
|
|
2021-08-25 20:59:45 +03:00
|
|
|
bool EventLoop::has_been_instantiated()
|
|
|
|
{
|
2022-01-02 16:52:38 +03:00
|
|
|
return s_event_loop_stack != nullptr && !s_event_loop_stack->is_empty();
|
2021-08-25 20:59:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
class SignalHandlers : public RefCounted<SignalHandlers> {
|
|
|
|
AK_MAKE_NONCOPYABLE(SignalHandlers);
|
|
|
|
AK_MAKE_NONMOVABLE(SignalHandlers);
|
|
|
|
|
|
|
|
public:
|
|
|
|
SignalHandlers(int signo, void (*handle_signal)(int));
|
|
|
|
~SignalHandlers();
|
|
|
|
|
|
|
|
void dispatch();
|
|
|
|
int add(Function<void(int)>&& handler);
|
|
|
|
bool remove(int handler_id);
|
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
if (m_calling_handlers) {
|
|
|
|
for (auto& handler : m_handlers_pending) {
|
|
|
|
if (handler.value)
|
|
|
|
return false; // an add is pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m_handlers.is_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool have(int handler_id) const
|
|
|
|
{
|
|
|
|
if (m_calling_handlers) {
|
|
|
|
auto it = m_handlers_pending.find(handler_id);
|
|
|
|
if (it != m_handlers_pending.end()) {
|
|
|
|
if (!it->value)
|
|
|
|
return false; // a deletion is pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m_handlers.contains(handler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_signo;
|
|
|
|
void (*m_original_handler)(int); // TODO: can't use sighandler_t?
|
|
|
|
HashMap<int, Function<void(int)>> m_handlers;
|
|
|
|
HashMap<int, Function<void(int)>> m_handlers_pending;
|
|
|
|
bool m_calling_handlers { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SignalHandlersInfo {
|
|
|
|
HashMap<int, NonnullRefPtr<SignalHandlers>> signal_handlers;
|
|
|
|
int next_signal_id { 0 };
|
|
|
|
};
|
|
|
|
|
2021-08-07 22:34:11 +03:00
|
|
|
static Singleton<SignalHandlersInfo> s_signals;
|
2021-01-08 22:09:39 +03:00
|
|
|
template<bool create_if_null = true>
|
|
|
|
inline SignalHandlersInfo* signals_info()
|
|
|
|
{
|
2021-06-24 11:28:36 +03:00
|
|
|
return s_signals.ptr();
|
2021-01-08 22:09:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t EventLoop::s_pid;
|
|
|
|
|
2021-05-13 23:42:11 +03:00
|
|
|
class InspectorServerConnection : public Object {
|
|
|
|
C_OBJECT(InspectorServerConnection)
|
2021-11-01 01:38:04 +03:00
|
|
|
private:
|
2022-01-15 14:57:58 +03:00
|
|
|
explicit InspectorServerConnection(NonnullOwnPtr<Stream::LocalSocket> socket)
|
2019-09-21 11:28:02 +03:00
|
|
|
: m_socket(move(socket))
|
2022-01-02 16:52:38 +03:00
|
|
|
, m_client_id(s_id_allocator.with_locked([](auto& allocator) {
|
|
|
|
return allocator->allocate();
|
|
|
|
}))
|
2019-09-11 22:19:23 +03:00
|
|
|
{
|
2021-01-12 17:08:52 +03:00
|
|
|
#ifdef __serenity__
|
2019-09-21 11:28:02 +03:00
|
|
|
m_socket->on_ready_to_read = [this] {
|
2020-02-20 14:54:15 +03:00
|
|
|
u32 length;
|
2022-01-15 14:57:58 +03:00
|
|
|
auto maybe_nread = m_socket->read({ (u8*)&length, sizeof(length) });
|
|
|
|
if (maybe_nread.is_error()) {
|
|
|
|
dbgln("InspectorServerConnection: Failed to read message length from inspector server connection: {}", maybe_nread.error());
|
|
|
|
shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nread = maybe_nread.release_value();
|
2019-09-11 22:19:23 +03:00
|
|
|
if (nread == 0) {
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "RPC client disconnected");
|
2019-09-22 01:17:53 +03:00
|
|
|
shutdown();
|
2019-09-11 22:19:23 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-15 14:57:58 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(nread == sizeof(length));
|
2019-09-11 22:19:23 +03:00
|
|
|
|
2022-01-15 14:57:58 +03:00
|
|
|
auto request_buffer = ByteBuffer::create_uninitialized(length).release_value();
|
|
|
|
maybe_nread = m_socket->read(request_buffer.bytes());
|
|
|
|
if (maybe_nread.is_error()) {
|
|
|
|
dbgln("InspectorServerConnection: Failed to read message content from inspector server connection: {}", maybe_nread.error());
|
|
|
|
shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nread = maybe_nread.release_value();
|
|
|
|
|
|
|
|
auto request_json = JsonValue::from_string(request_buffer);
|
2021-11-15 03:46:51 +03:00
|
|
|
if (request_json.is_error() || !request_json.value().is_object()) {
|
2020-10-15 14:21:23 +03:00
|
|
|
dbgln("RPC client sent invalid request");
|
2019-09-22 01:17:53 +03:00
|
|
|
shutdown();
|
2019-09-11 22:19:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-11 07:40:27 +03:00
|
|
|
handle_request(request_json.value().as_object());
|
2019-09-11 22:19:23 +03:00
|
|
|
};
|
2021-01-12 17:08:52 +03:00
|
|
|
#else
|
|
|
|
warnln("RPC Client constructed outside serenity, this is very likely a bug!");
|
|
|
|
#endif
|
2019-09-11 22:19:23 +03:00
|
|
|
}
|
2021-05-13 23:42:11 +03:00
|
|
|
virtual ~InspectorServerConnection() override
|
2019-09-11 22:19:23 +03:00
|
|
|
{
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
if (auto inspected_object = m_inspected_object.strong_ref())
|
|
|
|
inspected_object->decrement_inspector_count({});
|
2019-09-11 22:19:23 +03:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:38:04 +03:00
|
|
|
public:
|
2022-04-01 20:58:27 +03:00
|
|
|
void send_response(JsonObject const& response)
|
2019-09-11 22:19:23 +03:00
|
|
|
{
|
|
|
|
auto serialized = response.to_string();
|
2020-02-20 14:54:15 +03:00
|
|
|
u32 length = serialized.length();
|
2022-01-15 14:57:58 +03:00
|
|
|
// FIXME: Propagate errors
|
2022-04-01 20:58:27 +03:00
|
|
|
MUST(m_socket->write({ (u8 const*)&length, sizeof(length) }));
|
2022-01-15 14:57:58 +03:00
|
|
|
MUST(m_socket->write(serialized.bytes()));
|
2019-09-11 22:19:23 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void handle_request(JsonObject const& request)
|
2019-09-11 22:19:23 +03:00
|
|
|
{
|
|
|
|
auto type = request.get("type").as_string_or({});
|
|
|
|
|
|
|
|
if (type.is_null()) {
|
2020-10-15 14:21:23 +03:00
|
|
|
dbgln("RPC client sent request without type field");
|
2019-09-11 22:19:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == "Identify") {
|
|
|
|
JsonObject response;
|
|
|
|
response.set("type", type);
|
|
|
|
response.set("pid", getpid());
|
|
|
|
#ifdef __serenity__
|
|
|
|
char buffer[1024];
|
|
|
|
if (get_process_name(buffer, sizeof(buffer)) >= 0) {
|
|
|
|
response.set("process_name", buffer);
|
|
|
|
} else {
|
|
|
|
response.set("process_name", JsonValue());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
send_response(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == "GetAllObjects") {
|
|
|
|
JsonObject response;
|
|
|
|
response.set("type", type);
|
|
|
|
JsonArray objects;
|
2020-02-02 14:34:39 +03:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2019-09-11 22:19:23 +03:00
|
|
|
JsonObject json_object;
|
|
|
|
object.save_to(json_object);
|
|
|
|
objects.append(move(json_object));
|
|
|
|
}
|
|
|
|
response.set("objects", move(objects));
|
|
|
|
send_response(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:40:47 +03:00
|
|
|
if (type == "SetInspectedObject") {
|
2020-03-08 12:36:51 +03:00
|
|
|
auto address = request.get("address").to_number<FlatPtr>();
|
2020-03-05 16:40:47 +03:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2020-03-08 12:36:51 +03:00
|
|
|
if ((FlatPtr)&object == address) {
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
if (auto inspected_object = m_inspected_object.strong_ref())
|
|
|
|
inspected_object->decrement_inspector_count({});
|
|
|
|
m_inspected_object = object;
|
|
|
|
object.increment_inspector_count({});
|
2020-03-05 17:46:00 +03:00
|
|
|
break;
|
2020-03-05 16:40:47 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 17:46:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == "SetProperty") {
|
2020-03-08 12:36:51 +03:00
|
|
|
auto address = request.get("address").to_number<FlatPtr>();
|
2020-03-05 17:46:00 +03:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2020-03-08 12:36:51 +03:00
|
|
|
if ((FlatPtr)&object == address) {
|
2020-03-05 17:46:00 +03:00
|
|
|
bool success = object.set_property(request.get("name").to_string(), request.get("value"));
|
|
|
|
JsonObject response;
|
|
|
|
response.set("type", "SetProperty");
|
|
|
|
response.set("success", success);
|
|
|
|
send_response(response);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2020-03-05 16:40:47 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 22:19:23 +03:00
|
|
|
if (type == "Disconnect") {
|
2019-09-22 01:17:53 +03:00
|
|
|
shutdown();
|
2019-09-11 22:19:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 01:17:53 +03:00
|
|
|
void shutdown()
|
|
|
|
{
|
2022-01-02 16:52:38 +03:00
|
|
|
s_id_allocator.with_locked([this](auto& allocator) { allocator->deallocate(m_client_id); });
|
2019-09-22 01:17:53 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 22:19:23 +03:00
|
|
|
private:
|
2022-01-15 14:57:58 +03:00
|
|
|
NonnullOwnPtr<Stream::LocalSocket> m_socket;
|
2020-03-05 16:40:47 +03:00
|
|
|
WeakPtr<Object> m_inspected_object;
|
2020-01-03 22:27:48 +03:00
|
|
|
int m_client_id { -1 };
|
2019-09-11 22:19:23 +03:00
|
|
|
};
|
|
|
|
|
2021-05-13 23:42:11 +03:00
|
|
|
EventLoop::EventLoop([[maybe_unused]] MakeInspectable make_inspectable)
|
2022-02-11 18:57:10 +03:00
|
|
|
: m_wake_pipe_fds(&s_wake_pipe_fds)
|
|
|
|
, m_private(make<Private>())
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-01-23 16:47:10 +03:00
|
|
|
#ifdef __serenity__
|
|
|
|
if (!s_global_initializers_ran) {
|
|
|
|
// NOTE: Trying to have an event loop as a global variable will lead to initialization-order fiascos,
|
|
|
|
// as the event loop constructor accesses and/or sets other global variables.
|
|
|
|
// Therefore, we crash the program before ASAN catches us.
|
|
|
|
// If you came here because of the assertion failure, please redesign your program to not have global event loops.
|
|
|
|
// The common practice is to initialize the main event loop in the main function, and if necessary,
|
|
|
|
// pass event loop references around or access them with EventLoop::with_main_locked() and EventLoop::current().
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-10 18:30:34 +03:00
|
|
|
if (!s_event_loop_stack) {
|
2021-06-08 18:06:27 +03:00
|
|
|
s_event_loop_stack = new Vector<EventLoop&>;
|
2020-02-15 04:09:00 +03:00
|
|
|
s_timers = new HashMap<int, NonnullOwnPtr<EventLoopTimer>>;
|
2020-02-02 14:34:39 +03:00
|
|
|
s_notifiers = new HashTable<Notifier*>;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
2022-01-02 16:52:38 +03:00
|
|
|
s_main_event_loop.with_locked([&, this](auto*& main_event_loop) {
|
|
|
|
if (main_event_loop == nullptr) {
|
|
|
|
main_event_loop = this;
|
|
|
|
s_pid = getpid();
|
|
|
|
s_event_loop_stack->append(*this);
|
2019-08-17 12:35:09 +03:00
|
|
|
|
2021-01-12 17:08:52 +03:00
|
|
|
#ifdef __serenity__
|
2022-01-02 16:52:38 +03:00
|
|
|
if (getuid() != 0
|
|
|
|
&& make_inspectable == MakeInspectable::Yes
|
|
|
|
// FIXME: Deadlock potential; though the main loop and inspector server connection are rarely used in conjunction
|
2022-03-24 15:31:46 +03:00
|
|
|
&& !s_inspector_server_connection.with_locked([](auto inspector_server_connection) { return inspector_server_connection; })) {
|
2022-01-02 16:52:38 +03:00
|
|
|
if (!connect_to_inspector_server())
|
|
|
|
dbgln("Core::EventLoop: Failed to connect to InspectorServer");
|
|
|
|
}
|
2021-01-12 17:08:52 +03:00
|
|
|
#endif
|
2022-01-02 16:52:38 +03:00
|
|
|
}
|
|
|
|
});
|
2019-04-10 18:30:34 +03:00
|
|
|
|
2022-01-02 17:14:25 +03:00
|
|
|
initialize_wake_pipes();
|
|
|
|
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "{} Core::EventLoop constructed :)", getpid());
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoop::~EventLoop()
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2021-10-24 00:10:55 +03:00
|
|
|
// NOTE: Pop the main event loop off of the stack when destroyed.
|
2022-01-02 16:52:38 +03:00
|
|
|
s_main_event_loop.with_locked([this](auto*& main_event_loop) {
|
|
|
|
if (this == main_event_loop) {
|
|
|
|
s_event_loop_stack->take_last();
|
|
|
|
main_event_loop = nullptr;
|
|
|
|
}
|
|
|
|
});
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2021-05-13 23:42:11 +03:00
|
|
|
bool connect_to_inspector_server()
|
2020-05-28 18:19:49 +03:00
|
|
|
{
|
2021-01-12 17:08:52 +03:00
|
|
|
#ifdef __serenity__
|
2022-01-15 14:57:58 +03:00
|
|
|
auto maybe_socket = Core::Stream::LocalSocket::connect("/tmp/portal/inspectables");
|
|
|
|
if (maybe_socket.is_error()) {
|
|
|
|
dbgln("connect_to_inspector_server: Failed to connect: {}", maybe_socket.error());
|
2021-05-13 23:42:11 +03:00
|
|
|
return false;
|
2022-01-15 14:57:58 +03:00
|
|
|
}
|
2022-01-02 16:52:38 +03:00
|
|
|
s_inspector_server_connection.with_locked([&](auto& inspector_server_connection) {
|
2022-01-15 14:57:58 +03:00
|
|
|
inspector_server_connection = InspectorServerConnection::construct(maybe_socket.release_value());
|
2022-01-02 16:52:38 +03:00
|
|
|
});
|
2021-05-13 23:42:11 +03:00
|
|
|
return true;
|
2021-01-12 17:08:52 +03:00
|
|
|
#else
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-01-12 17:08:52 +03:00
|
|
|
#endif
|
2020-05-28 18:19:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 12:48:15 +03:00
|
|
|
#define VERIFY_EVENT_LOOP_INITIALIZED() \
|
|
|
|
do { \
|
|
|
|
if (!s_event_loop_stack) { \
|
|
|
|
warnln("EventLoop static API was called without prior EventLoop init!"); \
|
|
|
|
VERIFY_NOT_REACHED(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoop& EventLoop::current()
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2021-06-08 18:06:27 +03:00
|
|
|
return s_event_loop_stack->last();
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::quit(int code)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::quit({})", code);
|
2019-04-10 18:30:34 +03:00
|
|
|
m_exit_requested = true;
|
|
|
|
m_exit_code = code;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::unquit()
|
2019-10-25 08:18:31 +03:00
|
|
|
{
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::unquit()");
|
2019-10-25 08:18:31 +03:00
|
|
|
m_exit_requested = false;
|
|
|
|
m_exit_code = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
struct EventLoopPusher {
|
2019-04-10 18:30:34 +03:00
|
|
|
public:
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoopPusher(EventLoop& event_loop)
|
2019-05-28 12:53:16 +03:00
|
|
|
: m_event_loop(event_loop)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-02-09 01:25:18 +03:00
|
|
|
if (EventLoop::has_been_instantiated()) {
|
2020-02-02 14:34:39 +03:00
|
|
|
m_event_loop.take_pending_events_from(EventLoop::current());
|
2021-06-08 18:06:27 +03:00
|
|
|
s_event_loop_stack->append(event_loop);
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:34:39 +03:00
|
|
|
~EventLoopPusher()
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-02-09 01:25:18 +03:00
|
|
|
if (EventLoop::has_been_instantiated()) {
|
2019-04-10 18:30:34 +03:00
|
|
|
s_event_loop_stack->take_last();
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoop::current().take_pending_events_from(m_event_loop);
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-04-10 18:30:34 +03:00
|
|
|
private:
|
2022-01-02 16:52:38 +03:00
|
|
|
bool is_main_event_loop()
|
|
|
|
{
|
|
|
|
return s_main_event_loop.with_locked([this](auto* main_event_loop) { return &m_event_loop == main_event_loop; });
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoop& m_event_loop;
|
2019-04-10 18:30:34 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
int EventLoop::exec()
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2020-02-02 14:34:39 +03:00
|
|
|
EventLoopPusher pusher(*this);
|
2019-04-10 18:30:34 +03:00
|
|
|
for (;;) {
|
|
|
|
if (m_exit_requested)
|
|
|
|
return m_exit_code;
|
2019-05-18 14:39:21 +03:00
|
|
|
pump();
|
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-05-18 14:39:21 +03:00
|
|
|
}
|
2019-04-29 16:57:49 +03:00
|
|
|
|
2021-09-25 20:32:14 +03:00
|
|
|
void EventLoop::spin_until(Function<bool()> goal_condition)
|
|
|
|
{
|
|
|
|
EventLoopPusher pusher(*this);
|
|
|
|
while (!goal_condition())
|
|
|
|
pump();
|
|
|
|
}
|
|
|
|
|
2022-01-06 02:55:48 +03:00
|
|
|
size_t EventLoop::pump(WaitMode mode)
|
2019-05-18 14:39:21 +03:00
|
|
|
{
|
2020-05-16 23:02:53 +03:00
|
|
|
wait_for_event(mode);
|
2019-07-20 16:50:03 +03:00
|
|
|
|
2019-05-18 14:39:21 +03:00
|
|
|
decltype(m_queued_events) events;
|
|
|
|
{
|
2021-07-09 12:14:57 +03:00
|
|
|
Threading::MutexLocker locker(m_private->lock);
|
2019-05-18 14:39:21 +03:00
|
|
|
events = move(m_queued_events);
|
|
|
|
}
|
2019-04-29 16:57:49 +03:00
|
|
|
|
2022-01-06 02:55:48 +03:00
|
|
|
size_t processed_events = 0;
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < events.size(); ++i) {
|
2019-07-21 11:17:20 +03:00
|
|
|
auto& queued_event = events.at(i);
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
auto receiver = queued_event.receiver.strong_ref();
|
2019-05-18 14:39:21 +03:00
|
|
|
auto& event = *queued_event.event;
|
2019-07-26 17:00:56 +03:00
|
|
|
if (receiver)
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: {} event {}", *receiver, event.type());
|
|
|
|
|
2019-05-18 14:39:21 +03:00
|
|
|
if (!receiver) {
|
|
|
|
switch (event.type()) {
|
2020-02-02 14:34:39 +03:00
|
|
|
case Event::Quit:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-05-18 14:39:21 +03:00
|
|
|
default:
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Event type {} with no receiver :(", event.type());
|
2020-06-02 13:46:21 +03:00
|
|
|
break;
|
2019-05-18 14:39:21 +03:00
|
|
|
}
|
2020-02-02 14:34:39 +03:00
|
|
|
} else if (event.type() == Event::Type::DeferredInvoke) {
|
2021-05-01 22:10:08 +03:00
|
|
|
dbgln_if(DEFERRED_INVOKE_DEBUG, "DeferredInvoke: receiver = {}", *receiver);
|
2021-08-30 13:43:28 +03:00
|
|
|
static_cast<DeferredInvocationEvent&>(event).m_invokee();
|
2019-05-18 14:39:21 +03:00
|
|
|
} else {
|
2020-02-02 14:34:39 +03:00
|
|
|
NonnullRefPtr<Object> protector(*receiver);
|
2019-09-20 21:37:31 +03:00
|
|
|
receiver->dispatch_event(event);
|
2019-05-18 14:39:21 +03:00
|
|
|
}
|
2022-01-06 02:55:48 +03:00
|
|
|
++processed_events;
|
2019-04-10 18:30:34 +03:00
|
|
|
|
2019-05-18 14:39:21 +03:00
|
|
|
if (m_exit_requested) {
|
2021-07-09 12:14:57 +03:00
|
|
|
Threading::MutexLocker locker(m_private->lock);
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: Exit requested. Rejigging {} events.", events.size() - i);
|
2019-07-25 17:12:51 +03:00
|
|
|
decltype(m_queued_events) new_event_queue;
|
|
|
|
new_event_queue.ensure_capacity(m_queued_events.size() + events.size());
|
2020-04-03 23:55:48 +03:00
|
|
|
for (++i; i < events.size(); ++i)
|
2019-07-25 17:12:51 +03:00
|
|
|
new_event_queue.unchecked_append(move(events[i]));
|
2021-06-12 14:24:45 +03:00
|
|
|
new_event_queue.extend(move(m_queued_events));
|
2019-07-25 17:12:51 +03:00
|
|
|
m_queued_events = move(new_event_queue);
|
2022-01-06 02:55:48 +03:00
|
|
|
break;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 02:55:48 +03:00
|
|
|
|
|
|
|
return processed_events;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 18:57:10 +03:00
|
|
|
void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event, ShouldWake should_wake)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2021-07-09 12:14:57 +03:00
|
|
|
Threading::MutexLocker lock(m_private->lock);
|
2022-01-06 17:07:15 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::post_event: ({}) << receiver={}, event={}", m_queued_events.size(), receiver, event);
|
2020-02-15 00:29:06 +03:00
|
|
|
m_queued_events.empend(receiver, move(event));
|
2022-02-11 18:57:10 +03:00
|
|
|
if (should_wake == ShouldWake::Yes)
|
|
|
|
wake();
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
SignalHandlers::SignalHandlers(int signo, void (*handle_signal)(int))
|
2020-07-07 00:48:02 +03:00
|
|
|
: m_signo(signo)
|
2021-01-08 22:09:39 +03:00
|
|
|
, m_original_handler(signal(signo, handle_signal))
|
2020-07-07 00:48:02 +03:00
|
|
|
{
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: Registered handler for signal {}", m_signo);
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
SignalHandlers::~SignalHandlers()
|
2020-07-07 00:48:02 +03:00
|
|
|
{
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: Unregistering handler for signal {}", m_signo);
|
2021-01-08 20:29:11 +03:00
|
|
|
signal(m_signo, m_original_handler);
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
void SignalHandlers::dispatch()
|
2020-07-07 00:48:02 +03:00
|
|
|
{
|
2021-01-08 20:29:11 +03:00
|
|
|
TemporaryChange change(m_calling_handlers, true);
|
2020-07-07 00:48:02 +03:00
|
|
|
for (auto& handler : m_handlers)
|
|
|
|
handler.value(m_signo);
|
2021-01-08 20:29:11 +03:00
|
|
|
if (!m_handlers_pending.is_empty()) {
|
|
|
|
// Apply pending adds/removes
|
|
|
|
for (auto& handler : m_handlers_pending) {
|
|
|
|
if (handler.value) {
|
|
|
|
auto result = m_handlers.set(handler.key, move(handler.value));
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
2021-01-08 20:29:11 +03:00
|
|
|
} else {
|
|
|
|
m_handlers.remove(handler.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_handlers_pending.clear();
|
|
|
|
}
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
int SignalHandlers::add(Function<void(int)>&& handler)
|
2020-07-07 00:48:02 +03:00
|
|
|
{
|
2021-01-08 22:09:39 +03:00
|
|
|
int id = ++signals_info()->next_signal_id; // TODO: worry about wrapping and duplicates?
|
2021-01-08 20:29:11 +03:00
|
|
|
if (m_calling_handlers)
|
|
|
|
m_handlers_pending.set(id, move(handler));
|
|
|
|
else
|
|
|
|
m_handlers.set(id, move(handler));
|
2020-07-07 00:48:02 +03:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:09:39 +03:00
|
|
|
bool SignalHandlers::remove(int handler_id)
|
2020-07-07 00:48:02 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(handler_id != 0);
|
2021-01-08 20:29:11 +03:00
|
|
|
if (m_calling_handlers) {
|
|
|
|
auto it = m_handlers.find(handler_id);
|
|
|
|
if (it != m_handlers.end()) {
|
|
|
|
// Mark pending remove
|
2021-01-11 02:29:28 +03:00
|
|
|
m_handlers_pending.set(handler_id, {});
|
2021-01-08 20:29:11 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
it = m_handlers_pending.find(handler_id);
|
|
|
|
if (it != m_handlers_pending.end()) {
|
|
|
|
if (!it->value)
|
|
|
|
return false; // already was marked as deleted
|
|
|
|
it->value = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-07 00:48:02 +03:00
|
|
|
return m_handlers.remove(handler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::dispatch_signal(int signo)
|
|
|
|
{
|
2021-01-08 22:09:39 +03:00
|
|
|
auto& info = *signals_info();
|
|
|
|
auto handlers = info.signal_handlers.find(signo);
|
|
|
|
if (handlers != info.signal_handlers.end()) {
|
2021-01-08 20:29:11 +03:00
|
|
|
// Make sure we bump the ref count while dispatching the handlers!
|
|
|
|
// This allows a handler to unregister/register while the handlers
|
|
|
|
// are being called!
|
|
|
|
auto handler = handlers->value;
|
2021-04-14 11:29:33 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: dispatching signal {}", signo);
|
2021-01-08 20:29:11 +03:00
|
|
|
handler->dispatch();
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::handle_signal(int signo)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(signo != 0);
|
2020-07-07 00:48:02 +03:00
|
|
|
// We MUST check if the current pid still matches, because there
|
|
|
|
// is a window between fork() and exec() where a signal delivered
|
2022-01-06 17:07:15 +03:00
|
|
|
// to our fork could be inadvertently routed to the parent process!
|
2020-07-07 00:48:02 +03:00
|
|
|
if (getpid() == s_pid) {
|
|
|
|
int nwritten = write(s_wake_pipe_fds[1], &signo, sizeof(signo));
|
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("EventLoop::register_signal: write");
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We're a fork who received a signal, reset s_pid
|
|
|
|
s_pid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int EventLoop::register_signal(int signo, Function<void(int)> handler)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(signo != 0);
|
2021-01-08 22:09:39 +03:00
|
|
|
auto& info = *signals_info();
|
|
|
|
auto handlers = info.signal_handlers.find(signo);
|
|
|
|
if (handlers == info.signal_handlers.end()) {
|
2021-04-23 17:46:57 +03:00
|
|
|
auto signal_handlers = adopt_ref(*new SignalHandlers(signo, EventLoop::handle_signal));
|
2021-01-08 20:29:11 +03:00
|
|
|
auto handler_id = signal_handlers->add(move(handler));
|
2021-01-08 22:09:39 +03:00
|
|
|
info.signal_handlers.set(signo, move(signal_handlers));
|
2020-07-07 00:48:02 +03:00
|
|
|
return handler_id;
|
|
|
|
} else {
|
2021-01-08 20:29:11 +03:00
|
|
|
return handlers->value->add(move(handler));
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::unregister_signal(int handler_id)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(handler_id != 0);
|
2020-07-07 00:48:02 +03:00
|
|
|
int remove_signo = 0;
|
2021-01-08 22:09:39 +03:00
|
|
|
auto& info = *signals_info();
|
|
|
|
for (auto& h : info.signal_handlers) {
|
2021-01-08 20:29:11 +03:00
|
|
|
auto& handlers = *h.value;
|
|
|
|
if (handlers.remove(handler_id)) {
|
2020-07-07 00:48:02 +03:00
|
|
|
if (handlers.is_empty())
|
|
|
|
remove_signo = handlers.m_signo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remove_signo != 0)
|
2021-01-08 22:09:39 +03:00
|
|
|
info.signal_handlers.remove(remove_signo);
|
2020-07-07 00:48:02 +03:00
|
|
|
}
|
|
|
|
|
2020-09-07 21:14:42 +03:00
|
|
|
void EventLoop::notify_forked(ForkEvent event)
|
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2020-09-07 21:14:42 +03:00
|
|
|
switch (event) {
|
|
|
|
case ForkEvent::Child:
|
2022-01-02 16:52:38 +03:00
|
|
|
s_main_event_loop.with_locked([]([[maybe_unused]] auto*& main_event_loop) { main_event_loop = nullptr; });
|
2020-09-07 21:14:42 +03:00
|
|
|
s_event_loop_stack->clear();
|
|
|
|
s_timers->clear();
|
|
|
|
s_notifiers->clear();
|
2022-01-02 17:14:25 +03:00
|
|
|
s_wake_pipe_initialized = false;
|
|
|
|
initialize_wake_pipes();
|
2021-01-08 22:09:39 +03:00
|
|
|
if (auto* info = signals_info<false>()) {
|
|
|
|
info->signal_handlers.clear();
|
|
|
|
info->next_signal_id = 0;
|
|
|
|
}
|
2020-09-07 21:14:42 +03:00
|
|
|
s_pid = 0;
|
2021-01-12 17:08:52 +03:00
|
|
|
#ifdef __serenity__
|
2022-01-02 16:52:38 +03:00
|
|
|
s_main_event_loop.with_locked([]([[maybe_unused]] auto*& main_event_loop) { main_event_loop = nullptr; });
|
2021-01-12 17:08:52 +03:00
|
|
|
#endif
|
2020-09-07 21:14:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-09-07 21:14:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::wait_for_event(WaitMode mode)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2020-07-07 00:48:02 +03:00
|
|
|
retry:
|
2019-04-10 18:30:34 +03:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
|
|
|
|
int max_fd = 0;
|
2019-05-28 12:53:16 +03:00
|
|
|
auto add_fd_to_set = [&max_fd](int fd, fd_set& set) {
|
2019-04-10 18:30:34 +03:00
|
|
|
FD_SET(fd, &set);
|
|
|
|
if (fd > max_fd)
|
|
|
|
max_fd = fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
int max_fd_added = -1;
|
2019-07-14 15:28:24 +03:00
|
|
|
add_fd_to_set(s_wake_pipe_fds[0], rfds);
|
2019-04-10 18:30:34 +03:00
|
|
|
max_fd = max(max_fd, max_fd_added);
|
2022-01-07 02:45:27 +03:00
|
|
|
|
2022-01-02 16:52:38 +03:00
|
|
|
for (auto& notifier : *s_notifiers) {
|
|
|
|
if (notifier->event_mask() & Notifier::Read)
|
|
|
|
add_fd_to_set(notifier->fd(), rfds);
|
|
|
|
if (notifier->event_mask() & Notifier::Write)
|
|
|
|
add_fd_to_set(notifier->fd(), wfds);
|
|
|
|
if (notifier->event_mask() & Notifier::Exceptional)
|
|
|
|
VERIFY_NOT_REACHED();
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:57:49 +03:00
|
|
|
bool queued_events_is_empty;
|
|
|
|
{
|
2021-07-09 12:14:57 +03:00
|
|
|
Threading::MutexLocker locker(m_private->lock);
|
2019-04-29 16:57:49 +03:00
|
|
|
queued_events_is_empty = m_queued_events.is_empty();
|
|
|
|
}
|
|
|
|
|
2021-08-15 02:50:16 +03:00
|
|
|
Time now;
|
2019-04-10 18:30:34 +03:00
|
|
|
struct timeval timeout = { 0, 0 };
|
2019-05-18 14:39:21 +03:00
|
|
|
bool should_wait_forever = false;
|
2020-05-15 18:21:40 +03:00
|
|
|
if (mode == WaitMode::WaitForEvents && queued_events_is_empty) {
|
|
|
|
auto next_timer_expiration = get_next_timer_expiration();
|
|
|
|
if (next_timer_expiration.has_value()) {
|
2021-08-15 02:50:16 +03:00
|
|
|
now = Time::now_monotonic_coarse();
|
|
|
|
auto computed_timeout = next_timer_expiration.value() - now;
|
|
|
|
if (computed_timeout.is_negative())
|
|
|
|
computed_timeout = Time::zero();
|
|
|
|
timeout = computed_timeout.to_timeval();
|
2019-05-18 14:39:21 +03:00
|
|
|
} else {
|
|
|
|
should_wait_forever = true;
|
|
|
|
}
|
2019-05-18 03:00:01 +03:00
|
|
|
}
|
2019-04-10 18:30:34 +03:00
|
|
|
|
2020-07-07 00:48:02 +03:00
|
|
|
try_select_again:
|
2020-06-17 10:57:48 +03:00
|
|
|
int marked_fd_count = select(max_fd + 1, &rfds, &wfds, nullptr, should_wait_forever ? nullptr : &timeout);
|
|
|
|
if (marked_fd_count < 0) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
if (saved_errno == EINTR) {
|
|
|
|
if (m_exit_requested)
|
|
|
|
return;
|
|
|
|
goto try_select_again;
|
|
|
|
}
|
2021-12-19 12:56:50 +03:00
|
|
|
dbgln("Core::EventLoop::wait_for_event: {} ({}: {})", marked_fd_count, saved_errno, strerror(saved_errno));
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-17 10:57:48 +03:00
|
|
|
}
|
2019-07-14 15:28:24 +03:00
|
|
|
if (FD_ISSET(s_wake_pipe_fds[0], &rfds)) {
|
2020-07-07 00:48:02 +03:00
|
|
|
int wake_events[8];
|
2022-01-21 15:23:57 +03:00
|
|
|
ssize_t nread;
|
|
|
|
// We might receive another signal while read()ing here. The signal will go to the handle_signal properly,
|
|
|
|
// but we get interrupted. Therefore, just retry while we were interrupted.
|
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
nread = read(s_wake_pipe_fds[0], wake_events, sizeof(wake_events));
|
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
} while (nread < 0 && errno == EINTR);
|
2019-07-14 15:28:24 +03:00
|
|
|
if (nread < 0) {
|
2022-01-21 15:23:57 +03:00
|
|
|
perror("Core::EventLoop::wait_for_event: read from wake pipe");
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-07-14 15:28:24 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(nread > 0);
|
2020-07-07 00:48:02 +03:00
|
|
|
bool wake_requested = false;
|
|
|
|
int event_count = nread / sizeof(wake_events[0]);
|
|
|
|
for (int i = 0; i < event_count; i++) {
|
|
|
|
if (wake_events[i] != 0)
|
|
|
|
dispatch_signal(wake_events[i]);
|
|
|
|
else
|
|
|
|
wake_requested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wake_requested && nread == sizeof(wake_events))
|
|
|
|
goto retry;
|
2019-07-14 11:20:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-18 03:00:01 +03:00
|
|
|
if (!s_timers->is_empty()) {
|
2021-08-15 02:50:16 +03:00
|
|
|
now = Time::now_monotonic_coarse();
|
2019-05-18 03:00:01 +03:00
|
|
|
}
|
2019-04-18 02:37:23 +03:00
|
|
|
|
2019-04-10 18:30:34 +03:00
|
|
|
for (auto& it : *s_timers) {
|
|
|
|
auto& timer = *it.value;
|
2019-04-18 02:37:23 +03:00
|
|
|
if (!timer.has_expired(now))
|
2019-04-10 18:30:34 +03:00
|
|
|
continue;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
auto owner = timer.owner.strong_ref();
|
|
|
|
if (timer.fire_when_not_visible == TimerShouldFireWhenNotVisible::No
|
|
|
|
&& owner && !owner->is_visible_for_timer_purposes()) {
|
2019-12-29 17:58:07 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-14 11:29:33 +03:00
|
|
|
|
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop: Timer {} has expired, sending Core::TimerEvent to {}", timer.timer_id, *owner);
|
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
if (owner)
|
|
|
|
post_event(*owner, make<TimerEvent>(timer.timer_id));
|
2019-04-10 18:30:34 +03:00
|
|
|
if (timer.should_reload) {
|
2019-04-18 02:37:23 +03:00
|
|
|
timer.reload(now);
|
2019-04-10 18:30:34 +03:00
|
|
|
} else {
|
|
|
|
// FIXME: Support removing expired timers that don't want to reload.
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 11:57:28 +03:00
|
|
|
if (!marked_fd_count)
|
|
|
|
return;
|
|
|
|
|
2019-04-10 18:30:34 +03:00
|
|
|
for (auto& notifier : *s_notifiers) {
|
|
|
|
if (FD_ISSET(notifier->fd(), &rfds)) {
|
2020-07-07 00:02:07 +03:00
|
|
|
if (notifier->event_mask() & Notifier::Event::Read)
|
2020-02-02 14:34:39 +03:00
|
|
|
post_event(*notifier, make<NotifierReadEvent>(notifier->fd()));
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
if (FD_ISSET(notifier->fd(), &wfds)) {
|
2020-07-07 00:02:07 +03:00
|
|
|
if (notifier->event_mask() & Notifier::Event::Write)
|
2020-02-02 14:34:39 +03:00
|
|
|
post_event(*notifier, make<NotifierWriteEvent>(notifier->fd()));
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool EventLoopTimer::has_expired(Time const& now) const
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2021-08-15 02:50:16 +03:00
|
|
|
return now > fire_time;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void EventLoopTimer::reload(Time const& now)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2021-08-15 02:50:16 +03:00
|
|
|
fire_time = now + interval;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-15 02:50:16 +03:00
|
|
|
Optional<Time> EventLoop::get_next_timer_expiration()
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-02-15 16:47:32 +03:00
|
|
|
auto now = Time::now_monotonic_coarse();
|
2021-08-15 02:50:16 +03:00
|
|
|
Optional<Time> soonest {};
|
2019-04-10 18:30:34 +03:00
|
|
|
for (auto& it : *s_timers) {
|
|
|
|
auto& fire_time = it.value->fire_time;
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
auto owner = it.value->owner.strong_ref();
|
2019-12-29 17:58:07 +03:00
|
|
|
if (it.value->fire_when_not_visible == TimerShouldFireWhenNotVisible::No
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
&& owner && !owner->is_visible_for_timer_purposes()) {
|
2019-12-29 17:58:07 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-15 16:47:32 +03:00
|
|
|
// OPTIMIZATION: If we have a timer that needs to fire right away, we can stop looking here.
|
|
|
|
// FIXME: This whole operation could be O(1) with a better data structure.
|
|
|
|
if (fire_time < now)
|
|
|
|
return now;
|
2021-08-15 02:50:16 +03:00
|
|
|
if (!soonest.has_value() || fire_time < soonest.value())
|
2019-04-10 18:30:34 +03:00
|
|
|
soonest = fire_time;
|
|
|
|
}
|
2020-05-15 18:21:40 +03:00
|
|
|
return soonest;
|
2019-04-10 18:30:34 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
int EventLoop::register_timer(Object& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(milliseconds >= 0);
|
2019-04-10 18:30:34 +03:00
|
|
|
auto timer = make<EventLoopTimer>();
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
timer->owner = object;
|
2021-08-15 02:50:16 +03:00
|
|
|
timer->interval = Time::from_milliseconds(milliseconds);
|
|
|
|
timer->reload(Time::now_monotonic_coarse());
|
2019-04-10 18:30:34 +03:00
|
|
|
timer->should_reload = should_reload;
|
2019-12-29 17:58:07 +03:00
|
|
|
timer->fire_when_not_visible = fire_when_not_visible;
|
2022-01-02 16:52:38 +03:00
|
|
|
int timer_id = s_id_allocator.with_locked([](auto& allocator) { return allocator->allocate(); });
|
2019-04-10 18:30:34 +03:00
|
|
|
timer->timer_id = timer_id;
|
2019-07-23 15:55:12 +03:00
|
|
|
s_timers->set(timer_id, move(timer));
|
2019-04-10 18:30:34 +03:00
|
|
|
return timer_id;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
bool EventLoop::unregister_timer(int timer_id)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2022-01-02 16:52:38 +03:00
|
|
|
s_id_allocator.with_locked([&](auto& allocator) { allocator->deallocate(timer_id); });
|
2019-04-10 18:30:34 +03:00
|
|
|
auto it = s_timers->find(timer_id);
|
|
|
|
if (it == s_timers->end())
|
|
|
|
return false;
|
|
|
|
s_timers->remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2019-04-10 18:30:34 +03:00
|
|
|
s_notifiers->set(¬ifier);
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 18:30:34 +03:00
|
|
|
{
|
2022-03-10 12:48:15 +03:00
|
|
|
VERIFY_EVENT_LOOP_INITIALIZED();
|
2019-04-10 18:30:34 +03:00
|
|
|
s_notifiers->remove(¬ifier);
|
|
|
|
}
|
2019-07-14 11:20:57 +03:00
|
|
|
|
2022-02-11 18:57:10 +03:00
|
|
|
void EventLoop::wake_current()
|
|
|
|
{
|
|
|
|
EventLoop::current().wake();
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
void EventLoop::wake()
|
2019-07-14 11:20:57 +03:00
|
|
|
{
|
2022-02-11 18:57:10 +03:00
|
|
|
dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::wake()");
|
2020-07-07 00:48:02 +03:00
|
|
|
int wake_event = 0;
|
2022-02-11 18:57:10 +03:00
|
|
|
int nwritten = write((*m_wake_pipe_fds)[1], &wake_event, sizeof(wake_event));
|
2019-07-14 11:20:57 +03:00
|
|
|
if (nwritten < 0) {
|
2020-02-02 14:34:39 +03:00
|
|
|
perror("EventLoop::wake: write");
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-07-14 11:20:57 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:34:39 +03:00
|
|
|
|
2020-02-15 00:29:06 +03:00
|
|
|
EventLoop::QueuedEvent::QueuedEvent(Object& receiver, NonnullOwnPtr<Event> event)
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-30 01:26:13 +03:00
|
|
|
: receiver(receiver)
|
2020-02-15 00:29:06 +03:00
|
|
|
, event(move(event))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::QueuedEvent::QueuedEvent(QueuedEvent&& other)
|
|
|
|
: receiver(other.receiver)
|
|
|
|
, event(move(other.event))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
}
|