src/thread/windows/SDL_systhread.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 12 Apr 2016 18:12:04 -0400
changeset 10148 4ed1bd3724d6
parent 10145 cd87106d1d47
permissions -rw-r--r--
windows: created threads' stack sizes should be reserved, not committed.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9930
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
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: 5509
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: 5509
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
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: 5509
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: 5509
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
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: 5509
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: 5509
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: 5509
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
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: 5509
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: 5509
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7978
diff changeset
    21
#include "../../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6000
diff changeset
    23
#if SDL_THREAD_WINDOWS
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6000
diff changeset
    24
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* Win32 thread management routines for SDL */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_thread.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    28
#include "../SDL_thread_c.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    29
#include "../SDL_systhread.h"
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    30
#include "SDL_systhread_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    32
#ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    33
/* We'll use the C library from this DLL */
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    34
#include <process.h>
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    35
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    36
/* Cygwin gcc-3 ... MingW64 (even with a i386 host) does this like MSVC. */
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    37
#if (defined(__MINGW32__) && (__GNUC__ < 4))
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    38
typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
    39
        unsigned (__stdcall *func)(void *), void *arg,
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    40
        unsigned, unsigned *threadID);
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    41
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    42
1769
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1471
diff changeset
    43
#elif defined(__WATCOMC__)
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1471
diff changeset
    44
/* This is for Watcom targets except OS2 */
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1471
diff changeset
    45
#if __WATCOMC__ < 1240
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1471
diff changeset
    46
#define __watcall
290b5baf2fca Fixed bug #215
Sam Lantinga <slouken@libsdl.org>
parents: 1471
diff changeset
    47
#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: 1769
diff changeset
    48
typedef unsigned long (__watcall * pfnSDL_CurrentBeginThread) (void *,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    49
                                                               unsigned,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    50
                                                               unsigned
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    51
                                                               (__stdcall *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    52
                                                                func) (void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    53
                                                                       *),
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    54
                                                               void *arg,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    55
                                                               unsigned,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    56
                                                               unsigned
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    57
                                                               *threadID);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    58
typedef void (__watcall * pfnSDL_CurrentEndThread) (unsigned code);
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    59
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    60
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    61
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    62
                                                        unsigned (__stdcall *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    63
                                                                  func) (void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    64
                                                                         *),
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    65
                                                        void *arg, unsigned,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    66
                                                        unsigned *threadID);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    67
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    68
#endif
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    69
#endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    70
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
    71
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    72
typedef struct ThreadStartParms
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    73
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    74
    void *args;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    75
    pfnSDL_CurrentEndThread pfnCurrentEndThread;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    76
} tThreadStartParms, *pThreadStartParms;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    78
static DWORD
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    79
RunThread(void *data)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    81
    pThreadStartParms pThreadParms = (pThreadStartParms) data;
5962
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    82
    pfnSDL_CurrentEndThread pfnEndThread = pThreadParms->pfnCurrentEndThread;
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    83
    void *args = pThreadParms->args;
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    84
    SDL_free(pThreadParms);
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    85
    SDL_RunThread(args);
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    86
    if (pfnEndThread != NULL)
