2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <AK/Badge.h>
|
2020-11-15 12:11:21 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-01-04 23:28:42 +00:00
|
|
|
#include <AK/IDAllocator.h>
|
2019-08-17 09:35:09 +00:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-03-10 10:31:37 +00:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2020-07-06 21:48:02 +00:00
|
|
|
#include <AK/TemporaryChange.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Time.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <LibCore/LocalServer.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/LocalSocket.h>
|
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
#include <LibCore/Object.h>
|
|
|
|
#include <LibCore/SyscallUtils.h>
|
2019-08-25 20:51:27 +00:00
|
|
|
#include <LibThread/Lock.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2020-07-06 21:48:02 +00:00
|
|
|
#include <signal.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
2020-05-28 15:25:34 +00:00
|
|
|
#include <sys/stat.h>
|
2019-06-22 19:21:57 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-06-02 10:46:21 +00:00
|
|
|
//#define EVENTLOOP_DEBUG
|
2019-04-20 10:48:49 +00:00
|
|
|
//#define DEFERRED_INVOKE_DEBUG
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2020-01-03 19:27:48 +00:00
|
|
|
class RPCClient;
|
|
|
|
|
2020-02-15 01:09:00 +00:00
|
|
|
struct EventLoopTimer {
|
|
|
|
int timer_id { 0 };
|
|
|
|
int interval { 0 };
|
|
|
|
timeval fire_time { 0, 0 };
|
|
|
|
bool should_reload { false };
|
|
|
|
TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
|
|
|
|
WeakPtr<Object> owner;
|
|
|
|
|
|
|
|
void reload(const timeval& now);
|
|
|
|
bool has_expired(const timeval& now) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EventLoop::Private {
|
|
|
|
LibThread::Lock lock;
|
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
static EventLoop* s_main_event_loop;
|
|
|
|
static Vector<EventLoop*>* s_event_loop_stack;
|
2020-03-10 10:31:37 +00:00
|
|
|
static NeverDestroyed<IDAllocator> s_id_allocator;
|
2020-02-15 01:09:00 +00:00
|
|
|
static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
|
|
|
|
static HashTable<Notifier*>* s_notifiers;
|
2020-02-02 11:34:39 +00:00
|
|
|
int EventLoop::s_wake_pipe_fds[2];
|
2021-01-08 17:29:11 +00:00
|
|
|
HashMap<int, NonnullRefPtr<EventLoop::SignalHandlers>> EventLoop::s_signal_handlers;
|
2020-07-06 21:48:02 +00:00
|
|
|
int EventLoop::s_next_signal_id = 0;
|
|
|
|
pid_t EventLoop::s_pid;
|
2020-02-15 01:09:00 +00:00
|
|
|
static RefPtr<LocalServer> s_rpc_server;
|
2020-01-03 19:27:48 +00:00
|
|
|
HashMap<int, RefPtr<RPCClient>> s_rpc_clients;
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class RPCClient : public Object {
|
2019-09-11 19:19:23 +00:00
|
|
|
C_OBJECT(RPCClient)
|
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit RPCClient(RefPtr<LocalSocket> socket)
|
2019-09-21 08:28:02 +00:00
|
|
|
: m_socket(move(socket))
|
2020-03-10 10:31:37 +00:00
|
|
|
, m_client_id(s_id_allocator->allocate())
|
2019-09-11 19:19:23 +00:00
|
|
|
{
|
2020-01-03 19:27:48 +00:00
|
|
|
s_rpc_clients.set(m_client_id, this);
|
2019-09-21 08:28:02 +00:00
|
|
|
add_child(*m_socket);
|
|
|
|
m_socket->on_ready_to_read = [this] {
|
2020-02-20 11:54:15 +00:00
|
|
|
u32 length;
|
2019-09-21 08:28:02 +00:00
|
|
|
int nread = m_socket->read((u8*)&length, sizeof(length));
|
2019-09-11 19:19:23 +00:00
|
|
|
if (nread == 0) {
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("RPC client disconnected");
|
2020-06-02 10:46:21 +00:00
|
|
|
#endif
|
2019-09-21 22:17:53 +00:00
|
|
|
shutdown();
|
2019-09-11 19:19:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT(nread == sizeof(length));
|
2019-09-21 08:28:02 +00:00
|
|
|
auto request = m_socket->read(length);
|
2019-09-11 19:19:23 +00:00
|
|
|
|
|
|
|
auto request_json = JsonValue::from_string(request);
|
2020-06-11 04:40:27 +00:00
|
|
|
if (!request_json.has_value() || !request_json.value().is_object()) {
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("RPC client sent invalid request");
|
2019-09-21 22:17:53 +00:00
|
|
|
shutdown();
|
2019-09-11 19:19:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-11 04:40:27 +00:00
|
|
|
handle_request(request_json.value().as_object());
|
2019-09-11 19:19:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
virtual ~RPCClient() override
|
|
|
|
{
|
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-29 22:26:13 +00:00
|
|
|
if (auto inspected_object = m_inspected_object.strong_ref())
|
|
|
|
inspected_object->decrement_inspector_count({});
|
2019-09-11 19:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void send_response(const JsonObject& response)
|
|
|
|
{
|
|
|
|
auto serialized = response.to_string();
|
2020-02-20 11:54:15 +00:00
|
|
|
u32 length = serialized.length();
|
2019-09-21 08:28:02 +00:00
|
|
|
m_socket->write((const u8*)&length, sizeof(length));
|
|
|
|
m_socket->write(serialized);
|
2019-09-11 19:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_request(const JsonObject& request)
|
|
|
|
{
|
|
|
|
auto type = request.get("type").as_string_or({});
|
|
|
|
|
|
|
|
if (type.is_null()) {
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("RPC client sent request without type field");
|
2019-09-11 19:19:23 +00: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 11:34:39 +00:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2019-09-11 19:19:23 +00: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 13:40:47 +00:00
|
|
|
if (type == "SetInspectedObject") {
|
2020-03-08 09:36:51 +00:00
|
|
|
auto address = request.get("address").to_number<FlatPtr>();
|
2020-03-05 13:40:47 +00:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2020-03-08 09:36:51 +00: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-29 22:26:13 +00: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 14:46:00 +00:00
|
|
|
break;
|
2020-03-05 13:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 14:46:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == "SetProperty") {
|
2020-03-08 09:36:51 +00:00
|
|
|
auto address = request.get("address").to_number<FlatPtr>();
|
2020-03-05 14:46:00 +00:00
|
|
|
for (auto& object : Object::all_objects()) {
|
2020-03-08 09:36:51 +00:00
|
|
|
if ((FlatPtr)&object == address) {
|
2020-03-05 14:46:00 +00: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 13:40:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:19:23 +00:00
|
|
|
if (type == "Disconnect") {
|
2019-09-21 22:17:53 +00:00
|
|
|
shutdown();
|
2019-09-11 19:19:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 22:17:53 +00:00
|
|
|
void shutdown()
|
|
|
|
{
|
2020-01-05 16:53:42 +00:00
|
|
|
s_rpc_clients.remove(m_client_id);
|
2020-03-10 10:31:37 +00:00
|
|
|
s_id_allocator->deallocate(m_client_id);
|
2019-09-21 22:17:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:19:23 +00:00
|
|
|
private:
|
2020-02-02 11:34:39 +00:00
|
|
|
RefPtr<LocalSocket> m_socket;
|
2020-03-05 13:40:47 +00:00
|
|
|
WeakPtr<Object> m_inspected_object;
|
2020-01-03 19:27:48 +00:00
|
|
|
int m_client_id { -1 };
|
2019-09-11 19:19:23 +00:00
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop::EventLoop()
|
2020-02-15 01:09:00 +00:00
|
|
|
: m_private(make<Private>())
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
if (!s_event_loop_stack) {
|
2020-02-02 11:34:39 +00:00
|
|
|
s_event_loop_stack = new Vector<EventLoop*>;
|
2020-02-15 01:09:00 +00:00
|
|
|
s_timers = new HashMap<int, NonnullOwnPtr<EventLoopTimer>>;
|
2020-02-02 11:34:39 +00:00
|
|
|
s_notifiers = new HashTable<Notifier*>;
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s_main_event_loop) {
|
|
|
|
s_main_event_loop = this;
|
2020-07-06 21:48:02 +00:00
|
|
|
s_pid = getpid();
|
2019-12-25 16:09:52 +00:00
|
|
|
#if defined(SOCK_NONBLOCK)
|
2019-08-05 12:31:38 +00:00
|
|
|
int rc = pipe2(s_wake_pipe_fds, O_CLOEXEC);
|
2019-12-25 16:09:52 +00:00
|
|
|
#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
|
2019-07-14 08:20:57 +00:00
|
|
|
ASSERT(rc == 0);
|
2019-04-10 15:30:34 +00:00
|
|
|
s_event_loop_stack->append(this);
|
2019-08-17 09:35:09 +00:00
|
|
|
|
2020-05-28 15:19:49 +00:00
|
|
|
if (!s_rpc_server) {
|
|
|
|
if (!start_rpc_server())
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop: Failed to start an RPC server");
|
2019-08-17 09:35:09 +00:00
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("{} Core::EventLoop constructed :)", getpid());
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop::~EventLoop()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:19:49 +00:00
|
|
|
bool EventLoop::start_rpc_server()
|
|
|
|
{
|
2020-05-28 15:25:34 +00:00
|
|
|
// Create /tmp/rpc if it doesn't exist.
|
|
|
|
int rc = mkdir("/tmp/rpc", 0777);
|
|
|
|
if (rc == 0) {
|
|
|
|
// Ensure it gets created as 0777 despite our umask.
|
|
|
|
rc = chmod("/tmp/rpc", 0777);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("chmod /tmp/rpc");
|
|
|
|
// Continue further.
|
|
|
|
}
|
|
|
|
} else if (errno != EEXIST) {
|
|
|
|
perror("mkdir /tmp/rpc");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rpc_path = String::format("/tmp/rpc/%d", getpid());
|
|
|
|
rc = unlink(rpc_path.characters());
|
2020-05-28 15:19:49 +00:00
|
|
|
if (rc < 0 && errno != ENOENT) {
|
|
|
|
perror("unlink");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
s_rpc_server = LocalServer::construct();
|
|
|
|
s_rpc_server->set_name("Core::EventLoop_RPC_server");
|
|
|
|
s_rpc_server->on_ready_to_accept = [&] {
|
|
|
|
RPCClient::construct(s_rpc_server->accept());
|
|
|
|
};
|
|
|
|
return s_rpc_server->listen(rpc_path);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop& EventLoop::main()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
ASSERT(s_main_event_loop);
|
|
|
|
return *s_main_event_loop;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop& EventLoop::current()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop* event_loop = s_event_loop_stack->last();
|
2019-11-26 15:51:18 +00:00
|
|
|
ASSERT(event_loop != nullptr);
|
|
|
|
return *event_loop;
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::quit(int code)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop::quit({})", code);
|
2020-06-02 10:46:21 +00:00
|
|
|
#endif
|
2019-04-10 15:30:34 +00:00
|
|
|
m_exit_requested = true;
|
|
|
|
m_exit_code = code;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::unquit()
|
2019-10-25 05:18:31 +00:00
|
|
|
{
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop::unquit()");
|
2020-06-02 10:46:21 +00:00
|
|
|
#endif
|
2019-10-25 05:18:31 +00:00
|
|
|
m_exit_requested = false;
|
|
|
|
m_exit_code = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
struct EventLoopPusher {
|
2019-04-10 15:30:34 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoopPusher(EventLoop& event_loop)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_event_loop(event_loop)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
if (&m_event_loop != s_main_event_loop) {
|
2020-02-02 11:34:39 +00:00
|
|
|
m_event_loop.take_pending_events_from(EventLoop::current());
|
2019-04-10 15:30:34 +00:00
|
|
|
s_event_loop_stack->append(&event_loop);
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
~EventLoopPusher()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
if (&m_event_loop != s_main_event_loop) {
|
|
|
|
s_event_loop_stack->take_last();
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop::current().take_pending_events_from(m_event_loop);
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
private:
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoop& m_event_loop;
|
2019-04-10 15:30:34 +00:00
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int EventLoop::exec()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-02-02 11:34:39 +00:00
|
|
|
EventLoopPusher pusher(*this);
|
2019-04-10 15:30:34 +00:00
|
|
|
for (;;) {
|
|
|
|
if (m_exit_requested)
|
|
|
|
return m_exit_code;
|
2019-05-18 11:39:21 +00:00
|
|
|
pump();
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-04-29 13:57:49 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::pump(WaitMode mode)
|
2019-05-18 11:39:21 +00:00
|
|
|
{
|
2020-05-16 20:02:53 +00:00
|
|
|
wait_for_event(mode);
|
2019-07-20 13:50:03 +00:00
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
decltype(m_queued_events) events;
|
|
|
|
{
|
2020-02-15 01:09:00 +00:00
|
|
|
LOCKER(m_private->lock);
|
2019-05-18 11:39:21 +00:00
|
|
|
events = move(m_queued_events);
|
|
|
|
}
|
2019-04-29 13:57:49 +00:00
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
for (size_t i = 0; i < events.size(); ++i) {
|
2019-07-21 08:17:20 +00: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-29 22:26:13 +00:00
|
|
|
auto receiver = queued_event.receiver.strong_ref();
|
2019-05-18 11:39:21 +00:00
|
|
|
auto& event = *queued_event.event;
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2019-07-26 14:00:56 +00:00
|
|
|
if (receiver)
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop: {} event {}", *receiver, event.type());
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
2019-05-18 11:39:21 +00:00
|
|
|
if (!receiver) {
|
|
|
|
switch (event.type()) {
|
2020-02-02 11:34:39 +00:00
|
|
|
case Event::Quit:
|
2019-05-18 11:39:21 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return;
|
|
|
|
default:
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Event type {} with no receiver :(", event.type());
|
2020-06-02 10:46:21 +00:00
|
|
|
#endif
|
|
|
|
break;
|
2019-05-18 11:39:21 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
} else if (event.type() == Event::Type::DeferredInvoke) {
|
2019-04-20 10:48:49 +00:00
|
|
|
#ifdef DEFERRED_INVOKE_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("DeferredInvoke: receiver = {}", *receiver);
|
2019-04-20 10:48:49 +00:00
|
|
|
#endif
|
2020-02-02 11:34:39 +00:00
|
|
|
static_cast<DeferredInvocationEvent&>(event).m_invokee(*receiver);
|
2019-05-18 11:39:21 +00:00
|
|
|
} else {
|
2020-02-02 11:34:39 +00:00
|
|
|
NonnullRefPtr<Object> protector(*receiver);
|
2019-09-20 18:37:31 +00:00
|
|
|
receiver->dispatch_event(event);
|
2019-05-18 11:39:21 +00:00
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
if (m_exit_requested) {
|
2020-02-15 01:09:00 +00:00
|
|
|
LOCKER(m_private->lock);
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop: Exit requested. Rejigging {} events.", events.size() - i);
|
2019-07-26 14:00:56 +00:00
|
|
|
#endif
|
2019-07-25 14:12:51 +00:00
|
|
|
decltype(m_queued_events) new_event_queue;
|
|
|
|
new_event_queue.ensure_capacity(m_queued_events.size() + events.size());
|
2020-04-03 20:55:48 +00:00
|
|
|
for (++i; i < events.size(); ++i)
|
2019-07-25 14:12:51 +00:00
|
|
|
new_event_queue.unchecked_append(move(events[i]));
|
|
|
|
new_event_queue.append(move(m_queued_events));
|
|
|
|
m_queued_events = move(new_event_queue);
|
2019-07-13 17:52:56 +00:00
|
|
|
return;
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-02-15 01:09:00 +00:00
|
|
|
LOCKER(m_private->lock);
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop::post_event: ({}) << receivier={}, event={}", m_queued_events.size(), receiver, event);
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
2020-02-14 21:29:06 +00:00
|
|
|
m_queued_events.empend(receiver, move(event));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:48:02 +00:00
|
|
|
EventLoop::SignalHandlers::SignalHandlers(int signo)
|
|
|
|
: m_signo(signo)
|
|
|
|
, m_original_handler(signal(signo, EventLoop::handle_signal))
|
|
|
|
{
|
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop: Registered handler for signal {}", m_signo);
|
2020-07-06 21:48:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::SignalHandlers::~SignalHandlers()
|
|
|
|
{
|
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2021-01-08 17:29:11 +00:00
|
|
|
dbgln("Core::EventLoop: Unregistering handler for signal {}", m_signo);
|
2020-07-06 21:48:02 +00:00
|
|
|
#endif
|
2021-01-08 17:29:11 +00:00
|
|
|
signal(m_signo, m_original_handler);
|
2020-07-06 21:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::SignalHandlers::dispatch()
|
|
|
|
{
|
2021-01-08 17:29:11 +00:00
|
|
|
TemporaryChange change(m_calling_handlers, true);
|
2020-07-06 21:48:02 +00:00
|
|
|
for (auto& handler : m_handlers)
|
|
|
|
handler.value(m_signo);
|
2021-01-08 17:29:11 +00: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));
|
|
|
|
ASSERT(result == AK::HashSetResult::InsertedNewEntry);
|
|
|
|
} else {
|
|
|
|
m_handlers.remove(handler.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_handlers_pending.clear();
|
|
|
|
}
|
2020-07-06 21:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int EventLoop::SignalHandlers::add(Function<void(int)>&& handler)
|
|
|
|
{
|
|
|
|
int id = ++EventLoop::s_next_signal_id; // TODO: worry about wrapping and duplicates?
|
2021-01-08 17:29:11 +00:00
|
|
|
if (m_calling_handlers)
|
|
|
|
m_handlers_pending.set(id, move(handler));
|
|
|
|
else
|
|
|
|
m_handlers.set(id, move(handler));
|
2020-07-06 21:48:02 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventLoop::SignalHandlers::remove(int handler_id)
|
|
|
|
{
|
|
|
|
ASSERT(handler_id != 0);
|
2021-01-08 17:29:11 +00:00
|
|
|
if (m_calling_handlers) {
|
|
|
|
auto it = m_handlers.find(handler_id);
|
|
|
|
if (it != m_handlers.end()) {
|
|
|
|
// Mark pending remove
|
|
|
|
m_handlers_pending.set(handler_id, nullptr);
|
|
|
|
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-06 21:48:02 +00:00
|
|
|
return m_handlers.remove(handler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::dispatch_signal(int signo)
|
|
|
|
{
|
|
|
|
auto handlers = s_signal_handlers.find(signo);
|
|
|
|
if (handlers != s_signal_handlers.end()) {
|
2021-01-08 17:29:11 +00: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;
|
2020-07-06 21:48:02 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop: dispatching signal {}", signo);
|
2020-07-06 21:48:02 +00:00
|
|
|
#endif
|
2021-01-08 17:29:11 +00:00
|
|
|
handler->dispatch();
|
2020-07-06 21:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::handle_signal(int signo)
|
|
|
|
{
|
|
|
|
ASSERT(signo != 0);
|
|
|
|
// We MUST check if the current pid still matches, because there
|
|
|
|
// is a window between fork() and exec() where a signal delivered
|
|
|
|
// to our fork could be inadvertedly routed to the parent process!
|
|
|
|
if (getpid() == s_pid) {
|
|
|
|
int nwritten = write(s_wake_pipe_fds[1], &signo, sizeof(signo));
|
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("EventLoop::register_signal: write");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
{
|
|
|
|
ASSERT(signo != 0);
|
|
|
|
auto handlers = s_signal_handlers.find(signo);
|
|
|
|
if (handlers == s_signal_handlers.end()) {
|
2021-01-08 17:29:11 +00:00
|
|
|
auto signal_handlers = adopt(*new SignalHandlers(signo));
|
|
|
|
auto handler_id = signal_handlers->add(move(handler));
|
2020-07-06 21:48:02 +00:00
|
|
|
s_signal_handlers.set(signo, move(signal_handlers));
|
|
|
|
return handler_id;
|
|
|
|
} else {
|
2021-01-08 17:29:11 +00:00
|
|
|
return handlers->value->add(move(handler));
|
2020-07-06 21:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::unregister_signal(int handler_id)
|
|
|
|
{
|
|
|
|
ASSERT(handler_id != 0);
|
|
|
|
int remove_signo = 0;
|
|
|
|
for (auto& h : s_signal_handlers) {
|
2021-01-08 17:29:11 +00:00
|
|
|
auto& handlers = *h.value;
|
|
|
|
if (handlers.remove(handler_id)) {
|
2020-07-06 21:48:02 +00:00
|
|
|
if (handlers.is_empty())
|
|
|
|
remove_signo = handlers.m_signo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remove_signo != 0)
|
|
|
|
s_signal_handlers.remove(remove_signo);
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:14:42 +00:00
|
|
|
void EventLoop::notify_forked(ForkEvent event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case ForkEvent::Child:
|
|
|
|
s_main_event_loop = nullptr;
|
|
|
|
s_event_loop_stack->clear();
|
|
|
|
s_timers->clear();
|
|
|
|
s_notifiers->clear();
|
|
|
|
s_signal_handlers.clear();
|
|
|
|
s_next_signal_id = 0;
|
|
|
|
s_pid = 0;
|
|
|
|
s_rpc_server = nullptr;
|
|
|
|
s_rpc_clients.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::wait_for_event(WaitMode mode)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2020-07-06 21:48:02 +00:00
|
|
|
retry:
|
2019-04-10 15:30:34 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
|
|
|
|
int max_fd = 0;
|
2019-05-28 09:53:16 +00:00
|
|
|
auto add_fd_to_set = [&max_fd](int fd, fd_set& set) {
|
2019-04-10 15:30:34 +00:00
|
|
|
FD_SET(fd, &set);
|
|
|
|
if (fd > max_fd)
|
|
|
|
max_fd = fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
int max_fd_added = -1;
|
2019-07-14 12:28:24 +00:00
|
|
|
add_fd_to_set(s_wake_pipe_fds[0], rfds);
|
2019-04-10 15:30:34 +00:00
|
|
|
max_fd = max(max_fd, max_fd_added);
|
|
|
|
for (auto& notifier : *s_notifiers) {
|
2020-02-02 11:34:39 +00:00
|
|
|
if (notifier->event_mask() & Notifier::Read)
|
2019-04-10 15:30:34 +00:00
|
|
|
add_fd_to_set(notifier->fd(), rfds);
|
2020-02-02 11:34:39 +00:00
|
|
|
if (notifier->event_mask() & Notifier::Write)
|
2019-04-10 15:30:34 +00:00
|
|
|
add_fd_to_set(notifier->fd(), wfds);
|
2020-02-02 11:34:39 +00:00
|
|
|
if (notifier->event_mask() & Notifier::Exceptional)
|
2019-04-10 15:30:34 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:57:49 +00:00
|
|
|
bool queued_events_is_empty;
|
|
|
|
{
|
2020-02-15 01:09:00 +00:00
|
|
|
LOCKER(m_private->lock);
|
2019-04-29 13:57:49 +00:00
|
|
|
queued_events_is_empty = m_queued_events.is_empty();
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:00:01 +00:00
|
|
|
timeval now;
|
2019-04-10 15:30:34 +00:00
|
|
|
struct timeval timeout = { 0, 0 };
|
2019-05-18 11:39:21 +00:00
|
|
|
bool should_wait_forever = false;
|
2020-05-15 15:21:40 +00:00
|
|
|
if (mode == WaitMode::WaitForEvents && queued_events_is_empty) {
|
|
|
|
auto next_timer_expiration = get_next_timer_expiration();
|
|
|
|
if (next_timer_expiration.has_value()) {
|
2020-03-13 23:03:47 +00:00
|
|
|
timespec now_spec;
|
2020-12-04 05:12:50 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec);
|
2020-03-13 23:03:47 +00:00
|
|
|
now.tv_sec = now_spec.tv_sec;
|
|
|
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
2020-05-15 15:21:40 +00:00
|
|
|
timeval_sub(next_timer_expiration.value(), now, timeout);
|
2020-10-31 16:32:02 +00:00
|
|
|
if (timeout.tv_sec < 0 || (timeout.tv_sec == 0 && timeout.tv_usec < 0)) {
|
2020-02-02 07:10:37 +00:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
}
|
2019-05-18 11:39:21 +00:00
|
|
|
} else {
|
|
|
|
should_wait_forever = true;
|
|
|
|
}
|
2019-05-18 00:00:01 +00:00
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2020-07-06 21:48:02 +00:00
|
|
|
try_select_again:
|
2020-06-17 07:57:48 +00: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;
|
|
|
|
}
|
|
|
|
#ifdef EVENTLOOP_DEBUG
|
2020-10-15 11:21:23 +00:00
|
|
|
dbgln("Core::EventLoop::wait_for_event: {} ({}: {})", marked_fd_count, saved_errno, strerror(saved_errno));
|
2020-06-17 07:57:48 +00:00
|
|
|
#endif
|
|
|
|
// Blow up, similar to Core::safe_syscall.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-07-14 12:28:24 +00:00
|
|
|
if (FD_ISSET(s_wake_pipe_fds[0], &rfds)) {
|
2020-07-06 21:48:02 +00:00
|
|
|
int wake_events[8];
|
|
|
|
auto nread = read(s_wake_pipe_fds[0], wake_events, sizeof(wake_events));
|
2019-07-14 12:28:24 +00:00
|
|
|
if (nread < 0) {
|
|
|
|
perror("read from wake pipe");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
ASSERT(nread > 0);
|
2020-07-06 21:48:02 +00: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 08:20:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 00:00:01 +00:00
|
|
|
if (!s_timers->is_empty()) {
|
2020-03-13 23:03:47 +00:00
|
|
|
timespec now_spec;
|
2020-12-04 05:12:50 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec);
|
2020-03-13 23:03:47 +00:00
|
|
|
now.tv_sec = now_spec.tv_sec;
|
|
|
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
2019-05-18 00:00:01 +00:00
|
|
|
}
|
2019-04-17 23:37:23 +00:00
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
for (auto& it : *s_timers) {
|
|
|
|
auto& timer = *it.value;
|
2019-04-17 23:37:23 +00:00
|
|
|
if (!timer.has_expired(now))
|
2019-04-10 15:30:34 +00: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-29 22:26:13 +00: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 14:58:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-02 10:46:21 +00:00
|
|
|
#ifdef EVENTLOOP_DEBUG
|
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-29 22:26:13 +00:00
|
|
|
dbgln("Core::EventLoop: Timer {} has expired, sending Core::TimerEvent to {}", timer.timer_id, *owner);
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
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-29 22:26:13 +00:00
|
|
|
if (owner)
|
|
|
|
post_event(*owner, make<TimerEvent>(timer.timer_id));
|
2019-04-10 15:30:34 +00:00
|
|
|
if (timer.should_reload) {
|
2019-04-17 23:37:23 +00:00
|
|
|
timer.reload(now);
|
2019-04-10 15:30:34 +00:00
|
|
|
} else {
|
|
|
|
// FIXME: Support removing expired timers that don't want to reload.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:57:28 +00:00
|
|
|
if (!marked_fd_count)
|
|
|
|
return;
|
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
for (auto& notifier : *s_notifiers) {
|
|
|
|
if (FD_ISSET(notifier->fd(), &rfds)) {
|
2020-07-06 21:02:07 +00:00
|
|
|
if (notifier->event_mask() & Notifier::Event::Read)
|
2020-02-02 11:34:39 +00:00
|
|
|
post_event(*notifier, make<NotifierReadEvent>(notifier->fd()));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(notifier->fd(), &wfds)) {
|
2020-07-06 21:02:07 +00:00
|
|
|
if (notifier->event_mask() & Notifier::Event::Write)
|
2020-02-02 11:34:39 +00:00
|
|
|
post_event(*notifier, make<NotifierWriteEvent>(notifier->fd()));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:09:00 +00:00
|
|
|
bool EventLoopTimer::has_expired(const timeval& now) const
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:09:00 +00:00
|
|
|
void EventLoopTimer::reload(const timeval& now)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2019-04-17 23:37:23 +00:00
|
|
|
fire_time = now;
|
2019-04-10 15:30:34 +00:00
|
|
|
fire_time.tv_sec += interval / 1000;
|
|
|
|
fire_time.tv_usec += (interval % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:21:40 +00:00
|
|
|
Optional<struct timeval> EventLoop::get_next_timer_expiration()
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-05-15 15:21:40 +00:00
|
|
|
Optional<struct timeval> soonest {};
|
2019-04-10 15:30:34 +00: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-29 22:26:13 +00:00
|
|
|
auto owner = it.value->owner.strong_ref();
|
2019-12-29 14:58:07 +00: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-29 22:26:13 +00:00
|
|
|
&& owner && !owner->is_visible_for_timer_purposes()) {
|
2019-12-29 14:58:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-15 15:21:40 +00:00
|
|
|
if (!soonest.has_value() || fire_time.tv_sec < soonest.value().tv_sec || (fire_time.tv_sec == soonest.value().tv_sec && fire_time.tv_usec < soonest.value().tv_usec))
|
2019-04-10 15:30:34 +00:00
|
|
|
soonest = fire_time;
|
|
|
|
}
|
2020-05-15 15:21:40 +00:00
|
|
|
return soonest;
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
int EventLoop::register_timer(Object& object, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible fire_when_not_visible)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
ASSERT(milliseconds >= 0);
|
|
|
|
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-29 22:26:13 +00:00
|
|
|
timer->owner = object;
|
2019-04-10 15:30:34 +00:00
|
|
|
timer->interval = milliseconds;
|
2019-04-17 23:37:23 +00:00
|
|
|
timeval now;
|
2020-03-13 23:03:47 +00:00
|
|
|
timespec now_spec;
|
2020-12-04 05:12:50 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_COARSE, &now_spec);
|
2020-03-13 23:03:47 +00:00
|
|
|
now.tv_sec = now_spec.tv_sec;
|
|
|
|
now.tv_usec = now_spec.tv_nsec / 1000;
|
2019-04-17 23:37:23 +00:00
|
|
|
timer->reload(now);
|
2019-04-10 15:30:34 +00:00
|
|
|
timer->should_reload = should_reload;
|
2019-12-29 14:58:07 +00:00
|
|
|
timer->fire_when_not_visible = fire_when_not_visible;
|
2020-03-10 10:31:37 +00:00
|
|
|
int timer_id = s_id_allocator->allocate();
|
2019-04-10 15:30:34 +00:00
|
|
|
timer->timer_id = timer_id;
|
2019-07-23 12:55:12 +00:00
|
|
|
s_timers->set(timer_id, move(timer));
|
2019-04-10 15:30:34 +00:00
|
|
|
return timer_id;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool EventLoop::unregister_timer(int timer_id)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2020-03-10 10:31:37 +00:00
|
|
|
s_id_allocator->deallocate(timer_id);
|
2019-04-10 15:30:34 +00:00
|
|
|
auto it = s_timers->find(timer_id);
|
|
|
|
if (it == s_timers->end())
|
|
|
|
return false;
|
|
|
|
s_timers->remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::register_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
s_notifiers->set(¬ifier);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::unregister_notifier(Badge<Notifier>, Notifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
s_notifiers->remove(¬ifier);
|
|
|
|
}
|
2019-07-14 08:20:57 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void EventLoop::wake()
|
2019-07-14 08:20:57 +00:00
|
|
|
{
|
2020-07-06 21:48:02 +00:00
|
|
|
int wake_event = 0;
|
|
|
|
int nwritten = write(s_wake_pipe_fds[1], &wake_event, sizeof(wake_event));
|
2019-07-14 08:20:57 +00:00
|
|
|
if (nwritten < 0) {
|
2020-02-02 11:34:39 +00:00
|
|
|
perror("EventLoop::wake: write");
|
2019-07-14 08:20:57 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2020-02-14 21:29:06 +00: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-29 22:26:13 +00:00
|
|
|
: receiver(receiver)
|
2020-02-14 21:29:06 +00:00
|
|
|
, event(move(event))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::QueuedEvent::QueuedEvent(QueuedEvent&& other)
|
|
|
|
: receiver(other.receiver)
|
|
|
|
, event(move(other.event))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::QueuedEvent::~QueuedEvent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|