Skip to content

Commit f2943c9

Browse files
committed
Fix getting ID of current thread with SDL3
in SDL3 SDL_ThreadID is the type (that's called SDL_threadID with lowercase-t in SDL2), while in SDL1.2 and SDL2 SDL_ThreadID() is a function that returns the current thread's ID. So calling SDL_ThreadID() (to get the current thread's ID) will compile on SDL3 (calling the types implicit default-constructor) but not do anything useful... To avoid this clash always call SDL_GetCurrentThreadID() (the SDL3 name of that function) instead and provide a #define to SDL_ThreadID for SDL1.2 and SDL2 so it works there as well.
1 parent c3390f6 commit f2943c9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

neo/sys/threads.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ If you have questions concerning this license or the applicable additional terms
5555
#define SDL_CondSignal SDL_SignalCondition
5656
#endif
5757

58+
#if SDL_MAJOR_VERSION < 3
59+
// in SDL3 SDL_ThreadID is the type (that's called SDL_threadID with lowercase-t in SDL2),
60+
// in SDL1.2 and SDL2 SDL_ThreadID() is a function that returns the current thread's ID...
61+
// So use SDL_GetCurrentThreadID() in all cases to avoid this clash
62+
#define SDL_GetCurrentThreadID SDL_ThreadID
63+
#endif
64+
5865
#if __cplusplus >= 201103
5966
// xthreadinfo::threadId doesn't use SDL_threadID directly so we don't drag SDL headers into sys_public.h
6067
// but we should still make sure that the type fits (in SDL1.2 it's Uint32, in SDL2 it's unsigned long)
@@ -87,7 +94,7 @@ Sys_InitThreads
8794
==================
8895
*/
8996
void Sys_InitThreads() {
90-
mainThreadID = SDL_ThreadID();
97+
mainThreadID = SDL_GetCurrentThreadID();
9198
mainThreadIDset = true;
9299

93100
// critical sections
@@ -330,7 +337,7 @@ const char *Sys_GetThreadName(int *index) {
330337

331338
Sys_EnterCriticalSection();
332339

333-
SDL_threadID id = SDL_ThreadID();
340+
SDL_threadID id = SDL_GetCurrentThreadID();
334341

335342
for (int i = 0; i < thread_count; i++) {
336343
if (id == thread[i]->threadId) {
@@ -362,7 +369,7 @@ returns true if the current thread is the main thread
362369
*/
363370
bool Sys_IsMainThread() {
364371
if ( mainThreadIDset )
365-
return SDL_ThreadID() == mainThreadID;
372+
return SDL_GetCurrentThreadID() == mainThreadID;
366373
// if this is called before mainThreadID is set, we haven't created
367374
// any threads yet so it should be the main thread
368375
return true;

0 commit comments

Comments
 (0)