1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +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; 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 EventLoop* s_main_event_loop;
static Vector<EventLoop*>* s_event_loop_stack; static Vector<EventLoop*>* s_event_loop_stack;
static IDAllocator s_id_allocator; static IDAllocator s_id_allocator;
HashMap<int, NonnullOwnPtr<EventLoop::EventLoopTimer>>* EventLoop::s_timers; static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
HashTable<Notifier*>* EventLoop::s_notifiers; static HashTable<Notifier*>* s_notifiers;
int EventLoop::s_wake_pipe_fds[2]; 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; HashMap<int, RefPtr<RPCClient>> s_rpc_clients;
class RPCClient : public Object { class RPCClient : public Object {
@ -163,10 +179,11 @@ private:
}; };
EventLoop::EventLoop() EventLoop::EventLoop()
: m_private(make<Private>())
{ {
if (!s_event_loop_stack) { if (!s_event_loop_stack) {
s_event_loop_stack = new Vector<EventLoop*>; 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*>; s_notifiers = new HashTable<Notifier*>;
} }
@ -275,7 +292,7 @@ void EventLoop::pump(WaitMode mode)
decltype(m_queued_events) events; decltype(m_queued_events) events;
{ {
LOCKER(m_lock); LOCKER(m_private->lock);
events = move(m_queued_events); events = move(m_queued_events);
} }
@ -309,7 +326,7 @@ void EventLoop::pump(WaitMode mode)
} }
if (m_exit_requested) { if (m_exit_requested) {
LOCKER(m_lock); LOCKER(m_private->lock);
#ifdef CEVENTLOOP_DEBUG #ifdef CEVENTLOOP_DEBUG
dbg() << "Core::EventLoop: Exit requested. Rejigging " << (events.size() - i) << " events."; dbg() << "Core::EventLoop: Exit requested. Rejigging " << (events.size() - i) << " events.";
#endif #endif
@ -326,7 +343,7 @@ void EventLoop::pump(WaitMode mode)
void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event) void EventLoop::post_event(Object& receiver, NonnullOwnPtr<Event>&& event)
{ {
LOCKER(m_lock); LOCKER(m_private->lock);
#ifdef CEVENTLOOP_DEBUG #ifdef CEVENTLOOP_DEBUG
dbg() << "Core::EventLoop::post_event: {" << m_queued_events.size() << "} << receiver=" << receiver << ", event=" << event; dbg() << "Core::EventLoop::post_event: {" << m_queued_events.size() << "} << receiver=" << receiver << ", event=" << event;
#endif #endif
@ -361,7 +378,7 @@ void EventLoop::wait_for_event(WaitMode mode)
bool queued_events_is_empty; bool queued_events_is_empty;
{ {
LOCKER(m_lock); LOCKER(m_private->lock);
queued_events_is_empty = m_queued_events.is_empty(); 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); 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 = now;
fire_time.tv_sec += interval / 1000; fire_time.tv_sec += interval / 1000;

View file

@ -27,13 +27,11 @@
#pragma once #pragma once
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/HashMap.h> #include <AK/Noncopyable.h>
#include <AK/OwnPtr.h> #include <AK/NonnullOwnPtr.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibCore/Forward.h> #include <LibCore/Forward.h>
#include <LibCore/Object.h>
#include <LibThread/Lock.h>
#include <sys/time.h> #include <sys/time.h>
namespace Core { namespace Core {
@ -83,6 +81,7 @@ private:
struct QueuedEvent { struct QueuedEvent {
AK_MAKE_NONCOPYABLE(QueuedEvent); AK_MAKE_NONCOPYABLE(QueuedEvent);
public: public:
QueuedEvent(Object& receiver, NonnullOwnPtr<Event>); QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
QueuedEvent(QueuedEvent&&); QueuedEvent(QueuedEvent&&);
@ -99,25 +98,8 @@ private:
static int s_wake_pipe_fds[2]; static int s_wake_pipe_fds[2];
LibThread::Lock m_lock; struct Private;
NonnullOwnPtr<Private> m_private;
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;
};
static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
static HashTable<Notifier*>* s_notifiers;
static RefPtr<LocalServer> s_rpc_server;
}; };
} }

View file

@ -26,6 +26,7 @@
#pragma once #pragma once
#include <AK/OwnPtr.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/Object.h> #include <LibCore/Object.h>
#include <LibGfx/Forward.h> #include <LibGfx/Forward.h>

View file

@ -26,6 +26,7 @@
#pragma once #pragma once
#include <AK/HashMap.h>
#include <LibIPC/ServerConnection.h> #include <LibIPC/ServerConnection.h>
#include <ProtocolServer/ProtocolClientEndpoint.h> #include <ProtocolServer/ProtocolClientEndpoint.h>
#include <ProtocolServer/ProtocolServerEndpoint.h> #include <ProtocolServer/ProtocolServerEndpoint.h>

View file

@ -26,6 +26,7 @@
#pragma once #pragma once
#include <AK/HashMap.h>
#include <AudioServer/AudioServerEndpoint.h> #include <AudioServer/AudioServerEndpoint.h>
#include <LibIPC/ClientConnection.h> #include <LibIPC/ClientConnection.h>

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/Badge.h>
#include <ProtocolServer/Download.h> #include <ProtocolServer/Download.h>
#include <ProtocolServer/PSClientConnection.h> #include <ProtocolServer/PSClientConnection.h>
@ -84,4 +85,3 @@ void Download::did_progress(size_t total_size, size_t downloaded_size)
m_downloaded_size = downloaded_size; m_downloaded_size = downloaded_size;
m_client->did_progress_download({}, *this); m_client->did_progress_download({}, *this);
} }

View file

@ -24,11 +24,12 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/Badge.h>
#include <AK/SharedBuffer.h>
#include <ProtocolServer/Download.h> #include <ProtocolServer/Download.h>
#include <ProtocolServer/PSClientConnection.h> #include <ProtocolServer/PSClientConnection.h>
#include <ProtocolServer/Protocol.h> #include <ProtocolServer/Protocol.h>
#include <ProtocolServer/ProtocolClientEndpoint.h> #include <ProtocolServer/ProtocolClientEndpoint.h>
#include <AK/SharedBuffer.h>
static HashMap<int, RefPtr<PSClientConnection>> s_connections; static HashMap<int, RefPtr<PSClientConnection>> s_connections;

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <AK/Badge.h> #include <AK/HashMap.h>
#include <LibIPC/ClientConnection.h> #include <LibIPC/ClientConnection.h>
#include <ProtocolServer/ProtocolServerEndpoint.h> #include <ProtocolServer/ProtocolServerEndpoint.h>