src/thread/pthread/SDL_systhread.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 15 Aug 2015 21:21:29 +0200
changeset 9846 9f0a0c7de41a
parent 9755 e4f04633f291
child 9987 d64783aac765
permissions -rw-r--r--
Removed not needed call to pthread_attr_getstacksize() for SDL_CreateThread().
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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"
icculus@9648
    48
#include "SDL_hints.h"
slouken@1361
    49
#include "../SDL_thread_c.h"
slouken@1361
    50
#include "../SDL_systhread.h"
gabomdq@6354
    51
#ifdef __ANDROID__
gabomdq@6354
    52
#include "../../core/android/SDL_android.h"
gabomdq@6354
    53
#endif
slouken@1361
    54
icculus@7977
    55
#ifdef __HAIKU__
icculus@7977
    56
#include <be/kernel/OS.h>
icculus@7977
    57
#endif
icculus@7977
    58
icculus@6640
    59
#include "SDL_assert.h"
icculus@6640
    60
sbc@8879
    61
#ifndef __NACL__
slouken@1361
    62
/* List of signals to mask in the subthreads */
slouken@3162
    63
static const int sig_list[] = {
slouken@1895
    64
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@1895
    65
    SIGVTALRM, SIGPROF, 0
slouken@1361
    66
};
sbc@8879
    67
#endif
slouken@1361
    68
slouken@1895
    69
static void *
slouken@1895
    70
RunThread(void *data)
slouken@1361
    71
{
gabomdq@6354
    72
#ifdef __ANDROID__
gabomdq@6354
    73
    Android_JNI_SetupThread();
gabomdq@6354
    74
#endif
slouken@1895
    75
    SDL_RunThread(data);
icculus@5963
    76
    return NULL;
slouken@1361
    77
}
slouken@1361
    78
icculus@6640
    79
#if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6640
    80
static SDL_bool checked_setname = SDL_FALSE;
icculus@6641
    81
static int (*ppthread_setname_np)(const char*) = NULL;
icculus@6640
    82
#elif defined(__LINUX__)
icculus@6640
    83
static SDL_bool checked_setname = SDL_FALSE;
icculus@6641
    84
static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
icculus@6640
    85
#endif
slouken@1895
    86
int
slouken@1895
    87
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@1361
    88
{
slouken@1895
    89
    pthread_attr_t type;
icculus@9648
    90
    const char *hint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
slouken@1361
    91
icculus@6640
    92
    /* do this here before any threads exist, so there's no race condition. */
icculus@6640
    93
    #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
icculus@6640
    94
    if (!checked_setname) {
icculus@6640
    95
        void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
icculus@6640
    96
        #if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6641
    97
        ppthread_setname_np = (int(*)(const char*)) fn;
icculus@6640
    98
        #elif defined(__LINUX__)
icculus@6641
    99
        ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
icculus@6640
   100
        #endif
icculus@6640
   101
        checked_setname = SDL_TRUE;
icculus@6640
   102
    }
icculus@6640
   103
    #endif
icculus@6640
   104
slouken@1895
   105
    /* Set the thread attributes */
slouken@1895
   106
    if (pthread_attr_init(&type) != 0) {
icculus@7037
   107
        return SDL_SetError("Couldn't initialize pthread attributes");
slouken@1895
   108
    }
slouken@1895
   109
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
icculus@9648
   110
    
icculus@9648
   111
    /* If the SDL_HINT_THREAD_STACK_SIZE exists and it seems to be a positive number, use it */
icculus@9648
   112
    if (hint && hint[0] >= '0' && hint[0] <= '9') {
icculus@9649
   113
        const size_t stacksize = (size_t) SDL_atoi(hint);
icculus@9649
   114
        if (stacksize > 0) {
icculus@9651
   115
            pthread_attr_setstacksize(&type, stacksize);
icculus@9649
   116
        }
icculus@9648
   117
    }
slouken@1361
   118
slouken@1895
   119
    /* Create the thread and go! */
slouken@1895
   120
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
icculus@7037
   121
        return SDL_SetError("Not enough resources to create thread");
slouken@1895
   122
    }
slouken@1459
   123
icculus@7037
   124
    return 0;
slouken@1361
   125
}
slouken@1361
   126
slouken@1895
   127
void
icculus@5969
   128
