From 4734af747b3f7c4560cbf7757f4d857445382f01 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Tue, 14 Jan 2014 09:59:04 +0100
Subject: [PATCH] OutputThread: use real-time priority

---
 NEWS                 |  1 +
 src/OutputThread.cxx |  3 +++
 src/thread/Util.hxx  | 13 +++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/NEWS b/NEWS
index 3134095e3..335d1fe66 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,7 @@ ver 0.19 (not yet released)
   - shine: new encoder plugin
 * new resampler option using libsoxr
 * the update thread runs at "idle" priority
+* the output thread runs at "real-time" priority
 
 ver 0.18.7 (2013/01/13)
 * playlist
diff --git a/src/OutputThread.cxx b/src/OutputThread.cxx
index 1b5a44e9d..b56e7f1ca 100644
--- a/src/OutputThread.cxx
+++ b/src/OutputThread.cxx
@@ -30,6 +30,7 @@
 #include "PlayerControl.hxx"
 #include "MusicPipe.hxx"
 #include "MusicChunk.hxx"
+#include "thread/Util.hxx"
 #include "system/FatalError.hxx"
 #include "util/Error.hxx"
 #include "Log.hxx"
@@ -578,6 +579,8 @@ audio_output_task(void *arg)
 {
 	struct audio_output *ao = (struct audio_output *)arg;
 
+	SetThreadRealtime();
+
 	ao->mutex.lock();
 
 	while (1) {
diff --git a/src/thread/Util.hxx b/src/thread/Util.hxx
index fe36468c3..c09fef532 100644
--- a/src/thread/Util.hxx
+++ b/src/thread/Util.hxx
@@ -69,4 +69,17 @@ SetThreadIdlePriority()
 #endif
 };
 
+/**
+ * Raise the current thread's priority to "real-time" (very high).
+ */
+static inline void
+SetThreadRealtime()
+{
+#ifdef __linux__
+	struct sched_param sched_param;
+	sched_param.sched_priority = 50;
+	sched_setscheduler(0, SCHED_FIFO, &sched_param);
+#endif
+};
+
 #endif