From 355dd5cb24f1d36cb13a2463ce7585186474adc8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 7 Aug 2013 18:59:42 +0200 Subject: event/DeferredMonitor: new class wrapping g_idle_add() --- src/event/DeferredMonitor.cxx | 55 +++++++++++++++++++++++++++++++++++++++++++ src/event/DeferredMonitor.hxx | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/event/DeferredMonitor.cxx create mode 100644 src/event/DeferredMonitor.hxx (limited to 'src/event') diff --git a/src/event/DeferredMonitor.cxx b/src/event/DeferredMonitor.cxx new file mode 100644 index 000000000..10aa0d757 --- /dev/null +++ b/src/event/DeferredMonitor.cxx @@ -0,0 +1,55 @@ +/* + * 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 "DeferredMonitor.hxx" +#include "Loop.hxx" + +void +DeferredMonitor::Cancel() +{ + const auto id = source_id.exchange(0); + if (id != 0) + g_source_remove(id); +} + +void +DeferredMonitor::Schedule() +{ + const unsigned id = g_idle_add(Callback, this); + const auto old_id = source_id.exchange(id); + if (old_id != 0) + g_source_remove(old_id); +} + +void +DeferredMonitor::DoRun() +{ + const auto id = source_id.exchange(0); + if (id != 0) + Run(); +} + +gboolean +DeferredMonitor::Callback(gpointer data) +{ + DeferredMonitor &monitor = *(DeferredMonitor *)data; + monitor.Run(); + return false; +} diff --git a/src/event/DeferredMonitor.hxx b/src/event/DeferredMonitor.hxx new file mode 100644 index 000000000..8a08facba --- /dev/null +++ b/src/event/DeferredMonitor.hxx @@ -0,0 +1,54 @@ +/* + * 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_DEFERRED_MONITOR_HXX +#define MPD_SOCKET_DEFERRED_MONITOR_HXX + +#include "check.h" + +#include + +#include + +/** + * Defer execution of an event into an #EventLoop. + */ +class DeferredMonitor { + std::atomic source_id; + +public: + DeferredMonitor() + :source_id(0) {} + + ~DeferredMonitor() { + Cancel(); + } + + void Schedule(); + void Cancel(); + +protected: + virtual void Run() = 0; + +private: + void DoRun(); + static gboolean Callback(gpointer data); +}; + +#endif /* MAIN_NOTIFY_H */ -- cgit v1.2.3