src/thread/win32/SDL_systhread.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Mar 2006 06:00:45 +0000
changeset 1470 d47d96962fcc
parent 1433 bb6839704ed6
child 1471 9fb0eee04dd9
permissions -rw-r--r--
Fixed declarations to match mingw includes
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1330
    33
typedef struct ThreadStartParms
slouken@1330
    34
{
slouken@1330
    35
  void *args;
slouken@1330
    36
  pfnSDL_CurrentEndThread pfnCurrentEndThread;
slouken@1330
    37
} tThreadStartParms, *pThreadStartParms;
slouken@0
    38
icculus@1225
    39
static unsigned __stdcall RunThread(void *data)
slouken@0
    40
{
slouken@1330
    41
  pThreadStartParms pThreadParms = (pThreadStartParms)data;
slouken@1330
    42
  pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
slouken@1330
    43
slouken@1330
    44
  // Call the thread function!
slouken@1330
    45
  SDL_RunThread(pThreadParms->args);
slouken@1330
    46
slouken@1330
    47
  // Get the current endthread we have to use!
slouken@1330
    48
  if (pThreadParms)
slouken@1330
    49
  {
slouken@1330
    50
    pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
slouken@1336
    51
    SDL_free(pThreadParms);
slouken@1330
    52
  }
slouken@1330
    53
  // Call endthread!
slouken@1330
    54
  if (pfnCurrentEndThread)
slouken@1330
    55
    (*pfnCurrentEndThread)(0);
slouken@1330
    56
  return(0);
slouken@0
    57
}
slouken@0
    58
slouken@1330
    59
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread)
slouken@0
    60
{
icculus@1225
    61
	unsigned threadid;
slouken@1470
    62
	pThreadStartParms pThreadParms = (pThreadStartParms)SDL_malloc(sizeof(tThreadStartParms));
slouken@1470
    63
	if (!pThreadParms) {
slouken@1330
    64
		SDL_OutOfMemory();
slouken@1470
    65
		return(-1);
slouken@1470
    66
	}
slouken@0
    67
slouken@1470
    68
	// Save the function which we will have to call to clear the RTL of calling app!
slouken@1470
    69
	pThreadParms->pfnCurrentEndThread = pfnEndThread;
slouken@1470
    70
	// Also save the real parameters we have to pass to thread function
slouken@1470
    71
	pThreadParms->args = args;
slouken@1330
    72
slouken@1330
    73
	if (pfnBeginThread) {
slouken@1330
    74
		thread->handle = (SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread,
slouken@1330
    75
				pThreadParms, 0, &threadid);
slouken@1330
    76
	} else {
slouken@1330
    77
		thread->handle = CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid);
slouken@1330
    78
	}
slouken@0
    79
	if (thread->handle == NULL) {
slouken@0
    80
		SDL_SetError("Not enough resources to create thread");
slouken@0
    81
		return(-1);
slouken@0
    82
	}
slouken@0
    83
	return(0);
slouken@0
    84
}
slouken@0
    85
slouken@0
    86
void SDL_SYS_SetupThread(void)
slouken@0
    87
{
slouken@0
    88
	return;
slouken@0
    89
}
slouken@0
    90
slouken@0
    91
Uint32 SDL_ThreadID(void)
slouken@0
    92
{
slouken@0
    93
	return((Uint32)GetCurrentThreadId());
slouken@0
    94
}
slouken@0
    95
slouken@0
    96
void SDL_SYS_WaitThread(SDL_Thread *thread)
slouken@0
    97
{
slouken@0
    98
	WaitForSingleObject(thread->handle, INFINITE);
slouken@0
    99
	CloseHandle(thread->handle);
slouken@0
   100
}
slouken@0
   101
slouken@0
   102
/* WARNING: This function is really a last resort.
slouken@0
   103
 * Threads should be signaled and then exit by themselves.
slouken@0
   104
 * TerminateThread() doesn't perform stack and DLL cleanup.
slouken@0
   105
 */
slouken@0
   106
void SDL_SYS_KillThread(SDL_Thread *thread)
slouken@0
   107
{
slouken@0
   108
	TerminateThread(thread->handle, FALSE);
slouken@0
   109
}