aboutsummaryrefslogtreecommitdiffstats
path: root/src/thread
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread')
-rw-r--r--src/thread/Cond.hxx40
-rw-r--r--src/thread/CriticalSection.hxx4
-rw-r--r--src/thread/GLibCond.hxx88
-rw-r--r--src/thread/Id.hxx2
-rw-r--r--src/thread/Mutex.hxx40
-rw-r--r--src/thread/Name.hxx59
-rw-r--r--src/thread/PosixCond.hxx9
-rw-r--r--src/thread/PosixMutex.hxx9
-rw-r--r--src/thread/Slack.hxx54
-rw-r--r--src/thread/Thread.cxx11
-rw-r--r--src/thread/Thread.hxx4
-rw-r--r--src/thread/Util.hxx (renamed from src/thread/GLibMutex.hxx)103
-rw-r--r--src/thread/WindowsCond.hxx4
13 files changed, 249 insertions, 178 deletions
diff --git a/src/thread/Cond.hxx b/src/thread/Cond.hxx
index ed663dc9d..a05d1c67d 100644
--- a/src/thread/Cond.hxx
+++ b/src/thread/Cond.hxx
@@ -1,24 +1,34 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
- * http://www.musicpd.org
+ * Copyright (C) 2009-2014 Max Kellermann <max@duempel.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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
*
- * 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.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
*
- * 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.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_COND_HXX
-#define MPD_THREAD_COND_HXX
+#ifndef THREAD_COND_HXX
+#define THREAD_COND_HXX
#ifdef WIN32
diff --git a/src/thread/CriticalSection.hxx b/src/thread/CriticalSection.hxx
index 8bc05b8f5..bb25f6c47 100644
--- a/src/thread/CriticalSection.hxx
+++ b/src/thread/CriticalSection.hxx
@@ -27,8 +27,8 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_CRITICAL_SECTION_HXX
-#define MPD_THREAD_CRITICAL_SECTION_HXX
+#ifndef THREAD_CRITICAL_SECTION_HXX
+#define THREAD_CRITICAL_SECTION_HXX
#include <windows.h>
diff --git a/src/thread/GLibCond.hxx b/src/thread/GLibCond.hxx
deleted file mode 100644
index 9ab08e9fd..000000000
--- a/src/thread/GLibCond.hxx
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright (C) 2013 Max Kellermann <max@duempel.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef MPD_THREAD_GLIB_COND_HXX
-#define MPD_THREAD_GLIB_COND_HXX
-
-#include "GLibMutex.hxx"
-
-/**
- * A wrapper for GCond.
- */
-class GLibCond {
-#if GLIB_CHECK_VERSION(2,32,0)
- GCond cond;
-#else
- GCond *cond;
-#endif
-
-public:
- GLibCond() {
-#if GLIB_CHECK_VERSION(2,32,0)
- g_cond_init(&cond);
-#else
- cond = g_cond_new();
-#endif
- }
-
- ~GLibCond() {
-#if GLIB_CHECK_VERSION(2,32,0)
- g_cond_clear(&cond);
-#else
- g_cond_free(cond);
-#endif
- }
-
- GLibCond(const GLibCond &other) = delete;
- GLibCond &operator=(const GLibCond &other) = delete;
-
-private:
- GCond *GetNative() {
-#if GLIB_CHECK_VERSION(2,32,0)
- return &cond;
-#else
- return cond;
-#endif
- }
-
-public:
- void signal() {
- g_cond_signal(GetNative());
- }
-
- void broadcast() {
- g_cond_broadcast(GetNative());
- }
-
- void wait(GLibMutex &mutex) {
- g_cond_wait(GetNative(), mutex.GetNative());
- }
-};
-
-#endif
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..c17538549 100644
--- a/src/thread/Mutex.hxx
+++ b/src/thread/Mutex.hxx
@@ -1,24 +1,34 @@
/*
- * Copyright (C) 2003-2013 The Music Player Daemon Project
- * http://www.musicpd.org
+ * Copyright (C) 2009-2014 Max Kellermann <max@duempel.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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
*
- * 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.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
*
- * 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.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_MUTEX_HXX
-#define MPD_THREAD_MUTEX_HXX
+#ifndef THREAD_MUTEX_HXX
+#define THREAD_MUTEX_HXX
#ifdef WIN32
diff --git a/src/thread/Name.hxx b/src/thread/Name.hxx
new file mode 100644
index 000000000..284d1e147
--- /dev/null
+++ b/src/thread/Name.hxx
@@ -0,0 +1,59 @@
+/*
+ * 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>
+#elif defined(HAVE_PRCTL)
+#include <sys/prctl.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
+#elif defined(HAVE_PRCTL) && defined(PR_SET_NAME)
+ prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
+#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/PosixCond.hxx b/src/thread/PosixCond.hxx
index 6f98d3ad0..e0d6623dd 100644
--- a/src/thread/PosixCond.hxx
+++ b/src/thread/PosixCond.hxx
@@ -27,8 +27,8 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_POSIX_COND_HXX
-#define MPD_THREAD_POSIX_COND_HXX
+#ifndef THREAD_POSIX_COND_HXX
+#define THREAD_POSIX_COND_HXX
#include "PosixMutex.hxx"
@@ -41,7 +41,10 @@ class PosixCond {
pthread_cond_t cond;
public:
- constexpr PosixCond():cond(PTHREAD_COND_INITIALIZER) {}
+#ifndef __BIONIC__
+ constexpr
+#endif
+ PosixCond():cond(PTHREAD_COND_INITIALIZER) {}
PosixCond(const PosixCond &other) = delete;
PosixCond &operator=(const PosixCond &other) = delete;
diff --git a/src/thread/PosixMutex.hxx b/src/thread/PosixMutex.hxx
index d50764af4..9d1674dd4 100644
--- a/src/thread/PosixMutex.hxx
+++ b/src/thread/PosixMutex.hxx
@@ -27,8 +27,8 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_POSIX_MUTEX_HXX
-#define MPD_THREAD_POSIX_MUTEX_HXX
+#ifndef THREAD_POSIX_MUTEX_HXX
+#define THREAD_POSIX_MUTEX_HXX
#include <pthread.h>
@@ -41,7 +41,10 @@ class PosixMutex {
pthread_mutex_t mutex;
public:
- constexpr PosixMutex():mutex(PTHREAD_MUTEX_INITIALIZER) {}
+#ifndef __BIONIC__
+ constexpr
+#endif
+ PosixMutex():mutex(PTHREAD_MUTEX_INITIALIZER) {}
PosixMutex(const PosixMutex &other) = delete;
PosixMutex &operator=(const PosixMutex &other) = delete;
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..2932d478f 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
@@ -21,6 +21,10 @@
#include "Thread.hxx"
#include "util/Error.hxx"
+#ifdef ANDROID
+#include "java/Global.hxx"
+#endif
+
bool
Thread::Start(void (*_f)(void *ctx), void *_ctx, Error &error)
{
@@ -102,6 +106,11 @@ Thread::ThreadProc(void *ctx)
#endif
thread.f(thread.ctx);
+
+#ifdef ANDROID
+ Java::DetachCurrentThread();
+#endif
+
return nullptr;
}
diff --git a/src/thread/Thread.hxx b/src/thread/Thread.hxx
index d3bd75455..976ff5625 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
@@ -68,7 +68,7 @@ public:
Thread(const Thread &) = delete;
#ifndef NDEBUG
- virtual ~Thread() {
+ ~Thread() {
/* all Thread objects must be destructed manually by calling
Join(), to clean up */
assert(!IsDefined());
diff --git a/src/thread/GLibMutex.hxx b/src/thread/Util.hxx
index 2c666c1ea..ff8dbbe10 100644
--- a/src/thread/GLibMutex.hxx
+++ b/src/thread/Util.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Max Kellermann <max@duempel.org>
+ * Copyright (C) 2014 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -27,64 +27,75 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_GLIB_MUTEX_HXX
-#define MPD_THREAD_GLIB_MUTEX_HXX
+#ifndef THREAD_UTIL_HXX
+#define THREAD_UTIL_HXX
-#include <glib.h>
+#ifdef __linux__
+#include <sched.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#elif defined(WIN32)
+#include <windows.h>
+#endif
-/**
- * A wrapper for GMutex.
- */
-class GLibMutex {
- friend class GLibCond;
+#ifdef __linux__
-#if GLIB_CHECK_VERSION(2,32,0)
- GMutex mutex;
-#else
- GMutex *mutex;
-#endif
+static int
+ioprio_set(int which, int who, int ioprio)
+{
+ return syscall(__NR_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);
+}
-public:
- GLibMutex() {
-#if GLIB_CHECK_VERSION(2,32,0)
- g_mutex_init(&mutex);
-#else
- mutex = g_mutex_new();
#endif
- }
- ~GLibMutex() {
-#if GLIB_CHECK_VERSION(2,32,0)
- g_mutex_clear(&mutex);
-#else
- g_mutex_free(mutex);
+/**
+ * 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
- }
- GLibMutex(const GLibMutex &other) = delete;
- GLibMutex &operator=(const GLibMutex &other) = delete;
+ ioprio_set_idle();
-private:
- GMutex *GetNative() {
-#if GLIB_CHECK_VERSION(2,32,0)
- return &mutex;
-#else
- return mutex;
+#elif defined(WIN32)
+ SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
#endif
- }
+};
-public:
- void lock() {
- g_mutex_lock(GetNative());
- }
+/**
+ * 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;
- bool try_lock() {
- return g_mutex_trylock(GetNative());
- }
+ int policy = SCHED_FIFO;
+#ifdef SCHED_RESET_ON_FORK
+ policy |= SCHED_RESET_ON_FORK;
+#endif
- void unlock() {
- g_mutex_lock(GetNative());
- }
+ sched_setscheduler(0, policy, &sched_param);
+#endif
};
#endif
diff --git a/src/thread/WindowsCond.hxx b/src/thread/WindowsCond.hxx
index c05bc05b2..2ce7271b9 100644
--- a/src/thread/WindowsCond.hxx
+++ b/src/thread/WindowsCond.hxx
@@ -27,8 +27,8 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_WINDOWS_COND_HXX
-#define MPD_THREAD_WINDOWS_COND_HXX
+#ifndef THREAD_WINDOWS_COND_HXX
+#define THREAD_WINDOWS_COND_HXX
#include "CriticalSection.hxx"