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
Notes: sideshowbarker 2024-07-19 09:19:27 +09:00
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;

View file

@ -27,13 +27,11 @@
#pragma once
#include <AK/Forward.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibCore/Object.h>
#include <LibThread/Lock.h>
#include <sys/time.h>
namespace Core {
@ -83,6 +81,7 @@ private:
struct QueuedEvent {
AK_MAKE_NONCOPYABLE(QueuedEvent);
public:
QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
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<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;
struct Private;
NonnullOwnPtr<Private> m_private;
};
}

View file

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

View file

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

View file

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

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Badge.h>
#include <ProtocolServer/Download.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_client->did_progress_download({}, *this);
}

View file

@ -24,11 +24,12 @@
* 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/PSClientConnection.h>
#include <ProtocolServer/Protocol.h>
#include <ProtocolServer/ProtocolClientEndpoint.h>
#include <AK/SharedBuffer.h>
static HashMap<int, RefPtr<PSClientConnection>> s_connections;

View file

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