Make Linux dynamically look up pthread_setname_np() for older glibc compat.

Cleaned up the lookup code to make Mac OS X use most of the same code.

--HG--
extra : rebase_source : 7a7fb4e0f49ba242e69567ed68c0378794845118
This commit is contained in:
Ryan C. Gordon 2012-11-03 12:06:27 -04:00
parent 4256fc36c0
commit 20144052f8

View file

@ -28,21 +28,19 @@
#endif #endif
#include <signal.h> #include <signal.h>
#ifdef __LINUX__ #ifdef __LINUX__
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
#if HAVE_PTHREAD_SETNAME_NP
extern int pthread_setname_np (pthread_t __target_thread, __const char *__name) __THROW __nonnull ((2));
#endif
#endif // __LINUX__ #endif // __LINUX__
#if ( (__MACOSX__ && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)) || \ #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
(__IPHONEOS__ && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)) )
#define NEED_DYNAMIC_PTHREAD_SETNAME_NP
#include <dlfcn.h> #include <dlfcn.h>
#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT NULL
#endif
#endif #endif
#include "SDL_platform.h" #include "SDL_platform.h"
@ -53,6 +51,8 @@ extern int pthread_setname_np (pthread_t __target_thread, __const char *__name)
#include "../../core/android/SDL_android.h" #include "../../core/android/SDL_android.h"
#endif #endif
#include "SDL_assert.h"
/* List of signals to mask in the subthreads */ /* List of signals to mask in the subthreads */
static const int sig_list[] = { static const int sig_list[] = {
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH, SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
@ -70,11 +70,31 @@ RunThread(void *data)
return NULL; return NULL;
} }
#if defined(__MACOSX__) || defined(__IPHONEOS__)
static SDL_bool checked_setname = SDL_FALSE;
static int (*pthread_setname_np)(const char*) = NULL;
#elif defined(__LINUX__)
static SDL_bool checked_setname = SDL_FALSE;
static int (*pthread_setname_np)(pthread_t, const char*) = NULL;
#endif
int int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args) SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
{ {
pthread_attr_t type; pthread_attr_t type;
/* do this here before any threads exist, so there's no race condition. */
#if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
if (!checked_setname) {
void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
#if defined(__MACOSX__) || defined(__IPHONEOS__)
pthread_setname_np = (int(*)(const char*)) fn;
#elif defined(__LINUX__)
pthread_setname_np = (int(*)(pthread_t, const char*)) fn;
#endif
checked_setname = SDL_TRUE;
}
#endif
/* Set the thread attributes */ /* Set the thread attributes */
if (pthread_attr_init(&type) != 0) { if (pthread_attr_init(&type) != 0) {
SDL_SetError("Couldn't initialize pthread attributes"); SDL_SetError("Couldn't initialize pthread attributes");
@ -98,16 +118,20 @@ SDL_SYS_SetupThread(const char *name)
sigset_t mask; sigset_t mask;
if (name != NULL) { if (name != NULL) {
#ifdef NEED_DYNAMIC_PTHREAD_SETNAME_NP #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
int (*dynamic_pthread_setname_np)(const char*); SDL_assert(checked_setname);
*(void**)(&dynamic_pthread_setname_np) = dlsym(RTLD_DEFAULT, "pthread_setname_np"); if (pthread_setname_np) {
if ( dynamic_pthread_setname_np ) #if defined(__MACOSX__) || defined(__IPHONEOS__)
dynamic_pthread_setname_np( name ); pthread_setname_np(name);
#elif HAVE_PTHREAD_SETNAME_NP #elif defined(__LINUX__)
pthread_setname_np(pthread_self(), name); pthread_setname_np(pthread_self(), name);
#elif HAVE_PTHREAD_SET_NAME_NP #endif
pthread_set_name_np(pthread_self(), name); }
#endif #elif HAVE_PTHREAD_SETNAME_NP
pthread_setname_np(pthread_self(), name);
#elif HAVE_PTHREAD_SET_NAME_NP
pthread_set_name_np(pthread_self(), name);
#endif
} }
/* Mask asynchronous signals for this thread */ /* Mask asynchronous signals for this thread */