From 0e3a9d8e9db7b727c7e7ad2b61f29c3ff0f71279 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 15 Feb 2020 02:09:00 +0100 Subject: [PATCH] LibCore: Reduce header dependencies of EventLoop --- Libraries/LibCore/EventLoop.cpp | 37 ++++++++++++++----- Libraries/LibCore/EventLoop.h | 28 +++----------- Libraries/LibGUI/DragOperation.h | 1 + Libraries/LibProtocol/Client.h | 1 + Servers/AudioServer/ASClientConnection.h | 1 + Servers/ProtocolServer/Download.cpp | 2 +- Servers/ProtocolServer/PSClientConnection.cpp | 3 +- Servers/ProtocolServer/PSClientConnection.h | 2 +- 8 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Libraries/LibCore/EventLoop.cpp b/Libraries/LibCore/EventLoop.cpp index 0ab29af071..2b604e7f14 100644 --- a/Libraries/LibCore/EventLoop.cpp +++ b/Libraries/LibCore/EventLoop.cpp @@ -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 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* s_event_loop_stack; static IDAllocator s_id_allocator; -HashMap>* EventLoop::s_timers; -HashTable* EventLoop::s_notifiers; +static HashMap>* s_timers; +static HashTable* s_notifiers; int EventLoop::s_wake_pipe_fds[2]; -RefPtr EventLoop::s_rpc_server; +static RefPtr s_rpc_server; HashMap> s_rpc_clients; class RPCClient : public Object { @@ -163,10 +179,11 @@ private: }; EventLoop::EventLoop() + : m_private(make()) { if (!s_event_loop_stack) { s_event_loop_stack = new Vector; - s_timers = new HashMap>; + s_timers = new HashMap>; s_notifiers = new HashTable; } @@ -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) { - 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; diff --git a/Libraries/LibCore/EventLoop.h b/Libraries/LibCore/EventLoop.h index 4d7807b2e9..e1f4891ba6 100644 --- a/Libraries/LibCore/EventLoop.h +++ b/Libraries/LibCore/EventLoop.h @@ -27,13 +27,11 @@ #pragma once #include -#include -#include +#include +#include #include #include #include -#include -#include #include namespace Core { @@ -83,6 +81,7 @@ private: struct QueuedEvent { AK_MAKE_NONCOPYABLE(QueuedEvent); + public: QueuedEvent(Object& receiver, NonnullOwnPtr); QueuedEvent(QueuedEvent&&); @@ -99,25 +98,8 @@ private: static int s_wake_pipe_fds[2]; - LibThread::Lock m_lock; - - 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 owner; - - void reload(const timeval& now); - bool has_expired(const timeval& now) const; - }; - - static HashMap>* s_timers; - - static HashTable* s_notifiers; - - static RefPtr s_rpc_server; + struct Private; + NonnullOwnPtr m_private; }; } diff --git a/Libraries/LibGUI/DragOperation.h b/Libraries/LibGUI/DragOperation.h index ae39b9bd00..17f45cce6f 100644 --- a/Libraries/LibGUI/DragOperation.h +++ b/Libraries/LibGUI/DragOperation.h @@ -26,6 +26,7 @@ #pragma once +#include #include #include #include diff --git a/Libraries/LibProtocol/Client.h b/Libraries/LibProtocol/Client.h index f1d2c42492..eb51ac716d 100644 --- a/Libraries/LibProtocol/Client.h +++ b/Libraries/LibProtocol/Client.h @@ -26,6 +26,7 @@ #pragma once +#include #include #include #include diff --git a/Servers/AudioServer/ASClientConnection.h b/Servers/AudioServer/ASClientConnection.h index 973a3e1fe8..c76c51eb36 100644 --- a/Servers/AudioServer/ASClientConnection.h +++ b/Servers/AudioServer/ASClientConnection.h @@ -26,6 +26,7 @@ #pragma once +#include #include #include diff --git a/Servers/ProtocolServer/Download.cpp b/Servers/ProtocolServer/Download.cpp index 228b4d1507..000725ffb7 100644 --- a/Servers/ProtocolServer/Download.cpp +++ b/Servers/ProtocolServer/Download.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include @@ -84,4 +85,3 @@ void Download::did_progress(size_t total_size, size_t downloaded_size) m_downloaded_size = downloaded_size; m_client->did_progress_download({}, *this); } - diff --git a/Servers/ProtocolServer/PSClientConnection.cpp b/Servers/ProtocolServer/PSClientConnection.cpp index f88e574b3c..4253367d44 100644 --- a/Servers/ProtocolServer/PSClientConnection.cpp +++ b/Servers/ProtocolServer/PSClientConnection.cpp @@ -24,11 +24,12 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include #include #include #include #include -#include static HashMap> s_connections; diff --git a/Servers/ProtocolServer/PSClientConnection.h b/Servers/ProtocolServer/PSClientConnection.h index 0760ebe1e5..56a7c63c0c 100644 --- a/Servers/ProtocolServer/PSClientConnection.h +++ b/Servers/ProtocolServer/PSClientConnection.h @@ -26,7 +26,7 @@ #pragma once -#include +#include #include #include