17cadf0c466e Simplified Windows RunThread().
Ryan C. Gordon <icculus@icculus.org>
parents: 5904
diff changeset
    87
        pfnEndThread(0);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    88
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    89
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    91
static DWORD WINAPI
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    92
RunThreadViaCreateThread(LPVOID data)
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    93
{
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    94
  return RunThread(data);
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    95
}
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    96
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    97
static unsigned __stdcall
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    98
RunThreadViaBeginThreadEx(void *data)
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
    99
{
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   100
  return (unsigned) RunThread(data);
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   101
}
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   102
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
   103
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   104
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   105
SDL_SYS_CreateThread(SDL_Thread * thread, void *args,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   106
                     pfnSDL_CurrentBeginThread pfnBeginThread,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   107
                     pfnSDL_CurrentEndThread pfnEndThread)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   108
{
9930
84285c1d8ca4 WinRT: bug-fix - SDL_SetThreadPriority() didn't work on WinRT 8.x platforms
David Ludwig <dludwig@pobox.com>
parents: 9619
diff changeset
   109
#elif defined(__CYGWIN__) || defined(__WINRT__)
7498
bda3639deecc Fixed building with cygwin
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   110
int
bda3639deecc Fixed building with cygwin
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   111
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
bda3639deecc Fixed building with cygwin
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   112
{
bda3639deecc Fixed building with cygwin
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   113
    pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
bda3639deecc Fixed building with cygwin
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   114
    pfnSDL_CurrentEndThread pfnEndThread = NULL;
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
   115
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   116
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   117
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
   118
{
7828
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7735
diff changeset
   119
    pfnSDL_CurrentBeginThread pfnBeginThread = (pfnSDL_CurrentBeginThread)_beginthreadex;
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7735
diff changeset
   120
    pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread)_endthreadex;
1471
9fb0eee04dd9 Enabled libc support on Win32, so we don't break binary compatibility in 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 1470
diff changeset
   121
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   122
    pThreadStartParms pThreadParms =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   123
        (pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
10148
4ed1bd3724d6 windows: created threads' stack sizes should be reserved, not committed.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   124
    const DWORD flags = thread->stacksize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   125
    if (!pThreadParms) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   126
        return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   127
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   128
    /* Save the function which we will have to call to clear the RTL of calling app! */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   129
    pThreadParms->pfnCurrentEndThread = pfnEndThread;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   130
    /* Also save the real parameters we have to pass to thread function */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   131
    pThreadParms->args = args;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   132
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 10088
diff changeset
   133
    /* thread->stacksize == 0 means "system default", same as win32 expects */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   134
    if (pfnBeginThread) {
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   135
        unsigned threadid = 0;
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   136
        thread->handle = (SYS_ThreadHandle)
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 10088
diff changeset
   137
            ((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 10088
diff changeset
   138
                                     RunThreadViaBeginThreadEx,
10148
4ed1bd3724d6 windows: created threads' stack sizes should be reserved, not committed.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   139
                                     pThreadParms, flags, &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: 1769
diff changeset
   140
    } else {
5904
a21d78fc998c Clean up the win32 compiler warnings for SDL threads, in the 1.3 branch.
Ryan C. Gordon <icculus@icculus.org>
parents: 5889
diff changeset
   141
        DWORD threadid = 0;
10145
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 10088
diff changeset
   142
        thread->handle = CreateThread(NULL, thread->stacksize,
cd87106d1d47 threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
Ryan C. Gordon <icculus@icculus.org>
parents: 10088
diff changeset
   143
                                      RunThreadViaCreateThread,
10148
4ed1bd3724d6 windows: created threads' stack sizes should be reserved, not committed.
Ryan C. Gordon <icculus@icculus.org>
parents: 10145
diff changeset
   144
                                      pThreadParms, flags, &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: 1769
diff changeset
   145
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   146
    if (thread->handle == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   147
        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: 1769
diff changeset
   148
    }
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   149
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   150
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   152
#pragma pack(push,8)
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   153
typedef struct tagTHREADNAME_INFO
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   154
{
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   155
    DWORD dwType; /* must be 0x1000 */
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   156
    LPCSTR szName; /* pointer to name (in user addr space) */
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   157
    DWORD dwThreadID; /* thread ID (-1=caller thread) */
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   158
    DWORD dwFlags; /* reserved for future use, must be zero */
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   159
} THREADNAME_INFO;
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   160
#pragma pack(pop)
7724
54c06b0f65d9 Enabled thread naming on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 7498
diff changeset
   161
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   162
void
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   163
SDL_SYS_SetupThread(const char *name)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   164
{
10087
96f7be2c885b Windows: let threads be named in the debugger.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   165
    if ((name != NULL) && IsDebuggerPresent()) {
96f7be2c885b Windows: let threads be named in the debugger.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   166
        /* This magic tells the debugger to name a thread if it's listening. */
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   167
        THREADNAME_INFO inf;
10087
96f7be2c885b Windows: let threads be named in the debugger.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   168
        SDL_zero(inf);
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   169
        inf.dwType = 0x1000;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   170
        inf.szName = name;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   171
        inf.dwThreadID = (DWORD) -1;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   172
        inf.dwFlags = 0;
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   173
10087
96f7be2c885b Windows: let threads be named in the debugger.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   174
        /* The debugger catches this, renames the thread, continues on. */
96f7be2c885b Windows: let threads be named in the debugger.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   175
        RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf);
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   176
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   177
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   179
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: 1769
diff changeset
   180
SDL_ThreadID(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   181
{
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   182
    return ((SDL_threadID) GetCurrentThreadId());
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   183
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   184
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   185
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
   186
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   187
{
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   188
    int value;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   189
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   190
    if (priority == SDL_THREAD_PRIORITY_LOW) {
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   191
        value = THREAD_PRIORITY_LOWEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   192
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   193
        value = THREAD_PRIORITY_HIGHEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   194
    } else {
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   195
        value = THREAD_PRIORITY_NORMAL;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   196
    }
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   197
    if (!SetThreadPriority(GetCurrentThread(), value)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   198
        return WIN_SetError("SetThreadPriority()");
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
    return 0;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   201
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   202
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   203
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   204
SDL_SYS_WaitThread(SDL_Thread * thread)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
{
9930
84285c1d8ca4 WinRT: bug-fix - SDL_SetThreadPriority() didn't work on WinRT 8.x platforms
David Ludwig <dludwig@pobox.com>
parents: 9619
diff changeset
   206
    WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   207
    CloseHandle(thread->handle);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   208
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
7978
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   210
void
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   211
SDL_SYS_DetachThread(SDL_Thread * thread)
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   212
{
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   213
    CloseHandle(thread->handle);
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   214
}
70ac84e49797 Added SDL_DetachThread() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 7854
diff changeset
   215
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6000
diff changeset
   216
#endif /* SDL_THREAD_WINDOWS */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6000
diff changeset
   217
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   218
/* vi: set ts=4 sw=4 expandtab: */