src/thread/pthread/SDL_systhread.c
changeset 6640 44a2e00e7c66
parent 6605 59132ac5df74
child 6641 45187a87d35b
equal deleted inserted replaced
6639:c7e81fae23c4 6640:44a2e00e7c66
    26 #if HAVE_PTHREAD_NP_H
    26 #if HAVE_PTHREAD_NP_H
    27 #include <pthread_np.h>
    27 #include <pthread_np.h>
    28 #endif
    28 #endif
    29 
    29 
    30 #include <signal.h>
    30 #include <signal.h>
       
    31 
    31 #ifdef __LINUX__
    32 #ifdef __LINUX__
    32 #include <sys/time.h>
    33 #include <sys/time.h>
    33 #include <sys/resource.h>
    34 #include <sys/resource.h>
    34 #include <sys/syscall.h>
    35 #include <sys/syscall.h>
    35 #include <unistd.h>
    36 #include <unistd.h>
    36 
       
    37 #if HAVE_PTHREAD_SETNAME_NP
       
    38 extern int pthread_setname_np (pthread_t __target_thread, __const char *__name) __THROW __nonnull ((2));
       
    39 #endif
       
    40 #endif // __LINUX__
    37 #endif // __LINUX__
    41 
    38 
    42 #if ( (__MACOSX__ && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)) || \
    39 #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
    43       (__IPHONEOS__ && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)) )
       
    44 #define NEED_DYNAMIC_PTHREAD_SETNAME_NP
       
    45 #include <dlfcn.h>
    40 #include <dlfcn.h>
       
    41 #ifndef RTLD_DEFAULT
       
    42 #define RTLD_DEFAULT NULL
       
    43 #endif
    46 #endif
    44 #endif
    47 
    45 
    48 #include "SDL_platform.h"
    46 #include "SDL_platform.h"
    49 #include "SDL_thread.h"
    47 #include "SDL_thread.h"
    50 #include "../SDL_thread_c.h"
    48 #include "../SDL_thread_c.h"
    51 #include "../SDL_systhread.h"
    49 #include "../SDL_systhread.h"
    52 #ifdef __ANDROID__
    50 #ifdef __ANDROID__
    53 #include "../../core/android/SDL_android.h"
    51 #include "../../core/android/SDL_android.h"
    54 #endif
    52 #endif
    55 
    53 
       
    54 #include "SDL_assert.h"
       
    55 
    56 /* List of signals to mask in the subthreads */
    56 /* List of signals to mask in the subthreads */
    57 static const int sig_list[] = {
    57 static const int sig_list[] = {
    58     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    58     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    59     SIGVTALRM, SIGPROF, 0
    59     SIGVTALRM, SIGPROF, 0
    60 };
    60 };
    68 #endif
    68 #endif
    69     SDL_RunThread(data);
    69     SDL_RunThread(data);
    70     return NULL;
    70     return NULL;
    71 }
    71 }
    72 
    72 
       
    73 #if defined(__MACOSX__) || defined(__IPHONEOS__)
       
    74 static SDL_bool checked_setname = SDL_FALSE;
       
    75 static int (*pthread_setname_np)(const char*) = NULL;
       
    76 #elif defined(__LINUX__)
       
    77 static SDL_bool checked_setname = SDL_FALSE;
       
    78 static int (*pthread_setname_np)(pthread_t, const char*) = NULL;
       
    79 #endif
    73 int
    80 int
    74 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    81 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    75 {
    82 {
    76     pthread_attr_t type;
    83     pthread_attr_t type;
       
    84 
       
    85     /* do this here before any threads exist, so there's no race condition. */
       
    86     #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
       
    87     if (!checked_setname) {
       
    88         void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
       
    89         #if defined(__MACOSX__) || defined(__IPHONEOS__)
       
    90         pthread_setname_np = (int(*)(const char*)) fn;
       
    91         #elif defined(__LINUX__)
       
    92         pthread_setname_np = (int(*)(pthread_t, const char*)) fn;
       
    93         #endif
       
    94         checked_setname = SDL_TRUE;
       
    95     }
       
    96     #endif
    77 
    97 
    78     /* Set the thread attributes */
    98     /* Set the thread attributes */
    79     if (pthread_attr_init(&type) != 0) {
    99     if (pthread_attr_init(&type) != 0) {
    80         SDL_SetError("Couldn't initialize pthread attributes");
   100         SDL_SetError("Couldn't initialize pthread attributes");
    81         return (-1);
   101         return (-1);
    96 {
   116 {
    97     int i;
   117     int i;
    98     sigset_t mask;
   118     sigset_t mask;
    99 
   119 
   100     if (name != NULL) {
   120     if (name != NULL) {
   101 #ifdef NEED_DYNAMIC_PTHREAD_SETNAME_NP
   121         #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
   102         int (*dynamic_pthread_setname_np)(const char*);
   122         SDL_assert(checked_setname);
   103         *(void**)(&dynamic_pthread_setname_np) = dlsym(RTLD_DEFAULT, "pthread_setname_np");
   123         if (pthread_setname_np) {
   104         if ( dynamic_pthread_setname_np )
   124             #if defined(__MACOSX__) || defined(__IPHONEOS__)
   105             dynamic_pthread_setname_np( name );
   125             pthread_setname_np(name);
   106 #elif HAVE_PTHREAD_SETNAME_NP
   126             #elif defined(__LINUX__)
   107         pthread_setname_np(pthread_self(), name);
   127             pthread_setname_np(pthread_self(), name);
   108 #elif HAVE_PTHREAD_SET_NAME_NP
   128             #endif
   109         pthread_set_name_np(pthread_self(), name);
   129         }
   110 #endif
   130         #elif HAVE_PTHREAD_SETNAME_NP
       
   131             pthread_setname_np(pthread_self(), name);
       
   132         #elif HAVE_PTHREAD_SET_NAME_NP
       
   133             pthread_set_name_np(pthread_self(), name);
       
   134         #endif
   111     }
   135     }
   112 
   136 
   113     /* Mask asynchronous signals for this thread */
   137     /* Mask asynchronous signals for this thread */
   114     sigemptyset(&mask);
   138     sigemptyset(&mask);
   115     for (i = 0; sig_list[i]; ++i) {
   139     for (i = 0; sig_list[i]; ++i) {