event/DeferredMonitor: eliminate obsolete class

Move its code to DeferEvent instead.
This commit is contained in:
Max Kellermann 2017-11-12 17:34:06 +01:00
parent bf3ced6a34
commit b57e2f5521
6 changed files with 49 additions and 95 deletions

View File

@ -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 \

View File

@ -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);
}

View File

@ -21,28 +21,49 @@
#define MPD_DEFER_EVENT_HXX
#include "check.h"
#include "DeferredMonitor.hxx"
#include "util/BindMethod.hxx"
#include <boost/intrusive/list_hook.hpp>
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<void() noexcept> 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();
}
};

View File

@ -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 <boost/intrusive/list_hook.hpp>
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

View File

@ -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<Mutex> 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<Mutex> 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();

View File

@ -30,7 +30,7 @@
#include "SocketMonitor.hxx"
#include "TimerEvent.hxx"
#include "IdleMonitor.hxx"
#include "DeferredMonitor.hxx"
#include "DeferEvent.hxx"
#include <boost/intrusive/set.hpp>
#include <boost/intrusive/list.hpp>
@ -76,10 +76,10 @@ class EventLoop final : SocketMonitor
Mutex mutex;
typedef boost::intrusive::list<DeferredMonitor,
boost::intrusive::member_hook<DeferredMonitor,
DeferredMonitor::ListHook,
&DeferredMonitor::list_hook>,
typedef boost::intrusive::list<DeferEvent,
boost::intrusive::member_hook<DeferEvent,
DeferEvent::ListHook,
&DeferEvent::list_hook>,
boost::intrusive::constant_time_size<false>> 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.
*/