src/thread/pthread/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Jun 2013 01:08:14 -0700
changeset 7257 5997d00782e8
parent 7191 75360622e65f
child 7977 5272ae8fccd7
permissions -rw-r--r--
Fixed testnative on Mac OS X, which no longer ships X11 by default.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6641
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
     7
  arising from the use of this software.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    11
  freely, subject to the following restrictions:
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5513
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
5981
75caa8a7d559 Fixed a whole slew of compiler warnings that -Wall exposed.
Ryan C. Gordon <icculus@icculus.org>
parents: 5972
diff changeset
    21
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    22
#include "SDL_config.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#include <pthread.h>
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    25
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    26
#if HAVE_PTHREAD_NP_H
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    27
#include <pthread_np.h>
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    28
#endif
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    29
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include <signal.h>
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    31
5513
0a72c9c29099 Android defines linux, but doesn't have the gettid system call.
Sam Lantinga <slouken@libsdl.org>
parents: 5510
diff changeset
    32
#ifdef __LINUX__
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
    33
#include <sys/time.h>
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
    34
#include <sys/resource.h>
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
    35
#include <sys/syscall.h>
5981
75caa8a7d559 Fixed a whole slew of compiler warnings that -Wall exposed.
Ryan C. Gordon <icculus@icculus.org>
parents: 5972
diff changeset
    36
#include <unistd.h>
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
    37
#endif /* __LINUX__ */
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    39
#if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
6605
59132ac5df74 Really fixed the Mac build this time.
Sam Lantinga <slouken@libsdl.org>
parents: 6604
diff changeset
    40
#include <dlfcn.h>
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    41
#ifndef RTLD_DEFAULT
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    42
#define RTLD_DEFAULT NULL
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    43
#endif
6605
59132ac5df74 Really fixed the Mac build this time.
Sam Lantinga <slouken@libsdl.org>
parents: 6604
diff changeset
    44
#endif
59132ac5df74 Really fixed the Mac build this time.
Sam Lantinga <slouken@libsdl.org>
parents: 6604
diff changeset
    45
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
    46
#include "SDL_platform.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
#include "SDL_thread.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
#include "../SDL_thread_c.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#include "../SDL_systhread.h"
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    50
#ifdef __ANDROID__
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    51
#include "../../core/android/SDL_android.h"
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    52
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    54
#include "SDL_assert.h"
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    55
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
/* List of signals to mask in the subthreads */
3162
dc1eb82ffdaa Von: Thomas Zimmermann
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
    57
