src/thread/win32/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 4427 eada7e321df6
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Win32 thread management routines for SDL */
slouken@0
    25
slouken@1433
    26
#define WIN32_LEAN_AND_MEAN
slouken@1433
    27
#include <windows.h>
slouken@0
    28
slouken@0
    29
#include "SDL_thread.h"
slouken@1361
    30
#include "../SDL_thread_c.h"
slouken@1361
    31
#include "../SDL_systhread.h"
slouken@0
    32
slouken@1471
    33
#ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1471
    34
#ifndef _WIN32_WCE
slouken@1471
    35
/* We'll use the C library from this DLL */
slouken@1471
    36
#include <process.h>
slouken@1471
    37
#endif
slouken@1471
    38
slouken@1471
    39
#if __GNUC__
slouken@1471
    40
typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1471
    41
        unsigned (__stdcall *func)(void *), void *arg, 
slouken@1471
    42
        unsigned, unsigned *threadID);
slouken@1471
    43
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
slouken@1769
    44
#elif defined(__WATCOMC__)
slouken@1769
    45
/* This is for Watcom targets except OS2 */
slouken@1769
    46
#if __WATCOMC__ < 1240
slouken@1769
    47
#define __watcall
slouken@1769
    48
#endif
slouken@1769
    49
typedef unsigned long (__watcall *pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1769
    50
        unsigned (__stdcall *func)(void *), void *arg, 
slouken@1769
    51
        unsigned, unsigned *threadID);
slouken@1769
    52
typedef void (__watcall *pfnSDL_CurrentEndThread)(unsigned code);
slouken@1471
    53
#else
slouken@1471
    54
typedef uintptr_t (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
slouken@1471
    55
        unsigned (__stdcall *func)(void *), void *arg, 
slouken@1471
    56
        unsigned, unsigned *threadID);
slouken@1471
    57
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
slouken@1471
    58
#endif
slouken@1471
    59
#endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
slouken@1471
    60
slouken@1471
    61
slouken@1330
    62
typedef struct ThreadStartParms
slouken@1330
    63
{
slouken@1330
    64
  void *args;
slouken@1330
    65
  pfnSDL_CurrentEndThread pfnCurrentEndThread;
slouken@1330
    66
} tThreadStartParms, *pThreadStartParms;
slouken@0
    67
icculus@1225
    68
static unsigned __stdcall RunThread(void *data)
slouken@0
    69
{
slouken@1330
    70
  pThreadStartParms pThreadParms = (pThreadStartParms)data;
slouken@1330
    71
  pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
slouken@1330
    72
slouken@1330
    73
  // Call the thread function!
slouken@1330
    74
  SDL_RunThread(pThreadParms->args);
slouken@1330
    75
slouken@1330
    76
  // Get the current endthread we have to use!
slouken@1330
    77
  if (pThreadParms)
slouken@1330
    78
  {
slouken@1330
    79
    pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
slouken@1336
    80
    SDL_free(pThreadParms);
slouken@1330
    81
  }
slouken@1330
    82
  // Call endthread!
slouken@1330
    83
  if (pfnCurrentEndThread)
slouken@1330
    84
    (*pfnCurrentEndThread)(0);
slouken@1330
    85
  return(0);
slouken@0
    86
}
slouken@0
    87
slouken@1471
    88
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
slouken@1330
    89
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
slouken@0
    90
{
slouken@1471
    91
#else
slouken@1471
    92
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
slouken@1471
    93
{
slouken@1471
    94
#ifdef _WIN32_WCE
slouken@1471
    95
	pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
slouken@1471
    96
	pfnSDL_CurrentEndThread pfnEndThread = NULL;
slouken@1471
    97
#else
slouken@1471
    98
	pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
slouken@1471
    99
	pfnSDL_CurrentEndThread pfnEndThread = _endthreadex;
slouken@1471
   100
#endif
slouken@1471
   101
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
icculus@1225
   102
	unsigned threadid;
slouken@1470
   103
	pThreadStartParms pThreadParms = (pThreadStartParms)SDL_malloc(sizeof(tThreadStartParms));
slouken@1470
   104
	if (!pThreadParms) {
slouken@1330
   105
		SDL_OutOfMemory();
slouken@1470
   106
		return(-1);
slouken@1470
   107
	}
slouken@0
   108
slouken@1470
   109
	// Save the function which we will have to call to clear the RTL of calling app!
slouken@1470
   110
	pThreadParms->pfnCurrentEndThread = pfnEndThread;
slouken@1470
   111
	// Also save the real parameters we have to pass to thread function
slouken@1470
   112
	pThreadParms->args = args;
slouken@1330
   113
slouken@1330
   114
	if (pfnBeginThread) {
slouken@1330
   115
		thread->handle = (SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
slouken@1330
   116
				pThreadParms, 0, &threadid);
slouken@1330
   117
	} else {
slouken@1330
   118
		thread->handle = CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid);
slouken@1330
   119
	}
slouken@0
   120
	if (thread->handle == NULL) {
slouken@0
   121
		SDL_SetError("Not enough resources to create thread");
slouken@0
   122
		return(-1);
slouken@0
   123
	}
slouken@0
   124
	return(0);
slouken@0
   125
}
slouken@0
   126
slouken@0
   127
void SDL_SYS_SetupThread(void)
slouken@0
   128
{
slouken@0
   129
	return;
slouken@0
   130
}
slouken@0
   131
slouken@0
   132
Uint32 SDL_ThreadID(void)
slouken@0
   133
{
slouken@0
   134
	return((Uint32)GetCurrentThreadId());
slouken@0
   135
}
slouken@0
   136
slouken@0
   137
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@0
   138
{
slouken@0
   139
	WaitForSingleObject(thread->handle, INFINITE);
slouken@0
   140
	CloseHandle(thread->handle);
slouken@0
   141
}
slouken@0
   142
slouken@0
   143
/* WARNING: This function is really a last resort.
slouken@0
   144
 * Threads should be signaled and then exit by themselves.
slouken@0
   145
 * TerminateThread() doesn't perform stack and DLL cleanup.
slouken@0
   146
 */
slouken@0
   147
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@0
   148
{
slouken@0
   149
	TerminateThread(thread->handle, FALSE);
slouken@0
   150
}