1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:48:10 +00:00

LibCore: Reduce header dependencies of EventLoop

This commit is contained in:
Andreas Kling 2020-02-15 02:09:00 +01:00
parent b011ea9962
commit 0e3a9d8e9d
8 changed files with 39 additions and 36 deletions

View file

@ -55,13 +55,29 @@ namespace Core {
class RPCClient;
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;
};
static EventLoop* s_main_event_loop;
static Vector<EventLoop*>* s_event_loop_stack;
static IDAllocator s_id_allocator;
HashMap<int, NonnullOwnPtr<EventLoop::EventLoopTimer>>* EventLoop::s_timers;
HashTable<Notifier*>* EventLoop::s_notifiers;
static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
static HashTable<Notifier*>* s_notifiers;
int EventLoop::s_wake_pipe_fds[2];
RefPtr<LocalServer> EventLoop::s_rpc_server;
static RefPtr<LocalServer> s_rpc_server;
HashMap<int, RefPtr<RPCClient>> s_rpc_clients;
class RPCClient : public Object {
@ -163,10 +179,11 @@ private:
};
EventLoop::EventLoop()
: m_private(make<Private>())
{
if (!s_event_loop_stack) {
s_event_loop_stack = new Vector<EventLoop*>;
s_timers = new HashMap<int, NonnullOwnPtr<EventLoop::EventLoopTimer>>;
s_timers = new HashMap<int, NonnullOwnPtr<EventLoopTimer>>;
s_notifiers = new HashTable<Notifier*>;
}
@ -275,7 +292,7 @@ void EventLoop::pump(WaitMode mode)
decltype(m_queued_events) events;
{
LOCKER(m_lock);
LOCKER(m_private->lock);
events = move(m_queued_events);
}
@ -309,7 +326,7 @@ void EventLoop::pump(WaitMode mode)
}
if (m_exit_requested) {
LOCKER(m_lock);
LOCKER(m_private->lock);
#ifdef CEVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Exit requested. Rejigging " << (events.size() - i) << " events.";
#endif
@ -326,7 +343,7 @@ void EventLoop::pump(WaitMode mode)
void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
{
LOCKER(m_lock);
LOCKER(m_private->lock);
#ifdef CEVENTLOOP_DEBUG
dbg() << "Core::EventLoop::post_event: {" << m_queued_events.size() << "} << receiver=" << receiver << ", event=" << event;
#endif
@ -361,7 +378,7 @@ void EventLoop::wait_for_event(WaitMode mode)
bool queued_events_is_empty;
{
LOCKER(m_lock);
LOCKER(m_private->lock);
queued_events_is_empty = m_queued_events.is_empty();
}
@ -435,12 +452,12 @@ void EventLoop::wait_for_event(WaitMode mode)
}
}
bool EventLoop::EventLoopTimer::has_expired(const timeval& now) const
bool EventLoopTimer::has_expired(const timeval& now) const
{
return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
}
void EventLoop::EventLoopTimer::reload(const timeval& now)
void EventLoopTimer::reload(const timeval& now)
{
fire_time = now;
fire_time.tv_sec += interval / 1000;