mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-28 11:57:24 +08:00
thread: Remove semaphore in thread creation.
This was added so that the new thread definitely has its threadid set, via SDL_GetCurrentThreadID(), before SDL_CreatThread returns, but this broke Emscripten, which can't wait on a newly-created thread, since the thread won't start until a later mainloop iteration. Now we have the creating thread set this id in SDL_SYS_CreateThread, where platform-specific logic can figure out how to calculate the new thread's ID from the parent thread, without using SDL_GetCurrentThreadID(). Fixes #15509.
This commit is contained in:
@@ -335,11 +335,6 @@ void SDL_RunThread(SDL_Thread *thread)
|
|||||||
// Perform any system-dependent setup - this function may not fail
|
// Perform any system-dependent setup - this function may not fail
|
||||||
SDL_SYS_SetupThread(thread->name);
|
SDL_SYS_SetupThread(thread->name);
|
||||||
|
|
||||||
// Get the thread id
|
|
||||||
thread->threadid = SDL_GetCurrentThreadID();
|
|
||||||
|
|
||||||
SDL_SignalSemaphore(thread->ready_sem); // the thread is officially ready to run!
|
|
||||||
|
|
||||||
// Run the function
|
// Run the function
|
||||||
*statusloc = userfunc(userdata);
|
*statusloc = userfunc(userdata);
|
||||||
|
|
||||||
@@ -396,13 +391,6 @@ SDL_Thread *SDL_CreateThreadWithPropertiesRuntime(SDL_PropertiesID props,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
thread->ready_sem = SDL_CreateSemaphore(0);
|
|
||||||
if (!thread->ready_sem) {
|
|
||||||
SDL_free(thread->name);
|
|
||||||
SDL_free(thread);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread->userfunc = fn;
|
thread->userfunc = fn;
|
||||||
thread->userdata = userdata;
|
thread->userdata = userdata;
|
||||||
thread->stacksize = stacksize;
|
thread->stacksize = stacksize;
|
||||||
@@ -413,16 +401,11 @@ SDL_Thread *SDL_CreateThreadWithPropertiesRuntime(SDL_PropertiesID props,
|
|||||||
if (!SDL_SYS_CreateThread(thread, pfnBeginThread, pfnEndThread)) {
|
if (!SDL_SYS_CreateThread(thread, pfnBeginThread, pfnEndThread)) {
|
||||||
// Oops, failed. Gotta free everything
|
// Oops, failed. Gotta free everything
|
||||||
SDL_SetObjectValid(thread, SDL_OBJECT_TYPE_THREAD, false);
|
SDL_SetObjectValid(thread, SDL_OBJECT_TYPE_THREAD, false);
|
||||||
SDL_DestroySemaphore(thread->ready_sem);
|
|
||||||
SDL_free(thread->name);
|
SDL_free(thread->name);
|
||||||
SDL_free(thread);
|
SDL_free(thread);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_WaitSemaphore(thread->ready_sem);
|
|
||||||
SDL_DestroySemaphore(thread->ready_sem);
|
|
||||||
thread->ready_sem = NULL;
|
|
||||||
|
|
||||||
// Everything is running now
|
// Everything is running now
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ struct SDL_Thread
|
|||||||
SDL_error errbuf;
|
SDL_error errbuf;
|
||||||
char *name;
|
char *name;
|
||||||
size_t stacksize; // 0 for default, >0 for user-specified stack size.
|
size_t stacksize; // 0 for default, >0 for user-specified stack size.
|
||||||
SDL_Semaphore *ready_sem; // signals when the thread is set up and about to start running.
|
|
||||||
int(SDLCALL *userfunc)(void *);
|
int(SDLCALL *userfunc)(void *);
|
||||||
void *userdata;
|
void *userdata;
|
||||||
void *data;
|
void *data;
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
}
|
}
|
||||||
|
|
||||||
thread->handle = tid;
|
thread->handle = tid;
|
||||||
|
thread->threadid = (SDL_ThreadID) tid;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
return SDL_SetError("Couldn't create thread");
|
return SDL_SetError("Couldn't create thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 thread_ID = 0;
|
||||||
|
svcGetThreadId(&thread_ID, threadGetHandle(thread->handle));
|
||||||
|
thread->threadid = (SDL_ThreadID) thread_ID;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,9 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
if (StartThread(thread->handle, thread) < 0) {
|
if (StartThread(thread->handle, thread) < 0) {
|
||||||
return SDL_SetError("StartThread() failed");
|
return SDL_SetError("StartThread() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread->threadid = (SDL_ThreadID) thread->handle;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
return SDL_SetError("sceKernelCreateThread() failed");
|
return SDL_SetError("sceKernelCreateThread() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread->threadid = (SDL_ThreadID) thread->handle;
|
||||||
|
|
||||||
sceKernelStartThread(thread->handle, 4, &thread);
|
sceKernelStartThread(thread->handle, 4, &thread);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,8 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
return SDL_SetError("Not enough resources to create thread");
|
return SDL_SetError("Not enough resources to create thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread->threadid = (SDL_ThreadID) thread->handle; // the SDL thread ID is just the pthread_t.
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
return SDL_SetError("sceKernelCreateThread() failed");
|
return SDL_SetError("sceKernelCreateThread() failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread->threadid = (SDL_ThreadID) thread->handle;
|
||||||
|
|
||||||
sceKernelStartThread(thread->handle, 4, &thread);
|
sceKernelStartThread(thread->handle, 4, &thread);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,15 +77,18 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread,
|
|||||||
thread->handle = (SYS_ThreadHandle)((size_t)pfnBeginThread(NULL, (unsigned int)thread->stacksize,
|
thread->handle = (SYS_ThreadHandle)((size_t)pfnBeginThread(NULL, (unsigned int)thread->stacksize,
|
||||||
RunThreadViaBeginThreadEx,
|
RunThreadViaBeginThreadEx,
|
||||||
thread, flags, &threadid));
|
thread, flags, &threadid));
|
||||||
|
thread->threadid = (SDL_ThreadID) threadid;
|
||||||
} else {
|
} else {
|
||||||
DWORD threadid = 0;
|
DWORD threadid = 0;
|
||||||
thread->handle = CreateThread(NULL, thread->stacksize,
|
thread->handle = CreateThread(NULL, thread->stacksize,
|
||||||
RunThreadViaCreateThread,
|
RunThreadViaCreateThread,
|
||||||
thread, flags, &threadid);
|
thread, flags, &threadid);
|
||||||
|
thread->threadid = (SDL_ThreadID) threadid;
|
||||||
}
|
}
|
||||||
if (!thread->handle) {
|
if (!thread->handle) {
|
||||||
return SDL_SetError("Not enough resources to create thread");
|
return SDL_SetError("Not enough resources to create thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user