static const int sig_list[] = {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    58
    SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    59
    SIGVTALRM, SIGPROF, 0
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
};
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
    62
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    63
static void *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    64
RunThread(void *data)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
{
6354
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    66
#ifdef __ANDROID__
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    67
    Android_JNI_SetupThread();
17840f487124 Fixes #1422, removes global JNI Env, uses per thread copies, adds thread auto detaching.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 6138
diff changeset
    68
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    69
    SDL_RunThread(data);
5963
fe5d2285dffa Don't call pthread_exit(), returning from RunThread() is equivalent.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    70
    return NULL;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    73
#if defined(__MACOSX__) || defined(__IPHONEOS__)
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    74
static SDL_bool checked_setname = SDL_FALSE;
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
    75
static int (*ppthread_setname_np)(const char*) = NULL;
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    76
#elif defined(__LINUX__)
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    77
static SDL_bool checked_setname = SDL_FALSE;
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
    78
static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    79
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    80
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    81
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    83
    pthread_attr_t type;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    85
    /* do this here before any threads exist, so there's no race condition. */
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    86
    #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    87
    if (!checked_setname) {
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    88
        void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    89
        #if defined(__MACOSX__) || defined(__IPHONEOS__)
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
    90
        ppthread_setname_np = (int(*)(const char*)) fn;
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    91
        #elif defined(__LINUX__)
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
    92
        ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    93
        #endif
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    94
        checked_setname = SDL_TRUE;
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    95
    }
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    96
    #endif
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
    97
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    98
    /* Set the thread attributes */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
    99
    if (pthread_attr_init(&type) != 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   100
        return SDL_SetError("Couldn't initialize pthread attributes");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   101
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   102
    pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
1459
1e8582152d44 Date: Wed, 01 Mar 2006 10:14:31 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   103
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   104
    /* Create the thread and go! */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   105
    if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   106
        return SDL_SetError("Not enough resources to create thread");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   107
    }
1459
1e8582152d44 Date: Wed, 01 Mar 2006 10:14:31 +0000
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   108
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   109
    return 0;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   112
void
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
   113
SDL_SYS_SetupThread(const char *name)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   115
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   116
    sigset_t mask;
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5981
diff changeset
   118
    if (name != NULL) {
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   119
        #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   120
        SDL_assert(checked_setname);
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
   121
        if (ppthread_setname_np != NULL) {
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   122
            #if defined(__MACOSX__) || defined(__IPHONEOS__)
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
   123
            ppthread_setname_np(name);
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   124
            #elif defined(__LINUX__)
6641
45187a87d35b Whoops, let's not redefine the actual symbol.
Ryan C. Gordon <icculus@icculus.org>
parents: 6640
diff changeset
   125
            ppthread_setname_np(pthread_self(), name);
6640
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   126
            #endif
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   127
        }
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   128
        #elif HAVE_PTHREAD_SETNAME_NP
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   129
            pthread_setname_np(pthread_self(), name);
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   130
        #elif HAVE_PTHREAD_SET_NAME_NP
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   131
            pthread_set_name_np(pthread_self(), name);
44a2e00e7c66 Make Linux dynamically look up pthread_setname_np() for older glibc compat.
Ryan C. Gordon <icculus@icculus.org>
parents: 6605
diff changeset
   132
        #endif
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5981
diff changeset
   133
    }
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5963
diff changeset
   134
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   135
    /* Mask asynchronous signals for this thread */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   136
    sigemptyset(&mask);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   137
    for (i = 0; sig_list[i]; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   138
        sigaddset(&mask, sig_list[i]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   139
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   140
    pthread_sigmask(SIG_BLOCK, &mask, 0);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   143
    /* Allow ourselves to be asynchronously cancelled */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   144
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   145
        int oldstate;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   146
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   147
    }
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
#endif
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
   151
SDL_threadID
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   152
SDL_ThreadID(void)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
{
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 3162
diff changeset
   154
    return ((SDL_threadID) pthread_self());
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   157
int
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   158
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   159
{
5513
0a72c9c29099 Android defines linux, but doesn't have the gettid system call.
Sam Lantinga <slouken@libsdl.org>
parents: 5510
diff changeset
   160
#ifdef __LINUX__
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   161
    int value;
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   162
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   163
    if (priority == SDL_THREAD_PRIORITY_LOW) {
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   164
        value = 19;
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   165
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   166
        value = -20;
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   167
    } else {
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   168
        value = 0;
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   169
    }
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   170
    if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
5510
55519e27e5d1 Warn people not to run their applications as root!
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
   171
        /* Note that this fails if you're trying to set high priority
55519e27e5d1 Warn people not to run their applications as root!
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
   172
           and you don't have root permission. BUT DON'T RUN AS ROOT!
55519e27e5d1 Warn people not to run their applications as root!
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
   173
         */
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   174
        return SDL_SetError("setpriority() failed");
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   175
    }
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   176
    return 0;
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   177
#else
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   178
    struct sched_param sched;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   179
    int policy;
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   180
    pthread_t thread = pthread_self();
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   181
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   182
    if (pthread_getschedparam(thread, &policy, &sched) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   183
        return SDL_SetError("pthread_getschedparam() failed");
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   184
    }
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   185
    if (priority == SDL_THREAD_PRIORITY_LOW) {
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   186
        sched.sched_priority = sched_get_priority_min(policy);
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   187
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   188
        sched.sched_priority = sched_get_priority_max(policy);
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   189
    } else {
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   190
        int min_priority = sched_get_priority_min(policy);
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   191
        int max_priority = sched_get_priority_max(policy);
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   192
        sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   193
    }
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   194
    if (pthread_setschedparam(thread, policy, &sched) < 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   195
        return SDL_SetError("pthread_setschedparam() failed");
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   196
    }
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   197
    return 0;
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   198
#endif /* linux */
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   199
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   200
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   201
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   202
SDL_SYS_WaitThread(SDL_Thread * thread)
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   204
    pthread_join(thread->handle, 0);
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
}
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1459
diff changeset
   207
/* vi: set ts=4 sw=4 expandtab: */