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 10145 cd87106d1d47
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
--- a/src/thread/SDL_thread.c	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/SDL_thread.c	Tue Apr 12 14:38:50 2016 -0400
@@ -26,6 +26,7 @@
 #include "SDL_thread.h"
 #include "SDL_thread_c.h"
 #include "SDL_systhread.h"
+#include "SDL_hints.h"
 #include "../SDL_error_c.h"
 
 
@@ -304,15 +305,15 @@
 #endif
 
 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
-DECLSPEC SDL_Thread *SDLCALL
-SDL_CreateThread(int (SDLCALL * fn) (void *),
-                 const char *name, void *data,
+static SDL_Thread *
+SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
+                 const char *name, const size_t stacksize, void *data,
                  pfnSDL_CurrentBeginThread pfnBeginThread,
                  pfnSDL_CurrentEndThread pfnEndThread)
 #else
-DECLSPEC SDL_Thread *SDLCALL
-SDL_CreateThread(int (SDLCALL * fn) (void *),
-                 const char *name, void *data)
+static SDL_Thread *
+SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
+                const char *name, const size_t stacksize, void *data)
 #endif
 {
     SDL_Thread *thread;
@@ -362,6 +363,8 @@
         return (NULL);
     }
 
+    thread->stacksize = stacksize;
+
     /* Create the thread and go! */
 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
     ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
@@ -386,6 +389,40 @@
     return (thread);
 }
 
+#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
+DECLSPEC SDL_Thread *SDLCALL
+SDL_CreateThread(int (SDLCALL * fn) (void *),
+                 const char *name, void *data,
+                 pfnSDL_CurrentBeginThread pfnBeginThread,
+                 pfnSDL_CurrentEndThread pfnEndThread)
+#else
+DECLSPEC SDL_Thread *SDLCALL
+SDL_CreateThread(int (SDLCALL * fn) (void *),
+                 const char *name, void *data)
+#endif
+{
+    /* !!! FIXME: in 2.1, just make stackhint part of the usual API. */
+    const char *stackhint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
+    size_t stacksize = 0;
+
+    /* If the SDL_HINT_THREAD_STACK_SIZE exists, use it */
+    if (stackhint != NULL) {
+        char *endp = NULL;
+        const Sint64 hintval = SDL_strtoll(stackhint, &endp, 10);
+        if ((*stackhint != '\0') && (*endp == '\0')) {  /* a valid number? */
+            if (hintval > 0) {  /* reject bogus values. */
+                stacksize = (size_t) hintval;
+            }
+        }
+    }
+
+#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
+    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, pfnBeginThread, pfnEndThread);
+#else
+    return SDL_CreateThreadWithStackSize(fn, name, stacksize, data);
+#endif
+}
+
 SDL_threadID
 SDL_GetThreadID(SDL_Thread * thread)
 {
--- a/src/thread/SDL_thread_c.h	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/SDL_thread_c.h	Tue Apr 12 14:38:50 2016 -0400
@@ -59,6 +59,7 @@
     SDL_atomic_t state;  /* SDL_THREAD_STATE_* */
     SDL_error errbuf;
     char *name;
+    size_t stacksize;  /* 0 for default, >0 for user-specified stack size. */
     void *data;
 };
 
@@ -89,6 +90,19 @@
  */
 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: */
--- a/src/thread/psp/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/psp/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
@@ -52,8 +52,8 @@
         priority = status.currentPriority;
     }
 
-    thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
-                           priority, 0x8000,
+    thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
+                           priority, thread->stacksize ? ((int) stacksize) : 0x8000,
                            PSP_THREAD_ATTR_VFPU, NULL);
     if (thread->handle < 0) {
         return SDL_SetError("sceKernelCreateThread() failed");
--- a/src/thread/pthread/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/pthread/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
@@ -45,7 +45,6 @@
 
 #include "SDL_platform.h"
 #include "SDL_thread.h"
-#include "SDL_hints.h"
 #include "../SDL_thread_c.h"
 #include "../SDL_systhread.h"
 #ifdef __ANDROID__
@@ -87,7 +86,6 @@
 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
 {
     pthread_attr_t type;
-    const char *hint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
 
     /* do this here before any threads exist, so there's no race condition. */
     #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
@@ -108,12 +106,9 @@
     }
     pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
     
-    /* If the SDL_HINT_THREAD_STACK_SIZE exists and it seems to be a positive number, use it */
-    if (hint && hint[0] >= '0' && hint[0] <= '9') {
-        const size_t stacksize = (size_t) SDL_atoi(hint);
-        if (stacksize > 0) {
-            pthread_attr_setstacksize(&type, stacksize);
-        }
+    /* Set caller-requested stack size. Otherwise: use the system default. */
+    if (thread->stacksize) {
+        pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
     }
 
     /* Create the thread and go! */
--- a/src/thread/stdcpp/SDL_systhread.cpp	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/stdcpp/SDL_systhread.cpp	Tue Apr 12 14:38:50 2016 -0400
@@ -48,6 +48,7 @@
 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
 {
     try {
+        // !!! FIXME: no way to set a thread stack size here.
         std::thread cpp_thread(RunThread, args);
         thread->handle = (void *) new std::thread(std::move(cpp_thread));
         return 0;
--- a/src/thread/windows/SDL_systhread.c	Mon Apr 11 00:22:39 2016 -0400
+++ b/src/thread/windows/SDL_systhread.c	Tue Apr 12 14:38:50 2016 -0400
@@ -129,14 +129,17 @@
     /* Also save the real parameters we have to pass to thread function */
     pThreadParms->args = args;
 
+    /* thread->stacksize == 0 means "system default", same as win32 expects */
     if (pfnBeginThread) {
         unsigned threadid = 0;
         thread->handle = (SYS_ThreadHandle)
-            ((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx,
+            ((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
+                                     RunThreadViaBeginThreadEx,
                                      pThreadParms, 0, &threadid));
     } else {
         DWORD threadid = 0;
-        thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread,
+        thread->handle = CreateThread(NULL, thread->stacksize,
+                                      RunThreadViaCreateThread,
                                       pThreadParms, 0, &threadid);
     }
     if (thread->handle == NULL) {