event/TimeoutMonitor: wrapper for g_timeout_source_new()
This commit is contained in:
parent
92fb0e829a
commit
d3a479b7fa
@ -354,6 +354,7 @@ libutil_a_SOURCES = \
|
|||||||
|
|
||||||
libevent_a_SOURCES = \
|
libevent_a_SOURCES = \
|
||||||
src/event/WakeFD.cxx src/event/WakeFD.hxx \
|
src/event/WakeFD.cxx src/event/WakeFD.hxx \
|
||||||
|
src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \
|
||||||
src/event/Loop.hxx
|
src/event/Loop.hxx
|
||||||
|
|
||||||
# PCM library
|
# PCM library
|
||||||
|
@ -20,7 +20,6 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "InotifyQueue.hxx"
|
#include "InotifyQueue.hxx"
|
||||||
#include "UpdateGlue.hxx"
|
#include "UpdateGlue.hxx"
|
||||||
#include "Main.hxx"
|
|
||||||
#include "event/Loop.hxx"
|
#include "event/Loop.hxx"
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
@ -39,14 +38,8 @@ enum {
|
|||||||
INOTIFY_UPDATE_DELAY_S = 5,
|
INOTIFY_UPDATE_DELAY_S = 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
InotifyQueue::~InotifyQueue()
|
bool
|
||||||
{
|
InotifyQueue::OnTimeout()
|
||||||
if (source_id != 0)
|
|
||||||
g_source_remove(source_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool
|
|
||||||
InotifyQueue::Run()
|
|
||||||
{
|
{
|
||||||
unsigned id;
|
unsigned id;
|
||||||
|
|
||||||
@ -64,17 +57,9 @@ InotifyQueue::Run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* done, remove the timer event by returning false */
|
/* done, remove the timer event by returning false */
|
||||||
source_id = 0;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
|
||||||
InotifyQueue::Run(gpointer data)
|
|
||||||
{
|
|
||||||
InotifyQueue &queue = *(InotifyQueue *)data;
|
|
||||||
return queue.Run();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
path_in(const char *path, const char *possible_parent)
|
path_in(const char *path, const char *possible_parent)
|
||||||
{
|
{
|
||||||
@ -88,10 +73,7 @@ path_in(const char *path, const char *possible_parent)
|
|||||||
void
|
void
|
||||||
InotifyQueue::Enqueue(const char *uri_utf8)
|
InotifyQueue::Enqueue(const char *uri_utf8)
|
||||||
{
|
{
|
||||||
if (source_id != 0)
|
ScheduleSeconds(INOTIFY_UPDATE_DELAY_S);
|
||||||
g_source_remove(source_id);
|
|
||||||
source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
|
|
||||||
Run, nullptr);
|
|
||||||
|
|
||||||
for (auto i = queue.begin(), end = queue.end(); i != end;) {
|
for (auto i = queue.begin(), end = queue.end(); i != end;) {
|
||||||
const char *current_uri = i->c_str();
|
const char *current_uri = i->c_str();
|
||||||
|
@ -20,23 +20,22 @@
|
|||||||
#ifndef MPD_INOTIFY_QUEUE_HXX
|
#ifndef MPD_INOTIFY_QUEUE_HXX
|
||||||
#define MPD_INOTIFY_QUEUE_HXX
|
#define MPD_INOTIFY_QUEUE_HXX
|
||||||
|
|
||||||
#include <glib.h>
|
#include "event/TimeoutMonitor.hxx"
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class InotifyQueue {
|
class InotifyQueue final : private TimeoutMonitor {
|
||||||
std::list<std::string> queue;
|
std::list<std::string> queue;
|
||||||
guint source_id;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~InotifyQueue();
|
InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {}
|
||||||
|
|
||||||
void Enqueue(const char *uri_utf8);
|
void Enqueue(const char *uri_utf8);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool Run();
|
virtual bool OnTimeout() override;
|
||||||
static gboolean Run(gpointer ctx);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include "InotifySource.hxx"
|
#include "InotifySource.hxx"
|
||||||
#include "InotifyQueue.hxx"
|
#include "InotifyQueue.hxx"
|
||||||
#include "Mapper.hxx"
|
#include "Mapper.hxx"
|
||||||
|
#include "Main.hxx"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
@ -342,7 +343,7 @@ mpd_inotify_init(unsigned max_depth)
|
|||||||
|
|
||||||
recursive_watch_subdirectories(&inotify_root, path, 0);
|
recursive_watch_subdirectories(&inotify_root, path, 0);
|
||||||
|
|
||||||
inotify_queue = new InotifyQueue();
|
inotify_queue = new InotifyQueue(*main_loop);
|
||||||
|
|
||||||
g_debug("watching music directory");
|
g_debug("watching music directory");
|
||||||
}
|
}
|
||||||
|
@ -35,17 +35,11 @@
|
|||||||
#define G_LOG_DOMAIN "state_file"
|
#define G_LOG_DOMAIN "state_file"
|
||||||
|
|
||||||
StateFile::StateFile(const char *_path, Partition &_partition, EventLoop &_loop)
|
StateFile::StateFile(const char *_path, Partition &_partition, EventLoop &_loop)
|
||||||
:path(_path), partition(_partition), loop(_loop),
|
:TimeoutMonitor(_loop), path(_path), partition(_partition),
|
||||||
source_id(0),
|
|
||||||
prev_volume_version(0), prev_output_version(0),
|
prev_volume_version(0), prev_output_version(0),
|
||||||
prev_playlist_version(0)
|
prev_playlist_version(0)
|
||||||
{
|
{
|
||||||
source_id = loop.AddTimeoutSeconds(5 * 60, TimerCallback, this);
|
ScheduleSeconds(5 * 60);
|
||||||
}
|
|
||||||
|
|
||||||
StateFile::~StateFile()
|
|
||||||
{
|
|
||||||
g_source_remove(source_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -120,11 +114,9 @@ StateFile::AutoWrite()
|
|||||||
* This function is called every 5 minutes by the GLib main loop, and
|
* This function is called every 5 minutes by the GLib main loop, and
|
||||||
* saves the state file.
|
* saves the state file.
|
||||||
*/
|
*/
|
||||||
gboolean
|
bool
|
||||||
StateFile::TimerCallback(gpointer data)
|
StateFile::OnTimeout()
|
||||||
{
|
{
|
||||||
StateFile &state_file = *(StateFile *)data;
|
AutoWrite();
|
||||||
|
|
||||||
state_file.AutoWrite();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -20,21 +20,17 @@
|
|||||||
#ifndef MPD_STATE_FILE_HXX
|
#ifndef MPD_STATE_FILE_HXX
|
||||||
#define MPD_STATE_FILE_HXX
|
#define MPD_STATE_FILE_HXX
|
||||||
|
|
||||||
#include <glib.h>
|
#include "event/TimeoutMonitor.hxx"
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct Partition;
|
struct Partition;
|
||||||
class EventLoop;
|
|
||||||
|
|
||||||
class StateFile {
|
class StateFile final : private TimeoutMonitor {
|
||||||
std::string path;
|
std::string path;
|
||||||
|
|
||||||
Partition &partition;
|
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
|
* These version numbers determine whether we need to save the state
|
||||||
@ -45,14 +41,13 @@ class StateFile {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
StateFile(const char *path, Partition &partition, EventLoop &loop);
|
StateFile(const char *path, Partition &partition, EventLoop &loop);
|
||||||
~StateFile();
|
|
||||||
|
|
||||||
void Read();
|
void Read();
|
||||||
void Write();
|
void Write();
|
||||||
void AutoWrite();
|
void AutoWrite();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static gboolean TimerCallback(gpointer data);
|
virtual bool OnTimeout() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* STATE_FILE_H */
|
#endif /* STATE_FILE_H */
|
||||||
|
@ -64,22 +64,20 @@ public:
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
guint AddTimeout(guint interval_ms,
|
GSource *AddTimeout(guint interval_ms,
|
||||||
GSourceFunc function, gpointer data) {
|
GSourceFunc function, gpointer data) {
|
||||||
GSource *source = g_timeout_source_new(interval_ms);
|
GSource *source = g_timeout_source_new(interval_ms);
|
||||||
g_source_set_callback(source, function, data, nullptr);
|
g_source_set_callback(source, function, data, nullptr);
|
||||||
guint id = g_source_attach(source, GetContext());
|
g_source_attach(source, GetContext());
|
||||||
g_source_unref(source);
|
return source;
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
guint AddTimeoutSeconds(guint interval_s,
|
GSource *AddTimeoutSeconds(guint interval_s,
|
||||||
GSourceFunc function, gpointer data) {
|
GSourceFunc function, gpointer data) {
|
||||||
GSource *source = g_timeout_source_new_seconds(interval_s);
|
GSource *source = g_timeout_source_new_seconds(interval_s);
|
||||||
g_source_set_callback(source, function, data, nullptr);
|
g_source_set_callback(source, function, data, nullptr);
|
||||||
guint id = g_source_attach(source, GetContext());
|
g_source_attach(source, GetContext());
|
||||||
g_source_unref(source);
|
return source;
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
65
src/event/TimeoutMonitor.cxx
Normal file
65
src/event/TimeoutMonitor.cxx
Normal file
@ -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();
|
||||||
|
}
|
60
src/event/TimeoutMonitor.hxx
Normal file
60
src/event/TimeoutMonitor.hxx
Normal file
@ -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 */
|
Loading…
Reference in New Issue
Block a user