Skip to content

Commit 00a352f

Browse files
committed
event/Loop: explicit io_uring initialization
Log the io_uring initialization error at MPD startup.
1 parent 63cc07b commit 00a352f

File tree

4 files changed

+79
-34
lines changed

4 files changed

+79
-34
lines changed

src/event/Loop.cxx

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
#ifdef HAVE_URING
1515
#include "uring/Manager.hxx"
16-
#include "util/PrintException.hxx"
17-
#include <stdio.h>
1816
#endif
1917

2018
EventLoop::EventLoop(
@@ -53,26 +51,46 @@ EventLoop::~EventLoop() noexcept
5351
assert(ready_sockets.empty());
5452
}
5553

54+
void
55+
EventLoop::SetVolatile() noexcept
56+
{
57+
#ifdef HAVE_URING
58+
if (uring)
59+
uring->SetVolatile();
60+
#endif
61+
}
62+
5663
#ifdef HAVE_URING
5764

65+
void
66+
EventLoop::EnableUring(unsigned entries, unsigned flags)
67+
{
68+
assert(!uring);
69+
70+
uring = std::make_unique<Uring::Manager>(*this, entries, flags);
71+
}
72+
73+
void
74+
EventLoop::EnableUring(unsigned entries, struct io_uring_params &params)
75+
{
76+
assert(!uring);
77+
78+
uring = std::make_unique<Uring::Manager>(*this, entries, params);
79+
}
80+
81+
void
82+
EventLoop::DisableUring() noexcept
83+
{
84+
uring.reset();
85+
}
86+
5887
Uring::Queue *
5988
EventLoop::GetUring() noexcept
6089
{
61-
if (!uring_initialized) {
62-
uring_initialized = true;
63-
try {
64-
uring = std::make_unique<Uring::Manager>(*this, 1024,
65-
IORING_SETUP_SINGLE_ISSUER);
66-
} catch (...) {
67-
fprintf(stderr, "Failed to initialize io_uring: ");
68-
PrintException(std::current_exception());
69-
}
70-
}
71-
7290
return uring.get();
7391
}
7492

75-
#endif
93+
#endif // HAVE_URING
7694

7795
bool
7896
EventLoop::AddFD(int fd, unsigned events, SocketEvent &event) noexcept
@@ -280,17 +298,6 @@ EventLoop::Run() noexcept
280298
wake_event.Schedule(SocketEvent::READ);
281299
#endif
282300

283-
#ifdef HAVE_URING
284-
AtScopeExit(this) {
285-
/* make sure that the Uring::Manager gets destructed
286-
from within the EventThread, or else its
287-
destruction in another thread will cause assertion
288-
failures */
289-
uring.reset();
290-
uring_initialized = false;
291-
};
292-
#endif
293-
294301
#ifdef HAVE_THREADED_EVENT_LOOP
295302
AtScopeExit(this) {
296303
wake_event.Cancel();

src/event/Loop.hxx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "io/uring/Features.h"
2828
#ifdef HAVE_URING
2929
#include <memory>
30+
struct io_uring_params;
3031
namespace Uring { class Queue; class Manager; }
3132
#endif
3233

@@ -133,10 +134,6 @@ class EventLoop final
133134
bool busy = true;
134135
#endif
135136

136-
#ifdef HAVE_URING
137-
bool uring_initialized = false;
138-
#endif
139-
140137
ClockCache<std::chrono::steady_clock> steady_clock_cache;
141138

142139
public:
@@ -185,8 +182,27 @@ public:
185182
steady_clock_cache.flush();
186183
}
187184

185+
void SetVolatile() noexcept;
186+
188187
#ifdef HAVE_URING
189-
[[gnu::pure]]
188+
/**
189+
* Try to enable io_uring support. If this method succeeds,
190+
* GetUring() can be used to obtain a pointer to the queue
191+
* instance.
192+
*
193+
* Throws on error.
194+
*/
195+
void EnableUring(unsigned entries, unsigned flags);
196+
void EnableUring(unsigned entries, struct io_uring_params &params);
197+
198+
void DisableUring() noexcept;
199+
200+
/**
201+
* Returns a pointer to the io_uring queue instance or nullptr
202+
* if io_uring support is not available (or was not enabled
203+
* using EnableUring()).
204+
*/
205+
[[nodiscard]] [[gnu::const]]
190206
Uring::Queue *GetUring() noexcept;
191207
#endif
192208

src/event/Thread.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include "util/Domain.hxx"
1010
#include "Log.hxx"
1111

12+
#ifdef HAVE_URING
13+
#include "util/ScopeExit.hxx"
14+
#include <liburing.h>
15+
#endif
16+
1217
static constexpr Domain event_domain("event");
1318

1419
void
@@ -51,7 +56,27 @@ EventThread::Run() noexcept
5156
"RTIOThread could not get realtime scheduling, continuing anyway: {}",
5257
std::current_exception());
5358
}
59+
} else {
60+
#ifdef HAVE_URING
61+
try {
62+
event_loop.EnableUring(1024, IORING_SETUP_SINGLE_ISSUER);
63+
} catch (...) {
64+
FmtInfo(event_domain,
65+
"Failed to initialize io_uring: {}",
66+
std::current_exception());
67+
}
68+
#endif
5469
}
5570

71+
#ifdef HAVE_URING
72+
AtScopeExit(this) {
73+
/* make sure that the Uring::Manager gets destructed
74+
from within the EventThread, or else its
75+
destruction in another thread will cause assertion
76+
failures */
77+
event_loop.DisableUring();
78+
};
79+
#endif
80+
5681
event_loop.Run();
5782
}

src/event/Thread.hxx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22
// Copyright The Music Player Daemon Project
33

4-
#ifndef MPD_EVENT_THREAD_HXX
5-
#define MPD_EVENT_THREAD_HXX
4+
#pragma once
65

76
#include "Loop.hxx"
87
#include "thread/Thread.hxx"
@@ -36,5 +35,3 @@ public:
3635
private:
3736
void Run() noexcept;
3837
};
39-
40-
#endif /* MAIN_NOTIFY_H */

0 commit comments

Comments
 (0)