threads: Handle SDL_HINT_THREAD_STACK_SIZE at top level, implement elsewhere.

This commit is contained in:
Ryan C. Gordon 2016-04-12 14:38:50 -04:00
parent 8b65d225e8
commit 7ae2951fca
6 changed files with 68 additions and 18 deletions

View File

@ -26,6 +26,7 @@
#include "SDL_thread.h" #include "SDL_thread.h"
#include "SDL_thread_c.h" #include "SDL_thread_c.h"
#include "SDL_systhread.h" #include "SDL_systhread.h"
#include "SDL_hints.h"
#include "../SDL_error_c.h" #include "../SDL_error_c.h"
@ -304,15 +305,15 @@ SDL_RunThread(void *data)
#endif #endif
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
DECLSPEC SDL_Thread *SDLCALL static SDL_Thread *
SDL_CreateThread(int (SDLCALL * fn) (void *), SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
const char *name, void *data, const char *name, const size_t stacksize, void *data,
pfnSDL_CurrentBeginThread pfnBeginThread, pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread) pfnSDL_CurrentEndThread pfnEndThread)
#else #else
DECLSPEC SDL_Thread *SDLCALL static SDL_Thread *
SDL_CreateThread(int (SDLCALL * fn) (void *), SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
const char *name, void *data) const char *name, const size_t stacksize, void *data)
#endif #endif
{ {
SDL_Thread *thread; SDL_Thread *thread;
@ -362,6 +363,8 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
return (NULL); return (NULL);
} }
thread->stacksize = stacksize;
/* Create the thread and go! */ /* Create the thread and go! */
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread); ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
@ -386,6 +389,40 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
return (thread); 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_threadID
SDL_GetThreadID(SDL_Thread * thread) SDL_GetThreadID(SDL_Thread * thread)
{ {

View File

@ -59,6 +59,7 @@ struct SDL_Thread
SDL_atomic_t state; /* SDL_THREAD_STATE_* */ SDL_atomic_t state; /* SDL_THREAD_STATE_* */
SDL_error errbuf; SDL_error errbuf;
char *name; char *name;
size_t stacksize; /* 0 for default, >0 for user-specified stack size. */
void *data; void *data;
}; };
@ -89,6 +90,19 @@ extern SDL_TLSData *SDL_Generic_GetTLSData();
*/ */
extern int SDL_Generic_SetTLSData(SDL_TLSData *data); 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 */ #endif /* _SDL_thread_c_h */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -52,8 +52,8 @@ int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
priority = status.currentPriority; priority = status.currentPriority;
} }
thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry, thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
priority, 0x8000, priority, thread->stacksize ? ((int) stacksize) : 0x8000,
PSP_THREAD_ATTR_VFPU, NULL); PSP_THREAD_ATTR_VFPU, NULL);
if (thread->handle < 0) { if (thread->handle < 0) {
return SDL_SetError("sceKernelCreateThread() failed"); return SDL_SetError("sceKernelCreateThread() failed");

View File

@ -45,7 +45,6 @@
#include "SDL_platform.h" #include "SDL_platform.h"
#include "SDL_thread.h" #include "SDL_thread.h"
#include "SDL_hints.h"
#include "../SDL_thread_c.h" #include "../SDL_thread_c.h"
#include "../SDL_systhread.h" #include "../SDL_systhread.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
@ -87,7 +86,6 @@ int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args) SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
{ {
pthread_attr_t type; 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. */ /* do this here before any threads exist, so there's no race condition. */
#if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__) #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); 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 */ /* Set caller-requested stack size. Otherwise: use the system default. */
if (hint && hint[0] >= '0' && hint[0] <= '9') { if (thread->stacksize) {
const size_t stacksize = (size_t) SDL_atoi(hint); pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
if (stacksize > 0) {
pthread_attr_setstacksize(&type, stacksize);
}
} }
/* Create the thread and go! */ /* Create the thread and go! */

View File

@ -48,6 +48,7 @@ int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args) SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
{ {
try { try {
// !!! FIXME: no way to set a thread stack size here.
std::thread cpp_thread(RunThread, args); std::thread cpp_thread(RunThread, args);
thread->handle = (void *) new std::thread(std::move(cpp_thread)); thread->handle = (void *) new std::thread(std::move(cpp_thread));
return 0; return 0;

View File

@ -129,14 +129,17 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
/* Also save the real parameters we have to pass to thread function */ /* Also save the real parameters we have to pass to thread function */
pThreadParms->args = args; pThreadParms->args = args;
/* thread->stacksize == 0 means "system default", same as win32 expects */
if (pfnBeginThread) { if (pfnBeginThread) {
unsigned threadid = 0; unsigned threadid = 0;
thread->handle = (SYS_ThreadHandle) thread->handle = (SYS_ThreadHandle)
((size_t) pfnBeginThread(NULL, 0, RunThreadViaBeginThreadEx, ((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
RunThreadViaBeginThreadEx,
pThreadParms, 0, &threadid)); pThreadParms, 0, &threadid));
} else { } else {
DWORD threadid = 0; DWORD threadid = 0;
thread->handle = CreateThread(NULL, 0, RunThreadViaCreateThread, thread->handle = CreateThread(NULL, thread->stacksize,
RunThreadViaCreateThread,
pThreadParms, 0, &threadid); pThreadParms, 0, &threadid);
} }
if (thread->handle == NULL) { if (thread->handle == NULL) {