IdleMonitor: new class to replace GlobalEvents::IDLE

Use MaskMonitor to eliminate duplicate code.
This commit is contained in:
Max Kellermann 2016-03-10 09:27:07 +01:00
parent 07add0bd91
commit 5ffb82993e
7 changed files with 60 additions and 37 deletions

View File

@ -95,6 +95,7 @@ libmpd_a_SOURCES = \
src/command/OtherCommands.cxx src/command/OtherCommands.hxx \
src/command/CommandListBuilder.cxx src/command/CommandListBuilder.hxx \
src/Idle.cxx src/Idle.hxx \
src/IdleMaskMonitor.hxx \
src/IdleFlags.cxx src/IdleFlags.hxx \
src/decoder/DecoderError.cxx src/decoder/DecoderError.hxx \
src/decoder/DecoderThread.cxx src/decoder/DecoderThread.hxx \

View File

@ -24,9 +24,6 @@
namespace GlobalEvents {
enum Event {
/** an idle event was emitted */
IDLE,
/** must call playlist_sync() */
PLAYLIST,

View File

@ -27,25 +27,12 @@
#include "Main.hxx"
#include "Instance.hxx"
#include <atomic>
#include <assert.h>
static std::atomic_uint idle_flags;
void
idle_add(unsigned flags)
{
assert(flags != 0);
unsigned old_flags = idle_flags.fetch_or(flags);
if ((old_flags & flags) != flags)
instance->global_events.Emit(GlobalEvents::IDLE);
}
unsigned
idle_get(void)
{
return idle_flags.exchange(0);
instance->EmitIdle(flags);
}

View File

@ -34,10 +34,4 @@
void
idle_add(unsigned flags);
/**
* Atomically reads and resets the global idle flags value.
*/
unsigned
idle_get();
#endif

44
src/IdleMaskMonitor.hxx Normal file
View File

@ -0,0 +1,44 @@
/*
* Copyright 2003-2016 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_IDLE_MONITOR_HXX
#define MPD_IDLE_MONITOR_HXX
#include "event/MaskMonitor.hxx"
class IdleMaskMonitor : MaskMonitor {
public:
explicit IdleMaskMonitor(EventLoop &_loop)
:MaskMonitor(_loop) {}
void EmitIdle(unsigned mask) {
OrMask(mask);
}
protected:
virtual void OnIdle(unsigned mask) = 0;
private:
/* virtual methods from class MaskMonitor */
void HandleMask(unsigned mask) final {
OnIdle(mask);
}
};
#endif

View File

@ -22,6 +22,7 @@
#include "check.h"
#include "event/Loop.hxx"
#include "IdleMaskMonitor.hxx"
#include "GlobalEvents.hxx"
#include "Compiler.h"
@ -51,7 +52,8 @@ struct EventLoopHolder {
};
struct Instance final
: EventLoopHolder
: EventLoopHolder,
IdleMaskMonitor
#if defined(ENABLE_DATABASE) || defined(ENABLE_NEIGHBOR_PLUGINS)
,
#endif
@ -89,7 +91,9 @@ struct Instance final
StateFile *state_file;
Instance():global_events(event_loop) {}
Instance()
:IdleMaskMonitor(event_loop),
global_events(event_loop) {}
/**
* Initiate shutdown. Wrapper for EventLoop::Break().
@ -129,6 +133,9 @@ private:
virtual void FoundNeighbor(const NeighborInfo &info) override;
virtual void LostNeighbor(const NeighborInfo &info) override;
#endif
/* virtual methods from class IdleMaskMonitor */
void OnIdle(unsigned mask) override;
};
#endif

View File

@ -365,21 +365,16 @@ initialize_decoder_and_player(void)
buffered_before_play);
}
/**
* Handler for GlobalEvents::IDLE.
*/
static void
idle_event_emitted(void)
void
Instance::OnIdle(unsigned flags)
{
/* send "idle" notifications to all subscribed
clients */
unsigned flags = idle_get();
if (flags != 0)
instance->client_list->IdleAdd(flags);
client_list->IdleAdd(flags);
if (flags & (IDLE_PLAYLIST|IDLE_PLAYER|IDLE_MIXER|IDLE_OUTPUT) &&
instance->state_file != nullptr)
instance->state_file->CheckModified();
state_file != nullptr)
state_file->CheckModified();
}
#ifndef ANDROID
@ -517,8 +512,6 @@ static int mpd_main_after_fork(struct options options)
try {
Error error;
instance->global_events.Register(GlobalEvents::IDLE, idle_event_emitted);
if (!ConfigureFS(error)) {
LogError(error);
return EXIT_FAILURE;