src/thread/pthread/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6641 45187a87d35b
child 7037 3fedf1f25b94
permissions -rw-r--r--
Happy New Year!
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
slouken@1402
    22
#include "SDL_config.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@6600
    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@6640
    54
#include "SDL_assert.h"
icculus@6640
    55
slouken@1361
    56
/* List of signals to mask in the subthreads */
slouken@3162
    57
static const int sig_list[] = {
slouken@1895
    58
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@1895
    59
    SIGVTALRM, SIGPROF, 0
slouken@1361
    60
};
slouken@1361
    61
slouken@5509
    62
slouken@1895
    63
static void *
slouken@1895
    64
RunThread(void *data)
slouken@1361
    65
{
gabomdq@6354
    66
#ifdef __ANDROID__
gabomdq@6354
    67
    Android_JNI_SetupThread();
gabomdq@6354
    68
#endif
slouken@1895
    69
    SDL_RunThread(data);
icculus@5963
    70
    return NULL;
slouken@1361
    71
}
slouken@1361
    72
icculus@6640
    73
#if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6640
    74
static SDL_bool checked_setname = SDL_FALSE;
icculus@6641
    75
static int (*ppthread_setname_np)(const char*) = NULL;
icculus@6640
    76
#elif defined(__LINUX__)
icculus@6640
    77
static SDL_bool checked_setname = SDL_FALSE;
icculus@6641
    78
static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
icculus@6640
    79
#endif
slouken@1895
    80
int
slouken@1895
    81
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@1361
    82
{
slouken@1895
    83
    pthread_attr_t type;
slouken@1361
    84
icculus@6640
    85
    /* do this here before any threads exist, so there's no race condition. */
icculus@6640
    86
    #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
icculus@6640
    87
    if (!checked_setname) {
icculus@6640
    88
        void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
icculus@6640
    89
        #if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6641
    90
        ppthread_setname_np = (int(*)(const char*)) fn;
icculus@6640
    91
        #elif defined(__LINUX__)
icculus@6641
    92
        ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
icculus@6640
    93
        #endif
icculus@6640
    94
        checked_setname = SDL_TRUE;
icculus@6640
    95
    }
icculus@6640
    96
    #endif
icculus@6640
    97
slouken@1895
    98
    /* Set the thread attributes */
slouken@1895
    99
    if (pthread_attr_init(&type) != 0) {
slouken@1895
   100
        SDL_SetError("Couldn't initialize pthread attributes");
slouken@1895
   101
        return (-1);
slouken@1895
   102
    }
slouken@1895
   103
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
slouken@1361
   104
slouken@1895
   105
    /* Create the thread and go! */
slouken@1895
   106
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
slouken@1895
   107
        SDL_SetError("Not enough resources to create thread");
slouken@1895
   108
        return (-1);
slouken@1895
   109
    }
slouken@1459
   110
slouken@1895
   111
    return (0);
slouken@1361
   112
}
slouken@1361
   113
slouken@1895
   114
void
icculus@5969
   115
SDL_SYS_SetupThread(const char *name)
slouken@1361
   116
{
slouken@1895
   117
    int i;
slouken@1895
   118
    sigset_t mask;
slouken@1361
   119
icculus@6000
   120
    if (name != NULL) {
icculus@6640
   121
        #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
icculus@6640
   122
        SDL_assert(checked_setname);
icculus@6641
   123
        if (ppthread_setname_np != NULL) {
icculus@6640
   124
            #if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6641
   125
            ppthread_setname_np(name);
icculus@6640
   126
            #elif defined(__LINUX__)
icculus@6641
   127
            ppthread_setname_np(pthread_self(), name);
icculus@6640
   128
            #endif
icculus@6640
   129
        }
icculus@6640
   130
        #elif HAVE_PTHREAD_SETNAME_NP
icculus@6640
   131
            pthread_setname_np(pthread_self(), name);
icculus@6640
   132
        #elif HAVE_PTHREAD_SET_NAME_NP
icculus@6640
   133
            pthread_set_name_np(pthread_self(), name);
icculus@6640
   134
        #endif
icculus@6000
   135
    }
icculus@5969
   136
slouken@1895
   137
    /* Mask asynchronous signals for this thread */
slouken@1895
   138
    sigemptyset(&mask);
slouken@1895
   139
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
   140
        sigaddset(&mask, sig_list[i]);
slouken@1895
   141
    }
slouken@1895
   142
    pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@1361
   143
slouken@1361
   144
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@1895
   145
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
   146
    {
slouken@1895
   147
        int oldstate;
slouken@1895
   148
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@1895
   149
    }
slouken@1361
   150
#endif
slouken@1361
   151
}
slouken@1361
   152
slouken@3578
   153
SDL_threadID
slouken@1895
   154
SDL_ThreadID(void)
slouken@1361
   155
{
slouken@3578
   156
    return ((SDL_threadID) pthread_self());
slouken@1361
   157
}
slouken@1361
   158
slouken@5506
   159
int
slouken@5509
   160
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   161
{
slouken@5513
   162
#ifdef __LINUX__
slouken@5509
   163
    int value;
slouken@5509
   164
slouken@5509
   165
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   166
        value = 19;
slouken@5509
   167
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   168
        value = -20;
slouken@5509
   169
    } else {
slouken@5509
   170
        value = 0;
slouken@5509
   171
    }
slouken@5509
   172
    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
slouken@5510
   173
        /* Note that this fails if you're trying to set high priority
slouken@5510
   174
           and you don't have root permission. BUT DON'T RUN AS ROOT!
slouken@5510
   175
         */
slouken@5509
   176
        SDL_SetError("setpriority() failed");
slouken@5509
   177
        return -1;
slouken@5509
   178
    }
slouken@5509
   179
    return 0;
slouken@5509
   180
#else
slouken@5506
   181
    struct sched_param sched;
slouken@5506
   182
    int policy;
slouken@5509
   183
    pthread_t thread = pthread_self();
slouken@5506
   184
slouken@5509
   185
    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
slouken@5506
   186
        SDL_SetError("pthread_getschedparam() failed");
slouken@5506
   187
        return -1;
slouken@5506
   188
    }
slouken@5506
   189
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5506
   190
        sched.sched_priority = sched_get_priority_min(policy);
slouken@5506
   191
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5506
   192
        sched.sched_priority = sched_get_priority_max(policy);
slouken@5506
   193
    } else {
slouken@5506
   194
        int min_priority = sched_get_priority_min(policy);
slouken@5506
   195
        int max_priority = sched_get_priority_max(policy);
slouken@5509
   196
        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
slouken@5506
   197
    }
slouken@5509
   198
    if (pthread_setschedparam(thread, policy, &sched) < 0) {
slouken@5506
   199
        SDL_SetError("pthread_setschedparam() failed");
slouken@5506
   200
        return -1;
slouken@5506
   201
    }
slouken@5506
   202
    return 0;
slouken@5509
   203
#endif /* linux */
slouken@5506
   204
}
slouken@5506
   205
slouken@1895
   206
void
slouken@1895
   207
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@1361
   208
{
slouken@1895
   209
    pthread_join(thread->handle, 0);
slouken@1361
   210
}
slouken@1361
   211
slouken@1895
   212
/* vi: set ts=4 sw=4 expandtab: */