From b57e2f55215914b2d1e651635a7f1b0c6d97c91d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 12 Nov 2017 17:34:06 +0100 Subject: [PATCH] event/DeferredMonitor: eliminate obsolete class Move its code to DeferEvent instead. --- Makefile.am | 3 +- .../{DeferredMonitor.cxx => DeferEvent.cxx} | 6 +- src/event/DeferEvent.hxx | 37 ++++++++--- src/event/DeferredMonitor.hxx | 66 ------------------- src/event/Loop.cxx | 12 ++-- src/event/Loop.hxx | 20 +++--- 6 files changed, 49 insertions(+), 95 deletions(-) rename src/event/{DeferredMonitor.cxx => DeferEvent.cxx} (91%) delete mode 100644 src/event/DeferredMonitor.hxx diff --git a/Makefile.am b/Makefile.am index 7ff9b1497..d34b3ea28 100644 --- a/Makefile.am +++ b/Makefile.am @@ -523,8 +523,7 @@ libevent_a_SOURCES = \ src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \ src/event/TimerEvent.hxx src/event/TimerEvent.cxx \ src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \ - src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx \ - src/event/DeferEvent.hxx \ + src/event/DeferEvent.cxx src/event/DeferEvent.hxx \ src/event/MaskMonitor.hxx src/event/MaskMonitor.cxx \ src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \ src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \ diff --git a/src/event/DeferredMonitor.cxx b/src/event/DeferEvent.cxx similarity index 91% rename from src/event/DeferredMonitor.cxx rename to src/event/DeferEvent.cxx index 224473f3e..1d74b00d5 100644 --- a/src/event/DeferredMonitor.cxx +++ b/src/event/DeferEvent.cxx @@ -18,17 +18,17 @@ */ #include "config.h" -#include "DeferredMonitor.hxx" +#include "DeferEvent.hxx" #include "Loop.hxx" void -DeferredMonitor::Cancel() +DeferEvent::Cancel() noexcept { loop.RemoveDeferred(*this); } void -DeferredMonitor::Schedule() +DeferEvent::Schedule() noexcept { loop.AddDeferred(*this); } diff --git a/src/event/DeferEvent.hxx b/src/event/DeferEvent.hxx index dbd91090d..9ace5a4ce 100644 --- a/src/event/DeferEvent.hxx +++ b/src/event/DeferEvent.hxx @@ -21,28 +21,49 @@ #define MPD_DEFER_EVENT_HXX #include "check.h" -#include "DeferredMonitor.hxx" #include "util/BindMethod.hxx" +#include + +class EventLoop; + /** * Invoke a method call in the #EventLoop. * * This class is thread-safe. */ -class DeferEvent final : DeferredMonitor { +class DeferEvent final { + friend class EventLoop; + + typedef boost::intrusive::list_member_hook<> ListHook; + ListHook list_hook; + + EventLoop &loop; + typedef BoundMethod Callback; const Callback callback; public: - DeferEvent(EventLoop &_loop, Callback _callback) - :DeferredMonitor(_loop), callback(_callback) {} + DeferEvent(EventLoop &_loop, Callback _callback) noexcept + :loop(_loop), callback(_callback) {} - using DeferredMonitor::GetEventLoop; - using DeferredMonitor::Schedule; - using DeferredMonitor::Cancel; + ~DeferEvent() noexcept { + Cancel(); + } + + EventLoop &GetEventLoop() noexcept { + return loop; + } + + void Schedule() noexcept; + void Cancel() noexcept; private: - void RunDeferred() noexcept override { + bool IsPending() const noexcept { + return list_hook.is_linked(); + } + + void RunDeferred() noexcept { callback(); } }; diff --git a/src/event/DeferredMonitor.hxx b/src/event/DeferredMonitor.hxx deleted file mode 100644 index 917c06385..000000000 --- a/src/event/DeferredMonitor.hxx +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2003-2017 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 - -class EventLoop; - -/** - * Defer execution of an event into an #EventLoop. - * - * This class is thread-safe. - */ -class DeferredMonitor { - friend class EventLoop; - - typedef boost::intrusive::list_member_hook<> ListHook; - ListHook list_hook; - - EventLoop &loop; - -public: - DeferredMonitor(EventLoop &_loop) - :loop(_loop) {} - - ~DeferredMonitor() { - Cancel(); - } - - EventLoop &GetEventLoop() { - return loop; - } - - void Schedule(); - void Cancel(); - -private: - bool IsPending() const { - return list_hook.is_linked(); - } - -protected: - virtual void RunDeferred() = 0; -}; - -#endif diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index 92033e598..9bfeb9e9e 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -21,7 +21,7 @@ #include "Loop.hxx" #include "SocketMonitor.hxx" #include "IdleMonitor.hxx" -#include "DeferredMonitor.hxx" +#include "DeferEvent.hxx" #include "util/ScopeExit.hxx" EventLoop::EventLoop(ThreadId _thread) @@ -163,7 +163,7 @@ EventLoop::Run() return; } - /* try to handle DeferredMonitors without WakeFD + /* try to handle DeferEvents without WakeFD overhead */ { const std::lock_guard lock(mutex); @@ -211,7 +211,7 @@ EventLoop::Run() } void -EventLoop::AddDeferred(DeferredMonitor &d) +EventLoop::AddDeferred(DeferEvent &d) noexcept { bool must_wake; @@ -221,7 +221,7 @@ EventLoop::AddDeferred(DeferredMonitor &d) return; /* we don't need to wake up the EventLoop if another - DeferredMonitor has already done it */ + DeferEvent has already done it */ must_wake = !busy && deferred.empty(); deferred.push_back(d); @@ -233,7 +233,7 @@ EventLoop::AddDeferred(DeferredMonitor &d) } void -EventLoop::RemoveDeferred(DeferredMonitor &d) +EventLoop::RemoveDeferred(DeferEvent &d) noexcept { const std::lock_guard protect(mutex); @@ -245,7 +245,7 @@ void EventLoop::HandleDeferred() { while (!deferred.empty() && !quit) { - DeferredMonitor &m = deferred.front(); + auto &m = deferred.front(); assert(m.IsPending()); deferred.pop_front(); diff --git a/src/event/Loop.hxx b/src/event/Loop.hxx index eb8131609..7b9ec69e4 100644 --- a/src/event/Loop.hxx +++ b/src/event/Loop.hxx @@ -30,7 +30,7 @@ #include "SocketMonitor.hxx" #include "TimerEvent.hxx" #include "IdleMonitor.hxx" -#include "DeferredMonitor.hxx" +#include "DeferEvent.hxx" #include #include @@ -76,10 +76,10 @@ class EventLoop final : SocketMonitor Mutex mutex; - typedef boost::intrusive::list, + typedef boost::intrusive::list, boost::intrusive::constant_time_size> DeferredList; DeferredList deferred; @@ -160,19 +160,19 @@ public: void CancelTimer(TimerEvent &t); /** - * Schedule a call to DeferredMonitor::RunDeferred(). + * Schedule a call to DeferEvent::RunDeferred(). * * This method is thread-safe. */ - void AddDeferred(DeferredMonitor &d); + void AddDeferred(DeferEvent &d) noexcept; /** - * Cancel a pending call to DeferredMonitor::RunDeferred(). + * Cancel a pending call to DeferEvent::RunDeferred(). * However after returning, the call may still be running. * * This method is thread-safe. */ - void RemoveDeferred(DeferredMonitor &d); + void RemoveDeferred(DeferEvent &d) noexcept; /** * The main function of this class. It will loop until @@ -182,7 +182,7 @@ public: private: /** - * Invoke all pending DeferredMonitors. + * Invoke all pending DeferEvents. * * Caller must lock the mutex. */