Commit cdf8ac00 authored by Max Kellermann's avatar Max Kellermann

event/Loop: integrate io_uring support

parent 62d0ceab
...@@ -23,6 +23,12 @@ ...@@ -23,6 +23,12 @@
#include "DeferEvent.hxx" #include "DeferEvent.hxx"
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
#ifdef HAVE_URING
#include "UringManager.hxx"
#include "util/PrintException.hxx"
#include <stdio.h>
#endif
EventLoop::EventLoop(ThreadId _thread) EventLoop::EventLoop(ThreadId _thread)
:SocketMonitor(*this), :SocketMonitor(*this),
/* if this instance is hosted by an EventThread (no ThreadId /* if this instance is hosted by an EventThread (no ThreadId
...@@ -43,6 +49,25 @@ EventLoop::~EventLoop() noexcept ...@@ -43,6 +49,25 @@ EventLoop::~EventLoop() noexcept
assert(timers.empty()); assert(timers.empty());
} }
#ifdef HAVE_URING
Uring::Queue *
EventLoop::GetUring() noexcept
{
if (!uring_initialized) {
try {
uring = std::make_unique<Uring::Manager>(*this);
} catch (...) {
fprintf(stderr, "Failed to initialize io_uring: ");
PrintException(std::current_exception());
}
}
return uring.get();
}
#endif
void void
EventLoop::Break() noexcept EventLoop::Break() noexcept
{ {
...@@ -155,6 +180,15 @@ EventLoop::Run() noexcept ...@@ -155,6 +180,15 @@ EventLoop::Run() noexcept
SocketMonitor::Schedule(SocketMonitor::READ); SocketMonitor::Schedule(SocketMonitor::READ);
AtScopeExit(this) { AtScopeExit(this) {
#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
SocketMonitor::Cancel(); SocketMonitor::Cancel();
}; };
......
...@@ -38,6 +38,12 @@ ...@@ -38,6 +38,12 @@
#include <cassert> #include <cassert>
#include <chrono> #include <chrono>
#include "io/uring/Features.h"
#ifdef HAVE_URING
#include <memory>
namespace Uring { class Queue; class Manager; }
#endif
/** /**
* An event loop that polls for events on file/socket descriptors. * An event loop that polls for events on file/socket descriptors.
* *
...@@ -82,6 +88,10 @@ class EventLoop final : SocketMonitor ...@@ -82,6 +88,10 @@ class EventLoop final : SocketMonitor
boost::intrusive::constant_time_size<false>> DeferredList; boost::intrusive::constant_time_size<false>> DeferredList;
DeferredList deferred; DeferredList deferred;
#ifdef HAVE_URING
std::unique_ptr<Uring::Manager> uring;
#endif
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
/** /**
...@@ -108,6 +118,10 @@ class EventLoop final : SocketMonitor ...@@ -108,6 +118,10 @@ class EventLoop final : SocketMonitor
*/ */
bool busy = true; bool busy = true;
#ifdef HAVE_URING
bool uring_initialized = false;
#endif
PollGroup poll_group; PollGroup poll_group;
PollResult poll_result; PollResult poll_result;
...@@ -135,6 +149,11 @@ public: ...@@ -135,6 +149,11 @@ public:
return now; return now;
} }
#ifdef HAVE_URING
gcc_pure
Uring::Queue *GetUring() noexcept;
#endif
/** /**
* Stop execution of this #EventLoop at the next chance. This * Stop execution of this #EventLoop at the next chance. This
* method is thread-safe and non-blocking: after returning, it * method is thread-safe and non-blocking: after returning, it
......
/*
* Copyright 2003-2020 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.
*/
#include "UringManager.hxx"
#include "util/PrintException.hxx"
namespace Uring {
bool
Manager::OnSocketReady(unsigned) noexcept
{
try {
DispatchCompletions();
return true;
} catch (...) {
PrintException(std::current_exception());
return false;
}
}
void
Manager::OnIdle() noexcept
{
try {
Submit();
} catch (...) {
PrintException(std::current_exception());
}
}
} // namespace Uring
/*
* Copyright 2003-2020 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.
*/
#pragma once
#include "SocketMonitor.hxx"
#include "IdleMonitor.hxx"
#include "io/uring/Queue.hxx"
namespace Uring {
class Manager final : public Queue, SocketMonitor, IdleMonitor {
public:
explicit Manager(EventLoop &event_loop)
:Queue(1024, 0),
SocketMonitor(SocketDescriptor::FromFileDescriptor(GetFileDescriptor()),
event_loop),
IdleMonitor(event_loop)
{
SocketMonitor::ScheduleRead();
}
void Push(struct io_uring_sqe &sqe,
Operation &operation) noexcept override {
AddPending(sqe, operation);
IdleMonitor::Schedule();
}
private:
bool OnSocketReady(unsigned flags) noexcept override;
void OnIdle() noexcept override;
};
} // namespace Uring
event_sources = []
if uring_dep.found()
event_sources += 'UringManager.cxx'
endif
event = static_library( event = static_library(
'event', 'event',
'PollGroupPoll.cxx', 'PollGroupPoll.cxx',
...@@ -15,10 +21,12 @@ event = static_library( ...@@ -15,10 +21,12 @@ event = static_library(
'Call.cxx', 'Call.cxx',
'Thread.cxx', 'Thread.cxx',
'Loop.cxx', 'Loop.cxx',
event_sources,
include_directories: inc, include_directories: inc,
dependencies: [ dependencies: [
boost_dep, boost_dep,
log_dep, log_dep,
uring_dep,
], ],
) )
...@@ -29,5 +37,6 @@ event_dep = declare_dependency( ...@@ -29,5 +37,6 @@ event_dep = declare_dependency(
net_dep, net_dep,
system_dep, system_dep,
boost_dep, boost_dep,
uring_dep,
], ],
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment