Added a hint to specify new thread stack size (thanks, Gabriel!).
authorRyan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 21:13:27 -0400
changeset 9648e6bf740c92f8
parent 9647 5ab832f7bc38
child 9649 d7762e30ba24
Added a hint to specify new thread stack size (thanks, Gabriel!).

Fixes Bugzilla #2019.

(we'll do a better fix when we break the API in SDL 2.1.)
include/SDL_hints.h
src/SDL.c
src/thread/pthread/SDL_systhread.c
test/torturethread.c
     1.1 --- a/include/SDL_hints.h	Tue May 26 21:09:48 2015 -0400
     1.2 +++ b/include/SDL_hints.h	Tue May 26 21:13:27 2015 -0400
     1.3 @@ -348,6 +348,18 @@
     1.4  #define SDL_HINT_TIMER_RESOLUTION "SDL_TIMER_RESOLUTION"
     1.5  
     1.6  
     1.7 +
     1.8 +/**
     1.9 +*  \brief  A string specifying SDL's threads stack size in bytes or "-1" for the backend's default size
    1.10 +*
    1.11 +*  Use this hint in case you need to set SDL's threads stack size to other than the default.
    1.12 +*  This is specially useful if you build SDL against a non glibc libc library (such as musl) which
    1.13 +*  provides a relatively small default thread stack size (a few kilobytes versus the default 8MB glibc uses).
    1.14 +*  Support for this hint is currenly available only in the pthread backend.
    1.15 +*  As a precaution, this hint can not be set via an environment variable.
    1.16 +*/
    1.17 +#define SDL_HINT_THREAD_STACK_SIZE              "SDL_THREAD_STACK_SIZE"
    1.18 +
    1.19  /**
    1.20   *  \brief If set to 1, then do not allow high-DPI windows. ("Retina" on Mac and iOS)
    1.21   */
     2.1 --- a/src/SDL.c	Tue May 26 21:09:48 2015 -0400
     2.2 +++ b/src/SDL.c	Tue May 26 21:13:27 2015 -0400
     2.3 @@ -107,6 +107,7 @@
     2.4  int
     2.5  SDL_InitSubSystem(Uint32 flags)
     2.6  {
     2.7 +    static Uint32 hints_initialized = SDL_FALSE;
     2.8      if (!SDL_MainIsReady) {
     2.9          SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
    2.10          return -1;
    2.11 @@ -114,6 +115,13 @@
    2.12  
    2.13      /* Clear the error message */
    2.14      SDL_ClearError();
    2.15 +    
    2.16 +    if (hints_initialized == SDL_FALSE) {
    2.17 +        /* Set a default of -1 for SDL_HINT_THREAD_STACK_SIZE to prevent the
    2.18 +           end user from interfering it's value with environment variables */
    2.19 +        SDL_SetHintWithPriority(SDL_HINT_THREAD_STACK_SIZE, "-1", SDL_HINT_OVERRIDE);
    2.20 +        hints_initialized = SDL_TRUE;
    2.21 +    }
    2.22  
    2.23  #if SDL_VIDEO_DRIVER_WINDOWS
    2.24  	if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
     3.1 --- a/src/thread/pthread/SDL_systhread.c	Tue May 26 21:09:48 2015 -0400
     3.2 +++ b/src/thread/pthread/SDL_systhread.c	Tue May 26 21:13:27 2015 -0400
     3.3 @@ -45,6 +45,7 @@
     3.4  
     3.5  #include "SDL_platform.h"
     3.6  #include "SDL_thread.h"
     3.7 +#include "SDL_hints.h"
     3.8  #include "../SDL_thread_c.h"
     3.9  #include "../SDL_systhread.h"
    3.10  #ifdef __ANDROID__
    3.11 @@ -86,6 +87,8 @@
    3.12  SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    3.13  {
    3.14      pthread_attr_t type;
    3.15 +    size_t ss;
    3.16 +    const char *hint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
    3.17  
    3.18      /* do this here before any threads exist, so there's no race condition. */
    3.19      #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
    3.20 @@ -105,6 +108,13 @@
    3.21          return SDL_SetError("Couldn't initialize pthread attributes");
    3.22      }
    3.23      pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    3.24 +    
    3.25 +    /* If the SDL_HINT_THREAD_STACK_SIZE exists and it seems to be a positive number, use it */
    3.26 +    if (hint && hint[0] >= '0' && hint[0] <= '9') {
    3.27 +        pthread_attr_setstacksize(&type, (size_t)SDL_atoi(hint));
    3.28 +    }
    3.29 +    
    3.30 +    pthread_attr_getstacksize(&type, &ss);
    3.31  
    3.32      /* Create the thread and go! */
    3.33      if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
     4.1 --- a/test/torturethread.c	Tue May 26 21:09:48 2015 -0400
     4.2 +++ b/test/torturethread.c	Tue May 26 21:13:27 2015 -0400
     4.3 @@ -87,6 +87,8 @@
     4.4          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
     4.5          return (1);
     4.6      }
     4.7 +    
     4.8 +    SDL_SetHintWithPriority(SDL_HINT_THREAD_STACK_SIZE, SDL_getenv(SDL_HINT_THREAD_STACK_SIZE), SDL_HINT_OVERRIDE);
     4.9  
    4.10      signal(SIGSEGV, SIG_DFL);
    4.11      for (i = 0; i < NUMTHREADS; i++) {