threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 12 Apr 2016 14:38:50 -0400
changeset 10145cd87106d1d47
parent 10144 aa9904a56acf
child 10146 471eb08040ce
threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
src/thread/SDL_thread.c
src/thread/SDL_thread_c.h
src/thread/psp/SDL_systhread.c
src/thread/pthread/SDL_systhread.c
src/thread/stdcpp/SDL_systhread.cpp
src/thread/windows/SDL_systhread.c
     1.1 --- a/src/thread/SDL_thread.c	Mon Apr 11 00:22:39 2016 -0400
     1.2 +++ b/src/thread/SDL_thread.c	Tue Apr 12 14:38:50 2016 -0400
     1.3 @@ -26,6 +26,7 @@
     1.4  #include "SDL_thread.h"
     1.5  #include "SDL_thread_c.h"
     1.6  #include "SDL_systhread.h"
     1.7 +#include "SDL_hints.h"
     1.8  #include "../SDL_error_c.h"
     1.9  
    1.10  
    1.11 @@ -304,15 +305,15 @@
    1.12  #endif
    1.13  
    1.14  #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    1.15 -DECLSPEC SDL_Thread *SDLCALL
    1.16 -SDL_CreateThread(int (SDLCALL * fn) (void *),
    1.17 -                 const char *name, void *data,
    1.18 +static SDL_Thread *
    1.19 +SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    1.20 +                 const char *name, const size_t stacksize, void *data,
    1.21                   pfnSDL_CurrentBeginThread pfnBeginThread,
    1.22                   pfnSDL_CurrentEndThread pfnEndThread)
    1.23  #else
    1.24 -DECLSPEC SDL_Thread *SDLCALL
    1.25 -SDL_CreateThread(int (SDLCALL * fn) (void *),
    1.26 -                 const char *name, void *data)
    1.27 +static SDL_Thread *
    1.28 +SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    1.29 +                const char *name, const size_t stacksize, void *data)
    1.30  #endif
    1.31  {
    1.32      SDL_Thread *thread;
    1.33 @@ -362,6 +363,8 @@
    1.34          return (NULL);
    1.35      }
    1.36  
    1.37 +    thread->stacksize = stacksize;
    1.38 +
    1.39      /* Create the thread and go! */
    1.40  #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    1.41      ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
    1.42 @@ -386,6 +389,40 @@
    1.43      return (thread);
    1.44  }
    1.45  
    1.46 +#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    1.47 +DECLSPEC SDL_Thread *SDLCALL
    1.48 +SDL_CreateThread(int (SDLCALL * fn) (void *),
    1.49 +                 const char *name, void *data,
    1.50 +                 pfnSDL_CurrentBeginThread pfnBeginThread,
    1.51 +                 pfnSDL_CurrentEndThread pfnEndThread)
    1.52 +#else
    1.53 +DECLSPEC SDL_Thread *SDLCALL
    1.54 +SDL_CreateThread(int (SDLCALL * fn) (void *),
    1.55 +                 const char *name, void *data)
    1.56 +#endif
    1.57 +{
    1.58 +    /* !!! FIXME: in 2.1, just make stackhint part of the usual API. */
    1.59 +    const char *stackhint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
    1.60 +    size_t stacksize = 0;
    1.61 +
    1.62 +    /* If the SDL_HINT_THREAD_STACK_SIZE exists, use it */
    1.63 +    if (stackhint != NULL) {
    1.64 +        char *endp = NULL;
    1.65 +        const Sint64 hintval = SDL_strtoll(stackhint, &endp, 10);
    1.66 +        if ((*stackhint != '\0') && (*endp == '\0')) {  /* a valid number? */
    1.67 +            if (hintval > 0) {  /* reject bogus values. */
    1.68 +                stacksize = (size_t) hintval;
    1.69 +            }
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    1.74 +    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, pfnBeginThread, pfnEndThread);
    1.75 +#else
    1.76 +    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
    1.77 +#endif
    1.78 +}
    1.79 +
    1.80  SDL_threadID
    1.81  SDL_GetThreadID(SDL_Thread * thread)
    1.82  {
     2.1 --- a/src/thread/SDL_thread_c.h	Mon Apr 11 00:22:39 2016 -0400
     2.2 +++ b/src/thread/SDL_thread_c.h	Tue Apr 12 14:38:50 2016 -0400
     2.3 @@ -59,6 +59,7 @@
     2.4      SDL_atomic_t state;  /* SDL_THREAD_STATE_* */
     2.5      SDL_error errbuf;
     2.6      char *name;
     2.7 +    size_t stacksize;  /* 0 for default, >0 for user-specified stack size. */
     2.8      void *data;
     2.9  };
    2.10  
    2.11 @@ -89,6 +90,19 @@
    2.12   */
    2.13  extern int SDL_Generic_SetTLSData(SDL_TLSData *data);
    2.14  
    2.15 +/* !!! FIXME: for 2.1, remove this and make stack size part of SDL_CreateThread. */
    2.16 +#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
    2.17 +SDL_Thread *SDLCALL
    2.18 +SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    2.19 +                 const char *name, const size_t stacksize, void *data,
    2.20 +                 pfnSDL_CurrentBeginThread pfnBeginThread,
    2.21 +                 pfnSDL_CurrentEndThread pfnEndThread)
    2.22 +#else
    2.23 +SDL_Thread *SDLCALL
    2.24 +SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
    2.25 +                const char *name, const size_t stacksize, void *data)
    2.26 +#endif
    2.27 +
    2.28  #endif /* _SDL_thread_c_h */
    2.29  
    2.30  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/thread/psp/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
     3.2 +++ b/src/thread/psp/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
     3.3 @@ -52,8 +52,8 @@
     3.4          priority = status.currentPriority;
     3.5      }
     3.6  
     3.7 -    thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
     3.8 -                           priority, 0x8000,
     3.9 +    thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
    3.10 +                           priority, thread->stacksize ? ((int) stacksize) : 0x8000,
    3.11                             PSP_THREAD_ATTR_VFPU, NULL);
    3.12      if (thread->handle < 0) {
    3.13          return SDL_SetError("sceKernelCreateThread() failed");
     4.1 --- a/src/thread/pthread/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
     4.2 +++ b/src/thread/pthread/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
     4.3 @@ -45,7 +45,6 @@
     4.4  
     4.5  #include "SDL_platform.h"
     4.6  #include "SDL_thread.h"
     4.7 -#include "SDL_hints.h"
     4.8  #include "../SDL_thread_c.h"
     4.9  #include "../SDL_systhread.h"
    4.10  #ifdef __ANDROID__
    4.11 @@ -87,7 +86,6 @@
    4.12  SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    4.13  {
    4.14      pthread_attr_t type;
    4.15 -    const char *hint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
    4.16  
    4.17      /* do this here before any threads exist, so there's no race condition. */
    4.18      #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
    4.19 @@ -108,12 +106,9 @@
    4.20      }
    4.21      pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
    4.22      
    4.23 -    /* If the SDL_HINT_THREAD_STACK_SIZE exists and it seems to be a positive number, use it */
    4.24 -    if (hint && hint[0] >= '0' && hint[0] <= '9') {
    4.25 -        const size_t stacksize = (size_t) SDL_atoi(hint);
    4.26 -        if (stacksize > 0) {
    4.27 -            pthread_attr_setstacksize(&type, stacksize);
    4.28 -        }
    4.29 +    /* Set caller-requested stack size. Otherwise: use the system default. */
    4.30 +    if (thread->stacksize) {
    4.31 +        pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
    4.32      }
    4.33  
    4.34      /* Create the thread and go! */
     5.1 --- a/src/thread/stdcpp/SDL_systhread.cpp	Mon Apr 11 00:22:39 2016 -0400
     5.2 +++ b/src/thread/stdcpp/SDL_systhread.cpp	Tue Apr 12 14:38:50 2016 -0400
     5.3 @@ -48,6 +48,7 @@
     5.4  SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
     5.5  {
     5.6      try {
     5.7 +        // !!! FIXME: no way to set a thread stack size here.
     5.8          std::thread cpp_thread(RunThread, args);
     5.9          thread->handle = (void *) new std::thread(std::move(cpp_thread));
    5.10          return 0;
     6.1 --- a/src/thread/windows/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
     6.2 +++ b/src/thread/windows/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
     6.3 @@ -129,14 +129,17 @@
     6.4      /* Also save the real parameters we have to pass to thread function */
     6.5      pThreadParms->args = args;
     6.6  
     6.7 +    /* thread->stacksize == 0 means "system default", same as win32 expects */
     6.8      if (pfnBeginThread) {
     6.9          unsigned threadid = 0;
    6.10          thread->handle = (SYS_ThreadHandle)
    6.11 -            ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
    6.12 +            ((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
    6.13 +                                     RunThreadViaBeginThreadEx,
    6.14                                       pThreadParms, 0, &threadid));
    6.15      } else {
    6.16          DWORD threadid = 0;
    6.17 -        thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread,
    6.18 +        thread->handle = CreateThread(NULL, thread->stacksize,
    6.19 +                                      RunThreadViaCreateThread,
    6.20                                        pThreadParms, 0, &threadid);
    6.21      }
    6.22      if (thread->handle == NULL) {