threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c
index e66c581..1306ae0 100644
--- a/src/thread/SDL_thread.c
+++ b/src/thread/SDL_thread.c
@@ -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 @@ SDL_RunThread(void *data)
#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 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
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 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
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)
{
diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h
index a283a0e..7749183 100644
--- a/src/thread/SDL_thread_c.h
+++ b/src/thread/SDL_thread_c.h
@@ -59,6 +59,7 @@ struct SDL_Thread
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 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: */
diff --git a/src/thread/psp/SDL_systhread.c b/src/thread/psp/SDL_systhread.c
index ab8aff3..96f2e1d 100644
--- a/src/thread/psp/SDL_systhread.c
+++ b/src/thread/psp/SDL_systhread.c
@@ -52,8 +52,8 @@ int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
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");
diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c
index 22f7bd5..79d6543 100644
--- a/src/thread/pthread/SDL_systhread.c
+++ b/src/thread/pthread/SDL_systhread.c
@@ -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 @@ int
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 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
}
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! */
diff --git a/src/thread/stdcpp/SDL_systhread.cpp b/src/thread/stdcpp/SDL_systhread.cpp
index 219c67e..6e5ef47 100644
--- a/src/thread/stdcpp/SDL_systhread.cpp
+++ b/src/thread/stdcpp/SDL_systhread.cpp
@@ -48,6 +48,7 @@ int
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;
diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c
index 2f22b89..249e361 100644
--- a/src/thread/windows/SDL_systhread.c
+++ b/src/thread/windows/SDL_systhread.c
@@ -129,14 +129,17 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
/* 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) {