diff --git a/src/GlobalEvents.cxx b/src/GlobalEvents.cxx index 3cdafdd29..da389b940 100644 --- a/src/GlobalEvents.cxx +++ b/src/GlobalEvents.cxx @@ -19,35 +19,16 @@ #include "config.h" #include "GlobalEvents.hxx" -#include "util/Manual.hxx" -#include "event/MaskMonitor.hxx" #include -namespace GlobalEvents { - class Monitor final : public MaskMonitor { - public: - explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {} - - protected: - virtual void HandleMask(unsigned mask) override; - }; - - static Manual monitor; - - static Handler handlers[MAX]; -} - -/** - * Invoke the callback for a certain event. - */ -static void -InvokeGlobalEvent(GlobalEvents::Event event) +inline void +GlobalEvents::Monitor::Invoke(Event event) { assert((unsigned)event < GlobalEvents::MAX); - assert(GlobalEvents::handlers[event] != nullptr); + assert(handlers[event] != nullptr); - GlobalEvents::handlers[event](); + handlers[event](); } void @@ -56,35 +37,22 @@ GlobalEvents::Monitor::HandleMask(unsigned f) for (unsigned i = 0; i < MAX; ++i) if (f & (1u << i)) /* invoke the event handler */ - InvokeGlobalEvent(Event(i)); + Invoke(Event(i)); } void -GlobalEvents::Initialize(EventLoop &loop) -{ - monitor.Construct(loop); -} - -void -GlobalEvents::Deinitialize() -{ - monitor.Destruct(); -} - -void -GlobalEvents::Register(Event event, Handler callback) +GlobalEvents::Monitor::Register(Event event, Handler callback) { assert((unsigned)event < MAX); - assert(handlers[event] == nullptr); handlers[event] = callback; } void -GlobalEvents::Emit(Event event) +GlobalEvents::Monitor::Emit(Event event) { assert((unsigned)event < MAX); const unsigned mask = 1u << unsigned(event); - monitor->OrMask(mask); + OrMask(mask); } diff --git a/src/GlobalEvents.hxx b/src/GlobalEvents.hxx index ff3c57df8..c67b07519 100644 --- a/src/GlobalEvents.hxx +++ b/src/GlobalEvents.hxx @@ -20,7 +20,7 @@ #ifndef MPD_GLOBAL_EVENTS_HXX #define MPD_GLOBAL_EVENTS_HXX -class EventLoop; +#include "event/MaskMonitor.hxx" namespace GlobalEvents { enum Event { @@ -38,13 +38,25 @@ namespace GlobalEvents { typedef void (*Handler)(); - void Initialize(EventLoop &loop); + class Monitor final : MaskMonitor { + Handler handlers[MAX]; - void Deinitialize(); + public: + explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {} - void Register(Event event, Handler handler); + void Register(Event event, Handler handler); - void Emit(Event event); + void Emit(Event event); + + private: + /** + * Invoke the callback for a certain event. + */ + void Invoke(Event event); + + protected: + void HandleMask(unsigned mask) override; + }; } #endif /* MAIN_NOTIFY_H */ diff --git a/src/Idle.cxx b/src/Idle.cxx index e1e71d3a9..d0f43a3d7 100644 --- a/src/Idle.cxx +++ b/src/Idle.cxx @@ -24,7 +24,8 @@ #include "config.h" #include "Idle.hxx" -#include "GlobalEvents.hxx" +#include "Main.hxx" +#include "Instance.hxx" #include "util/ASCII.hxx" #include @@ -58,7 +59,7 @@ idle_add(unsigned flags) unsigned old_flags = idle_flags.fetch_or(flags); if ((old_flags & flags) != flags) - GlobalEvents::Emit(GlobalEvents::IDLE); + instance->global_events.Emit(GlobalEvents::IDLE); } unsigned diff --git a/src/Instance.hxx b/src/Instance.hxx index fcb087d08..ff0b484f0 100644 --- a/src/Instance.hxx +++ b/src/Instance.hxx @@ -22,6 +22,7 @@ #include "check.h" #include "event/Loop.hxx" +#include "GlobalEvents.hxx" #include "Compiler.h" #ifdef ENABLE_NEIGHBOR_PLUGINS @@ -36,7 +37,6 @@ class Storage; class UpdateService; #endif -class EventLoop; class Error; class ClientList; struct Partition; @@ -57,6 +57,8 @@ struct Instance final { EventLoop event_loop; + GlobalEvents::Monitor global_events; + #ifdef ENABLE_NEIGHBOR_PLUGINS NeighborGlue *neighbors; #endif @@ -77,6 +79,8 @@ struct Instance final Partition *partition; + Instance():global_events(event_loop) {} + /** * Initiate shutdown. Wrapper for EventLoop::Break(). */ diff --git a/src/Main.cxx b/src/Main.cxx index cd153d09f..3798dd8fb 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -38,7 +38,6 @@ #include "Idle.hxx" #include "Log.hxx" #include "LogInit.hxx" -#include "GlobalEvents.hxx" #include "input/Init.hxx" #include "event/Loop.hxx" #include "IOThread.hxx" @@ -520,8 +519,7 @@ static int mpd_main_after_fork(struct options options) try { Error error; - GlobalEvents::Initialize(instance->event_loop); - GlobalEvents::Register(GlobalEvents::IDLE, idle_event_emitted); + instance->global_events.Register(GlobalEvents::IDLE, idle_event_emitted); if (!ConfigureFS(error)) { LogError(error); @@ -689,8 +687,6 @@ try { sticker_global_finish(); #endif - GlobalEvents::Deinitialize(); - playlist_list_global_finish(); input_stream_global_finish(); diff --git a/src/Partition.cxx b/src/Partition.cxx index f0cfe11cd..115ffde0a 100644 --- a/src/Partition.cxx +++ b/src/Partition.cxx @@ -67,13 +67,13 @@ Partition::SyncWithPlayer() void Partition::OnPlayerSync() { - GlobalEvents::Emit(GlobalEvents::PLAYLIST); + instance.global_events.Emit(GlobalEvents::PLAYLIST); } void Partition::OnPlayerTagModified() { - GlobalEvents::Emit(GlobalEvents::TAG); + instance.global_events.Emit(GlobalEvents::TAG); } void diff --git a/src/PlaylistGlobal.cxx b/src/PlaylistGlobal.cxx index d53da4784..837d0acfe 100644 --- a/src/PlaylistGlobal.cxx +++ b/src/PlaylistGlobal.cxx @@ -26,7 +26,6 @@ #include "PlaylistGlobal.hxx" #include "Main.hxx" #include "Instance.hxx" -#include "GlobalEvents.hxx" static void playlist_tag_event(void) @@ -43,6 +42,6 @@ playlist_event(void) void playlist_global_init() { - GlobalEvents::Register(GlobalEvents::TAG, playlist_tag_event); - GlobalEvents::Register(GlobalEvents::PLAYLIST, playlist_event); + instance->global_events.Register(GlobalEvents::TAG, playlist_tag_event); + instance->global_events.Register(GlobalEvents::PLAYLIST, playlist_event); }