threads: Move SDL's own thread creation to a new internal API.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 12 Apr 2016 16:45:10 -0400
changeset 10146471eb08040ce
parent 10145 cd87106d1d47
child 10147 ddbdc9c1b92f
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.
src/audio/SDL_audio.c
src/audio/pulseaudio/SDL_pulseaudio.c
src/haptic/windows/SDL_xinputhaptic.c
src/joystick/psp/SDL_sysjoystick.c
src/joystick/windows/SDL_windowsjoystick.c
src/main/haiku/SDL_BeApp.cc
src/thread/SDL_systhread.h
src/thread/SDL_thread.c
src/thread/SDL_thread_c.h
src/timer/SDL_timer.c
src/video/cocoa/SDL_cocoamousetap.m
src/video/psp/SDL_pspevents.c
src/video/winrt/SDL_winrtevents.cpp
     1.1 --- a/src/audio/SDL_audio.c	Tue Apr 12 14:38:50 2016 -0400
     1.2 +++ b/src/audio/SDL_audio.c	Tue Apr 12 16:45:10 2016 -0400
     1.3 @@ -27,6 +27,7 @@
     1.4  #include "SDL_audio_c.h"
     1.5  #include "SDL_audiomem.h"
     1.6  #include "SDL_sysaudio.h"
     1.7 +#include "../thread/SDL_systhread.h"
     1.8  
     1.9  #define _THIS SDL_AudioDevice *_this
    1.10  
    1.11 @@ -1191,19 +1192,15 @@
    1.12      /* Start the audio thread if necessary */
    1.13      if (!current_audio.impl.ProvidesOwnCallbackThread) {
    1.14          /* Start the audio thread */
    1.15 +
    1.16 +        /* !!! FIXME: we don't force the audio thread stack size here because it calls into user code, but maybe we should? */
    1.17 +        /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
    1.18          char name[64];
    1.19 +        const size_t stacksize = (device->spec.callback == SDL_BufferQueueDrainCallback) ? 64 * 1024 : 0;
    1.20 +
    1.21          SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) device->id);
    1.22 -/* !!! FIXME: this is nasty. */
    1.23 -#if defined(__WIN32__) && !defined(HAVE_LIBC)
    1.24 -#undef SDL_CreateThread
    1.25 -#if SDL_DYNAMIC_API
    1.26 -        device->thread = SDL_CreateThread_REAL(SDL_RunAudio, name, device, NULL, NULL);
    1.27 -#else
    1.28 -        device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
    1.29 -#endif
    1.30 -#else
    1.31 -        device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
    1.32 -#endif
    1.33 +        device->thread = SDL_CreateThreadInternal(SDL_RunAudio, name, stacksize, device);
    1.34 +
    1.35          if (device->thread == NULL) {
    1.36              SDL_CloseAudioDevice(device->id);
    1.37              SDL_SetError("Couldn't create audio thread");
     2.1 --- a/src/audio/pulseaudio/SDL_pulseaudio.c	Tue Apr 12 14:38:50 2016 -0400
     2.2 +++ b/src/audio/pulseaudio/SDL_pulseaudio.c	Tue Apr 12 16:45:10 2016 -0400
     2.3 @@ -46,6 +46,7 @@
     2.4  #include "../SDL_audio_c.h"
     2.5  #include "SDL_pulseaudio.h"
     2.6  #include "SDL_loadso.h"
     2.7 +#include "../thread/SDL_systhread.h"
     2.8  
     2.9  #if (PA_API_VERSION < 12)
    2.10  /** Return non-zero if the passed state is one of the connected states */
    2.11 @@ -646,7 +647,7 @@
    2.12      WaitForPulseOperation(hotplug_mainloop, PULSEAUDIO_pa_context_get_source_info_list(hotplug_context, SourceInfoCallback, NULL));
    2.13  
    2.14      /* ok, we have a sane list, let's set up hotplug notifications now... */
    2.15 -    hotplug_thread = SDL_CreateThread(HotplugThread, "PulseHotplug", NULL);
    2.16 +    hotplug_thread = SDL_CreateThreadInternal(HotplugThread, "PulseHotplug", 256 * 1024, NULL);
    2.17  }
    2.18  
    2.19  static void
     3.1 --- a/src/haptic/windows/SDL_xinputhaptic.c	Tue Apr 12 14:38:50 2016 -0400
     3.2 +++ b/src/haptic/windows/SDL_xinputhaptic.c	Tue Apr 12 16:45:10 2016 -0400
     3.3 @@ -33,6 +33,7 @@
     3.4  #include "SDL_xinputhaptic_c.h"
     3.5  #include "../../core/windows/SDL_xinput.h"
     3.6  #include "../../joystick/windows/SDL_windowsjoystick_c.h"
     3.7 +#include "../thread/SDL_systhread.h"
     3.8  
     3.9  /*
    3.10   * Internal stuff.
    3.11 @@ -205,17 +206,8 @@
    3.12      }
    3.13  
    3.14      SDL_snprintf(threadName, sizeof(threadName), "SDLXInputDev%d", (int)userid);
    3.15 +    haptic->hwdata->thread = SDL_CreateThreadInternal(SDL_RunXInputHaptic, threadName, 64 * 1024, haptic->hwdata);
    3.16  
    3.17 -#if defined(__WIN32__) && !defined(HAVE_LIBC)  /* !!! FIXME: this is nasty. */
    3.18 -#undef SDL_CreateThread
    3.19 -#if SDL_DYNAMIC_API
    3.20 -    haptic->hwdata->thread = SDL_CreateThread_REAL(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
    3.21 -#else
    3.22 -    haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
    3.23 -#endif
    3.24 -#else
    3.25 -    haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata);
    3.26 -#endif
    3.27      if (haptic->hwdata->thread == NULL) {
    3.28          SDL_DestroyMutex(haptic->hwdata->mutex);
    3.29          SDL_free(haptic->effects);
     4.1 --- a/src/joystick/psp/SDL_sysjoystick.c	Tue Apr 12 14:38:50 2016 -0400
     4.2 +++ b/src/joystick/psp/SDL_sysjoystick.c	Tue Apr 12 16:45:10 2016 -0400
     4.3 @@ -34,9 +34,9 @@
     4.4  
     4.5  #include "SDL_events.h"
     4.6  #include "SDL_error.h"
     4.7 -#include "SDL_thread.h"
     4.8  #include "SDL_mutex.h"
     4.9  #include "SDL_timer.h"
    4.10 +#include "../thread/SDL_systhread.h"
    4.11  
    4.12  /* Current pad state */
    4.13  static SceCtrlData pad = { .Lx = 0, .Ly = 0, .Buttons = 0 };
    4.14 @@ -116,7 +116,7 @@
    4.15          return SDL_SetError("Can't create input semaphore");
    4.16      }
    4.17      running = 1;
    4.18 -    if((thread = SDL_CreateThread(JoystickUpdate, "JoySitckThread",NULL)) == NULL) {
    4.19 +    if((thread = SDL_CreateThreadInternal(JoystickUpdate, "JoystickThread", 4096, NULL)) == NULL) {
    4.20          return SDL_SetError("Can't create input thread");
    4.21      }
    4.22  
     5.1 --- a/src/joystick/windows/SDL_windowsjoystick.c	Tue Apr 12 14:38:50 2016 -0400
     5.2 +++ b/src/joystick/windows/SDL_windowsjoystick.c	Tue Apr 12 16:45:10 2016 -0400
     5.3 @@ -35,13 +35,13 @@
     5.4  #include "SDL_error.h"
     5.5  #include "SDL_assert.h"
     5.6  #include "SDL_events.h"
     5.7 -#include "SDL_thread.h"
     5.8  #include "SDL_timer.h"
     5.9  #include "SDL_mutex.h"
    5.10  #include "SDL_events.h"
    5.11  #include "SDL_hints.h"
    5.12  #include "SDL_joystick.h"
    5.13  #include "../SDL_sysjoystick.h"
    5.14 +#include "../thread/SDL_systhread.h"
    5.15  #if !SDL_EVENTS_DISABLED
    5.16  #include "../../events/SDL_events_c.h"
    5.17  #endif
    5.18 @@ -301,18 +301,9 @@
    5.19      SDL_SYS_JoystickDetect();
    5.20  
    5.21      if (!s_threadJoystick) {
    5.22 +        /* spin up the thread to detect hotplug of devices */
    5.23          s_bJoystickThreadQuit = SDL_FALSE;
    5.24 -        /* spin up the thread to detect hotplug of devices */
    5.25 -#if defined(__WIN32__) && !defined(HAVE_LIBC)
    5.26 -#undef SDL_CreateThread
    5.27 -#if SDL_DYNAMIC_API
    5.28 -        s_threadJoystick= SDL_CreateThread_REAL(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
    5.29 -#else
    5.30 -        s_threadJoystick= SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
    5.31 -#endif
    5.32 -#else
    5.33 -        s_threadJoystick = SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL);
    5.34 -#endif
    5.35 +        s_threadJoystick = SDL_CreateThreadInternal(SDL_JoystickThread, "SDL_joystick", 64 * 1024, NULL);
    5.36      }
    5.37      return SDL_SYS_NumJoysticks();
    5.38  }
     6.1 --- a/src/main/haiku/SDL_BeApp.cc	Tue Apr 12 14:38:50 2016 -0400
     6.2 +++ b/src/main/haiku/SDL_BeApp.cc	Tue Apr 12 16:45:10 2016 -0400
     6.3 @@ -31,9 +31,9 @@
     6.4  
     6.5  #include "SDL_BApp.h"	/* SDL_BApp class definition */
     6.6  #include "SDL_BeApp.h"
     6.7 -#include "SDL_thread.h"
     6.8  #include "SDL_timer.h"
     6.9  #include "SDL_error.h"
    6.10 +#include "../thread/SDL_systhread.h"
    6.11  
    6.12  #include "../../video/haiku/SDL_BWin.h"
    6.13  
    6.14 @@ -62,7 +62,7 @@
    6.15  {
    6.16      /* Create the BApplication that handles appserver interaction */
    6.17      if (SDL_BeAppActive <= 0) {
    6.18 -        SDL_AppThread = SDL_CreateThread(StartBeApp, "SDLApplication", NULL);
    6.19 +        SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
    6.20          if (SDL_AppThread == NULL) {
    6.21              return SDL_SetError("Couldn't create BApplication thread");
    6.22          }
     7.1 --- a/src/thread/SDL_systhread.h	Tue Apr 12 14:38:50 2016 -0400
     7.2 +++ b/src/thread/SDL_systhread.h	Tue Apr 12 16:45:10 2016 -0400
     7.3 @@ -60,6 +60,11 @@
     7.4  /* Set the thread local storage for this thread */
     7.5  extern int SDL_SYS_SetTLSData(SDL_TLSData *data);
     7.6  
     7.7 +/* This is for internal SDL use, so we don't need #ifdefs everywhere. */
     7.8 +extern SDL_Thread *
     7.9 +SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
    7.10 +                         const size_t stacksize, void *data);
    7.11 +
    7.12  #endif /* _SDL_systhread_h */
    7.13  
    7.14  /* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/src/thread/SDL_thread.c	Tue Apr 12 14:38:50 2016 -0400
     8.2 +++ b/src/thread/SDL_thread.c	Tue Apr 12 16:45:10 2016 -0400
     8.3 @@ -423,6 +423,16 @@
     8.4  #endif
     8.5  }
     8.6  
     8.7 +SDL_Thread *
     8.8 +SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
     8.9 +                         const size_t stacksize, void *data) {
    8.10 +#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    8.11 +    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, NULL, NULL);
    8.12 +#else
    8.13 +    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
    8.14 +#endif
    8.15 +}
    8.16 +
    8.17  SDL_threadID
    8.18  SDL_GetThreadID(SDL_Thread * thread)
    8.19  {
     9.1 --- a/src/thread/SDL_thread_c.h	Tue Apr 12 14:38:50 2016 -0400
     9.2 +++ b/src/thread/SDL_thread_c.h	Tue Apr 12 16:45:10 2016 -0400
     9.3 @@ -90,19 +90,6 @@
     9.4   */
     9.5  extern int SDL_Generic_SetTLSData(SDL_TLSData *data);
     9.6  
     9.7 -/* !!! FIXME: for 2.1, remove this and make stack size part of SDL_CreateThread. */
     9.8 -#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
     9.9 -SDL_Thread *SDLCALL
    9.10 -SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    9.11 -                 const char *name, const size_t stacksize, void *data,
    9.12 -                 pfnSDL_CurrentBeginThread pfnBeginThread,
    9.13 -                 pfnSDL_CurrentEndThread pfnEndThread)
    9.14 -#else
    9.15 -SDL_Thread *SDLCALL
    9.16 -SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    9.17 -                const char *name, const size_t stacksize, void *data)
    9.18 -#endif
    9.19 -
    9.20  #endif /* _SDL_thread_c_h */
    9.21  
    9.22  /* vi: set ts=4 sw=4 expandtab: */
    10.1 --- a/src/timer/SDL_timer.c	Tue Apr 12 14:38:50 2016 -0400
    10.2 +++ b/src/timer/SDL_timer.c	Tue Apr 12 16:45:10 2016 -0400
    10.3 @@ -24,7 +24,7 @@
    10.4  #include "SDL_timer_c.h"
    10.5  #include "SDL_atomic.h"
    10.6  #include "SDL_cpuinfo.h"
    10.7 -#include "SDL_thread.h"
    10.8 +#include "../thread/SDL_systhread.h"
    10.9  
   10.10  /* #define DEBUG_TIMERS */
   10.11  
   10.12 @@ -221,17 +221,9 @@
   10.13          }
   10.14  
   10.15          SDL_AtomicSet(&data->active, 1);
   10.16 -        /* !!! FIXME: this is nasty. */
   10.17 -#if defined(__WIN32__) && !defined(HAVE_LIBC)
   10.18 -#undef SDL_CreateThread
   10.19 -#if SDL_DYNAMIC_API
   10.20 -        data->thread = SDL_CreateThread_REAL(SDL_TimerThread, name, data, NULL, NULL);
   10.21 -#else
   10.22 -        data->thread = SDL_CreateThread(SDL_TimerThread, name, data, NULL, NULL);
   10.23 -#endif
   10.24 -#else
   10.25 -        data->thread = SDL_CreateThread(SDL_TimerThread, name, data);
   10.26 -#endif
   10.27 +
   10.28 +        /* Timer threads use a callback into the app, so we can't set a limited stack size here. */
   10.29 +        data->thread = SDL_CreateThreadInternal(SDL_TimerThread, name, 0, data);
   10.30          if (!data->thread) {
   10.31              SDL_TimerQuit();
   10.32              return -1;
    11.1 --- a/src/video/cocoa/SDL_cocoamousetap.m	Tue Apr 12 14:38:50 2016 -0400
    11.2 +++ b/src/video/cocoa/SDL_cocoamousetap.m	Tue Apr 12 16:45:10 2016 -0400
    11.3 @@ -32,8 +32,8 @@
    11.4  #if SDL_MAC_NO_SANDBOX
    11.5  
    11.6  #include "SDL_keyboard.h"
    11.7 -#include "SDL_thread.h"
    11.8  #include "SDL_cocoavideo.h"
    11.9 +#include "../thread/SDL_systhread.h"
   11.10  
   11.11  #include "../../events/SDL_mouse_c.h"
   11.12  
   11.13 @@ -202,7 +202,7 @@
   11.14  
   11.15      tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
   11.16      if (tapdata->runloopStartedSemaphore) {
   11.17 -        tapdata->thread = SDL_CreateThread(&Cocoa_MouseTapThread, "Event Tap Loop", tapdata);
   11.18 +        tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 64 * 1024, tapdata);
   11.19          if (!tapdata->thread) {
   11.20              SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
   11.21          }
    12.1 --- a/src/video/psp/SDL_pspevents.c	Tue Apr 12 14:38:50 2016 -0400
    12.2 +++ b/src/video/psp/SDL_pspevents.c	Tue Apr 12 16:45:10 2016 -0400
    12.3 @@ -31,8 +31,8 @@
    12.4  #include "../../events/SDL_keyboard_c.h"
    12.5  #include "SDL_pspvideo.h"
    12.6  #include "SDL_pspevents_c.h"
    12.7 -#include "SDL_thread.h"
    12.8  #include "SDL_keyboard.h"
    12.9 +#include "../thread/SDL_systhread.h"
   12.10  #include <psphprm.h>
   12.11  
   12.12  #ifdef PSPIRKEYB
   12.13 @@ -264,7 +264,7 @@
   12.14          return;
   12.15      }
   12.16      running = 1;
   12.17 -    if((thread = SDL_CreateThread(EventUpdate, "PSPInputThread",NULL)) == NULL) {
   12.18 +    if((thread = SDL_CreateThreadInternal(EventUpdate, "PSPInputThread", 4096, NULL)) == NULL) {
   12.19          SDL_SetError("Can't create input thread\n");
   12.20          return;
   12.21      }
    13.1 --- a/src/video/winrt/SDL_winrtevents.cpp	Tue Apr 12 14:38:50 2016 -0400
    13.2 +++ b/src/video/winrt/SDL_winrtevents.cpp	Tue Apr 12 16:45:10 2016 -0400
    13.3 @@ -38,6 +38,7 @@
    13.4  #include "../../core/winrt/SDL_winrtapp_xaml.h"
    13.5  #include "SDL_assert.h"
    13.6  #include "SDL_system.h"
    13.7 +#include "../thread/SDL_systhread.h"
    13.8  
    13.9  extern "C" {
   13.10  #include "../SDL_sysvideo.h"
   13.11 @@ -113,7 +114,7 @@
   13.12  
   13.13              _mutex = SDL_CreateMutex();
   13.14              _threadState = ThreadState_Running;
   13.15 -            _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
   13.16 +            _XAMLThread = SDL_CreateThreadInternal(WINRT_XAMLThreadMain, "SDL/XAML App Thread", 0, nullptr);
   13.17  
   13.18              SDL_LockMutex(_mutex);
   13.19              while (_threadState != ThreadState_Yielding) {