summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2016-03-10 09:27:07 +0100
committerMax Kellermann <max@duempel.org>2016-03-10 20:32:43 +0100
commit5ffb82993ecf27ea1493771281d19f90fa73c46c (patch)
tree15dc9db438216f6c1f4b355b0127f783074845cb /src
parent07add0bd91b4f772f2895cd201edc615101a7fc3 (diff)
IdleMonitor: new class to replace GlobalEvents::IDLE
Use MaskMonitor to eliminate duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/GlobalEvents.hxx3
-rw-r--r--src/Idle.cxx15
-rw-r--r--src/Idle.hxx6
-rw-r--r--src/IdleMaskMonitor.hxx44
-rw-r--r--src/Instance.hxx11
-rw-r--r--src/Main.cxx17
6 files changed, 59 insertions, 37 deletions
diff --git a/src/GlobalEvents.hxx b/src/GlobalEvents.hxx
index c67b07519..abd6949f6 100644
--- a/src/GlobalEvents.hxx
+++ b/src/GlobalEvents.hxx
@@ -24,9 +24,6 @@
namespace GlobalEvents {
enum Event {
- /** an idle event was emitted */
- IDLE,
-
/** must call playlist_sync() */
PLAYLIST,
diff --git a/src/Idle.cxx b/src/Idle.cxx
index 4280c4b5e..24cbf26e4 100644
--- a/src/Idle.cxx
+++ b/src/Idle.cxx
@@ -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);
}
diff --git a/src/Idle.hxx b/src/Idle.hxx
index b4328bc60..974757c83 100644
--- a/src/Idle.hxx
+++ b/src/Idle.hxx
@@ -34,10 +34,4 @@
void
idle_add(unsigned flags);
-/**
- * Atomically reads and resets the global idle flags value.
- */
-unsigned
-idle_get();
-
#endif
diff --git a/src/IdleMaskMonitor.hxx b/src/IdleMaskMonitor.hxx
new file mode 100644
index 000000000..f13f296dd
--- /dev/null
+++ b/src/IdleMaskMonitor.hxx
@@ -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
diff --git a/src/Instance.hxx b/src/Instance.hxx
index d3e928750..e4a2eed7d 100644
--- a/src/Instance.hxx
+++ b/src/Instance.hxx
@@ -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
diff --git a/src/Main.cxx b/src/Main.cxx
index 39b48be80..7bbcd59d7 100644
--- a/src/Main.cxx
+++ b/src/Main.cxx
@@ -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;