src/thread/pthread/SDL_systhread.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 Oct 2011 00:49:52 -0400
changeset 5972 8b7f8d297943
parent 5969 3a041d215edc
child 5981 75caa8a7d559
permissions -rw-r--r--
Patched to compile on Mac OS X (I think).
slouken@1361
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@1361
    22
slouken@1361
    23
#include <pthread.h>
icculus@5969
    24
icculus@5969
    25
#if HAVE_PTHREAD_NP_H
icculus@5969
    26
#include <pthread_np.h>
icculus@5969
    27
#endif
icculus@5969
    28
slouken@1361
    29
#include <signal.h>
slouken@5513
    30
#ifdef __LINUX__
slouken@5509
    31
#include <sys/time.h>
slouken@5509
    32
#include <sys/resource.h>
slouken@5509
    33
#include <sys/syscall.h>
slouken@5509
    34
#endif
slouken@1361
    35
icculus@5969
    36
#include "SDL_platform.h"
slouken@1361
    37
#include "SDL_thread.h"
slouken@1361
    38
#include "../SDL_thread_c.h"
slouken@1361
    39
#include "../SDL_systhread.h"
slouken@1361
    40
slouken@1361
    41
/* List of signals to mask in the subthreads */
slouken@3162
    42
static const int sig_list[] = {
slouken@1895
    43
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
slouken@1895
    44
    SIGVTALRM, SIGPROF, 0
slouken@1361
    45
};
slouken@1361
    46
slouken@5509
    47
slouken@1895
    48
static void *
slouken@1895
    49
RunThread(void *data)
slouken@1361
    50
{
slouken@1895
    51
    SDL_RunThread(data);
icculus@5963
    52
    return NULL;
slouken@1361
    53
}
slouken@1361
    54
slouken@1895
    55
int
slouken@1895
    56
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@1361
    57
{
slouken@1895
    58
    pthread_attr_t type;
slouken@1361
    59
slouken@1895
    60
    /* Set the thread attributes */
slouken@1895
    61
    if (pthread_attr_init(&type) != 0) {
slouken@1895
    62
        SDL_SetError("Couldn't initialize pthread attributes");
slouken@1895
    63
        return (-1);
slouken@1895
    64
    }
slouken@1895
    65
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
slouken@1361
    66
slouken@1895
    67
    /* Create the thread and go! */
slouken@1895
    68
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
slouken@1895
    69
        SDL_SetError("Not enough resources to create thread");
slouken@1895
    70
        return (-1);
slouken@1895
    71
    }
slouken@1459
    72
slouken@1895
    73
    return (0);
slouken@1361
    74
}
slouken@1361
    75
slouken@1895
    76
void
icculus@5969
    77
SDL_SYS_SetupThread(const char *name)
slouken@1361
    78
{
slouken@1895
    79
    int i;
slouken@1895
    80
    sigset_t mask;
slouken@1361
    81
icculus@5972
    82
#if ( (__MACOSX__ && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)) || \
icculus@5972
    83
      (__IPHONEOS__ && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)) )
icculus@5969
    84
    if (pthread_setname_np != NULL) { pthread_setname_np(name); }
icculus@5969
    85
#elif HAVE_PTHREAD_SETNAME_NP
icculus@5969
    86
    pthread_setname_np(pthread_self(), name);
icculus@5969
    87
#elif HAVE_PTHREAD_SET_NAME_NP
icculus@5969
    88
    pthread_set_name_np(pthread_self(), name);
icculus@5969
    89
#endif
icculus@5969
    90
slouken@1895
    91
    /* Mask asynchronous signals for this thread */
slouken@1895
    92
    sigemptyset(&mask);
slouken@1895
    93
    for (i = 0; sig_list[i]; ++i) {
slouken@1895
    94
        sigaddset(&mask, sig_list[i]);
slouken@1895
    95
    }
slouken@1895
    96
    pthread_sigmask(SIG_BLOCK, &mask, 0);
slouken@1361
    97
slouken@1361
    98
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
slouken@1895
    99
    /* Allow ourselves to be asynchronously cancelled */
slouken@1895
   100
    {
slouken@1895
   101
        int oldstate;
slouken@1895
   102
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
slouken@1895
   103
    }
slouken@1361
   104
#endif
slouken@1361
   105
}
slouken@1361
   106
slouken@3578
   107
SDL_threadID
slouken@1895
   108
SDL_ThreadID(void)
slouken@1361
   109
{
slouken@3578
   110
    return ((SDL_threadID) pthread_self());
slouken@1361
   111
}
slouken@1361
   112
slouken@5506
   113
int
slouken@5509
   114
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   115
{
slouken@5513
   116
#ifdef __LINUX__
slouken@5509
   117
    int value;
slouken@5509
   118
slouken@5509
   119
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   120
        value = 19;
slouken@5509
   121
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   122
        value = -20;
slouken@5509
   123
    } else {
slouken@5509
   124
        value = 0;
slouken@5509
   125
    }
slouken@5509
   126
    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
slouken@5510
   127
        /* Note that this fails if you're trying to set high priority
slouken@5510
   128
           and you don't have root permission. BUT DON'T RUN AS ROOT!
slouken@5510
   129
         */
slouken@5509
   130
        SDL_SetError("setpriority() failed");
slouken@5509
   131
        return -1;
slouken@5509
   132
    }
slouken@5509
   133
    return 0;
slouken@5509
   134
#else
slouken@5506
   135
    struct sched_param sched;
slouken@5506
   136
    int policy;
slouken@5509
   137
    pthread_t thread = pthread_self();
slouken@5506
   138
slouken@5509
   139
    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
slouken@5506
   140
        SDL_SetError("pthread_getschedparam() failed");
slouken@5506
   141
        return -1;
slouken@5506
   142
    }
slouken@5506
   143
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5506
   144
        sched.sched_priority = sched_get_priority_min(policy);
slouken@5506
   145
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5506
   146
        sched.sched_priority = sched_get_priority_max(policy);
slouken@5506
   147
    } else {
slouken@5506
   148
        int min_priority = sched_get_priority_min(policy);
slouken@5506
   149
        int max_priority = sched_get_priority_max(policy);
slouken@5509
   150
        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
slouken@5506
   151
    }
slouken@5509
   152
    if (pthread_setschedparam(thread, policy, &sched) < 0) {
slouken@5506
   153
        SDL_SetError("pthread_setschedparam() failed");
slouken@5506
   154
        return -1;
slouken@5506
   155
    }
slouken@5506
   156
    return 0;
slouken@5509
   157
#endif /* linux */
slouken@5506
   158
}
slouken@5506
   159
slouken@1895
   160
void
slouken@1895
   161
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@1361
   162
{
slouken@1895
   163
    pthread_join(thread->handle, 0);
slouken@1361
   164
}
slouken@1361
   165
slouken@1895
   166
/* vi: set ts=4 sw=4 expandtab: */