aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/InotifyQueue.cxx24
-rw-r--r--src/InotifyQueue.hxx11
-rw-r--r--src/InotifyUpdate.cxx3
-rw-r--r--src/StateFile.cxx18
-rw-r--r--src/StateFile.hxx13
-rw-r--r--src/event/Loop.hxx18
-rw-r--r--src/event/TimeoutMonitor.cxx65
-rw-r--r--src/event/TimeoutMonitor.hxx60
8 files changed, 152 insertions, 60 deletions
diff --git a/src/InotifyQueue.cxx b/src/InotifyQueue.cxx
index 2b7899ecb..3212f95f9 100644
--- a/src/InotifyQueue.cxx
+++ b/src/InotifyQueue.cxx
@@ -20,7 +20,6 @@
#include "config.h"
#include "InotifyQueue.hxx"
#include "UpdateGlue.hxx"
-#include "Main.hxx"
#include "event/Loop.hxx"
#include <glib.h>
@@ -39,14 +38,8 @@ enum {
INOTIFY_UPDATE_DELAY_S = 5,
};
-InotifyQueue::~InotifyQueue()
-{
- if (source_id != 0)
- g_source_remove(source_id);
-}
-
-inline bool
-InotifyQueue::Run()
+bool
+InotifyQueue::OnTimeout()
{
unsigned id;
@@ -64,17 +57,9 @@ InotifyQueue::Run()
}
/* done, remove the timer event by returning false */
- source_id = 0;
return false;
}
-gboolean
-InotifyQueue::Run(gpointer data)
-{
- InotifyQueue &queue = *(InotifyQueue *)data;
- return queue.Run();
-}
-
static bool
path_in(const char *path, const char *possible_parent)
{
@@ -88,10 +73,7 @@ path_in(const char *path, const char *possible_parent)
void
InotifyQueue::Enqueue(const char *uri_utf8)
{
- if (source_id != 0)
- g_source_remove(source_id);
- source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
- Run, nullptr);
+ ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
for (auto i = queue.begin(), end = queue.end(); i != end;) {
const char *current_uri = i->c_str();
diff --git a/src/InotifyQueue.hxx b/src/InotifyQueue.hxx
index a30cdf094..761df574a 100644
--- a/src/InotifyQueue.hxx
+++ b/src/InotifyQueue.hxx
@@ -20,23 +20,22 @@
#ifndef MPD_INOTIFY_QUEUE_HXX
#define MPD_INOTIFY_QUEUE_HXX
-#include <glib.h>
+#include "event/TimeoutMonitor.hxx"
+#include "gcc.h"
#include <list>
#include <string>
-class InotifyQueue {
+class InotifyQueue final : private TimeoutMonitor {
std::list<std::string> queue;
- guint source_id;
public:
- ~InotifyQueue();
+ InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {}
void Enqueue(const char *uri_utf8);
private:
- bool Run();
- static gboolean Run(gpointer ctx);
+ virtual bool OnTimeout() override;
};
#endif
diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx
index 9fe657a1b..0fe22385b 100644
--- a/src/InotifyUpdate.cxx
+++ b/src/InotifyUpdate.cxx
@@ -22,6 +22,7 @@
#include "InotifySource.hxx"
#include "InotifyQueue.hxx"
#include "Mapper.hxx"
+#include "Main.hxx"
extern "C" {
#include "path.h"
@@ -342,7 +343,7 @@ mpd_inotify_init(unsigned max_depth)
recursive_watch_subdirectories(&inotify_root, path, 0);
- inotify_queue = new InotifyQueue();
+ inotify_queue = new InotifyQueue(*main_loop);
g_debug("watching music directory");
}
diff --git a/src/StateFile.cxx b/src/StateFile.cxx
index a19eac55c..b78056520 100644
--- a/src/StateFile.cxx
+++ b/src/StateFile.cxx
@@ -35,17 +35,11 @@
#define G_LOG_DOMAIN "state_file"
StateFile::StateFile(const char *_path, Partition &_partition, EventLoop &_loop)
- :path(_path), partition(_partition), loop(_loop),
- source_id(0),
+ :TimeoutMonitor(_loop), path(_path), partition(_partition),
prev_volume_version(0), prev_output_version(0),
prev_playlist_version(0)
{
- source_id = loop.AddTimeoutSeconds(5 * 60, TimerCallback, this);
-}
-
-StateFile::~StateFile()
-{
- g_source_remove(source_id);
+ ScheduleSeconds(5 * 60);
}
void
@@ -120,11 +114,9 @@ StateFile::AutoWrite()
* This function is called every 5 minutes by the GLib main loop, and
* saves the state file.
*/
-gboolean
-StateFile::TimerCallback(gpointer data)
+bool
+StateFile::OnTimeout()
{
- StateFile &state_file = *(StateFile *)data;
-
- state_file.AutoWrite();
+ AutoWrite();
return true;
}
diff --git a/src/StateFile.hxx b/src/StateFile.hxx
index 0888b25ad..39c3fcdf6 100644
--- a/src/StateFile.hxx
+++ b/src/StateFile.hxx
@@ -20,21 +20,17 @@
#ifndef MPD_STATE_FILE_HXX
#define MPD_STATE_FILE_HXX
-#include <glib.h>
+#include "event/TimeoutMonitor.hxx"
+#include "gcc.h"
#include <string>
struct Partition;
-class EventLoop;
-class StateFile {
+class StateFile final : private TimeoutMonitor {
std::string path;
Partition &partition;
- EventLoop &loop;
-
- /** the GLib source id for the save timer */
- guint source_id;
/**
* These version numbers determine whether we need to save the state
@@ -45,14 +41,13 @@ class StateFile {
public:
StateFile(const char *path, Partition &partition, EventLoop &loop);
- ~StateFile();
void Read();
void Write();
void AutoWrite();
private:
- static gboolean TimerCallback(gpointer data);
+ virtual bool OnTimeout() override;
};
#endif /* STATE_FILE_H */
diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx
index 9c139e08e..31ef1613c 100644
--- a/src/event/Loop.hxx
+++ b/src/event/Loop.hxx
@@ -64,22 +64,20 @@ public:
return id;
}
- guint AddTimeout(guint interval_ms,
- GSourceFunc function, gpointer data) {
+ GSource *AddTimeout(guint interval_ms,
+ GSourceFunc function, gpointer data) {
GSource *source = g_timeout_source_new(interval_ms);
g_source_set_callback(source, function, data, nullptr);
- guint id = g_source_attach(source, GetContext());
- g_source_unref(source);
- return id;
+ g_source_attach(source, GetContext());
+ return source;
}
- guint AddTimeoutSeconds(guint interval_s,
- GSourceFunc function, gpointer data) {
+ GSource *AddTimeoutSeconds(guint interval_s,
+ GSourceFunc function, gpointer data) {
GSource *source = g_timeout_source_new_seconds(interval_s);
g_source_set_callback(source, function, data, nullptr);
- guint id = g_source_attach(source, GetContext());
- g_source_unref(source);
- return id;
+ g_source_attach(source, GetContext());
+ return source;
}
};
diff --git a/src/event/TimeoutMonitor.cxx b/src/event/TimeoutMonitor.cxx
new file mode 100644
index 000000000..e0bf997a0
--- /dev/null
+++ b/src/event/TimeoutMonitor.cxx
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#include "config.h"
+#include "TimeoutMonitor.hxx"
+#include "Loop.hxx"
+
+void
+TimeoutMonitor::Cancel()
+{
+ if (source != nullptr) {
+ g_source_destroy(source);
+ g_source_unref(source);
+ source = nullptr;
+ }
+}
+
+void
+TimeoutMonitor::Schedule(unsigned ms)
+{
+ Cancel();
+ source = loop.AddTimeout(ms, Callback, this);
+}
+
+void
+TimeoutMonitor::ScheduleSeconds(unsigned s)
+{
+ Cancel();
+ source = loop.AddTimeoutSeconds(s, Callback, this);
+}
+
+bool
+TimeoutMonitor::Run()
+{
+ bool result = OnTimeout();
+ if (!result && source != nullptr) {
+ g_source_unref(source);
+ source = nullptr;
+ }
+
+ return result;
+}
+
+gboolean
+TimeoutMonitor::Callback(gpointer data)
+{
+ TimeoutMonitor &monitor = *(TimeoutMonitor *)data;
+ return monitor.Run();
+}
diff --git a/src/event/TimeoutMonitor.hxx b/src/event/TimeoutMonitor.hxx
new file mode 100644
index 000000000..6914bcb61
--- /dev/null
+++ b/src/event/TimeoutMonitor.hxx
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2003-2013 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_SOCKET_TIMEOUT_MONITOR_HXX
+#define MPD_SOCKET_TIMEOUT_MONITOR_HXX
+
+#include "check.h"
+
+#include <glib.h>
+
+class EventLoop;
+
+class TimeoutMonitor {
+ EventLoop &loop;
+ GSource *source;
+
+public:
+ TimeoutMonitor(EventLoop &_loop)
+ :loop(_loop), source(nullptr) {}
+
+ ~TimeoutMonitor() {
+ Cancel();
+ }
+
+ bool IsActive() const {
+ return source != nullptr;
+ }
+
+ void Schedule(unsigned ms);
+ void ScheduleSeconds(unsigned s);
+ void Cancel();
+
+protected:
+ /**
+ * @return true reschedules the timeout again
+ */
+ virtual bool OnTimeout() = 0;
+
+private:
+ bool Run();
+ static gboolean Callback(gpointer data);
+};
+
+#endif /* MAIN_NOTIFY_H */