2019-08-17 09:35:09 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Time.h>
|
|
|
|
#include <LibCore/CEvent.h>
|
|
|
|
#include <LibCore/CEventLoop.h>
|
2019-08-17 09:35:09 +00:00
|
|
|
#include <LibCore/CLocalSocket.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <LibCore/CNotifier.h>
|
|
|
|
#include <LibCore/CObject.h>
|
2019-07-21 11:46:53 +00:00
|
|
|
#include <LibCore/CSyscallUtils.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>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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 15:30:34 +00:00
|
|
|
|
|
|
|
//#define CEVENTLOOP_DEBUG
|
2019-04-20 10:48:49 +00:00
|
|
|
//#define DEFERRED_INVOKE_DEBUG
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
static CEventLoop* s_main_event_loop;
|
|
|
|
static Vector<CEventLoop*>* s_event_loop_stack;
|
2019-07-24 06:42:55 +00:00
|
|
|
HashMap<int, NonnullOwnPtr<CEventLoop::EventLoopTimer>>* CEventLoop::s_timers;
|
2019-04-10 15:35:43 +00:00
|
|
|
HashTable<CNotifier*>* CEventLoop::s_notifiers;
|
2019-04-10 15:30:34 +00:00
|
|
|
int CEventLoop::s_next_timer_id = 1;
|
2019-07-14 08:20:57 +00:00
|
|
|
int CEventLoop::s_wake_pipe_fds[2];
|
2019-08-17 09:35:09 +00:00
|
|
|
CLocalServer CEventLoop::s_rpc_server;
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
CEventLoop::CEventLoop()
|
|
|
|
{
|
|
|
|
if (!s_event_loop_stack) {
|
|
|
|
s_event_loop_stack = new Vector<CEventLoop*>;
|
2019-07-24 06:42:55 +00:00
|
|
|
s_timers = new HashMap<int, NonnullOwnPtr<CEventLoop::EventLoopTimer>>;
|
2019-04-10 15:35:43 +00:00
|
|
|
s_notifiers = new HashTable<CNotifier*>;
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s_main_event_loop) {
|
|
|
|
s_main_event_loop = this;
|
2019-08-05 12:31:38 +00:00
|
|
|
int rc = pipe2(s_wake_pipe_fds, O_CLOEXEC);
|
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
|
|
|
|
|
|
|
|
|
|
|
auto rpc_path = String::format("/tmp/rpc.%d", getpid());
|
|
|
|
rc = unlink(rpc_path.characters());
|
|
|
|
if (rc < 0 && errno != ENOENT) {
|
|
|
|
perror("unlink");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
bool listening = s_rpc_server.listen(rpc_path);
|
|
|
|
ASSERT(listening);
|
|
|
|
|
|
|
|
s_rpc_server.on_ready_to_accept = [&] {
|
|
|
|
auto* client = s_rpc_server.accept();
|
|
|
|
ASSERT(client);
|
|
|
|
JsonArray objects;
|
|
|
|
for (auto& object : CObject::all_objects()) {
|
|
|
|
JsonObject json_object;
|
2019-08-18 18:36:37 +00:00
|
|
|
object.save_to(json_object);
|
2019-08-17 09:35:09 +00:00
|
|
|
objects.append(move(json_object));
|
|
|
|
}
|
|
|
|
client->write(objects.to_string());
|
|
|
|
client->delete_later();
|
|
|
|
};
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CEVENTLOOP_DEBUG
|
2019-07-26 14:00:56 +00:00
|
|
|
dbg() << getpid() << " CEventLoop constructed :)";
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CEventLoop::~CEventLoop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CEventLoop& CEventLoop::main()
|
|
|
|
{
|
|
|
|
ASSERT(s_main_event_loop);
|
|
|
|
return *s_main_event_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEventLoop& CEventLoop::current()
|
|
|
|
{
|
|
|
|
return *s_event_loop_stack->last();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CEventLoop::quit(int code)
|
|
|
|
{
|
|
|
|
m_exit_requested = true;
|
|
|
|
m_exit_code = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CEventLoopPusher {
|
|
|
|
public:
|
2019-05-28 09:53:16 +00:00
|
|
|
CEventLoopPusher(CEventLoop& event_loop)
|
|
|
|
: m_event_loop(event_loop)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
if (&m_event_loop != s_main_event_loop) {
|
|
|
|
m_event_loop.take_pending_events_from(CEventLoop::current());
|
|
|
|
s_event_loop_stack->append(&event_loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~CEventLoopPusher()
|
|
|
|
{
|
|
|
|
if (&m_event_loop != s_main_event_loop) {
|
|
|
|
s_event_loop_stack->take_last();
|
|
|
|
CEventLoop::current().take_pending_events_from(m_event_loop);
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
private:
|
|
|
|
CEventLoop& m_event_loop;
|
|
|
|
};
|
|
|
|
|
|
|
|
int CEventLoop::exec()
|
|
|
|
{
|
|
|
|
CEventLoopPusher pusher(*this);
|
|
|
|
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
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
void CEventLoop::pump(WaitMode mode)
|
|
|
|
{
|
2019-07-20 13:50:03 +00:00
|
|
|
if (m_queued_events.is_empty())
|
2019-05-18 11:39:21 +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;
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
events = move(m_queued_events);
|
|
|
|
}
|
2019-04-29 13:57:49 +00:00
|
|
|
|
2019-07-21 08:17:20 +00:00
|
|
|
for (int i = 0; i < events.size(); ++i) {
|
|
|
|
auto& queued_event = events.at(i);
|
2019-07-26 14:00:56 +00:00
|
|
|
ASSERT(queued_event.event);
|
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
auto* receiver = queued_event.receiver.ptr();
|
|
|
|
auto& event = *queued_event.event;
|
2019-04-10 15:30:34 +00:00
|
|
|
#ifdef CEVENTLOOP_DEBUG
|
2019-07-26 14:00:56 +00:00
|
|
|
if (receiver)
|
|
|
|
dbg() << "CEventLoop: " << *receiver << " event " << (int)event.type();
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
2019-05-18 11:39:21 +00:00
|
|
|
if (!receiver) {
|
|
|
|
switch (event.type()) {
|
|
|
|
case CEvent::Quit:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return;
|
|
|
|
default:
|
2019-07-21 08:17:20 +00:00
|
|
|
dbg() << "Event type " << event.type() << " with no receiver :(";
|
2019-05-18 11:39:21 +00:00
|
|
|
}
|
|
|
|
} else if (event.type() == CEvent::Type::DeferredInvoke) {
|
2019-04-20 10:48:49 +00:00
|
|
|
#ifdef DEFERRED_INVOKE_DEBUG
|
2019-05-18 11:39:21 +00:00
|
|
|
printf("DeferredInvoke: receiver=%s{%p}\n", receiver->class_name(), receiver);
|
2019-04-20 10:48:49 +00:00
|
|
|
#endif
|
2019-05-18 11:39:21 +00:00
|
|
|
static_cast<CDeferredInvocationEvent&>(event).m_invokee(*receiver);
|
|
|
|
} else {
|
|
|
|
receiver->event(event);
|
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
if (m_exit_requested) {
|
|
|
|
LOCKER(m_lock);
|
2019-07-26 14:00:56 +00:00
|
|
|
#ifdef CEVENTLOOP_DEBUG
|
|
|
|
dbg() << "CEventLoop: Exit requested. Rejigging " << (events.size() - i) << " events.";
|
|
|
|
#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());
|
|
|
|
for (; i < events.size(); ++i)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:38:00 +00:00
|
|
|
void CEventLoop::post_event(CObject& receiver, NonnullOwnPtr<CEvent>&& event)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
2019-04-29 13:57:49 +00:00
|
|
|
LOCKER(m_lock);
|
2019-04-10 15:30:34 +00:00
|
|
|
#ifdef CEVENTLOOP_DEBUG
|
2019-07-26 14:00:56 +00:00
|
|
|
dbg() << "CEventLoop::post_event: {" << m_queued_events.size() << "} << receiver=" << receiver << ", event=" << event;
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
|
|
|
m_queued_events.append({ receiver.make_weak_ptr(), move(event) });
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:39:21 +00:00
|
|
|
void CEventLoop::wait_for_event(WaitMode mode)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
|
|
|
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) {
|
2019-04-10 15:35:43 +00:00
|
|
|
if (notifier->event_mask() & CNotifier::Read)
|
2019-04-10 15:30:34 +00:00
|
|
|
add_fd_to_set(notifier->fd(), rfds);
|
2019-04-10 15:35:43 +00:00
|
|
|
if (notifier->event_mask() & CNotifier::Write)
|
2019-04-10 15:30:34 +00:00
|
|
|
add_fd_to_set(notifier->fd(), wfds);
|
2019-04-10 15:35:43 +00:00
|
|
|
if (notifier->event_mask() & CNotifier::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;
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
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;
|
|
|
|
if (mode == WaitMode::WaitForEvents) {
|
|
|
|
if (!s_timers->is_empty() && queued_events_is_empty) {
|
|
|
|
gettimeofday(&now, nullptr);
|
|
|
|
get_next_timer_expiration(timeout);
|
2019-06-06 16:02:28 +00:00
|
|
|
timeval_sub(timeout, now, timeout);
|
2019-05-18 11:39:21 +00:00
|
|
|
} else {
|
|
|
|
should_wait_forever = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
should_wait_forever = false;
|
2019-05-18 00:00:01 +00:00
|
|
|
}
|
2019-04-10 15:30:34 +00:00
|
|
|
|
2019-07-21 11:46:53 +00:00
|
|
|
int marked_fd_count = CSyscallUtils::safe_syscall(select, max_fd + 1, &rfds, &wfds, nullptr, should_wait_forever ? nullptr : &timeout);
|
2019-07-14 12:28:24 +00:00
|
|
|
if (FD_ISSET(s_wake_pipe_fds[0], &rfds)) {
|
2019-07-14 08:20:57 +00:00
|
|
|
char buffer[32];
|
2019-07-14 12:28:24 +00:00
|
|
|
auto nread = read(s_wake_pipe_fds[0], buffer, sizeof(buffer));
|
|
|
|
if (nread < 0) {
|
|
|
|
perror("read from wake pipe");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
ASSERT(nread > 0);
|
2019-07-14 08:20:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 00:00:01 +00:00
|
|
|
if (!s_timers->is_empty()) {
|
2019-04-18 11:23:59 +00:00
|
|
|
gettimeofday(&now, nullptr);
|
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;
|
|
|
|
#ifdef CEVENTLOOP_DEBUG
|
2019-07-26 14:00:56 +00:00
|
|
|
dbg() << "CEventLoop: Timer " << timer.timer_id << " has expired, sending CTimerEvent to " << timer.owner;
|
2019-04-10 15:30:34 +00:00
|
|
|
#endif
|
|
|
|
post_event(*timer.owner, make<CTimerEvent>(timer.timer_id));
|
|
|
|
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)) {
|
|
|
|
if (notifier->on_ready_to_read)
|
2019-07-16 18:31:14 +00:00
|
|
|
post_event(*notifier, make<CNotifierReadEvent>(notifier->fd()));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(notifier->fd(), &wfds)) {
|
|
|
|
if (notifier->on_ready_to_write)
|
2019-07-16 18:31:14 +00:00
|
|
|
post_event(*notifier, make<CNotifierWriteEvent>(notifier->fd()));
|
2019-04-10 15:30:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:37:23 +00:00
|
|
|
bool CEventLoop::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);
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:37:23 +00:00
|
|
|
void CEventLoop::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CEventLoop::get_next_timer_expiration(timeval& soonest)
|
|
|
|
{
|
|
|
|
ASSERT(!s_timers->is_empty());
|
|
|
|
bool has_checked_any = false;
|
|
|
|
for (auto& it : *s_timers) {
|
|
|
|
auto& fire_time = it.value->fire_time;
|
|
|
|
if (!has_checked_any || fire_time.tv_sec < soonest.tv_sec || (fire_time.tv_sec == soonest.tv_sec && fire_time.tv_usec < soonest.tv_usec))
|
|
|
|
soonest = fire_time;
|
|
|
|
has_checked_any = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CEventLoop::register_timer(CObject& object, int milliseconds, bool should_reload)
|
|
|
|
{
|
|
|
|
ASSERT(milliseconds >= 0);
|
|
|
|
auto timer = make<EventLoopTimer>();
|
|
|
|
timer->owner = object.make_weak_ptr();
|
|
|
|
timer->interval = milliseconds;
|
2019-04-17 23:37:23 +00:00
|
|
|
timeval now;
|
|
|
|
gettimeofday(&now, nullptr);
|
|
|
|
timer->reload(now);
|
2019-04-10 15:30:34 +00:00
|
|
|
timer->should_reload = should_reload;
|
2019-05-28 09:53:16 +00:00
|
|
|
int timer_id = ++s_next_timer_id; // FIXME: This will eventually wrap around.
|
|
|
|
ASSERT(timer_id); // FIXME: Aforementioned wraparound.
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CEventLoop::unregister_timer(int timer_id)
|
|
|
|
{
|
|
|
|
auto it = s_timers->find(timer_id);
|
|
|
|
if (it == s_timers->end())
|
|
|
|
return false;
|
|
|
|
s_timers->remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
void CEventLoop::register_notifier(Badge<CNotifier>, CNotifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
s_notifiers->set(¬ifier);
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
void CEventLoop::unregister_notifier(Badge<CNotifier>, CNotifier& notifier)
|
2019-04-10 15:30:34 +00:00
|
|
|
{
|
|
|
|
s_notifiers->remove(¬ifier);
|
|
|
|
}
|
2019-07-14 08:20:57 +00:00
|
|
|
|
|
|
|
void CEventLoop::wake()
|
|
|
|
{
|
|
|
|
char ch = '!';
|
2019-07-14 12:28:24 +00:00
|
|
|
int nwritten = write(s_wake_pipe_fds[1], &ch, 1);
|
2019-07-14 08:20:57 +00:00
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("CEventLoop::wake: write");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|