src/thread/pthread/SDL_systhread.c
author Philipp Wiesemann
Thu, 30 May 2013 12:23:36 +0200
changeset 7242 96f4133dfafa
parent 7191 75360622e65f
child 7977 5272ae8fccd7
permissions -rw-r--r--
Added deactivated loading of SDL2_net to Java file.
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@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@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) {
icculus@7037
   100
        return SDL_SetError("Couldn't initialize pthread attributes");
slouken@1895
   101
    }
slouken@1895
   102
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
slouken@1459
   103
slouken@1895
   104
    /* Create the thread and go! */
slouken@1895
   105
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
icculus@7037
   106
        return SDL_SetError("Not enough resources to create thread");
slouken@1895
   107
    }
slouken@1459
   108
icculus@7037
   109
    return 0;
slouken@1361
   110
}
slouken@1361
   111
slouken@1895
   112
void
icculus@5969
   113
SDL_SYS_SetupThread(const char *name)
slouken@1361
   114
{
slouken@1895
   115
    int i;
slouken@1895
   116
    sigset_t mask;
slouken@1361
   117
icculus@6000
   118
    if (name != NULL) {
icculus@6640
   119
        #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
icculus@6640
   120
        SDL_assert(checked_setname);
icculus@6641
   121
        if (ppthread_setname_np != NULL) {
icculus@6640
   122
            #if defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@6641
   123
            ppthread_setname_np(name);
icculus@6640
   124
            #elif defined(__LINUX__)
icculus@6641
   125
            ppthread_setname_np(pthread_self(), name);
icculus@6640
   126
            #endif
icculus@6640
   127
        }
icculus@6640
   128
        #elif HAVE_PTHREAD_SETNAME_NP
icculus@6640
   129
            pthread_setname_np(pthread_self(), name);
icculus@6640
   130
        #elif HAVE_PTHREAD_SET_NAME_NP
icculus@6640
   131
            pthread_set_name_np(pthread_self(), name);
icculus@6640
   132
        #endif
icculus@6000
   133
    }
icculus@5969
   134
slouken@1895
   135
    /* Mask asynchronous signals for this thread */
slouken@1895
   136
    sigemptyset(&mask);
slouken@1895
   137
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
   138
        sigaddset(&mask, sig_list[i]);
slouken@1895
   139
    }
slouken@1895
   140
    pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@1361
   141
slouken@1361
   142
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@1895
   143
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
   144
    {
slouken@1895
   145
        int oldstate;
slouken@1895
   146
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@1895
   147
    }
slouken@1361
   148
#endif
slouken@1361
   149
}
slouken@1361
   150
slouken@3578
   151
SDL_threadID
slouken@1895
   152
SDL_ThreadID(void)
slouken@1361
   153
{
slouken@3578
   154
    return ((SDL_threadID) pthread_self());
slouken@1361
   155
}
slouken@1361
   156
slouken@5506
   157
int
slouken@5509
   158
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   159
{
slouken@5513
   160
#ifdef __LINUX__
slouken@5509
   161
    int value;
slouken@5509
   162
slouken@5509
   163
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   164
        value = 19;
slouken@5509
   165
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   166
        value = -20;
slouken@5509
   167
    } else {
slouken@5509
   168
        value = 0;
slouken@5509
   169
    }
slouken@5509
   170
    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
slouken@5510
   171
        /* Note that this fails if you're trying to set high priority
slouken@5510
   172
           and you don't have root permission. BUT DON'T RUN AS ROOT!
slouken@5510
   173
         */
icculus@7037
   174
        return SDL_SetError("setpriority() failed");
slouken@5509
   175
    }
slouken@5509
   176
    return 0;
slouken@5509
   177
#else
slouken@5506
   178
    struct sched_param sched;
slouken@5506
   179
    int policy;
slouken@5509
   180
    pthread_t thread = pthread_self();
slouken@5506
   181
slouken@5509
   182
    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
icculus@7037
   183
        return SDL_SetError("pthread_getschedparam() failed");
slouken@5506
   184
    }
slouken@5506
   185
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5506
   186
        sched.sched_priority = sched_get_priority_min(policy);
slouken@5506
   187
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5506
   188
        sched.sched_priority = sched_get_priority_max(policy);
slouken@5506
   189
    } else {
slouken@5506
   190
        int min_priority = sched_get_priority_min(policy);
slouken@5506
   191
        int max_priority = sched_get_priority_max(policy);
slouken@5509
   192
        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
slouken@5506
   193
    }
slouken@5509
   194
    if (pthread_setschedparam(thread, policy, &sched) < 0) {
icculus@7037
   195
        return SDL_SetError("pthread_setschedparam() failed");
slouken@5506
   196
    }
slouken@5506
   197
    return 0;
slouken@5509
   198
#endif /* linux */
slouken@5506
   199
}
slouken@5506
   200
slouken@1895
   201
void
slouken@1895
   202
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@1361
   203
{
slouken@1895
   204
    pthread_join(thread->handle, 0);
slouken@1361
   205
}
slouken@1361
   206
slouken@1895
   207
/* vi: set ts=4 sw=4 expandtab: */