Use function overloading to handle pthread_setname_np differences

This commit is contained in:
Chris Robinson 2022-02-23 05:56:29 -08:00
parent 598c1aa224
commit 625b0d380a
3 changed files with 26 additions and 59 deletions

View File

@ -566,51 +566,11 @@ if(NOT WIN32)
check_symbol_exists(pthread_setname_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SETNAME_NP) check_symbol_exists(pthread_setname_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SETNAME_NP)
if(NOT HAVE_PTHREAD_SETNAME_NP) if(NOT HAVE_PTHREAD_SETNAME_NP)
check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP) check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" HAVE_PTHREAD_SET_NAME_NP)
else()
check_c_source_compiles("
#include <pthread.h>
#include <pthread_np.h>
int main()
{
pthread_setname_np(\"testname\");
return 0;
}"
PTHREAD_SETNAME_NP_ONE_PARAM
)
check_c_source_compiles("
#include <pthread.h>
#include <pthread_np.h>
int main()
{
pthread_setname_np(pthread_self(), \"%s\", \"testname\");
return 0;
}"
PTHREAD_SETNAME_NP_THREE_PARAMS
)
endif() endif()
else() else()
check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP) check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
if(NOT HAVE_PTHREAD_SETNAME_NP) if(NOT HAVE_PTHREAD_SETNAME_NP)
check_symbol_exists(pthread_set_name_np pthread.h HAVE_PTHREAD_SET_NAME_NP) check_symbol_exists(pthread_set_name_np pthread.h HAVE_PTHREAD_SET_NAME_NP)
else()
check_c_source_compiles("
#include <pthread.h>
int main()
{
pthread_setname_np(\"testname\");
return 0;
}"
PTHREAD_SETNAME_NP_ONE_PARAM
)
check_c_source_compiles("
#include <pthread.h>
int main()
{
pthread_setname_np(pthread_self(), \"%s\", \"testname\");
return 0;
}"
PTHREAD_SETNAME_NP_THREE_PARAMS
)
endif() endif()
endif() endif()
endif() endif()

View File

@ -90,30 +90,43 @@ bool semaphore::try_wait() noexcept
#else #else
#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(HAVE_PTHREAD_SET_NAME_NP)
#include <pthread.h> #include <pthread.h>
#ifdef HAVE_PTHREAD_NP_H #ifdef HAVE_PTHREAD_NP_H
#include <pthread_np.h> #include <pthread_np.h>
#endif #endif
#include <tuple>
namespace {
using setname_t1 = int(*)(const char*);
using setname_t2 = int(*)(pthread_t, const char*);
using setname_t3 = int(*)(pthread_t, const char*, void*);
void setname_caller(setname_t1 func, const char *name)
{ func(name); }
void setname_caller(setname_t2 func, const char *name)
{ func(pthread_self(), name); }
void setname_caller(setname_t3 func, const char *name)
{ func(pthread_self(), "%s", static_cast<void*>(const_cast<char*>(name))); }
} // namespace
void althrd_setname(const char *name) void althrd_setname(const char *name)
{ {
#if defined(HAVE_PTHREAD_SET_NAME_NP) #if defined(HAVE_PTHREAD_SET_NAME_NP)
pthread_set_name_np(pthread_self(), name); setname_caller(pthread_set_name_np, name);
#elif defined(PTHREAD_SETNAME_NP_ONE_PARAM) #elif defined(HAVE_PTHREAD_SETNAME_NP)
pthread_setname_np(name); setname_caller(pthread_setname_np, name);
#elif defined(PTHREAD_SETNAME_NP_THREE_PARAMS)
pthread_setname_np(pthread_self(), "%s", (void*)name);
#else
pthread_setname_np(pthread_self(), name);
#endif #endif
/* Avoid unused function/parameter warnings. */
std::ignore = name;
std::ignore = static_cast<void(*)(setname_t1,const char*)>(&setname_caller);
std::ignore = static_cast<void(*)(setname_t2,const char*)>(&setname_caller);
std::ignore = static_cast<void(*)(setname_t3,const char*)>(&setname_caller);
} }
#else
void althrd_setname(const char*) { }
#endif
#ifdef __APPLE__ #ifdef __APPLE__
namespace al { namespace al {

View File

@ -112,11 +112,5 @@
/* Define if we have pthread_setname_np() */ /* Define if we have pthread_setname_np() */
#cmakedefine HAVE_PTHREAD_SETNAME_NP #cmakedefine HAVE_PTHREAD_SETNAME_NP
/* Define if pthread_setname_np() only accepts one parameter */
#cmakedefine PTHREAD_SETNAME_NP_ONE_PARAM
/* Define if pthread_setname_np() accepts three parameters */
#cmakedefine PTHREAD_SETNAME_NP_THREE_PARAMS
/* Define if we have pthread_set_name_np() */ /* Define if we have pthread_set_name_np() */
#cmakedefine HAVE_PTHREAD_SET_NAME_NP #cmakedefine HAVE_PTHREAD_SET_NAME_NP