InotifyQueue: convert to a class

This commit is contained in:
Max Kellermann 2013-01-14 10:10:21 +01:00
parent 8e3982dd42
commit c8b408beae
3 changed files with 44 additions and 40 deletions

View File

@ -23,9 +23,6 @@
#include "Main.hxx"
#include "event/Loop.hxx"
#include <list>
#include <string>
#include <glib.h>
#include <string.h>
@ -42,28 +39,19 @@ enum {
INOTIFY_UPDATE_DELAY_S = 5,
};
static std::list<std::string> inotify_queue;
static guint queue_source_id;
void
mpd_inotify_queue_init(void)
InotifyQueue::~InotifyQueue()
{
if (source_id != 0)
g_source_remove(source_id);
}
void
mpd_inotify_queue_finish(void)
{
if (queue_source_id != 0)
g_source_remove(queue_source_id);
}
static gboolean
mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
inline bool
InotifyQueue::Run()
{
unsigned id;
while (!inotify_queue.empty()) {
const char *uri_utf8 = inotify_queue.front().c_str();
while (!queue.empty()) {
const char *uri_utf8 = queue.front().c_str();
id = update_enqueue(uri_utf8, false);
if (id == 0)
@ -72,14 +60,21 @@ mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
g_debug("updating '%s' job=%u", uri_utf8, id);
inotify_queue.pop_front();
queue.pop_front();
}
/* done, remove the timer event by returning false */
queue_source_id = 0;
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)
{
@ -91,16 +86,14 @@ path_in(const char *path, const char *possible_parent)
}
void
mpd_inotify_enqueue(const char *uri_utf8)
InotifyQueue::Enqueue(const char *uri_utf8)
{
if (queue_source_id != 0)
g_source_remove(queue_source_id);
queue_source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
mpd_inotify_run_update,
nullptr);
if (source_id != 0)
g_source_remove(source_id);
source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
Run, nullptr);
for (auto i = inotify_queue.begin(), end = inotify_queue.end();
i != end;) {
for (auto i = queue.begin(), end = queue.end(); i != end;) {
const char *current_uri = i->c_str();
if (path_in(uri_utf8, current_uri))
@ -111,10 +104,10 @@ mpd_inotify_enqueue(const char *uri_utf8)
/* existing path is a sub-path of the new
path; we can dequeue the existing path and
update the new path instead */
i = inotify_queue.erase(i);
i = queue.erase(i);
else
++i;
}
inotify_queue.emplace_back(uri_utf8);
queue.emplace_back(uri_utf8);
}

View File

@ -20,13 +20,23 @@
#ifndef MPD_INOTIFY_QUEUE_HXX
#define MPD_INOTIFY_QUEUE_HXX
void
mpd_inotify_queue_init(void);
#include <glib.h>
void
mpd_inotify_queue_finish(void);
#include <list>
#include <string>
void
mpd_inotify_enqueue(const char *uri_utf8);
class InotifyQueue {
std::list<std::string> queue;
guint source_id;
public:
~InotifyQueue();
void Enqueue(const char *uri_utf8);
private:
bool Run();
static gboolean Run(gpointer ctx);
};
#endif

View File

@ -57,6 +57,7 @@ struct watch_directory {
};
static InotifySource *inotify_source;
static InotifyQueue *inotify_queue;
static unsigned inotify_max_depth;
static struct watch_directory inotify_root;
@ -295,7 +296,7 @@ mpd_inotify_callback(int wd, unsigned mask,
: g_strdup("");
if (uri_utf8 != NULL) {
mpd_inotify_enqueue(uri_utf8);
inotify_queue->Enqueue(uri_utf8);
g_free(uri_utf8);
}
}
@ -341,7 +342,7 @@ mpd_inotify_init(unsigned max_depth)
recursive_watch_subdirectories(&inotify_root, path, 0);
mpd_inotify_queue_init();
inotify_queue = new InotifyQueue();
g_debug("watching music directory");
}
@ -367,7 +368,7 @@ mpd_inotify_finish(void)
if (inotify_source == NULL)
return;
mpd_inotify_queue_finish();
delete inotify_queue;
delete inotify_source;
g_tree_foreach(inotify_directories, free_watch_directory, NULL);