src/thread/windows/SDL_systhread.c
author Ryan C. Gordon
Sun, 02 Oct 2011 00:43:56 -0400
changeset 5971 ca2d4b8e9e40
parent 5970 34263edce4f4
child 5973 9392717808aa
permissions -rw-r--r--
Further patched to compile on Windows.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* Win32 thread management routines for SDL */
slouken@0
    24
slouken@0
    25
#include "SDL_thread.h"
slouken@1361
    26
#include "../SDL_thread_c.h"
slouken@1361
    27
#include "../SDL_systhread.h"
slouken@5086
    28
#include "SDL_systhread_c.h"
slouken@0
    29
slouken@1471
    30
#ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1471
    31
#ifndef _WIN32_WCE
slouken@1471
    32
/* We'll use the C library from this DLL */
slouken@1471
    33
#include <process.h>
slouken@1471
    34
#endif
slouken@1471
    35
icculus@5904
    36
/* Cygwin gcc-3 ... MingW64 (even with a i386 host) does this like MSVC. */
icculus@5904
    37
#if (defined(__MINGW32__) && (__GNUC__ < 4))
icculus@5904
    38
typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
icculus@5904
    39
        unsigned (__stdcall *func)(void *), void *arg, 
icculus@5904
    40
        unsigned, unsigned *threadID);
icculus@5904
    41
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
icculus@5904
    42
slouken@1769
    43
#elif defined(__WATCOMC__)
slouken@1769
    44
/* This is for Watcom targets except OS2 */
slouken@1769
    45
#if __WATCOMC__ < 1240
slouken@1769
    46
#define __watcall
slouken@1769
    47
#endif
slouken@1895
    48
typedef unsigned long (__watcall * pfnSDL_CurrentBeginThread) (void *,
slouken@1895
    49
                                                               unsigned,
slouken@1895
    50
                                                               unsigned
slouken@1895
    51
                                                               (__stdcall *
slouken@1895
    52
                                                                func) (void
slouken@1895
    53
                                                                       *),
slouken@1895
    54
                                                               void *arg,
slouken@1895
    55
                                                               unsigned,
slouken@1895
    56
                                                               unsigned
slouken@1895
    57
                                                               *threadID);
slouken@1895
    58
typedef void (__watcall * pfnSDL_CurrentEndThread) (unsigned code);
icculus@5904
    59
slouken@1471
    60
#else
slouken@1895
    61
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1895
    62
                                                        unsigned (__stdcall *
slouken@1895
    63
                                                                  func) (void
slouken@1895
    64
                                                                         *),
slouken@1895
    65
                                                        void *arg, unsigned,
slouken@1895
    66
                                                        unsigned *threadID);
slouken@1895
    67
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
slouken@1471
    68
#endif
slouken@1471
    69
#endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
slouken@1471
    70
slouken@1471
    71
slouken@1330
    72
typedef struct ThreadStartParms
slouken@1330
    73
{
slouken@1895
    74
    void *args;
slouken@1895
    75
    pfnSDL_CurrentEndThread pfnCurrentEndThread;
slouken@1330
    76
} tThreadStartParms, *pThreadStartParms;
slouken@0
    77
icculus@5904
    78
static DWORD
slouken@1895
    79
RunThread(void *data)
slouken@0
    80
{
slouken@1895
    81
    pThreadStartParms pThreadParms = (pThreadStartParms) data;
icculus@5962
    82
    pfnSDL_CurrentEndThread pfnEndThread = pThreadParms->pfnCurrentEndThread;
icculus@5962
    83
    void *args = pThreadParms->args;
icculus@5962
    84
    SDL_free(pThreadParms);
icculus@5962
    85
    SDL_RunThread(args);
icculus@5962
    86
    if (pfnEndThread != NULL)
icculus@5962
    87
        pfnEndThread(0);
slouken@1895
    88
    return (0);
slouken@0
    89
}
slouken@0
    90
icculus@5904
    91
static DWORD WINAPI
icculus@5904
    92
RunThreadViaCreateThread(LPVOID data)
icculus@5904
    93
{
icculus@5904
    94
  return RunThread(data);
icculus@5904
    95
}
icculus@5904
    96
icculus@5904
    97
static unsigned __stdcall
icculus@5904
    98
RunThreadViaBeginThreadEx(void *data)
icculus@5904
    99
{
icculus@5904
   100
  return (unsigned) RunThread(data);
icculus@5904
   101
}
icculus@5904
   102
slouken@1471
   103
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1895
   104
int
slouken@1895
   105
SDL_SYS_CreateThread(SDL_Thread * thread, void *args,
slouken@1895
   106
                     pfnSDL_CurrentBeginThread pfnBeginThread,
slouken@1895
   107
                     pfnSDL_CurrentEndThread pfnEndThread)
