Commit d3a479b7 authored by Max Kellermann's avatar Max Kellermann

event/TimeoutMonitor: wrapper for g_timeout_source_new()

parent 92fb0e82
......@@ -354,6 +354,7 @@ libutil_a_SOURCES = \
libevent_a_SOURCES = \
src/event/WakeFD.cxx src/event/WakeFD.hxx \
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
src/event/Loop.hxx
# PCM library
......
......@@ -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();
......
......@@ -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
......@@ -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");
}
......
......@@ -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;
}
......@@ -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 */
......@@ -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;
}
};
......
/*
* 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();
}
/*
* 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 */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment