GlobalEvents: expose the internal class

Move the GlobalEvents::Monitor instance into class Instance.
Eliminate all global variables.
This commit is contained in:
Max Kellermann 2016-03-05 20:14:12 +01:00
parent b4d594eeff
commit 0e87ce4680
7 changed files with 38 additions and 58 deletions

View File

@ -19,35 +19,16 @@
#include "config.h"
#include "GlobalEvents.hxx"
#include "util/Manual.hxx"
#include "event/MaskMonitor.hxx"
#include <assert.h>
namespace GlobalEvents {
class Monitor final : public MaskMonitor {
public:
explicit Monitor(EventLoop &_loop):MaskMonitor(_loop) {}
protected:
virtual void HandleMask(unsigned mask) override;
};
static Manual<Monitor> 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);
}

View File

@ -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 */

View File

@ -24,7 +24,8 @@
#include "config.h"
#include "Idle.hxx"
#include "GlobalEvents.hxx"
#include "Main.hxx"
#include "Instance.hxx"
#include "util/ASCII.hxx"
#include <atomic>
@ -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

View File

@ -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().
*/

View File

@ -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();

View File

@ -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

View File

@ -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);
}