comparison src/thread/win32/SDL_systhread.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
35 /* We'll use the C library from this DLL */ 35 /* We'll use the C library from this DLL */
36 #include <process.h> 36 #include <process.h>
37 #endif 37 #endif
38 38
39 #if __GNUC__ 39 #if __GNUC__
40 typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned, 40 typedef unsigned long (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
41 unsigned (__stdcall *func)(void *), void *arg, 41 unsigned
42 unsigned, unsigned *threadID); 42 (__stdcall *
43 typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code); 43 func) (void *),
44 void *arg,
45 unsigned,
46 unsigned
47 *threadID);
48 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
44 #elif defined(__WATCOMC__) 49 #elif defined(__WATCOMC__)
45 /* This is for Watcom targets except OS2 */ 50 /* This is for Watcom targets except OS2 */
46 #if __WATCOMC__ < 1240 51 #if __WATCOMC__ < 1240
47 #define __watcall 52 #define __watcall
48 #endif 53 #endif
49 typedef unsigned long (__watcall *pfnSDL_CurrentBeginThread) (void *, unsigned, 54 typedef unsigned long (__watcall * pfnSDL_CurrentBeginThread) (void *,
50 unsigned (__stdcall *func)(void *), void *arg, 55 unsigned,
51 unsigned, unsigned *threadID); 56 unsigned
52 typedef void (__watcall *pfnSDL_CurrentEndThread)(unsigned code); 57 (__stdcall *
58 func) (void
59 *),
60 void *arg,
61 unsigned,
62 unsigned
63 *threadID);
64 typedef void (__watcall * pfnSDL_CurrentEndThread) (unsigned code);
53 #else 65 #else
54 typedef uintptr_t (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned, 66 typedef uintptr_t (__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
55 unsigned (__stdcall *func)(void *), void *arg, 67 unsigned (__stdcall *
56 unsigned, unsigned *threadID); 68 func) (void
57 typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code); 69 *),
70 void *arg, unsigned,
71 unsigned *threadID);
72 typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
58 #endif 73 #endif
59 #endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */ 74 #endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
60 75
61 76
62 typedef struct ThreadStartParms 77 typedef struct ThreadStartParms
63 { 78 {
64 void *args; 79 void *args;
65 pfnSDL_CurrentEndThread pfnCurrentEndThread; 80 pfnSDL_CurrentEndThread pfnCurrentEndThread;
66 } tThreadStartParms, *pThreadStartParms; 81 } tThreadStartParms, *pThreadStartParms;
67 82
68 static unsigned __stdcall RunThread(void *data) 83 static unsigned __stdcall
84 RunThread (void *data)
69 { 85 {
70 pThreadStartParms pThreadParms = (pThreadStartParms)data; 86 pThreadStartParms pThreadParms = (pThreadStartParms) data;
71 pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL; 87 pfnSDL_CurrentEndThread pfnCurrentEndThread = NULL;
72 88
73 // Call the thread function! 89 // Call the thread function!
74 SDL_RunThread(pThreadParms->args); 90 SDL_RunThread (pThreadParms->args);
75 91
76 // Get the current endthread we have to use! 92 // Get the current endthread we have to use!
77 if (pThreadParms) 93 if (pThreadParms) {
78 { 94 pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread;
79 pfnCurrentEndThread = pThreadParms->pfnCurrentEndThread; 95 SDL_free (pThreadParms);
80 SDL_free(pThreadParms); 96 }
81 } 97 // Call endthread!
82 // Call endthread! 98 if (pfnCurrentEndThread)
83 if (pfnCurrentEndThread) 99 (*pfnCurrentEndThread) (0);
84 (*pfnCurrentEndThread)(0); 100 return (0);
85 return(0);
86 } 101 }
87 102
88 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD 103 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
89 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args, pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentEndThread pfnEndThread) 104 int
105 SDL_SYS_CreateThread (SDL_Thread * thread, void *args,
106 pfnSDL_CurrentBeginThread pfnBeginThread,
107 pfnSDL_CurrentEndThread pfnEndThread)
90 { 108 {
91 #else 109 #else
92 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) 110 int
111 SDL_SYS_CreateThread (SDL_Thread * thread, void *args)
93 { 112 {
94 #ifdef _WIN32_WCE 113 #ifdef _WIN32_WCE
95 pfnSDL_CurrentBeginThread pfnBeginThread = NULL; 114 pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
96 pfnSDL_CurrentEndThread pfnEndThread = NULL; 115 pfnSDL_CurrentEndThread pfnEndThread = NULL;
97 #else 116 #else
98 pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex; 117 pfnSDL_CurrentBeginThread pfnBeginThread = _beginthreadex;
99 pfnSDL_CurrentEndThread pfnEndThread = _endthreadex; 118 pfnSDL_CurrentEndThread pfnEndThread = _endthreadex;
100 #endif 119 #endif
101 #endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */ 120 #endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
102 unsigned threadid; 121 unsigned threadid;
103 pThreadStartParms pThreadParms = (pThreadStartParms)SDL_malloc(sizeof(tThreadStartParms)); 122 pThreadStartParms pThreadParms =
104 if (!pThreadParms) { 123 (pThreadStartParms) SDL_malloc (sizeof (tThreadStartParms));
105 SDL_OutOfMemory(); 124 if (!pThreadParms) {
106 return(-1); 125 SDL_OutOfMemory ();
107 } 126 return (-1);
127 }
128 // Save the function which we will have to call to clear the RTL of calling app!
129 pThreadParms->pfnCurrentEndThread = pfnEndThread;
130 // Also save the real parameters we have to pass to thread function
131 pThreadParms->args = args;
108 132
109 // Save the function which we will have to call to clear the RTL of calling app! 133 if (pfnBeginThread) {
110 pThreadParms->pfnCurrentEndThread = pfnEndThread; 134 thread->handle =
111 // Also save the real parameters we have to pass to thread function 135 (SYS_ThreadHandle) pfnBeginThread (NULL, 0, RunThread,
112 pThreadParms->args = args; 136 pThreadParms, 0, &threadid);
113 137 } else {
114 if (pfnBeginThread) { 138 thread->handle =
115 thread->handle = (SYS_ThreadHandle) pfnBeginThread(NULL, 0, RunThread, 139 CreateThread (NULL, 0, RunThread, pThreadParms, 0, &threadid);
116 pThreadParms, 0, &threadid); 140 }
117 } else { 141 if (thread->handle == NULL) {
118 thread->handle = CreateThread(NULL, 0, RunThread, pThreadParms, 0, &threadid); 142 SDL_SetError ("Not enough resources to create thread");
119 } 143 return (-1);
120 if (thread->handle == NULL) { 144 }
121 SDL_SetError("Not enough resources to create thread"); 145 return (0);
122 return(-1);
123 }
124 return(0);
125 } 146 }
126 147
127 void SDL_SYS_SetupThread(void) 148 void
149 SDL_SYS_SetupThread (void)
128 { 150 {
129 return; 151 return;
130 } 152 }
131 153
132 Uint32 SDL_ThreadID(void) 154 Uint32
155 SDL_ThreadID (void)
133 { 156 {
134 return((Uint32)GetCurrentThreadId()); 157 return ((Uint32) GetCurrentThreadId ());
135 } 158 }
136 159
137 void SDL_SYS_WaitThread(SDL_Thread *thread) 160 void
161 SDL_SYS_WaitThread (SDL_Thread * thread)
138 { 162 {
139 WaitForSingleObject(thread->handle, INFINITE); 163 WaitForSingleObject (thread->handle, INFINITE);
140 CloseHandle(thread->handle); 164 CloseHandle (thread->handle);
141 } 165 }
142 166
143 /* WARNING: This function is really a last resort. 167 /* WARNING: This function is really a last resort.
144 * Threads should be signaled and then exit by themselves. 168 * Threads should be signaled and then exit by themselves.
145 * TerminateThread() doesn't perform stack and DLL cleanup. 169 * TerminateThread() doesn't perform stack and DLL cleanup.
146 */ 170 */
147 void SDL_SYS_KillThread(SDL_Thread *thread) 171 void
172 SDL_SYS_KillThread (SDL_Thread * thread)
148 { 173 {
149 TerminateThread(thread->handle, FALSE); 174 TerminateThread (thread->handle, FALSE);
150 } 175 }
176
177 /* vi: set ts=4 sw=4 expandtab: */