Commit d29d186d authored by Max Kellermann's avatar Max Kellermann

output/alsa: use a new I/O thread with real-time scheduling

The normal I/O event thread can have a large latency, e.g. when libgnutls loads all TLS CA certificates for a https connect. This makes it unreliable for the ALSA I/O notifications, and causes ring buffer xruns. To avoid interfering with high latency events such as CURL's, we move the ALSA I/O events to a separate I/O thread which also obtains real-time scheduling (if possible). Closes #221
parent 61f2ce67
...@@ -40,7 +40,8 @@ ...@@ -40,7 +40,8 @@
#include <exception> #include <exception>
Instance::Instance() Instance::Instance()
:idle_monitor(event_loop, BIND_THIS_METHOD(OnIdle)) :rtio_thread(true),
idle_monitor(event_loop, BIND_THIS_METHOD(OnIdle))
{ {
} }
......
...@@ -76,8 +76,19 @@ struct Instance final ...@@ -76,8 +76,19 @@ struct Instance final
, public RemoteTagCacheHandler , public RemoteTagCacheHandler
#endif #endif
{ {
/**
* A thread running an #EventLoop for non-blocking (bulk) I/O.
*/
EventThread io_thread; EventThread io_thread;
/**
* Another thread running an #EventLoop for non-blocking
* (real-time) I/O. This is used instead of #io_thread for
* events which require low latency, e.g. for filling hardware
* ring buffers.
*/
EventThread rtio_thread;
MaskMonitor idle_monitor; MaskMonitor idle_monitor;
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
......
...@@ -604,7 +604,7 @@ try { ...@@ -604,7 +604,7 @@ try {
command_init(); command_init();
for (auto &partition : instance->partitions) { for (auto &partition : instance->partitions) {
partition.outputs.Configure(instance->io_thread.GetEventLoop(), partition.outputs.Configure(instance->rtio_thread.GetEventLoop(),
config.replay_gain, config.replay_gain,
partition.pc); partition.pc);
partition.UpdateEffectiveReplayGainMode(); partition.UpdateEffectiveReplayGainMode();
...@@ -625,6 +625,7 @@ try { ...@@ -625,6 +625,7 @@ try {
#endif #endif
instance->io_thread.Start(); instance->io_thread.Start();
instance->rtio_thread.Start();
#ifdef ENABLE_NEIGHBOR_PLUGINS #ifdef ENABLE_NEIGHBOR_PLUGINS
if (instance->neighbors != nullptr) if (instance->neighbors != nullptr)
...@@ -736,6 +737,7 @@ try { ...@@ -736,6 +737,7 @@ try {
archive_plugin_deinit_all(); archive_plugin_deinit_all();
#endif #endif
config_global_finish(); config_global_finish();
instance->rtio_thread.Stop();
instance->io_thread.Stop(); instance->io_thread.Stop();
#ifndef ANDROID #ifndef ANDROID
SignalHandlersFinish(); SignalHandlersFinish();
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "config.h" #include "config.h"
#include "Thread.hxx" #include "Thread.hxx"
#include "thread/Name.hxx" #include "thread/Name.hxx"
#include "thread/Util.hxx"
#include "Log.hxx"
void void
EventThread::Start() EventThread::Start()
...@@ -41,7 +43,16 @@ EventThread::Stop() noexcept ...@@ -41,7 +43,16 @@ EventThread::Stop() noexcept
void void
EventThread::Run() noexcept EventThread::Run() noexcept
{ {
SetThreadName("io"); SetThreadName(realtime ? "rtio" : "io");
if (realtime) {
try {
SetThreadRealtime();
} catch (...) {
LogError(std::current_exception(),
"RTIOThread could not get realtime scheduling, continuing anyway");
}
}
event_loop.Run(); event_loop.Run();
} }
...@@ -32,9 +32,12 @@ class EventThread final { ...@@ -32,9 +32,12 @@ class EventThread final {
Thread thread; Thread thread;
const bool realtime;
public: public:
EventThread() explicit EventThread(bool _realtime=false)
:event_loop(ThreadId::Null()), thread(BIND_THIS_METHOD(Run)) {} :event_loop(ThreadId::Null()), thread(BIND_THIS_METHOD(Run)),
realtime(_realtime) {}
~EventThread() noexcept { ~EventThread() noexcept {
Stop(); Stop();
......
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