2013-01-10 19:13:00 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2013-01-10 19:13:00 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-01 20:18:20 +01:00
|
|
|
#ifndef EVENT_LOOP_HXX
|
|
|
|
#define EVENT_LOOP_HXX
|
2013-01-10 19:13:00 +01:00
|
|
|
|
2020-10-08 19:35:36 +02:00
|
|
|
#include "Chrono.hxx"
|
2021-02-04 21:25:57 +01:00
|
|
|
#include "TimerList.hxx"
|
2020-10-19 14:14:32 +02:00
|
|
|
#include "Backend.hxx"
|
2020-10-14 14:24:16 +02:00
|
|
|
#include "SocketEvent.hxx"
|
2020-10-13 15:50:12 +02:00
|
|
|
#include "event/Features.h"
|
2020-12-01 20:22:52 +01:00
|
|
|
#include "time/ClockCache.hxx"
|
2020-10-18 19:21:47 +02:00
|
|
|
#include "util/IntrusiveList.hxx"
|
2020-10-13 15:50:12 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
|
|
|
#include "WakeFD.hxx"
|
2020-10-08 19:38:26 +02:00
|
|
|
#include "thread/Id.hxx"
|
|
|
|
#include "thread/Mutex.hxx"
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2017-08-29 14:25:23 +02:00
|
|
|
|
|
|
|
#include <boost/intrusive/set.hpp>
|
2017-08-29 15:43:16 +02:00
|
|
|
#include <boost/intrusive/list.hpp>
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2017-12-22 11:04:24 +01:00
|
|
|
#include <atomic>
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2013-08-07 22:49:46 +02:00
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#include "io/uring/Features.h"
|
|
|
|
#ifdef HAVE_URING
|
|
|
|
#include <memory>
|
|
|
|
namespace Uring { class Queue; class Manager; }
|
|
|
|
#endif
|
|
|
|
|
2020-10-14 14:04:57 +02:00
|
|
|
class DeferEvent;
|
2020-12-01 16:25:11 +01:00
|
|
|
class InjectEvent;
|
2020-10-08 17:23:03 +02:00
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* An event loop that polls for events on file/socket descriptors.
|
|
|
|
*
|
|
|
|
* This class is not thread-safe, all methods must be called from the
|
|
|
|
* thread that runs it, except where explicitly documented as
|
|
|
|
* thread-safe.
|
|
|
|
*
|
2020-12-01 17:04:14 +01:00
|
|
|
* @see SocketEvent, MultiSocketMonitor, TimerEvent, DeferEvent, InjectEvent
|
2013-11-24 19:28:04 +01:00
|
|
|
*/
|
2020-10-13 15:50:12 +02:00
|
|
|
class EventLoop final
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2017-08-29 14:25:23 +02:00
|
|
|
WakeFD wake_fd;
|
2021-01-11 20:14:18 +01:00
|
|
|
SocketEvent wake_event{*this, BIND_THIS_METHOD(OnSocketReady), wake_fd.GetSocket()};
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2021-02-04 21:25:57 +01:00
|
|
|
TimerList timers;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-12-01 16:56:58 +01:00
|
|
|
using DeferList = IntrusiveList<DeferEvent>;
|
|
|
|
|
|
|
|
DeferList defer;
|
2020-12-01 16:25:11 +01:00
|
|
|
|
2020-12-01 17:04:14 +01:00
|
|
|
/**
|
|
|
|
* This is like #defer, but gets invoked when the loop is idle.
|
|
|
|
*/
|
|
|
|
DeferList idle;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2013-08-07 22:16:59 +02:00
|
|
|
Mutex mutex;
|
2017-08-29 15:43:16 +02:00
|
|
|
|
2020-12-01 16:25:11 +01:00
|
|
|
using InjectList =
|
|
|
|
boost::intrusive::list<InjectEvent,
|
2020-10-14 14:02:11 +02:00
|
|
|
boost::intrusive::base_hook<boost::intrusive::list_base_hook<>>,
|
2020-10-08 16:58:34 +02:00
|
|
|
boost::intrusive::constant_time_size<false>>;
|
2020-12-01 16:25:11 +01:00
|
|
|
InjectList inject;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-10-18 19:31:58 +02:00
|
|
|
using SocketList = IntrusiveList<SocketEvent>;
|
2020-10-08 20:53:43 +02:00
|
|
|
|
2020-10-18 19:39:21 +02:00
|
|
|
/**
|
|
|
|
* A list of scheduled #SocketEvent instances, without those
|
|
|
|
* which are ready (these are in #ready_sockets).
|
|
|
|
*/
|
|
|
|
SocketList sockets;
|
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
/**
|
2020-10-14 14:24:16 +02:00
|
|
|
* A linked list of #SocketEvent instances which have a
|
2020-10-08 20:53:43 +02:00
|
|
|
* non-zero "ready_flags" field, and need to be dispatched.
|
|
|
|
*/
|
2020-10-18 19:39:21 +02:00
|
|
|
SocketList ready_sockets;
|
2020-10-08 20:53:43 +02:00
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
|
|
|
std::unique_ptr<Uring::Manager> uring;
|
|
|
|
#endif
|
|
|
|
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
|
|
|
/**
|
|
|
|
* A reference to the thread that is currently inside Run().
|
|
|
|
*/
|
|
|
|
ThreadId thread = ThreadId::Null();
|
|
|
|
|
2018-01-29 22:52:13 +01:00
|
|
|
/**
|
2019-02-05 22:38:45 +01:00
|
|
|
* Is this #EventLoop alive, i.e. can events be scheduled?
|
|
|
|
* This is used by BlockingCall() to determine whether
|
|
|
|
* schedule in the #EventThread or to call directly (if
|
|
|
|
* there's no #EventThread yet/anymore).
|
2018-01-29 22:52:13 +01:00
|
|
|
*/
|
2019-02-05 22:38:45 +01:00
|
|
|
bool alive;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2019-02-05 22:38:45 +01:00
|
|
|
|
2020-12-01 20:07:04 +01:00
|
|
|
std::atomic_bool quit{false};
|
2018-01-29 22:52:13 +01:00
|
|
|
|
2014-01-05 01:38:51 +01:00
|
|
|
/**
|
|
|
|
* True when the object has been modified and another check is
|
2020-10-19 14:14:32 +02:00
|
|
|
* necessary before going to sleep via EventPollBackend::ReadEvents().
|
2014-01-05 01:38:51 +01:00
|
|
|
*/
|
|
|
|
bool again;
|
|
|
|
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2014-01-05 01:37:22 +01:00
|
|
|
/**
|
|
|
|
* True when handling callbacks, false when waiting for I/O or
|
|
|
|
* timeout.
|
|
|
|
*
|
|
|
|
* Protected with #mutex.
|
|
|
|
*/
|
2016-12-27 23:08:11 +01:00
|
|
|
bool busy = true;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2014-01-05 01:37:22 +01:00
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
|
|
|
bool uring_initialized = false;
|
|
|
|
#endif
|
|
|
|
|
2020-10-19 14:14:32 +02:00
|
|
|
EventPollBackend poll_backend;
|
2013-01-10 19:13:00 +01:00
|
|
|
|
2020-12-01 20:22:52 +01:00
|
|
|
ClockCache<std::chrono::steady_clock> steady_clock_cache;
|
|
|
|
|
2013-01-10 19:13:00 +01:00
|
|
|
public:
|
2018-08-06 11:49:38 +02:00
|
|
|
/**
|
|
|
|
* Throws on error.
|
|
|
|
*/
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2017-08-18 17:57:59 +02:00
|
|
|
explicit EventLoop(ThreadId _thread);
|
2018-08-06 11:49:38 +02:00
|
|
|
|
2017-08-18 17:57:59 +02:00
|
|
|
EventLoop():EventLoop(ThreadId::GetCurrent()) {}
|
2020-10-13 15:50:12 +02:00
|
|
|
#else
|
|
|
|
EventLoop();
|
|
|
|
#endif
|
2017-08-18 17:57:59 +02:00
|
|
|
|
2018-01-29 21:46:07 +01:00
|
|
|
~EventLoop() noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-12-01 20:18:20 +01:00
|
|
|
EventLoop(const EventLoop &other) = delete;
|
|
|
|
EventLoop &operator=(const EventLoop &other) = delete;
|
|
|
|
|
2020-12-01 20:22:52 +01:00
|
|
|
const auto &GetSteadyClockCache() const noexcept {
|
|
|
|
return steady_clock_cache;
|
|
|
|
}
|
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
2020-12-01 20:22:52 +01:00
|
|
|
* Caching wrapper for std::chrono::steady_clock::now(). The
|
|
|
|
* real clock is queried at most once per event loop
|
|
|
|
* iteration, because it is assumed that the event loop runs
|
|
|
|
* for a negligible duration.
|
2013-11-24 19:28:04 +01:00
|
|
|
*/
|
2021-02-04 21:26:48 +01:00
|
|
|
[[gnu::pure]]
|
2020-12-01 20:22:52 +01:00
|
|
|
const auto &SteadyNow() const noexcept {
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2014-01-05 01:40:50 +01:00
|
|
|
assert(IsInside());
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2020-12-01 20:22:52 +01:00
|
|
|
return steady_clock_cache.now();
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
2021-02-04 21:26:48 +01:00
|
|
|
[[gnu::pure]]
|
2020-05-05 15:18:02 +02:00
|
|
|
Uring::Queue *GetUring() noexcept;
|
|
|
|
#endif
|
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* Stop execution of this #EventLoop at the next chance. This
|
|
|
|
* method is thread-safe and non-blocking: after returning, it
|
|
|
|
* is not guaranteed that the EventLoop has really stopped.
|
|
|
|
*/
|
2018-01-29 21:46:07 +01:00
|
|
|
void Break() noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-10-18 19:43:35 +02:00
|
|
|
bool AddFD(int fd, unsigned events, SocketEvent &event) noexcept;
|
|
|
|
bool ModifyFD(int fd, unsigned events, SocketEvent &event) noexcept;
|
2020-10-18 19:39:21 +02:00
|
|
|
bool RemoveFD(int fd, SocketEvent &event) noexcept;
|
2020-10-14 16:24:55 +02:00
|
|
|
|
2013-11-06 18:20:51 +01:00
|
|
|
/**
|
2020-10-14 14:24:16 +02:00
|
|
|
* Remove the given #SocketEvent after the file descriptor
|
2013-11-06 18:20:51 +01:00
|
|
|
* has been closed. This is like RemoveFD(), but does not
|
|
|
|
* attempt to use #EPOLL_CTL_DEL.
|
|
|
|
*/
|
2020-10-28 14:58:23 +01:00
|
|
|
bool AbandonFD(SocketEvent &event) noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2021-02-04 21:48:27 +01:00
|
|
|
void Insert(TimerEvent &t) noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2014-01-04 14:56:02 +01:00
|
|
|
/**
|
2017-11-12 17:34:06 +01:00
|
|
|
* Schedule a call to DeferEvent::RunDeferred().
|
2014-01-04 14:56:02 +01:00
|
|
|
*/
|
2020-12-01 17:26:39 +01:00
|
|
|
void AddDefer(DeferEvent &d) noexcept;
|
2020-12-01 17:04:14 +01:00
|
|
|
void AddIdle(DeferEvent &e) noexcept;
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2020-12-01 16:25:11 +01:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
|
|
|
/**
|
|
|
|
* Schedule a call to the InjectEvent.
|
2014-01-04 14:56:02 +01:00
|
|
|
*
|
|
|
|
* This method is thread-safe.
|
|
|
|
*/
|
2020-12-01 16:25:11 +01:00
|
|
|
void AddInject(InjectEvent &d) noexcept;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a pending call to the InjectEvent.
|
|
|
|
* However after returning, the call may still be running.
|
|
|
|
*
|
|
|
|
* This method is thread-safe.
|
|
|
|
*/
|
|
|
|
void RemoveInject(InjectEvent &d) noexcept;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* The main function of this class. It will loop until
|
|
|
|
* Break() gets called. Can be called only once.
|
|
|
|
*/
|
2018-01-29 21:46:07 +01:00
|
|
|
void Run() noexcept;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
private:
|
2020-12-01 16:25:11 +01:00
|
|
|
void RunDeferred() noexcept;
|
|
|
|
|
2020-12-01 17:04:14 +01:00
|
|
|
/**
|
|
|
|
* Invoke one "idle" #DeferEvent.
|
|
|
|
*
|
|
|
|
* @return false if there was no such event
|
|
|
|
*/
|
|
|
|
bool RunOneIdle() noexcept;
|
|
|
|
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2014-01-05 01:32:59 +01:00
|
|
|
/**
|
2020-12-01 16:25:11 +01:00
|
|
|
* Invoke all pending InjectEvents.
|
2014-01-05 01:32:59 +01:00
|
|
|
*
|
|
|
|
* Caller must lock the mutex.
|
|
|
|
*/
|
2020-12-01 16:25:11 +01:00
|
|
|
void HandleInject() noexcept;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2014-01-05 01:32:59 +01:00
|
|
|
|
2018-01-29 21:45:36 +01:00
|
|
|
/**
|
|
|
|
* Invoke all expired #TimerEvent instances and return the
|
|
|
|
* duration until the next timer expires. Returns a negative
|
|
|
|
* duration if there is no timeout.
|
|
|
|
*/
|
2020-10-08 19:35:36 +02:00
|
|
|
Event::Duration HandleTimers() noexcept;
|
2018-01-29 21:45:36 +01:00
|
|
|
|
2020-10-15 20:19:01 +02:00
|
|
|
/**
|
|
|
|
* Call epoll_wait() and pass all returned events to
|
|
|
|
* SocketEvent::SetReadyFlags().
|
|
|
|
*
|
|
|
|
* @return true if one or more sockets have become ready
|
|
|
|
*/
|
|
|
|
bool Wait(Event::Duration timeout) noexcept;
|
|
|
|
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2020-10-14 14:24:16 +02:00
|
|
|
void OnSocketReady(unsigned flags) noexcept;
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
public:
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2019-02-05 22:38:45 +01:00
|
|
|
void SetAlive(bool _alive) noexcept {
|
|
|
|
alive = _alive;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAlive() const noexcept {
|
|
|
|
return alive;
|
2018-01-29 22:52:13 +01:00
|
|
|
}
|
2020-10-13 15:50:12 +02:00
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Are we currently running inside this EventLoop's thread?
|
|
|
|
*/
|
2021-02-04 21:26:48 +01:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsInside() const noexcept {
|
2020-10-13 15:50:12 +02:00
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
2013-08-07 22:16:59 +02:00
|
|
|
return thread.IsInside();
|
2020-10-13 15:50:12 +02:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
2013-01-10 19:13:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* MAIN_NOTIFY_H */
|