src/thread/windows/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 21:57:31 -0700
changeset 7396 a36ab6149a10
parent 7191 75360622e65f
child 7498 bda3639deecc
permissions -rw-r--r--
Fixed bug 1953 - Crash at memcpy X11_DispatchEvent(_THIS) Function Nitz In Function X11_DispatchEvent(_THIS), case SelectionNotify : static void X11_DispatchEvent(_THIS) { // Some Code case SelectionNotify: { //Some Code SDL_bool expect_lf = SDL_FALSE; char *start = NULL; // Initialised with NULL char *scan = (char*)p.data; char *fn; char *uri; int length = 0; while (p.count--) { if (!expect_lf) { if (*scan==0x0D) { expect_lf = SDL_TRUE; } else if(start == NULL) { start = scan; length = 0; } length++; } else { if (*scan==0x0A && length>0) { uri = malloc(length--); memcpy(uri, start, length); // Problem is Here, start is still NULL if control comes to else statement without initialising the start pointer, which is wrong uri[length] = 0; fn = X11_URIToLocal(uri); if (fn) SDL_SendDropFile(fn); free(uri); } expect_lf = SDL_FALSE; start = NULL; } scan++; } } As shown above how start pointer remains NULL, Patch for this issue would be: if (*scan==0x0D) { expect_lf = SDL_TRUE; } if(start == NULL) { start = scan; length = 0; } Just replace else if statement with if.
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
     3
  Copyright (C) 1997-2013 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
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
{
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
{
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
   113
    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
   114
    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
   115
#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
   116
    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
   117
        (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
   118
    if (!pThreadParms) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   119
        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
   120
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   121
    /* 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
   122
    pThreadParms->pfnCurrentEndThread = pfnEndThread;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   123
    /* 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
   124
    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
   125
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
   126
    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
   127
        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
   128
        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
   129
            ((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
   130
                                     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
   131
    } 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
   132
        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
   133
        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
   134
                                      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
   135
    }
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
   136
    if (thread->handle == NULL) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   137
        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
   138
    }
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   139
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   140
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   142
#ifdef _MSC_VER
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   143
#pragma pack(push,8)
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   144
typedef struct tagTHREADNAME_INFO
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   145
{
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   146
    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
   147
    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
   148
    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
   149
    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
   150
} THREADNAME_INFO;
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   151
#pragma pack(pop)
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   152
#endif
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   153
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
   154
void
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   155
SDL_SYS_SetupThread(const char *name)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   156
{
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   157
    if (name != NULL) {
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   158
        #if 0  /* !!! FIXME: __except needs C runtime, which we don't link against. */
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   159
        #ifdef _MSC_VER  /* !!! FIXME: can we do SEH on other compilers yet? */
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   160
        /* This magic tells the debugger to name a thread if it's listening. */
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   161
        THREADNAME_INFO inf;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   162
        inf.dwType = 0x1000;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   163
        inf.szName = name;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   164
        inf.dwThreadID = (DWORD) -1;
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   165
        inf.dwFlags = 0;
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   166
6000
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   167
        __try
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   168
        {
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   169
            RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   170
        }
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   171
        __except(EXCEPTION_CONTINUE_EXECUTION)
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   172
        {
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   173
            /* The program itself should ignore this bogus exception. */
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   174
        }
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   175
        #endif
3c980a9f983c Don't crash with a NULL thread name.
Ryan C. Gordon <icculus@icculus.org>
parents: 5973
diff changeset
   176
        #endif
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5962
diff changeset
   177
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   179
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   180
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
   181
SDL_ThreadID(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   182
{
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   183
    return ((SDL_threadID) GetCurrentThreadId());
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   184
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   186
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
   187
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   188
{
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   189
    int value;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   190
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   191
    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
   192
        value = THREAD_PRIORITY_LOWEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   193
    } 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
   194
        value = THREAD_PRIORITY_HIGHEST;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   195
    } 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
   196
        value = THREAD_PRIORITY_NORMAL;
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   197
    }
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
    if (!SetThreadPriority(GetCurrentThread(), value)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   199
        return WIN_SetError("SetThreadPriority()");
5506
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   200
    }
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   201
    return 0;
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   202
}
82a09d5d0f07 Implemented SDL_SetThreadPriority()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
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: 1769
diff changeset
   204
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
   205
SDL_SYS_WaitThread(SDL_Thread * thread)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1769
diff changeset
   207
    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
   208
    CloseHandle(thread->handle);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   210
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6000
diff changeset
   211
#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
   212
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
   213
/* vi: set ts=4 sw=4 expandtab: */