2014-01-23 10:07:14 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2014-01-23 10:07:14 +01:00
|
|
|
* 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
|
|
|
|
|
2018-11-19 12:49:45 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-01-06 12:08:36 +01:00
|
|
|
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__NetBSD__)
|
2015-01-06 12:04:02 +01:00
|
|
|
# define HAVE_THREAD_NAME
|
2015-01-06 12:04:15 +01:00
|
|
|
# include <pthread.h>
|
2014-03-01 23:47:46 +01:00
|
|
|
#elif defined(HAVE_PRCTL)
|
2015-01-06 12:04:15 +01:00
|
|
|
# include <sys/prctl.h>
|
2015-01-06 12:04:02 +01:00
|
|
|
# ifdef PR_SET_NAME
|
|
|
|
# define HAVE_THREAD_NAME
|
|
|
|
# endif
|
2014-01-23 10:07:14 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-21 20:40:56 +01:00
|
|
|
#ifdef HAVE_THREAD_NAME
|
2018-01-24 12:52:43 +01:00
|
|
|
#include "util/StringFormat.hxx"
|
2015-01-21 20:40:56 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-23 10:07:14 +01:00
|
|
|
static inline void
|
2017-11-26 11:58:53 +01:00
|
|
|
SetThreadName(const char *name) noexcept
|
2014-01-23 10:07:14 +01:00
|
|
|
{
|
2015-01-06 12:08:36 +01:00
|
|
|
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__NetBSD__)
|
|
|
|
/* not using pthread_setname_np() on NetBSD because it
|
|
|
|
requires a non-const pointer argument, which we don't have
|
|
|
|
here */
|
|
|
|
|
2014-01-28 11:31:37 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_setname_np(name);
|
|
|
|
#else
|
2014-01-23 10:07:14 +01:00
|
|
|
pthread_setname_np(pthread_self(), name);
|
2014-01-28 11:31:37 +01:00
|
|
|
#endif
|
2014-03-01 23:47:46 +01:00
|
|
|
#elif defined(HAVE_PRCTL) && defined(PR_SET_NAME)
|
|
|
|
prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
|
2014-01-23 10:07:14 +01:00
|
|
|
#else
|
|
|
|
(void)name;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
static inline void
|
2020-03-12 20:56:11 +01:00
|
|
|
FormatThreadName(const char *fmt, [[maybe_unused]] Args&&... args) noexcept
|
2014-01-23 10:07:14 +01:00
|
|
|
{
|
2015-01-06 12:04:02 +01:00
|
|
|
#ifdef HAVE_THREAD_NAME
|
2018-01-24 12:52:43 +01:00
|
|
|
SetThreadName(StringFormat<16>(fmt, args...));
|
2014-01-23 10:07:14 +01:00
|
|
|
#else
|
|
|
|
(void)fmt;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|