SDL_SYS_SetupThread(const char *name)
slouken@1361
   129
{
philipp@9755
   130
#if !defined(__ANDROID__) && !defined(__NACL__)
slouken@1895
   131
    int i;
slouken@1895
   132
    sigset_t mask;
philipp@9755
   133
#endif /* !__ANDROID__ && !__NACL__ */
slouken@1361
   134
icculus@6000
   135
    if (name != NULL) {
icculus@6640
   136
        #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
icculus@6640
   137
        SDL_assert(checked_setname);
icculus@6641
   138
        if (ppthread_setname_np != NULL) {
icculus@6640
   139
            #if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6641
   140
            ppthread_setname_np(name);
icculus@6640
   141
            #elif defined(__LINUX__)
icculus@6641
   142
            ppthread_setname_np(pthread_self(), name);
icculus@6640
   143
            #endif
icculus@6640
   144
        }
icculus@6640
   145
        #elif HAVE_PTHREAD_SETNAME_NP
icculus@6640
   146
            pthread_setname_np(pthread_self(), name);
icculus@6640
   147
        #elif HAVE_PTHREAD_SET_NAME_NP
icculus@6640
   148
            pthread_set_name_np(pthread_self(), name);
icculus@7977
   149
        #elif defined(__HAIKU__)
icculus@7977
   150
            /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
icculus@7977
   151
            char namebuf[B_OS_NAME_LENGTH];
icculus@7977
   152
            SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
icculus@7977
   153
            namebuf[sizeof (namebuf) - 1] = '\0';
icculus@7977
   154
            rename_thread(find_thread(NULL), namebuf);
icculus@6640
   155
        #endif
icculus@6000
   156
    }
icculus@5969
   157
gabomdq@8833
   158
   /* NativeClient does not yet support signals.*/
slouken@9752
   159
#if !defined(__ANDROID__) && !defined(__NACL__)
slouken@1895
   160
    /* Mask asynchronous signals for this thread */
slouken@1895
   161
    sigemptyset(&mask);
slouken@1895
   162
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
   163
        sigaddset(&mask, sig_list[i]);
slouken@1895
   164
    }
slouken@1895
   165
    pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@9752
   166
#endif /* !__ANDROID__ && !__NACL__ */
slouken@9752
   167
slouken@1361
   168
slouken@1361
   169
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@1895
   170
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
   171
    {
slouken@1895
   172
        int oldstate;
slouken@1895
   173
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@1895
   174
    }
slouken@1361
   175
#endif
slouken@1361
   176
}
slouken@1361
   177
slouken@3578
   178
SDL_threadID
slouken@1895
   179
SDL_ThreadID(void)
slouken@1361
   180
{
slouken@3578
   181
    return ((SDL_threadID) pthread_self());
slouken@1361
   182
}
slouken@1361
   183
slouken@5506
   184
int
slouken@5509
   185
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   186
{
gabomdq@8833
   187
#if __NACL__ 
gabomdq@8833
   188
    /* FIXME: Setting thread priority does not seem to be supported in NACL */
gabomdq@8833
   189
    return 0;
gabomdq@8833
   190
#elif __LINUX__
slouken@5509
   191
    int value;
slouken@5509
   192
slouken@5509
   193
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   194
        value = 19;
slouken@5509
   195
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   196
        value = -20;
slouken@5509
   197
    } else {
slouken@5509
   198
        value = 0;
slouken@5509
   199
    }
slouken@5509
   200
    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
slouken@5510
   201
        /* Note that this fails if you're trying to set high priority
slouken@5510
   202
           and you don't have root permission. BUT DON'T RUN AS ROOT!
slouken@5510
   203
         */
icculus@7037
   204
        return SDL_SetError("setpriority() failed");
slouken@5509
   205
    }
slouken@5509
   206
    return 0;
slouken@5509
   207
#else
slouken@5506
   208
    struct sched_param sched;
slouken@5506
   209
    int policy;
slouken@5509
   210
    pthread_t thread = pthread_self();
slouken@5506
   211
slouken@5509
   212
    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
icculus@7037
   213
        return SDL_SetError("pthread_getschedparam() failed");
slouken@5506
   214
    }
slouken@5506
   215
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5506
   216
        sched.sched_priority = sched_get_priority_min(policy);
slouken@5506
   217
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5506
   218
        sched.sched_priority = sched_get_priority_max(policy);
slouken@5506
   219
    } else {
slouken@5506
   220
        int min_priority = sched_get_priority_min(policy);
slouken@5506
   221
        int max_priority = sched_get_priority_max(policy);
slouken@5509
   222
        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
slouken@5506
   223
    }
slouken@5509
   224
    if (pthread_setschedparam(thread, policy, &sched) < 0) {
icculus@7037
   225
        return SDL_SetError("pthread_setschedparam() failed");
slouken@5506
   226
    }
slouken@5506
   227
    return 0;
slouken@5509
   228
#endif /* linux */
slouken@5506
   229
}
slouken@5506
   230
slouken@1895
   231
void
slouken@1895
   232
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@1361
   233
{
slouken@1895
   234
    pthread_join(thread->handle, 0);
slouken@1361
   235
}
slouken@1361
   236
icculus@7978
   237
void
icculus@7978
   238
SDL_SYS_DetachThread(SDL_Thread * thread)
icculus@7978
   239
{
icculus@7978
   240
    pthread_detach(thread->handle);
icculus@7978
   241
}
icculus@7978
   242
slouken@1895
   243
/* vi: set ts=4 sw=4 expandtab: */