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.
This commit is contained in:
Ryan C. Gordon 2016-04-12 16:45:10 -04:00
parent 7ae2951fca
commit c61675dc5d
13 changed files with 44 additions and 68 deletions

View File

@ -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");

View File

@ -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

View File

@ -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);

View File

@ -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");
}

View File

@ -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();
}

View File

@ -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");
}

View File

@ -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: */

View File

@ -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)
{

View File

@ -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: */

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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) {