diff --git a/Makefile.am b/Makefile.am index c367ca455..e0fcde553 100644 --- a/Makefile.am +++ b/Makefile.am @@ -285,6 +285,7 @@ libutil_a_SOURCES = \ # Multi-threading library libthread_a_SOURCES = \ + src/thread/Util.hxx \ src/thread/Mutex.hxx \ src/thread/PosixMutex.hxx \ src/thread/CriticalSection.hxx \ diff --git a/NEWS b/NEWS index a3be05562..3134095e3 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,7 @@ ver 0.19 (not yet released) * encoder: - shine: new encoder plugin * new resampler option using libsoxr +* the update thread runs at "idle" priority ver 0.18.7 (2013/01/13) * playlist diff --git a/src/UpdateGlue.cxx b/src/UpdateGlue.cxx index 72938018a..29e5f3ca7 100644 --- a/src/UpdateGlue.cxx +++ b/src/UpdateGlue.cxx @@ -34,6 +34,7 @@ #include "system/FatalError.hxx" #include "thread/Id.hxx" #include "thread/Thread.hxx" +#include "thread/Util.hxx" #include @@ -68,6 +69,8 @@ update_task(gcc_unused void *ctx) else LogDebug(update_domain, "starting"); + SetThreadIdlePriority(); + modified = update_walk(next.path_utf8.c_str(), next.discard); if (modified || !db_exists()) { diff --git a/src/thread/Util.hxx b/src/thread/Util.hxx new file mode 100644 index 000000000..fe36468c3 --- /dev/null +++ b/src/thread/Util.hxx @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2003-2014 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. + */ + +#ifndef MPD_THREAD_UTIL_HXX +#define MPD_THREAD_UTIL_HXX + +#ifdef __linux__ +#include +#include +#include +#elif defined(WIN32) +#include +#endif + +#ifdef __linux__ + +static int +ioprio_set(int which, int who, int ioprio) +{ + return syscall(SYS_ioprio_set, which, who, ioprio); +} + +static void +ioprio_set_idle() +{ + static constexpr int _IOPRIO_WHO_PROCESS = 1; + static constexpr int _IOPRIO_CLASS_IDLE = 3; + static constexpr int _IOPRIO_CLASS_SHIFT = 13; + static constexpr int _IOPRIO_IDLE = + (_IOPRIO_CLASS_IDLE << _IOPRIO_CLASS_SHIFT) | 7; + + ioprio_set(_IOPRIO_WHO_PROCESS, 0, _IOPRIO_IDLE); +} + +#endif + +/** + * Lower the current thread's priority to "idle" (very low). + */ +static inline void +SetThreadIdlePriority() +{ +#ifdef __linux__ +#ifdef SCHED_IDLE + static struct sched_param sched_param; + sched_setscheduler(0, SCHED_IDLE, &sched_param); +#endif + + ioprio_set_idle(); + +#elif defined(WIN32) + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); +#endif +}; + +#endif