event/DeferredMonitor: eliminate obsolete class
Move its code to DeferEvent instead.
This commit is contained in:
parent
bf3ced6a34
commit
b57e2f5521
@ -523,8 +523,7 @@ libevent_a_SOURCES = \
|
|||||||
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
src/event/SignalMonitor.hxx src/event/SignalMonitor.cxx \
|
||||||
src/event/TimerEvent.hxx src/event/TimerEvent.cxx \
|
src/event/TimerEvent.hxx src/event/TimerEvent.cxx \
|
||||||
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
|
src/event/IdleMonitor.hxx src/event/IdleMonitor.cxx \
|
||||||
src/event/DeferredMonitor.hxx src/event/DeferredMonitor.cxx \
|
src/event/DeferEvent.cxx src/event/DeferEvent.hxx \
|
||||||
src/event/DeferEvent.hxx \
|
|
||||||
src/event/MaskMonitor.hxx src/event/MaskMonitor.cxx \
|
src/event/MaskMonitor.hxx src/event/MaskMonitor.cxx \
|
||||||
src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \
|
src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \
|
||||||
src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \
|
src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \
|
||||||
|
@ -18,17 +18,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "DeferredMonitor.hxx"
|
#include "DeferEvent.hxx"
|
||||||
#include "Loop.hxx"
|
#include "Loop.hxx"
|
||||||
|
|
||||||
void
|
void
|
||||||
DeferredMonitor::Cancel()
|
DeferEvent::Cancel() noexcept
|
||||||
{
|
{
|
||||||
loop.RemoveDeferred(*this);
|
loop.RemoveDeferred(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
DeferredMonitor::Schedule()
|
DeferEvent::Schedule() noexcept
|
||||||
{
|
{
|
||||||
loop.AddDeferred(*this);
|
loop.AddDeferred(*this);
|
||||||
}
|
}
|
@ -21,28 +21,49 @@
|
|||||||
#define MPD_DEFER_EVENT_HXX
|
#define MPD_DEFER_EVENT_HXX
|
||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
#include "DeferredMonitor.hxx"
|
|
||||||
#include "util/BindMethod.hxx"
|
#include "util/BindMethod.hxx"
|
||||||
|
|
||||||
|
#include <boost/intrusive/list_hook.hpp>
|
||||||
|
|
||||||
|
class EventLoop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke a method call in the #EventLoop.
|
* Invoke a method call in the #EventLoop.
|
||||||
*
|
*
|
||||||
* This class is thread-safe.
|
* 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;
|
typedef BoundMethod<void() noexcept> Callback;
|
||||||
const Callback callback;
|
const Callback callback;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DeferEvent(EventLoop &_loop, Callback _callback)
|
DeferEvent(EventLoop &_loop, Callback _callback) noexcept
|
||||||
:DeferredMonitor(_loop), callback(_callback) {}
|
:loop(_loop), callback(_callback) {}
|
||||||
|
|
||||||
using DeferredMonitor::GetEventLoop;
|
~DeferEvent() noexcept {
|
||||||
using DeferredMonitor::Schedule;
|
Cancel();
|
||||||
using DeferredMonitor::Cancel;
|
}
|
||||||
|
|
||||||
|
EventLoop &GetEventLoop() noexcept {
|
||||||
|
return loop;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Schedule() noexcept;
|
||||||
|
void Cancel() noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RunDeferred() noexcept override {
|
bool IsPending() const noexcept {
|
||||||
|
return list_hook.is_linked();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunDeferred() noexcept {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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
|
|
@ -21,7 +21,7 @@
|
|||||||
#include "Loop.hxx"
|
#include "Loop.hxx"
|
||||||
#include "SocketMonitor.hxx"
|
#include "SocketMonitor.hxx"
|
||||||
#include "IdleMonitor.hxx"
|
#include "IdleMonitor.hxx"
|
||||||
#include "DeferredMonitor.hxx"
|
#include "DeferEvent.hxx"
|
||||||
#include "util/ScopeExit.hxx"
|
#include "util/ScopeExit.hxx"
|
||||||
|
|
||||||
EventLoop::EventLoop(ThreadId _thread)
|
EventLoop::EventLoop(ThreadId _thread)
|
||||||
@ -163,7 +163,7 @@ EventLoop::Run()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* try to handle DeferredMonitors without WakeFD
|
/* try to handle DeferEvents without WakeFD
|
||||||
overhead */
|
overhead */
|
||||||
{
|
{
|
||||||
const std::lock_guard<Mutex> lock(mutex);
|
const std::lock_guard<Mutex> lock(mutex);
|
||||||
@ -211,7 +211,7 @@ EventLoop::Run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
EventLoop::AddDeferred(DeferredMonitor &d)
|
EventLoop::AddDeferred(DeferEvent &d) noexcept
|
||||||
{
|
{
|
||||||
bool must_wake;
|
bool must_wake;
|
||||||
|
|
||||||
@ -221,7 +221,7 @@ EventLoop::AddDeferred(DeferredMonitor &d)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* we don't need to wake up the EventLoop if another
|
/* 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();
|
must_wake = !busy && deferred.empty();
|
||||||
|
|
||||||
deferred.push_back(d);
|
deferred.push_back(d);
|
||||||
@ -233,7 +233,7 @@ EventLoop::AddDeferred(DeferredMonitor &d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
EventLoop::RemoveDeferred(DeferredMonitor &d)
|
EventLoop::RemoveDeferred(DeferEvent &d) noexcept
|
||||||
{
|
{
|
||||||
const std::lock_guard<Mutex> protect(mutex);
|
const std::lock_guard<Mutex> protect(mutex);
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ void
|
|||||||
EventLoop::HandleDeferred()
|
EventLoop::HandleDeferred()
|
||||||
{
|
{
|
||||||
while (!deferred.empty() && !quit) {
|
while (!deferred.empty() && !quit) {
|
||||||
DeferredMonitor &m = deferred.front();
|
auto &m = deferred.front();
|
||||||
assert(m.IsPending());
|
assert(m.IsPending());
|
||||||
|
|
||||||
deferred.pop_front();
|
deferred.pop_front();
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
#include "SocketMonitor.hxx"
|
#include "SocketMonitor.hxx"
|
||||||
#include "TimerEvent.hxx"
|
#include "TimerEvent.hxx"
|
||||||
#include "IdleMonitor.hxx"
|
#include "IdleMonitor.hxx"
|
||||||
#include "DeferredMonitor.hxx"
|
#include "DeferEvent.hxx"
|
||||||
|
|
||||||
#include <boost/intrusive/set.hpp>
|
#include <boost/intrusive/set.hpp>
|
||||||
#include <boost/intrusive/list.hpp>
|
#include <boost/intrusive/list.hpp>
|
||||||
@ -76,10 +76,10 @@ class EventLoop final : SocketMonitor
|
|||||||
|
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
|
|
||||||
typedef boost::intrusive::list<DeferredMonitor,
|
typedef boost::intrusive::list<DeferEvent,
|
||||||
boost::intrusive::member_hook<DeferredMonitor,
|
boost::intrusive::member_hook<DeferEvent,
|
||||||
DeferredMonitor::ListHook,
|
DeferEvent::ListHook,
|
||||||
&DeferredMonitor::list_hook>,
|
&DeferEvent::list_hook>,
|
||||||
boost::intrusive::constant_time_size<false>> DeferredList;
|
boost::intrusive::constant_time_size<false>> DeferredList;
|
||||||
DeferredList deferred;
|
DeferredList deferred;
|
||||||
|
|
||||||
@ -160,19 +160,19 @@ public:
|
|||||||
void CancelTimer(TimerEvent &t);
|
void CancelTimer(TimerEvent &t);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule a call to DeferredMonitor::RunDeferred().
|
* Schedule a call to DeferEvent::RunDeferred().
|
||||||
*
|
*
|
||||||
* This method is thread-safe.
|
* 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.
|
* However after returning, the call may still be running.
|
||||||
*
|
*
|
||||||
* This method is thread-safe.
|
* This method is thread-safe.
|
||||||
*/
|
*/
|
||||||
void RemoveDeferred(DeferredMonitor &d);
|
void RemoveDeferred(DeferEvent &d) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main function of this class. It will loop until
|
* The main function of this class. It will loop until
|
||||||
@ -182,7 +182,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* Invoke all pending DeferredMonitors.
|
* Invoke all pending DeferEvents.
|
||||||
*
|
*
|
||||||
* Caller must lock the mutex.
|
* Caller must lock the mutex.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user