From 01e8396301e0866abd2ba7c5e2d90b3691d73afb Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Mon, 23 Apr 2018 10:33:44 -0400 Subject: [PATCH] Fix build when pthread_getname_np is not available On Android _setname_ is always available but _getname_ is available only with API level >= 26. https://bugzilla.gnome.org/show_bug.cgi?id=795406 --- config.h.meson | 3 +++ configure.ac | 9 +++++++++ glib/tests/thread.c | 2 +- meson.build | 18 ++++++++++-------- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/config.h.meson b/config.h.meson index 84cafa261..80402c87d 100644 --- a/config.h.meson +++ b/config.h.meson @@ -340,6 +340,9 @@ /* Have function pthread_setname_np with TID as argument */ #mesondefine HAVE_PTHREAD_SETNAME_NP_WITH_TID +/* Have function pthread_getname_np */ +#mesondefine HAVE_PTHREAD_GETNAME_NP + /* Define to 1 if the system has the type `ptrdiff_t'. */ #mesondefine HAVE_PTRDIFF_T diff --git a/configure.ac b/configure.ac index 8dac7cbaf..e568399cc 100644 --- a/configure.ac +++ b/configure.ac @@ -2245,6 +2245,15 @@ AS_IF([ test x"$have_threads" = xposix], [ AC_DEFINE(HAVE_PTHREAD_SETNAME_NP_WITH_TID,1, [Have function pthread_setname_np(pthread_t, const char*)])], [AC_MSG_RESULT(no)]) + AC_MSG_CHECKING(for pthread_getname_np(pthread_t, name, len)) + AC_LINK_IFELSE( + [AC_LANG_PROGRAM( + [#include ], + [[char name[16]; pthread_getname_np(pthread_self(), name, 16);]])], + [AC_MSG_RESULT(yes) + AC_DEFINE(HAVE_PTHREAD_GETNAME_NP,1, + [Have function pthread_getname_np(pthread_t, name, len)])], + [AC_MSG_RESULT(no)]) CPPFLAGS="$glib_save_CPPFLAGS" ]) diff --git a/glib/tests/thread.c b/glib/tests/thread.c index 544783692..a79b9513f 100644 --- a/glib/tests/thread.c +++ b/glib/tests/thread.c @@ -174,7 +174,7 @@ test_thread5 (void) static gpointer thread6_func (gpointer data) { -#ifdef HAVE_PTHREAD_SETNAME_NP_WITH_TID +#if defined (HAVE_PTHREAD_SETNAME_NP_WITH_TID) && defined (HAVE_PTHREAD_GETNAME_NP) char name[16]; pthread_getname_np (pthread_self(), name, 16); diff --git a/meson.build b/meson.build index 8c48bf55a..4d37973cd 100644 --- a/meson.build +++ b/meson.build @@ -1477,6 +1477,11 @@ if host_system == 'windows' glibconfig_conf.set('g_threads_impl_def', 'WIN32') glib_conf.set('THREADS_WIN32', 1) else + pthread_prefix = ''' + #ifndef _GNU_SOURCE + # define _GNU_SOURCE + #endif + #include ''' glibconfig_conf.set('g_threads_impl_def', 'POSIX') glib_conf.set('THREADS_POSIX', 1) if cc.has_header_symbol('pthread.h', 'pthread_attr_setstacksize') @@ -1488,11 +1493,11 @@ else if cc.has_header_symbol('pthread.h', 'pthread_cond_timedwait_relative_np') glib_conf.set('HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP', 1) endif + if cc.has_header_symbol('pthread.h', 'pthread_getname_np', prefix : pthread_prefix) + glib_conf.set('HAVE_PTHREAD_GETNAME_NP', 1) + endif # Assume that pthread_setname_np is available in some form; same as configure - if cc.links('''#ifndef _GNU_SOURCE - # define _GNU_SOURCE - #endif - #include + if cc.links(pthread_prefix + ''' int main() { pthread_setname_np("example"); }''', @@ -1500,10 +1505,7 @@ else dependencies : thread_dep) # macOS and iOS glib_conf.set('HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID', 1) - elif cc.links('''#ifndef _GNU_SOURCE - # define _GNU_SOURCE - #endif - #include + elif cc.links(pthread_prefix + ''' int main() { pthread_setname_np(pthread_self(), "example"); }''',