00001 /* 00002 SDL - Simple DirectMedia Layer 00003 Copyright (C) 1997-2009 Sam Lantinga 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 00019 Sam Lantinga 00020 slouken@libsdl.org 00021 */ 00022 00023 #ifndef _SDL_thread_h 00024 #define _SDL_thread_h 00025 00032 #include "SDL_stdinc.h" 00033 #include "SDL_error.h" 00034 00035 /* Thread synchronization primitives */ 00036 #include "SDL_mutex.h" 00037 00038 #include "begin_code.h" 00039 /* Set up for C function definitions, even when using C++ */ 00040 #ifdef __cplusplus 00041 /* *INDENT-OFF* */ 00042 extern "C" { 00043 /* *INDENT-ON* */ 00044 #endif 00045 00046 /* The SDL thread structure, defined in SDL_thread.c */ 00047 struct SDL_Thread; 00048 typedef struct SDL_Thread SDL_Thread; 00049 00050 /* Create a thread */ 00051 #if defined(__WIN32__) && !defined(HAVE_LIBC) 00052 /* 00053 We compile SDL into a DLL. This means, that it's the DLL which 00054 creates a new thread for the calling process with the SDL_CreateThread() 00055 API. There is a problem with this, that only the RTL of the SDL.DLL will 00056 be initialized for those threads, and not the RTL of the calling application! 00057 To solve this, we make a little hack here. 00058 We'll always use the caller's _beginthread() and _endthread() APIs to 00059 start a new thread. This way, if it's the SDL.DLL which uses this API, 00060 then the RTL of SDL.DLL will be used to create the new thread, and if it's 00061 the application, then the RTL of the application will be used. 00062 So, in short: 00063 Always use the _beginthread() and _endthread() of the calling runtime library! 00064 */ 00065 #define SDL_PASSED_BEGINTHREAD_ENDTHREAD 00066 #ifndef _WIN32_WCE 00067 #include <process.h> /* This has _beginthread() and _endthread() defined! */ 00068 #endif 00069 00070 #ifdef __GNUC__ 00071 typedef unsigned long (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned, 00072 unsigned 00073 (__stdcall * 00074 func) (void *), 00075 void *arg, 00076 unsigned, 00077 unsigned 00078 *threadID); 00079 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code); 00080 #else 00081 typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned, 00082 unsigned (__stdcall * 00083 func) (void 00084 *), 00085 void *arg, unsigned, 00086 unsigned *threadID); 00087 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code); 00088 #endif 00089 00090 extern DECLSPEC SDL_Thread *SDLCALL 00091 SDL_CreateThread(int (SDLCALL * f) (void *), void *data, 00092 pfnSDL_CurrentBeginThread pfnBeginThread, 00093 pfnSDL_CurrentEndThread pfnEndThread); 00094 00095 #if defined(_WIN32_WCE) 00096 #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, NULL, NULL) 00097 #else 00098 #define SDL_CreateThread(fn, data) SDL_CreateThread(fn, data, _beginthreadex, _endthreadex) 00099 #endif 00100 #else 00101 extern DECLSPEC SDL_Thread *SDLCALL 00102 SDL_CreateThread(int (SDLCALL * fn) (void *), void *data); 00103 #endif 00104 00105 /* Get the 32-bit thread identifier for the current thread */ 00106 extern DECLSPEC Uint32 SDLCALL SDL_ThreadID(void); 00107 00108 /* Get the 32-bit thread identifier for the specified thread, 00109 equivalent to SDL_ThreadID() if the specified thread is NULL. 00110 */ 00111 extern DECLSPEC Uint32 SDLCALL SDL_GetThreadID(SDL_Thread * thread); 00112 00113 /* Wait for a thread to finish. 00114 The return code for the thread function is placed in the area 00115 pointed to by 'status', if 'status' is not NULL. 00116 */ 00117 extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status); 00118 00119 /* This function is here for binary compatibility with legacy apps, but 00120 in SDL 1.3 and later, it's a no-op. You cannot forcibly kill a thread 00121 in a safe manner on many platforms. You should instead find a way to 00122 alert your thread that it is time to terminate, and then have it gracefully 00123 exit on its own. Do not ever call this function! 00124 */ 00125 extern DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread * thread); 00126 00127 00128 /* Ends C function definitions when using C++ */ 00129 #ifdef __cplusplus 00130 /* *INDENT-OFF* */ 00131 } 00132 /* *INDENT-ON* */ 00133 #endif 00134 #include "close_code.h" 00135 00136 #endif /* _SDL_thread_h */ 00137 00138 /* vi: set ts=4 sw=4 expandtab: */