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