From f44a591e61bdab2082ba8dc16cc1f7f1810b06a9 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 16 Dec 2009 04:48:11 +0000 Subject: [PATCH] Fixed bug #741 The thread ID is an unsigned long so it can hold pthread_t so people can do naughty things with it. I'm going to be adding additional useful thread API functions, but this should prevent crashes in people's existing code on 64-bit architectures. --- include/SDL_thread.h | 11 +++++++---- src/audio/SDL_sysaudio.h | 2 +- src/events/SDL_events.c | 4 ++-- src/events/SDL_events_c.h | 3 ++- src/thread/SDL_thread.c | 8 ++++---- src/thread/SDL_thread_c.h | 2 +- src/thread/beos/SDL_systhread.c | 4 ++-- src/thread/generic/SDL_sysmutex.c | 4 ++-- src/thread/generic/SDL_systhread.c | 2 +- src/thread/irix/SDL_systhread.c | 6 ++---- src/thread/nds/SDL_sysmutex.c | 4 ++-- src/thread/nds/SDL_systhread.c | 2 +- src/thread/pth/SDL_systhread.c | 5 ++--- src/thread/pthread/SDL_systhread.c | 7 +++---- src/thread/pthread/SDL_systhread_c.h | 1 + src/thread/riscos/SDL_systhread.c | 2 +- src/thread/win32/SDL_systhread.c | 4 ++-- src/thread/win32/SDL_systhread_c.h | 1 + src/timer/SDL_timer.c | 4 ++-- src/timer/riscos/SDL_systimer.c | 6 +++--- test/testerror.c | 2 +- test/testhread.c | 2 +- test/testlock.c | 18 +++++++++--------- 23 files changed, 53 insertions(+), 51 deletions(-) diff --git a/include/SDL_thread.h b/include/SDL_thread.h index 4951e67db..816529578 100644 --- a/include/SDL_thread.h +++ b/include/SDL_thread.h @@ -47,6 +47,9 @@ extern "C" { struct SDL_Thread; typedef struct SDL_Thread SDL_Thread; +/* The SDL thread ID */ +typedef unsigned long SDL_threadID; + #if defined(__WIN32__) && !defined(HAVE_LIBC) /** * \file SDL_thread.h @@ -127,16 +130,16 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data); #endif /** - * Get the 32-bit thread identifier for the current thread. + * Get the thread identifier for the current thread. */ -extern DECLSPEC Uint32 SDLCALL SDL_ThreadID(void); +extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void); /** - * Get the 32-bit thread identifier for the specified thread. + * Get the thread identifier for the specified thread. * * Equivalent to SDL_ThreadID() if the specified thread is NULL. */ -extern DECLSPEC Uint32 SDLCALL SDL_GetThreadID(SDL_Thread * thread); +extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread); /** * Wait for a thread to finish. diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h index ee9402fca..94bae35ab 100644 --- a/src/audio/SDL_sysaudio.h +++ b/src/audio/SDL_sysaudio.h @@ -108,7 +108,7 @@ struct SDL_AudioDevice /* A thread to feed the audio device */ SDL_Thread *thread; - Uint32 threadid; + SDL_threadID threadid; /* * * */ /* Data private to this driver */ diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index 088a3d33e..383e018fc 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -62,7 +62,7 @@ static struct /* Thread functions */ static SDL_Thread *SDL_EventThread = NULL; /* Thread handle */ -static Uint32 event_thread; /* The event thread id */ +static SDL_threadID event_thread; /* The event thread id */ void SDL_Lock_EventThread(void) @@ -183,7 +183,7 @@ SDL_StopEventThread(void) } } -Uint32 +SDL_threadID SDL_EventThreadID(void) { return (event_thread); diff --git a/src/events/SDL_events_c.h b/src/events/SDL_events_c.h index 53a4ab554..252603766 100644 --- a/src/events/SDL_events_c.h +++ b/src/events/SDL_events_c.h @@ -23,6 +23,7 @@ /* Useful functions and variables from SDL_events.c */ #include "SDL_events.h" +#include "SDL_thread.h" #include "SDL_mouse_c.h" #include "SDL_keyboard_c.h" #include "SDL_windowevents_c.h" @@ -34,7 +35,7 @@ extern void SDL_QuitInterrupt(void); extern void SDL_Lock_EventThread(void); extern void SDL_Unlock_EventThread(void); -extern Uint32 SDL_EventThreadID(void); +extern SDL_threadID SDL_EventThreadID(void); extern int SDL_SendSysWMEvent(SDL_SysWMmsg * message); diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c index b32eb0798..92e0511e7 100644 --- a/src/thread/SDL_thread.c +++ b/src/thread/SDL_thread.c @@ -159,7 +159,7 @@ SDL_GetErrBuf(void) errbuf = &SDL_global_error; if (SDL_Threads) { int i; - Uint32 this_thread; + SDL_threadID this_thread; this_thread = SDL_ThreadID(); SDL_mutexP(thread_lock); @@ -292,17 +292,17 @@ SDL_WaitThread(SDL_Thread * thread, int *status) } } -Uint32 +SDL_threadID SDL_GetThreadID(SDL_Thread * thread) { - Uint32 id; + SDL_threadID id; if (thread) { id = thread->threadid; } else { id = SDL_ThreadID(); } - return (id); + return id; } void diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h index 91ebec39a..07b77d1a2 100644 --- a/src/thread/SDL_thread_c.h +++ b/src/thread/SDL_thread_c.h @@ -50,7 +50,7 @@ /* This is the system-independent thread info structure */ struct SDL_Thread { - Uint32 threadid; + SDL_threadID threadid; SYS_ThreadHandle handle; int status; SDL_error errbuf; diff --git a/src/thread/beos/SDL_systhread.c b/src/thread/beos/SDL_systhread.c index f50e89f44..5a7ec7e4b 100644 --- a/src/thread/beos/SDL_systhread.c +++ b/src/thread/beos/SDL_systhread.c @@ -84,10 +84,10 @@ SDL_SYS_SetupThread(void) SDL_MaskSignals(NULL); } -Uint32 +SDL_threadID SDL_ThreadID(void) { - return ((Uint32) find_thread(NULL)); + return ((SDL_threadID) find_thread(NULL)); } void diff --git a/src/thread/generic/SDL_sysmutex.c b/src/thread/generic/SDL_sysmutex.c index 3a889e225..598db5c64 100644 --- a/src/thread/generic/SDL_sysmutex.c +++ b/src/thread/generic/SDL_sysmutex.c @@ -30,7 +30,7 @@ struct SDL_mutex { int recursive; - Uint32 owner; + SDL_threadID owner; SDL_sem *sem; }; @@ -76,7 +76,7 @@ SDL_mutexP(SDL_mutex * mutex) #if SDL_THREADS_DISABLED return 0; #else - Uint32 this_thread; + SDL_threadID this_thread; if (mutex == NULL) { SDL_SetError("Passed a NULL mutex"); diff --git a/src/thread/generic/SDL_systhread.c b/src/thread/generic/SDL_systhread.c index 6a180a649..3d93e5ff0 100644 --- a/src/thread/generic/SDL_systhread.c +++ b/src/thread/generic/SDL_systhread.c @@ -39,7 +39,7 @@ SDL_SYS_SetupThread(void) return; } -Uint32 +SDL_threadID SDL_ThreadID(void) { return (0); diff --git a/src/thread/irix/SDL_systhread.c b/src/thread/irix/SDL_systhread.c index 0dc7f3f44..e30f0d16a 100644 --- a/src/thread/irix/SDL_systhread.c +++ b/src/thread/irix/SDL_systhread.c @@ -64,14 +64,12 @@ SDL_SYS_SetupThread(void) sigprocmask(SIG_BLOCK, &mask, NULL); } -/* WARNING: This may not work for systems with 64-bit pid_t */ -Uint32 +SDL_threadID SDL_ThreadID(void) { - return ((Uint32) getpid()); + return ((SDL_threadID) getpid()); } -/* WARNING: This may not work for systems with 64-bit pid_t */ void SDL_WaitThread(SDL_Thread * thread, int *status) { diff --git a/src/thread/nds/SDL_sysmutex.c b/src/thread/nds/SDL_sysmutex.c index 2621625c4..6571966b8 100644 --- a/src/thread/nds/SDL_sysmutex.c +++ b/src/thread/nds/SDL_sysmutex.c @@ -38,7 +38,7 @@ static char rcsid = struct SDL_mutex { int recursive; - Uint32 owner; + SDL_threadID owner; SDL_sem *sem; }; @@ -84,7 +84,7 @@ SDL_mutexP(SDL_mutex * mutex) #ifdef DISABLE_THREADS return 0; #else - Uint32 this_thread; + SDL_threadID this_thread; if (mutex == NULL) { SDL_SetError("Passed a NULL mutex"); diff --git a/src/thread/nds/SDL_systhread.c b/src/thread/nds/SDL_systhread.c index 6499c8126..3bf15f7d0 100644 --- a/src/thread/nds/SDL_systhread.c +++ b/src/thread/nds/SDL_systhread.c @@ -44,7 +44,7 @@ SDL_SYS_SetupThread(void) return; } -Uint32 +SDL_threadID SDL_ThreadID(void) { return (0); diff --git a/src/thread/pth/SDL_systhread.c b/src/thread/pth/SDL_systhread.c index f7c178f96..90ab53fad 100644 --- a/src/thread/pth/SDL_systhread.c +++ b/src/thread/pth/SDL_systhread.c @@ -88,11 +88,10 @@ SDL_SYS_SetupThread(void) pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate); } -/* WARNING: This may not work for systems with 64-bit pid_t */ -Uint32 +SDL_threadID SDL_ThreadID(void) { - return ((Uint32) pth_self()); + return ((SDL_threadID) pth_self()); } void diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index 2a48c78cc..8aeace477 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -38,7 +38,7 @@ static const int sig_list[] = { /* RISC OS needs to know the main thread for * it's timer and event processing. */ int riscos_using_threads = 0; -Uint32 riscos_main_thread = 0; /* Thread running events */ +SDL_threadID riscos_main_thread = 0; /* Thread running events */ #endif @@ -99,11 +99,10 @@ SDL_SYS_SetupThread(void) #endif } -/* WARNING: This may not work for systems with 64-bit pid_t */ -Uint32 +SDL_threadID SDL_ThreadID(void) { - return ((Uint32) pthread_self()); + return ((SDL_threadID) pthread_self()); } void diff --git a/src/thread/pthread/SDL_systhread_c.h b/src/thread/pthread/SDL_systhread_c.h index 449ada358..ae3baad86 100644 --- a/src/thread/pthread/SDL_systhread_c.h +++ b/src/thread/pthread/SDL_systhread_c.h @@ -24,4 +24,5 @@ #include typedef pthread_t SYS_ThreadHandle; + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/thread/riscos/SDL_systhread.c b/src/thread/riscos/SDL_systhread.c index f115e3096..3f42f6a98 100644 --- a/src/thread/riscos/SDL_systhread.c +++ b/src/thread/riscos/SDL_systhread.c @@ -42,7 +42,7 @@ SDL_SYS_SetupThread(void) return; } -Uint32 +SDL_threadID SDL_ThreadID(void) { return (0); diff --git a/src/thread/win32/SDL_systhread.c b/src/thread/win32/SDL_systhread.c index 4e2adab36..7784b7585 100644 --- a/src/thread/win32/SDL_systhread.c +++ b/src/thread/win32/SDL_systhread.c @@ -151,10 +151,10 @@ SDL_SYS_SetupThread(void) return; } -Uint32 +SDL_threadID SDL_ThreadID(void) { - return ((Uint32) GetCurrentThreadId()); + return ((SDL_threadID) GetCurrentThreadId()); } void diff --git a/src/thread/win32/SDL_systhread_c.h b/src/thread/win32/SDL_systhread_c.h index bb2f7a576..805ce052d 100644 --- a/src/thread/win32/SDL_systhread_c.h +++ b/src/thread/win32/SDL_systhread_c.h @@ -25,4 +25,5 @@ #include typedef HANDLE SYS_ThreadHandle; + /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/timer/SDL_timer.c b/src/timer/SDL_timer.c index 7f4a33911..72c98bdec 100644 --- a/src/timer/SDL_timer.c +++ b/src/timer/SDL_timer.c @@ -128,7 +128,7 @@ SDL_ThreadedTimerCheck(void) t->last_alarm = now; } #ifdef DEBUG_TIMERS - printf("Executing timer %p (thread = %d)\n", t, SDL_ThreadID()); + printf("Executing timer %p (thread = %lu)\n", t, SDL_ThreadID()); #endif timer = *t; SDL_mutexV(SDL_timer_mutex); @@ -235,7 +235,7 @@ SDL_RemoveTimer(SDL_TimerID id) } } #ifdef DEBUG_TIMERS - printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", + printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %lu\n", (Uint32) id, removed, SDL_timer_running, SDL_ThreadID()); #endif SDL_mutexV(SDL_timer_mutex); diff --git a/src/timer/riscos/SDL_systimer.c b/src/timer/riscos/SDL_systimer.c index 0d44e3902..2f2fc33a0 100644 --- a/src/timer/riscos/SDL_systimer.c +++ b/src/timer/riscos/SDL_systimer.c @@ -40,10 +40,10 @@ static Uint32 timerStart; void RISCOS_CheckTimer(); #else #include -extern Uint32 riscos_main_thread; +extern SDL_threadID riscos_main_thread; extern int riscos_using_threads; -extern Uint32 SDL_ThreadID(); -extern Uint32 SDL_EventThreadID(void); +extern SDL_threadID SDL_ThreadID(); +extern SDL_threadID SDL_EventThreadID(void); #endif diff --git a/test/testerror.c b/test/testerror.c index 1ff768db4..09f0ac71b 100644 --- a/test/testerror.c +++ b/test/testerror.c @@ -22,7 +22,7 @@ int SDLCALL ThreadFunc(void *data) { /* Set the child thread error string */ - SDL_SetError("Thread %s (%d) had a problem: %s", + SDL_SetError("Thread %s (%lu) had a problem: %s", (char *) data, SDL_ThreadID(), "nevermind"); while (alive) { printf("Thread '%s' is alive!\n", (char *) data); diff --git a/test/testhread.c b/test/testhread.c index 3ebe0ea58..0676036bf 100644 --- a/test/testhread.c +++ b/test/testhread.c @@ -21,7 +21,7 @@ quit(int rc) int SDLCALL ThreadFunc(void *data) { - printf("Started thread %s: My thread id is %u\n", + printf("Started thread %s: My thread id is %lu\n", (char *) data, SDL_ThreadID()); while (alive) { printf("Thread '%s' is alive!\n", (char *) data); diff --git a/test/testlock.c b/test/testlock.c index 3a5d19f39..545abaf07 100644 --- a/test/testlock.c +++ b/test/testlock.c @@ -11,7 +11,7 @@ #include "SDL_thread.h" static SDL_mutex *mutex = NULL; -static Uint32 mainthread; +static SDL_threadID mainthread; static SDL_Thread *threads[6]; static volatile int doterminate = 0; @@ -28,7 +28,7 @@ SDL_Quit_Wrapper(void) void printid(void) { - printf("Process %u: exiting\n", SDL_ThreadID()); + printf("Process %lu: exiting\n", SDL_ThreadID()); } void @@ -41,9 +41,9 @@ terminate(int sig) void closemutex(int sig) { - Uint32 id = SDL_ThreadID(); + SDL_threadID id = SDL_ThreadID(); int i; - printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id); + printf("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id); doterminate = 1; for (i = 0; i < 6; ++i) SDL_WaitThread(threads[i], NULL); @@ -57,14 +57,14 @@ Run(void *data) if (SDL_ThreadID() == mainthread) signal(SIGTERM, closemutex); while (!doterminate) { - printf("Process %u ready to work\n", SDL_ThreadID()); + printf("Process %lu ready to work\n", SDL_ThreadID()); if (SDL_mutexP(mutex) < 0) { fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError()); exit(1); } - printf("Process %u, working!\n", SDL_ThreadID()); + printf("Process %lu, working!\n", SDL_ThreadID()); SDL_Delay(1 * 1000); - printf("Process %u, done!\n", SDL_ThreadID()); + printf("Process %lu, done!\n", SDL_ThreadID()); if (SDL_mutexV(mutex) < 0) { fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError()); exit(1); @@ -73,7 +73,7 @@ Run(void *data) SDL_Delay(10); } if (SDL_ThreadID() == mainthread && doterminate) { - printf("Process %u: raising SIGTERM\n", SDL_ThreadID()); + printf("Process %lu: raising SIGTERM\n", SDL_ThreadID()); raise(SIGTERM); } return (0); @@ -98,7 +98,7 @@ main(int argc, char *argv[]) } mainthread = SDL_ThreadID(); - printf("Main thread: %u\n", mainthread); + printf("Main thread: %lu\n", mainthread); atexit(printid); for (i = 0; i < maxproc; ++i) { if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)