threads: Move SDL's own thread creation to a new internal API. This allows us to set an explicit stack size (overriding the system default and the global hint an app might have set), and remove all the macro salsa for dealing with _beginthreadex and such, as internal threads always set those to NULL anyhow. I've taken some guesses on reasonable (and tiny!) stack sizes for our internal threads, but some of these might turn out to be too small in practice and need an increase. Most of them are simple functions, though.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 2ffd216..05674eb 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -27,6 +27,7 @@
#include "SDL_audio_c.h"
#include "SDL_audiomem.h"
#include "SDL_sysaudio.h"
+#include "../thread/SDL_systhread.h"
#define _THIS SDL_AudioDevice *_this
@@ -1191,19 +1192,15 @@ open_audio_device(const char *devname, int iscapture,
/* Start the audio thread if necessary */
if (!current_audio.impl.ProvidesOwnCallbackThread) {
/* Start the audio thread */
+
+ /* !!! FIXME: we don't force the audio thread stack size here because it calls into user code, but maybe we should? */
+ /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
char name[64];
+ const size_t stacksize = (device->spec.callback == SDL_BufferQueueDrainCallback) ? 64 * 1024 : 0;
+
SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) device->id);
-/* !!! FIXME: this is nasty. */
-#if defined(__WIN32__) && !defined(HAVE_LIBC)
-#undef SDL_CreateThread
-#if SDL_DYNAMIC_API
- device->thread = SDL_CreateThread_REAL(SDL_RunAudio, name, device, NULL, NULL);
-#else
- device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
-#endif
-#else
- device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
-#endif
+ device->thread = SDL_CreateThreadInternal(SDL_RunAudio, name, stacksize, device);
+
if (device->thread == NULL) {
SDL_CloseAudioDevice(device->id);
SDL_SetError("Couldn't create audio thread");
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 6b11e06..00aad1b 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -46,6 +46,7 @@
#include "../SDL_audio_c.h"
#include "SDL_pulseaudio.h"
#include "SDL_loadso.h"
+#include "../thread/SDL_systhread.h"
#if (PA_API_VERSION < 12)
/** Return non-zero if the passed state is one of the connected states */
@@ -646,7 +647,7 @@ PULSEAUDIO_DetectDevices()
WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_source_info_list(hotplug_context, SourceInfoCallback, NULL));
/* ok, we have a sane list, let's set up hotplug notifications now... */
- hotplug_thread = SDL_CreateThread(HotplugThread, "PulseHotplug", NULL);
+ hotplug_thread = SDL_CreateThreadInternal(HotplugThread, "PulseHotplug", 256 * 1024, NULL);
}
static void
diff --git a/src/haptic/windows/SDL_xinputhaptic.c b/src/haptic/windows/SDL_xinputhaptic.c
index aefbabb..3a2bbfb 100644
--- a/src/haptic/windows/SDL_xinputhaptic.c
+++ b/src/haptic/windows/SDL_xinputhaptic.c
@@ -33,6 +33,7 @@
#include "SDL_xinputhaptic_c.h"
#include "../../core/windows/SDL_xinput.h"
#include "../../joystick/windows/SDL_windowsjoystick_c.h"
+#include "../thread/SDL_systhread.h"
/*
* Internal stuff.
@@ -205,17 +206,8 @@ SDL_XINPUT_HapticOpenFromUserIndex(SDL_Haptic *haptic, const Uint8 userid)
}
SDL_snprintf(threadName, sizeof(threadName), "SDLXInputDev%d", (int)userid);
+ haptic->hwdata->thread = SDL_CreateThreadInternal(SDL_RunXInputHaptic, threadName, 64 * 1024, haptic->hwdata);
-#if defined(__WIN32__) && !defined(HAVE_LIBC) /* !!! FIXME: this is nasty. */
-#undef SDL_CreateThread
-#if SDL_DYNAMIC_API
- haptic->hwdata->thread = SDL_CreateThread_REAL(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
-#else
- haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
-#endif
-#else
- haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata);
-#endif
if (haptic->hwdata->thread == NULL) {
SDL_DestroyMutex(haptic->hwdata->mutex);
SDL_free(haptic->effects);
diff --git a/src/joystick/psp/SDL_sysjoystick.c b/src/joystick/psp/SDL_sysjoystick.c
index e524725..1c7180e 100644
--- a/src/joystick/psp/SDL_sysjoystick.c
+++ b/src/joystick/psp/SDL_sysjoystick.c
@@ -34,9 +34,9 @@
#include "SDL_events.h"
#include "SDL_error.h"
-#include "SDL_thread.h"
#include "SDL_mutex.h"
#include "SDL_timer.h"
+#include "../thread/SDL_systhread.h"
/* Current pad state */
static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
@@ -116,7 +116,7 @@ int SDL_SYS_JoystickInit(void)
return SDL_SetError("Can't create input semaphore");
}
running = 1;
- if((thread = SDL_CreateThread(JoystickUpdate, "JoySitckThread",NULL)) == NULL) {
+ if((thread = SDL_CreateThreadInternal(JoystickUpdate, "JoystickThread", 4096, NULL)) == NULL) {
return SDL_SetError("Can't create input thread");
}
diff --git a/src/joystick/windows/SDL_windowsjoystick.c b/src/joystick/windows/SDL_windowsjoystick.c
index 7123c61..74a25f9 100644
--- a/src/joystick/windows/SDL_windowsjoystick.c
+++ b/src/joystick/windows/SDL_windowsjoystick.c
@@ -35,13 +35,13 @@
#include "SDL_error.h"
#include "SDL_assert.h"
#include "SDL_events.h"
-#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_mutex.h"
#include "SDL_events.h"
#include "SDL_hints.h"
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
+#include "../thread/SDL_systhread.h"
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
@@ -301,18 +301,9 @@ SDL_SYS_JoystickInit(void)
SDL_SYS_JoystickDetect();
if (!s_threadJoystick) {
- s_bJoystickThreadQuit = SDL_FALSE;
/* spin up the thread to detect hotplug of devices */
-#if defined(__WIN32__) && !defined(HAVE_LIBC)
-#undef SDL_CreateThread
-#if SDL_DYNAMIC_API
- s_threadJoystick= SDL_CreateThread_REAL(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
-#else
- s_threadJoystick= SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
-#endif
-#else
- s_threadJoystick = SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL);
-#endif
+ s_bJoystickThreadQuit = SDL_FALSE;
+ s_threadJoystick = SDL_CreateThreadInternal(SDL_JoystickThread, "SDL_joystick", 64 * 1024, NULL);
}
return SDL_SYS_NumJoysticks();
}
diff --git a/src/main/haiku/SDL_BeApp.cc b/src/main/haiku/SDL_BeApp.cc
index 36c2a1a..32fd6e8 100644
--- a/src/main/haiku/SDL_BeApp.cc
+++ b/src/main/haiku/SDL_BeApp.cc
@@ -31,9 +31,9 @@
#include "SDL_BApp.h" /* SDL_BApp class definition */
#include "SDL_BeApp.h"
-#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_error.h"
+#include "../thread/SDL_systhread.h"
#include "../../video/haiku/SDL_BWin.h"
@@ -62,7 +62,7 @@ SDL_InitBeApp(void)
{
/* Create the BApplication that handles appserver interaction */
if (SDL_BeAppActive <= 0) {
- SDL_AppThread = SDL_CreateThread(StartBeApp, "SDLApplication", NULL);
+ SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
if (SDL_AppThread == NULL) {
return SDL_SetError("Couldn't create BApplication thread");
}
diff --git a/src/thread/SDL_systhread.h b/src/thread/SDL_systhread.h
index f13f3e2..05a0125 100644
--- a/src/thread/SDL_systhread.h
+++ b/src/thread/SDL_systhread.h
@@ -60,6 +60,11 @@ extern SDL_TLSData *SDL_SYS_GetTLSData();
/* Set the thread local storage for this thread */
extern int SDL_SYS_SetTLSData(SDL_TLSData *data);
+/* This is for internal SDL use, so we don't need #ifdefs everywhere. */
+extern SDL_Thread *
+SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
+ const size_t stacksize, void *data);
+
#endif /* _SDL_systhread_h */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c
index 1306ae0..ae86579 100644
--- a/src/thread/SDL_thread.c
+++ b/src/thread/SDL_thread.c
@@ -423,6 +423,16 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
#endif
}
+SDL_Thread *
+SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
+ const size_t stacksize, void *data) {
+#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
+ return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, NULL, NULL);
+#else
+ return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
+#endif
+}
+
SDL_threadID
SDL_GetThreadID(SDL_Thread * thread)
{
diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h
index 7749183..554325d 100644
--- a/src/thread/SDL_thread_c.h
+++ b/src/thread/SDL_thread_c.h
@@ -90,19 +90,6 @@ extern SDL_TLSData *SDL_Generic_GetTLSData();
*/
extern int SDL_Generic_SetTLSData(SDL_TLSData *data);
-/* !!! FIXME: for 2.1, remove this and make stack size part of SDL_CreateThread. */
-#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
-SDL_Thread *SDLCALL
-SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
- const char *name, const size_t stacksize, void *data,
- pfnSDL_CurrentBeginThread pfnBeginThread,
- pfnSDL_CurrentEndThread pfnEndThread)
-#else
-SDL_Thread *SDLCALL
-SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
- const char *name, const size_t stacksize, void *data)
-#endif
-
#endif /* _SDL_thread_c_h */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/timer/SDL_timer.c b/src/timer/SDL_timer.c
index ea24af8..abe968e 100644
--- a/src/timer/SDL_timer.c
+++ b/src/timer/SDL_timer.c
@@ -24,7 +24,7 @@
#include "SDL_timer_c.h"
#include "SDL_atomic.h"
#include "SDL_cpuinfo.h"
-#include "SDL_thread.h"
+#include "../thread/SDL_systhread.h"
/* #define DEBUG_TIMERS */
@@ -221,17 +221,9 @@ SDL_TimerInit(void)
}
SDL_AtomicSet(&data->active, 1);
- /* !!! FIXME: this is nasty. */
-#if defined(__WIN32__) && !defined(HAVE_LIBC)
-#undef SDL_CreateThread
-#if SDL_DYNAMIC_API
- data->thread = SDL_CreateThread_REAL(SDL_TimerThread, name, data, NULL, NULL);
-#else
- data->thread = SDL_CreateThread(SDL_TimerThread, name, data, NULL, NULL);
-#endif
-#else
- data->thread = SDL_CreateThread(SDL_TimerThread, name, data);
-#endif
+
+ /* Timer threads use a callback into the app, so we can't set a limited stack size here. */
+ data->thread = SDL_CreateThreadInternal(SDL_TimerThread, name, 0, data);
if (!data->thread) {
SDL_TimerQuit();
return -1;
diff --git a/src/video/cocoa/SDL_cocoamousetap.m b/src/video/cocoa/SDL_cocoamousetap.m
index 0f29daf..0ab98ed 100644
--- a/src/video/cocoa/SDL_cocoamousetap.m
+++ b/src/video/cocoa/SDL_cocoamousetap.m
@@ -32,8 +32,8 @@
#if SDL_MAC_NO_SANDBOX
#include "SDL_keyboard.h"
-#include "SDL_thread.h"
#include "SDL_cocoavideo.h"
+#include "../thread/SDL_systhread.h"
#include "../../events/SDL_mouse_c.h"
@@ -202,7 +202,7 @@ Cocoa_InitMouseEventTap(SDL_MouseData* driverdata)
tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
if (tapdata->runloopStartedSemaphore) {
- tapdata->thread = SDL_CreateThread(&Cocoa_MouseTapThread, "Event Tap Loop", tapdata);
+ tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 64 * 1024, tapdata);
if (!tapdata->thread) {
SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
}
diff --git a/src/video/psp/SDL_pspevents.c b/src/video/psp/SDL_pspevents.c
index c1a095d..931aeaf 100644
--- a/src/video/psp/SDL_pspevents.c
+++ b/src/video/psp/SDL_pspevents.c
@@ -31,8 +31,8 @@
#include "../../events/SDL_keyboard_c.h"
#include "SDL_pspvideo.h"
#include "SDL_pspevents_c.h"
-#include "SDL_thread.h"
#include "SDL_keyboard.h"
+#include "../thread/SDL_systhread.h"
#include <psphprm.h>
#ifdef PSPIRKEYB
@@ -264,7 +264,7 @@ void PSP_EventInit(_THIS)
return;
}
running = 1;
- if((thread = SDL_CreateThread(EventUpdate, "PSPInputThread",NULL)) == NULL) {
+ if((thread = SDL_CreateThreadInternal(EventUpdate, "PSPInputThread", 4096, NULL)) == NULL) {
SDL_SetError("Can't create input thread\n");
return;
}
diff --git a/src/video/winrt/SDL_winrtevents.cpp b/src/video/winrt/SDL_winrtevents.cpp
index e9df726..093adfd 100644
--- a/src/video/winrt/SDL_winrtevents.cpp
+++ b/src/video/winrt/SDL_winrtevents.cpp
@@ -38,6 +38,7 @@ using Windows::UI::Core::CoreCursor;
#include "../../core/winrt/SDL_winrtapp_xaml.h"
#include "SDL_assert.h"
#include "SDL_system.h"
+#include "../thread/SDL_systhread.h"
extern "C" {
#include "../SDL_sysvideo.h"
@@ -113,7 +114,7 @@ WINRT_CycleXAMLThread()
_mutex = SDL_CreateMutex();
_threadState = ThreadState_Running;
- _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
+ _XAMLThread = SDL_CreateThreadInternal(WINRT_XAMLThreadMain, "SDL/XAML App Thread", 0, nullptr);
SDL_LockMutex(_mutex);
while (_threadState != ThreadState_Yielding) {