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