slouken@0
   108
{
slouken@1471
   109
#else
slouken@1895
   110
int
slouken@1895
   111
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
slouken@1471
   112
{
slouken@1471
   113
#ifdef _WIN32_WCE
slouken@1895
   114
    pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
slouken@1895
   115
    pfnSDL_CurrentEndThread pfnEndThread = NULL;
slouken@1471
   116
#else
slouken@1895
   117
    pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
slouken@1895
   118
    pfnSDL_CurrentEndThread pfnEndThread = _endthreadex;
slouken@1471
   119
#endif
slouken@1471
   120
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
slouken@1895
   121
    pThreadStartParms pThreadParms =
slouken@1895
   122
        (pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
slouken@1895
   123
    if (!pThreadParms) {
slouken@1895
   124
        SDL_OutOfMemory();
slouken@1895
   125
        return (-1);
slouken@1895
   126
    }
slouken@1895
   127
    // Save the function which we will have to call to clear the RTL of calling app!
slouken@1895
   128
    pThreadParms->pfnCurrentEndThread = pfnEndThread;
slouken@1895
   129
    // Also save the real parameters we have to pass to thread function
slouken@1895
   130
    pThreadParms->args = args;
slouken@1330
   131
slouken@1895
   132
    if (pfnBeginThread) {
icculus@5904
   133
        unsigned threadid = 0;
icculus@5904
   134
        thread->handle = (SYS_ThreadHandle)
icculus@5904
   135
            ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
icculus@5904
   136
                                     pThreadParms, 0, &threadid));
slouken@1895
   137
    } else {
icculus@5904
   138
        DWORD threadid = 0;
icculus@5904
   139
        thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread,
icculus@5904
   140
                                      pThreadParms, 0, &threadid);
slouken@1895
   141
    }
slouken@1895
   142
    if (thread->handle == NULL) {
slouken@1895
   143
        SDL_SetError("Not enough resources to create thread");
slouken@1895
   144
        return (-1);
slouken@1895
   145
    }
slouken@1895
   146
    return (0);
slouken@0
   147
}
slouken@0
   148
icculus@5969
   149
#ifdef _MSC_VER
icculus@5969
   150
#pragma pack(push,8)
icculus@5969
   151
typedef struct tagTHREADNAME_INFO
icculus@5969
   152
{
icculus@5969
   153
    DWORD dwType; /* must be 0x1000 */
icculus@5969
   154
    LPCSTR szName; /* pointer to name (in user addr space) */
icculus@5969
   155
    DWORD dwThreadID; /* thread ID (-1=caller thread) */
icculus@5969
   156
    DWORD dwFlags; /* reserved for future use, must be zero */
icculus@5969
   157
} THREADNAME_INFO;
icculus@5969
   158
#pragma pack(pop)
icculus@5969
   159
#endif
icculus@5969
   160
slouken@1895
   161
void
icculus@5969
   162
SDL_SYS_SetupThread(const char *name)
slouken@0
   163
{
icculus@5969
   164
#ifdef _MSC_VER  /* !!! FIXME: can we do SEH on other compilers yet? */
icculus@5969
   165
    /* This magic tells the debugger to name a thread if it's listening. */
icculus@5969
   166
    THREADNAME_INFO inf;
icculus@5970
   167
    inf.dwType = 0x1000;
icculus@5970
   168
    inf.szName = name;
icculus@5970
   169
    inf.dwThreadID = (DWORD) -1;
icculus@5970
   170
    inf.dwFlags = 0;
icculus@5969
   171
icculus@5969
   172
    __try
icculus@5969
   173
    {
icculus@5969
   174
        RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
icculus@5969
   175
    }
icculus@5971
   176
    __except(EXCEPTION_CONTINUE_EXECUTION)
icculus@5969
   177
    {
icculus@5969
   178
        /* The program itself should ignore this bogus exception. */
icculus@5969
   179
    }
icculus@5969
   180
#endif
slouken@0
   181
}
slouken@0
   182
slouken@3578
   183
SDL_threadID
slouken@1895
   184
SDL_ThreadID(void)
slouken@0
   185
{
slouken@3578
   186
    return ((SDL_threadID) GetCurrentThreadId());
slouken@0
   187
}
slouken@0
   188
slouken@5506
   189
int
slouken@5509
   190
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   191
{
slouken@5509
   192
    int value;
slouken@5506
   193
slouken@5506
   194
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   195
        value = THREAD_PRIORITY_LOWEST;
slouken@5506
   196
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   197
        value = THREAD_PRIORITY_HIGHEST;
slouken@5506
   198
    } else {
slouken@5509
   199
        value = THREAD_PRIORITY_NORMAL;
slouken@5506
   200
    }
slouken@5509
   201
    if (!SetThreadPriority(GetCurrentThread(), value)) {
slouken@5506
   202
        WIN_SetError("SetThreadPriority()");
slouken@5506
   203
        return -1;
slouken@5506
   204
    }
slouken@5506
   205
    return 0;
slouken@5506
   206
}
slouken@5506
   207
slouken@1895
   208
void
slouken@1895
   209
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@0
   210
{
slouken@1895
   211
    WaitForSingleObject(thread->handle, INFINITE);
slouken@1895
   212
    CloseHandle(thread->handle);
slouken@0
   213
}
slouken@0
   214
slouken@1895
   215
/* vi: set ts=4 sw=4 expandtab: */