src/thread/windows/SDL_systhread.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Sep 2011 17:42:58 -0400
changeset 5962 17cadf0c466e
parent 5904 a21d78fc998c
child 5969 3a041d215edc
permissions -rw-r--r--
Simplified Windows RunThread(). Removed checks for things that are always true, free unneeded struct before calling thread entry point, instead of after thread completes.
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
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5509
diff changeset
     3
  Copyright (C) 1997-2011 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
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
/* Win32 thread management routines for SDL */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#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
    26
#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
    27
#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
    28
#include "SDL_systhread_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
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
    30
#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
    31
#ifndef _WIN32_WCE
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
/* 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
    33
#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
    34
#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
    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,
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
    39
        unsigned (__stdcall *func)(void *), void *arg, 
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
{
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
   109
#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
   110
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
   111
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
   112
{
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
   113
#ifdef _WIN32_WCE
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
   114
    pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
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
   115
    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
   116
#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
   117
    pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
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
   118
    pfnSDL_CurrentEndThread pfnEndThread = _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
   119
#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
   120
#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
   121
    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
   122
        (pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
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
    if (!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
   124
        SDL_OutOfMemory();
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
        return (-1);
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
   126
    }
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
    // Save the function which we will have to call to clear the RTL of calling app!
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
   128
    pThreadParms->pfnCurrentEndThread = pfnEndThread;
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
    // Also save the real parameters we have to pass to thread function
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
   130
    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
   131
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
   132
    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
   133
        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
   134
        thread->handle = (SYS_ThreadHandle)
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
            ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
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
                                     pThreadParms, 0, &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
   137
    } 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
   138
        DWORD 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
   139
        thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread,
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
   140
                                      pThreadParms, 0, &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
   141
    }
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
   142
    if (thread->handle == NULL) {
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
   143
        SDL_SetError("Not enough resources to create thread");
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
   144
        return (-1);
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
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   148
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
   149
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
   150
SDL_SYS_SetupThread(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
{
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
   152
    return;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   154
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   155
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
   156
SDL_ThreadID(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   157
{
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   158
    return ((SDL_threadID) GetCurrentThreadId());
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   159
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   160
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   161
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
   162
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   163
{
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   164
    int value;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   165
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   166
    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
   167
        value = THREAD_PRIORITY_LOWEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   168
    } 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
   169
        value = THREAD_PRIORITY_HIGHEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   170
    } 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
   171
        value = THREAD_PRIORITY_NORMAL;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   172
    }
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   173
    if (!SetThreadPriority(GetCurrentThread(), value)) {
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   174
        WIN_SetError("SetThreadPriority()");
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   175
        return -1;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   176
    }
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   177
    return 0;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   178
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   179
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
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
   181
SDL_SYS_WaitThread(SDL_Thread * thread)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   182
{
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
   183
    WaitForSingleObject(thread->handle, INFINITE);
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
   184
    CloseHandle(thread->handle);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   186
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
   187
/* vi: set ts=4 sw=4 expandtab: */