src/thread/pthread/SDL_systhread.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7978 70ac84e49797
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

The new header will include SDL_config.h, but allows for other global stuff.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "../../SDL_internal.h"
    23 
    24 #include <pthread.h>
    25 
    26 #if HAVE_PTHREAD_NP_H
    27 #include <pthread_np.h>
    28 #endif
    29 
    30 #include <signal.h>
    31 
    32 #ifdef __LINUX__
    33 #include <sys/time.h>
    34 #include <sys/resource.h>
    35 #include <sys/syscall.h>
    36 #include <unistd.h>
    37 #endif /* __LINUX__ */
    38 
    39 #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
    40 #include <dlfcn.h>
    41 #ifndef RTLD_DEFAULT
    42 #define RTLD_DEFAULT NULL
    43 #endif
    44 #endif
    45 
    46 #include "SDL_platform.h"
    47 #include "SDL_thread.h"
    48 #include "../SDL_thread_c.h"
    49 #include "../SDL_systhread.h"
    50 #ifdef __ANDROID__
    51 #include "../../core/android/SDL_android.h"
    52 #endif
    53 
    54 #ifdef __HAIKU__
    55 #include <be/kernel/OS.h>
    56 #endif
    57 
    58 #include "SDL_assert.h"
    59 
    60 /* List of signals to mask in the subthreads */
    61 static const int sig_list[] = {
    62     SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
    63     SIGVTALRM, SIGPROF, 0
    64 };
    65 
    66 static void *
    67 RunThread(void *data)
    68 {
    69 #ifdef __ANDROID__
    70     Android_JNI_SetupThread();
    71 #endif
    72     SDL_RunThread(data);
    73     return NULL;
    74 }
    75 
    76 #if defined(__MACOSX__) || defined(__IPHONEOS__)
    77 static SDL_bool checked_setname = SDL_FALSE;
    78 static int (*ppthread_setname_np)(const char*) = NULL;
    79 #elif defined(__LINUX__)
    80 static SDL_bool checked_setname = SDL_FALSE;
    81 static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
    82 #endif
    83 int
    84 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    85 {
    86     pthread_attr_t type;
    87 
    88     /* do this here before any threads exist, so there's no race condition. */
    89     #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
    90     if (!checked_setname) {
    91         void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
    92         #if defined(__MACOSX__) || defined(__IPHONEOS__)
    93         ppthread_setname_np = (int(*)(const char*)) fn;
    94         #elif defined(__LINUX__)
    95         ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
    96         #endif
    97         checked_setname = SDL_TRUE;
    98     }
    99     #endif
   100 
   101     /* Set the thread attributes */
   102     if (pthread_attr_init(&type) != 0) {
   103         return SDL_SetError("Couldn't initialize pthread attributes");
   104     }
   105     pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
   106 
   107     /* Create the thread and go! */
   108     if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
   109         return SDL_SetError("Not enough resources to create thread");
   110     }
   111 
   112     return 0;
   113 }
   114 
   115 void
   116 SDL_SYS_SetupThread(const char *name)
   117 {
   118     int i;
   119     sigset_t mask;
   120 
   121     if (name != NULL) {
   122         #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
   123         SDL_assert(checked_setname);
   124         if (ppthread_setname_np != NULL) {
   125             #if defined(__MACOSX__) || defined(__IPHONEOS__)
   126             ppthread_setname_np(name);
   127             #elif defined(__LINUX__)
   128             ppthread_setname_np(pthread_self(), name);
   129             #endif
   130         }
   131         #elif HAVE_PTHREAD_SETNAME_NP
   132             pthread_setname_np(pthread_self(), name);
   133         #elif HAVE_PTHREAD_SET_NAME_NP
   134             pthread_set_name_np(pthread_self(), name);
   135         #elif defined(__HAIKU__)
   136             /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
   137             char namebuf[B_OS_NAME_LENGTH];
   138             SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
   139             namebuf[sizeof (namebuf) - 1] = '\0';
   140             rename_thread(find_thread(NULL), namebuf);
   141         #endif
   142     }
   143 
   144     /* Mask asynchronous signals for this thread */
   145     sigemptyset(&mask);
   146     for (i = 0; sig_list[i]; ++i) {
   147         sigaddset(&mask, sig_list[i]);
   148     }
   149     pthread_sigmask(SIG_BLOCK, &mask, 0);
   150 
   151 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   152     /* Allow ourselves to be asynchronously cancelled */
   153     {
   154         int oldstate;
   155         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
   156     }
   157 #endif
   158 }
   159 
   160 SDL_threadID
   161 SDL_ThreadID(void)
   162 {
   163     return ((SDL_threadID) pthread_self());
   164 }
   165 
   166 int
   167 SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
   168 {
   169 #ifdef __LINUX__
   170     int value;
   171 
   172     if (priority == SDL_THREAD_PRIORITY_LOW) {
   173         value = 19;
   174     } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
   175         value = -20;
   176     } else {
   177         value = 0;
   178     }
   179     if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
   180         /* Note that this fails if you're trying to set high priority
   181            and you don't have root permission. BUT DON'T RUN AS ROOT!
   182          */
   183         return SDL_SetError("setpriority() failed");
   184     }
   185     return 0;
   186 #else
   187     struct sched_param sched;
   188     int policy;
   189     pthread_t thread = pthread_self();
   190 
   191     if (pthread_getschedparam(thread, &policy, &sched) < 0) {
   192         return SDL_SetError("pthread_getschedparam() failed");
   193     }
   194     if (priority == SDL_THREAD_PRIORITY_LOW) {
   195         sched.sched_priority = sched_get_priority_min(policy);
   196     } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
   197         sched.sched_priority = sched_get_priority_max(policy);
   198     } else {
   199         int min_priority = sched_get_priority_min(policy);
   200         int max_priority = sched_get_priority_max(policy);
   201         sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
   202     }
   203     if (pthread_setschedparam(thread, policy, &sched) < 0) {
   204         return SDL_SetError("pthread_setschedparam() failed");
   205     }
   206     return 0;
   207 #endif /* linux */
   208 }
   209 
   210 void
   211 SDL_SYS_WaitThread(SDL_Thread * thread)
   212 {
   213     pthread_join(thread->handle, 0);
   214 }
   215 
   216 void
   217 SDL_SYS_DetachThread(SDL_Thread * thread)
   218 {
   219     pthread_detach(thread->handle);
   220 }
   221 
   222 /* vi: set ts=4 sw=4 expandtab: */