diff options
Diffstat (limited to 'src/thread')
-rw-r--r-- | src/thread/Cond.hxx | 2 | ||||
-rw-r--r-- | src/thread/Id.hxx | 2 | ||||
-rw-r--r-- | src/thread/Mutex.hxx | 2 | ||||
-rw-r--r-- | src/thread/Name.hxx | 55 | ||||
-rw-r--r-- | src/thread/Slack.hxx | 54 | ||||
-rw-r--r-- | src/thread/Thread.cxx | 2 | ||||
-rw-r--r-- | src/thread/Thread.hxx | 2 | ||||
-rw-r--r-- | src/thread/Util.hxx | 85 |
8 files changed, 199 insertions, 5 deletions
diff --git a/src/thread/Cond.hxx b/src/thread/Cond.hxx index ed663dc9d..93d5deb74 100644 --- a/src/thread/Cond.hxx +++ b/src/thread/Cond.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 diff --git a/src/thread/Id.hxx b/src/thread/Id.hxx index fa1cf2cab..7b10de074 100644 --- a/src/thread/Id.hxx +++ b/src/thread/Id.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 diff --git a/src/thread/Mutex.hxx b/src/thread/Mutex.hxx index 4ed48c972..dbf3fb7fe 100644 --- a/src/thread/Mutex.hxx +++ b/src/thread/Mutex.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 diff --git a/src/thread/Name.hxx b/src/thread/Name.hxx new file mode 100644 index 000000000..f86fc6e42 --- /dev/null +++ b/src/thread/Name.hxx @@ -0,0 +1,55 @@ +/* + * 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_NAME_HXX +#define MPD_THREAD_NAME_HXX + +#ifdef HAVE_PTHREAD_SETNAME_NP +#include <pthread.h> +#include <stdio.h> +#endif + +static inline void +SetThreadName(const char *name) +{ +#ifdef HAVE_PTHREAD_SETNAME_NP +#ifdef __APPLE__ + pthread_setname_np(name); +#else + pthread_setname_np(pthread_self(), name); +#endif +#else + (void)name; +#endif +} + +template<typename... Args> +static inline void +FormatThreadName(const char *fmt, gcc_unused Args&&... args) +{ +#ifdef HAVE_PTHREAD_SETNAME_NP + char buffer[16]; + snprintf(buffer, sizeof(buffer), fmt, args...); + SetThreadName(buffer); +#else + (void)fmt; +#endif +} + +#endif diff --git a/src/thread/Slack.hxx b/src/thread/Slack.hxx new file mode 100644 index 000000000..66b2254a4 --- /dev/null +++ b/src/thread/Slack.hxx @@ -0,0 +1,54 @@ +/* + * 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_SLACK_HXX +#define MPD_THREAD_SLACK_HXX + +#ifdef HAVE_PRCTL +#include <sys/prctl.h> +#endif + +/** + * Set the current thread's timer slack to the specified number of + * nanoseconds (requires Linux 2.6.28). This allows the kernel to + * merge multiple wakeups, which is a trick to save energy. + */ +static inline void +SetThreadTimerSlackNS(unsigned long slack_ns) +{ +#if defined(HAVE_PRCTL) && defined(PR_SET_TIMERSLACK) + prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0); +#else + (void)slack_ns; +#endif +} + +static inline void +SetThreadTimerSlackUS(unsigned long slack_us) +{ + SetThreadTimerSlackNS(slack_us * 1000ul); +} + +static inline void +SetThreadTimerSlackMS(unsigned long slack_ms) +{ + SetThreadTimerSlackNS(slack_ms * 1000000ul); +} + +#endif diff --git a/src/thread/Thread.cxx b/src/thread/Thread.cxx index 67bcf7184..9016680e4 100644 --- a/src/thread/Thread.cxx +++ b/src/thread/Thread.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 diff --git a/src/thread/Thread.hxx b/src/thread/Thread.hxx index d3bd75455..1b1bca0e8 100644 --- a/src/thread/Thread.hxx +++ b/src/thread/Thread.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 diff --git a/src/thread/Util.hxx b/src/thread/Util.hxx new file mode 100644 index 000000000..d71230c20 --- /dev/null +++ b/src/thread/Util.hxx @@ -0,0 +1,85 @@ +/* + * 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 <sched.h> +#include <sys/syscall.h> +#include <unistd.h> +#elif defined(WIN32) +#include <windows.h> +#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 +}; + +/** + * 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_RESET_ON_FORK, &sched_param); +#endif +}; + +#endif |