2013-08-08 21:50:07 +02:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2013-08-08 21:50:07 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Loop.hxx"
|
2020-10-08 17:23:03 +02:00
|
|
|
#include "TimerEvent.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
#include "SocketMonitor.hxx"
|
|
|
|
#include "IdleMonitor.hxx"
|
2017-11-12 17:34:06 +01:00
|
|
|
#include "DeferEvent.hxx"
|
2017-08-18 18:08:06 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
|
|
|
#include "UringManager.hxx"
|
|
|
|
#include "util/PrintException.hxx"
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-08 17:23:03 +02:00
|
|
|
constexpr bool
|
|
|
|
EventLoop::TimerCompare::operator()(const TimerEvent &a,
|
|
|
|
const TimerEvent &b) const noexcept
|
|
|
|
{
|
|
|
|
return a.due < b.due;
|
|
|
|
}
|
|
|
|
|
2017-08-18 17:57:59 +02:00
|
|
|
EventLoop::EventLoop(ThreadId _thread)
|
2018-01-29 22:52:13 +01:00
|
|
|
:SocketMonitor(*this),
|
2019-02-05 22:38:45 +01:00
|
|
|
/* if this instance is hosted by an EventThread (no ThreadId
|
|
|
|
known yet) then we're not yet alive until the thread is
|
|
|
|
started; for the main EventLoop instance, we assume it's
|
|
|
|
already alive, because nobody but EventThread will call
|
|
|
|
SetAlive() */
|
|
|
|
alive(!_thread.IsNull()),
|
|
|
|
quit(false),
|
2018-01-29 22:52:13 +01:00
|
|
|
thread(_thread)
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2017-08-10 18:25:22 +02:00
|
|
|
SocketMonitor::Open(SocketDescriptor(wake_fd.Get()));
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::~EventLoop() noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
|
|
|
assert(idle.empty());
|
|
|
|
assert(timers.empty());
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
|
|
|
|
|
|
|
Uring::Queue *
|
|
|
|
EventLoop::GetUring() noexcept
|
|
|
|
{
|
|
|
|
if (!uring_initialized) {
|
2020-10-06 18:58:42 +02:00
|
|
|
uring_initialized = true;
|
2020-05-05 15:18:02 +02:00
|
|
|
try {
|
|
|
|
uring = std::make_unique<Uring::Manager>(*this);
|
|
|
|
} catch (...) {
|
|
|
|
fprintf(stderr, "Failed to initialize io_uring: ");
|
|
|
|
PrintException(std::current_exception());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uring.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
void
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::Break() noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2017-12-22 11:04:24 +01:00
|
|
|
if (quit.exchange(true))
|
2017-02-10 22:13:13 +01:00
|
|
|
return;
|
|
|
|
|
2014-01-04 15:58:51 +01:00
|
|
|
wake_fd.Write();
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
bool
|
2020-10-08 20:53:43 +02:00
|
|
|
EventLoop::Abandon(int _fd) noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2020-05-05 17:08:52 +02:00
|
|
|
assert(!IsAlive() || IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
return poll_group.Abandon(_fd);
|
2013-11-06 18:20:51 +01:00
|
|
|
}
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2013-11-06 18:20:51 +01:00
|
|
|
bool
|
2020-10-08 20:53:43 +02:00
|
|
|
EventLoop::RemoveFD(int _fd) noexcept
|
2013-11-06 18:20:51 +01:00
|
|
|
{
|
2020-05-05 17:08:52 +02:00
|
|
|
assert(!IsAlive() || IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2013-11-28 11:37:23 +01:00
|
|
|
return poll_group.Remove(_fd);
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::AddIdle(IdleMonitor &i) noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(IsInside());
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2017-08-29 15:43:16 +02:00
|
|
|
idle.push_back(i);
|
2014-01-05 01:38:51 +01:00
|
|
|
again = true;
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::RemoveIdle(IdleMonitor &i) noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2017-08-29 15:43:16 +02:00
|
|
|
idle.erase(idle.iterator_to(i));
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-08 19:35:36 +02:00
|
|
|
EventLoop::AddTimer(TimerEvent &t, Event::Duration d) noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
|
2017-08-29 13:34:08 +02:00
|
|
|
t.due = now + d;
|
2017-08-29 14:25:23 +02:00
|
|
|
timers.insert(t);
|
2014-01-05 01:38:51 +01:00
|
|
|
again = true;
|
2013-08-07 22:16:59 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 19:35:36 +02:00
|
|
|
inline Event::Duration
|
2018-01-29 21:45:36 +01:00
|
|
|
EventLoop::HandleTimers() noexcept
|
|
|
|
{
|
2020-10-08 19:35:36 +02:00
|
|
|
Event::Duration timeout;
|
2018-01-29 21:45:36 +01:00
|
|
|
|
|
|
|
while (!quit) {
|
|
|
|
auto i = timers.begin();
|
|
|
|
if (i == timers.end())
|
|
|
|
break;
|
|
|
|
|
|
|
|
TimerEvent &t = *i;
|
|
|
|
timeout = t.due - now;
|
|
|
|
if (timeout > timeout.zero())
|
|
|
|
return timeout;
|
|
|
|
|
|
|
|
timers.erase(i);
|
|
|
|
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:35:36 +02:00
|
|
|
return Event::Duration(-1);
|
2018-01-29 21:45:36 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 23:06:34 +01:00
|
|
|
/**
|
|
|
|
* Convert the given timeout specification to a milliseconds integer,
|
|
|
|
* to be used by functions like poll() and epoll_wait(). Any negative
|
|
|
|
* value (= never times out) is translated to the magic value -1.
|
|
|
|
*/
|
|
|
|
static constexpr int
|
2020-10-08 19:35:36 +02:00
|
|
|
ExportTimeoutMS(Event::Duration timeout)
|
2016-12-27 23:06:34 +01:00
|
|
|
{
|
|
|
|
return timeout >= timeout.zero()
|
2019-12-22 11:58:15 +01:00
|
|
|
/* round up (+1) to avoid unnecessary wakeups */
|
|
|
|
? int(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count()) + 1
|
2016-12-27 23:06:34 +01:00
|
|
|
: -1;
|
|
|
|
}
|
|
|
|
|
2013-08-08 21:50:07 +02:00
|
|
|
void
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::Run() noexcept
|
2013-08-08 21:50:07 +02:00
|
|
|
{
|
2017-08-18 17:57:59 +02:00
|
|
|
if (thread.IsNull())
|
|
|
|
thread = ThreadId::GetCurrent();
|
2013-08-07 22:49:46 +02:00
|
|
|
|
2017-08-18 17:57:59 +02:00
|
|
|
assert(IsInside());
|
2013-08-07 22:16:59 +02:00
|
|
|
assert(!quit);
|
2019-02-05 22:38:45 +01:00
|
|
|
assert(alive);
|
2014-01-05 01:37:22 +01:00
|
|
|
assert(busy);
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2017-08-18 18:08:06 +02:00
|
|
|
SocketMonitor::Schedule(SocketMonitor::READ);
|
2018-01-29 22:52:13 +01:00
|
|
|
AtScopeExit(this) {
|
2020-05-05 15:18:02 +02:00
|
|
|
#ifdef HAVE_URING
|
|
|
|
/* make sure that the Uring::Manager gets destructed
|
|
|
|
from within the EventThread, or else its
|
|
|
|
destruction in another thread will cause assertion
|
|
|
|
failures */
|
|
|
|
uring.reset();
|
|
|
|
uring_initialized = false;
|
|
|
|
#endif
|
|
|
|
|
2018-01-29 22:52:13 +01:00
|
|
|
SocketMonitor::Cancel();
|
|
|
|
};
|
2017-08-18 18:08:06 +02:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
do {
|
2016-12-27 23:06:34 +01:00
|
|
|
now = std::chrono::steady_clock::now();
|
2014-01-05 01:38:51 +01:00
|
|
|
again = false;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/* invoke timers */
|
|
|
|
|
2018-01-29 21:45:36 +01:00
|
|
|
const auto timeout = HandleTimers();
|
|
|
|
if (quit)
|
|
|
|
break;
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/* invoke idle */
|
|
|
|
|
|
|
|
while (!idle.empty()) {
|
2017-08-29 15:43:16 +02:00
|
|
|
IdleMonitor &m = idle.front();
|
2013-08-07 22:16:59 +02:00
|
|
|
idle.pop_front();
|
|
|
|
m.Run();
|
|
|
|
|
|
|
|
if (quit)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:34:06 +01:00
|
|
|
/* try to handle DeferEvents without WakeFD
|
2014-01-05 01:37:22 +01:00
|
|
|
overhead */
|
2017-02-09 21:26:55 +01:00
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
HandleDeferred();
|
|
|
|
busy = false;
|
|
|
|
|
|
|
|
if (again)
|
|
|
|
/* re-evaluate timers because one of
|
|
|
|
the IdleMonitors may have added a
|
|
|
|
new timeout */
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
/* wait for new event */
|
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
PollResult poll_result;
|
2016-12-27 23:06:34 +01:00
|
|
|
poll_group.ReadEvents(poll_result, ExportTimeoutMS(timeout));
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
ready_sockets.clear();
|
|
|
|
for (size_t i = 0; i < poll_result.GetSize(); ++i) {
|
|
|
|
auto &sm = *(SocketMonitor *)poll_result.GetObject(i);
|
|
|
|
sm.SetReadyFlags(poll_result.GetEvents(i));
|
|
|
|
ready_sockets.push_back(sm);
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:06:34 +01:00
|
|
|
now = std::chrono::steady_clock::now();
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2017-02-09 21:26:55 +01:00
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
|
|
|
busy = true;
|
|
|
|
}
|
2014-01-05 01:37:22 +01:00
|
|
|
|
2013-08-07 22:16:59 +02:00
|
|
|
/* invoke sockets */
|
2020-10-08 20:53:43 +02:00
|
|
|
while (!ready_sockets.empty() && !quit) {
|
|
|
|
auto &sm = ready_sockets.front();
|
|
|
|
ready_sockets.pop_front();
|
2013-11-27 12:04:38 +01:00
|
|
|
|
2020-10-08 20:53:43 +02:00
|
|
|
sm.Dispatch();
|
|
|
|
}
|
2013-11-28 11:37:23 +01:00
|
|
|
} while (!quit);
|
2013-08-07 22:49:46 +02:00
|
|
|
|
2014-01-05 01:40:50 +01:00
|
|
|
#ifndef NDEBUG
|
2013-08-10 09:00:04 +02:00
|
|
|
assert(thread.IsInside());
|
2014-01-05 01:40:50 +01:00
|
|
|
#endif
|
2013-08-08 21:50:07 +02:00
|
|
|
}
|
|
|
|
|
2014-01-04 14:56:02 +01:00
|
|
|
void
|
2017-11-12 17:34:06 +01:00
|
|
|
EventLoop::AddDeferred(DeferEvent &d) noexcept
|
2014-01-04 14:56:02 +01:00
|
|
|
{
|
2017-02-09 21:26:55 +01:00
|
|
|
bool must_wake;
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2017-08-29 15:43:16 +02:00
|
|
|
if (d.IsPending())
|
2017-02-09 21:26:55 +01:00
|
|
|
return;
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2017-02-09 21:26:55 +01:00
|
|
|
/* we don't need to wake up the EventLoop if another
|
2017-11-12 17:34:06 +01:00
|
|
|
DeferEvent has already done it */
|
2017-02-09 21:26:55 +01:00
|
|
|
must_wake = !busy && deferred.empty();
|
2014-01-05 01:50:25 +01:00
|
|
|
|
2017-08-29 15:43:16 +02:00
|
|
|
deferred.push_back(d);
|
2017-02-09 21:26:55 +01:00
|
|
|
again = true;
|
|
|
|
}
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2014-01-05 01:50:25 +01:00
|
|
|
if (must_wake)
|
|
|
|
wake_fd.Write();
|
2014-01-04 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-12 17:34:06 +01:00
|
|
|
EventLoop::RemoveDeferred(DeferEvent &d) noexcept
|
2014-01-04 14:56:02 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2017-08-29 16:28:24 +02:00
|
|
|
if (d.IsPending())
|
2017-08-29 15:43:16 +02:00
|
|
|
deferred.erase(deferred.iterator_to(d));
|
2014-01-04 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
void
|
2018-01-29 21:46:07 +01:00
|
|
|
EventLoop::HandleDeferred() noexcept
|
2013-08-07 22:16:59 +02:00
|
|
|
{
|
2014-01-04 14:56:02 +01:00
|
|
|
while (!deferred.empty() && !quit) {
|
2017-11-12 17:34:06 +01:00
|
|
|
auto &m = deferred.front();
|
2017-08-29 15:43:16 +02:00
|
|
|
assert(m.IsPending());
|
2014-01-04 14:56:02 +01:00
|
|
|
|
|
|
|
deferred.pop_front();
|
|
|
|
|
2017-02-09 21:26:55 +01:00
|
|
|
const ScopeUnlock unlock(mutex);
|
2014-01-04 14:56:02 +01:00
|
|
|
m.RunDeferred();
|
|
|
|
}
|
2014-01-05 01:32:59 +01:00
|
|
|
}
|
2014-01-04 14:56:02 +01:00
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
bool
|
2020-03-12 20:56:11 +01:00
|
|
|
EventLoop::OnSocketReady([[maybe_unused]] unsigned flags) noexcept
|
2014-01-05 01:32:59 +01:00
|
|
|
{
|
2014-01-05 01:40:50 +01:00
|
|
|
assert(IsInside());
|
|
|
|
|
2014-01-05 01:32:59 +01:00
|
|
|
wake_fd.Read();
|
|
|
|
|
2017-02-09 21:26:55 +01:00
|
|
|
const std::lock_guard<Mutex> lock(mutex);
|
2014-01-05 01:32:59 +01:00
|
|
|
HandleDeferred();
|
2013-08-07 22:16:59 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|