src/thread/windows/SDL_systhread.c
author Philipp Wiesemann
Thu, 30 May 2013 12:23:36 +0200
changeset 7242 96f4133dfafa
parent 7191 75360622e65f
child 7498 bda3639deecc
permissions -rw-r--r--
Added deactivated loading of SDL2_net to Java file.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6044
    23
#if SDL_THREAD_WINDOWS
slouken@6044
    24
slouken@0
    25
/* Win32 thread management routines for SDL */
slouken@0
    26
slouken@0
    27
#include "SDL_thread.h"
slouken@1361
    28
#include "../SDL_thread_c.h"
slouken@1361
    29
#include "../SDL_systhread.h"
slouken@5086
    30
#include "SDL_systhread_c.h"
slouken@0
    31
slouken@1471
    32
#ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1471
    33
/* We'll use the C library from this DLL */
slouken@1471
    34
#include <process.h>
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,
slouken@7191
    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@1895
   113
    pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
slouken@1895
   114
    pfnSDL_CurrentEndThread pfnEndThread = _endthreadex;
slouken@1471
   115
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
slouken@1895
   116
    pThreadStartParms pThreadParms =
slouken@1895
   117
        (pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
slouken@1895
   118
    if (!pThreadParms) {
icculus@7037
   119
        return SDL_OutOfMemory();
slouken@1895
   120
    }
slouken@7191
   121
    /* Save the function which we will have to call to clear the RTL of calling app! */
slouken@1895
   122
    pThreadParms->pfnCurrentEndThread = pfnEndThread;
slouken@7191
   123
    /* Also save the real parameters we have to pass to thread function */
slouken@1895
   124
    pThreadParms->args = args;
slouken@1330
   125
slouken@1895
   126
    if (pfnBeginThread) {
icculus@5904
   127
        unsigned threadid = 0;
icculus@5904
   128
        thread->handle = (SYS_ThreadHandle)
icculus@5904
   129
            ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
icculus@5904
   130
                                     pThreadParms, 0, &threadid));
slouken@1895
   131
    } else {
icculus@5904
   132
        DWORD threadid = 0;
icculus@5904
   133
        thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread,
icculus@5904
   134
                                      pThreadParms, 0, &threadid);
slouken@1895
   135
    }
slouken@1895
   136
    if (thread->handle == NULL) {
icculus@7037
   137
        return SDL_SetError("Not enough resources to create thread");
slouken@1895
   138
    }
icculus@7037
   139
    return 0;
slouken@0
   140
}
slouken@0
   141
icculus@5969
   142
#ifdef _MSC_VER
icculus@5969
   143
#pragma pack(push,8)
icculus@5969
   144
typedef struct tagTHREADNAME_INFO
icculus@5969
   145
{
icculus@5969
   146
    DWORD dwType; /* must be 0x1000 */
icculus@5969
   147
    LPCSTR szName; /* pointer to name (in user addr space) */
icculus@5969
   148
    DWORD dwThreadID; /* thread ID (-1=caller thread) */
icculus@5969
   149
    DWORD dwFlags; /* reserved for future use, must be zero */
icculus@5969
   150
} THREADNAME_INFO;
icculus@5969
   151
#pragma pack(pop)
icculus@5969
   152
#endif
icculus@5969
   153
slouken@1895
   154
void
icculus@5969
   155
SDL_SYS_SetupThread(const char *name)
slouken@0
   156
{
icculus@6000
   157
    if (name != NULL) {
icculus@6000
   158
        #if 0  /* !!! FIXME: __except needs C runtime, which we don't link against. */
icculus@6000
   159
        #ifdef _MSC_VER  /* !!! FIXME: can we do SEH on other compilers yet? */
icculus@6000
   160
        /* This magic tells the debugger to name a thread if it's listening. */
icculus@6000
   161
        THREADNAME_INFO inf;
icculus@6000
   162
        inf.dwType = 0x1000;
icculus@6000
   163
        inf.szName = name;
icculus@6000
   164
        inf.dwThreadID = (DWORD) -1;
icculus@6000
   165
        inf.dwFlags = 0;
icculus@5969
   166
icculus@6000
   167
        __try
icculus@6000
   168
        {
icculus@6000
   169
            RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
icculus@6000
   170
        }
icculus@6000
   171
        __except(EXCEPTION_CONTINUE_EXECUTION)
icculus@6000
   172
        {
icculus@6000
   173
            /* The program itself should ignore this bogus exception. */
icculus@6000
   174
        }
icculus@6000
   175
        #endif
icculus@6000
   176
        #endif
icculus@5969
   177
    }
slouken@0
   178
}
slouken@0
   179
slouken@3578
   180
SDL_threadID
slouken@1895
   181
SDL_ThreadID(void)
slouken@0
   182
{
slouken@3578
   183
    return ((SDL_threadID) GetCurrentThreadId());
slouken@0
   184
}
slouken@0
   185
slouken@5506
   186
int
slouken@5509
   187
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
slouken@5506
   188
{
slouken@5509
   189
    int value;
slouken@5506
   190
slouken@5506
   191
    if (priority == SDL_THREAD_PRIORITY_LOW) {
slouken@5509
   192
        value = THREAD_PRIORITY_LOWEST;
slouken@5506
   193
    } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
slouken@5509
   194
        value = THREAD_PRIORITY_HIGHEST;
slouken@5506
   195
    } else {
slouken@5509
   196
        value = THREAD_PRIORITY_NORMAL;
slouken@5506
   197
    }
slouken@5509
   198
    if (!SetThreadPriority(GetCurrentThread(), value)) {
icculus@7037
   199
        return WIN_SetError("SetThreadPriority()");
slouken@5506
   200
    }
slouken@5506
   201
    return 0;
slouken@5506
   202
}
slouken@5506
   203
slouken@1895
   204
void
slouken@1895
   205
SDL_SYS_WaitThread(SDL_Thread * thread)
slouken@0
   206
{
slouken@1895
   207
    WaitForSingleObject(thread->handle, INFINITE);
slouken@1895
   208
    CloseHandle(thread->handle);
slouken@0
   209
}
slouken@0
   210
slouken@6044
   211
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
   212
slouken@1895
   213
/* vi: set ts=4 sw=4 expandtab: */