Commit 3f0f9188 authored by Sam Lantinga's avatar Sam Lantinga

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.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%404327
parent 83fbb698
...@@ -47,6 +47,9 @@ extern "C" { ...@@ -47,6 +47,9 @@ extern "C" {
struct SDL_Thread; struct SDL_Thread;
typedef struct SDL_Thread SDL_Thread; typedef struct SDL_Thread SDL_Thread;
/* The SDL thread ID */
typedef unsigned long SDL_threadID;
#if defined(__WIN32__) && !defined(HAVE_LIBC) #if defined(__WIN32__) && !defined(HAVE_LIBC)
/** /**
* \file SDL_thread.h * \file SDL_thread.h
...@@ -127,16 +130,16 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data); ...@@ -127,16 +130,16 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data);
#endif #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. * 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. * Wait for a thread to finish.
......
...@@ -108,7 +108,7 @@ struct SDL_AudioDevice ...@@ -108,7 +108,7 @@ struct SDL_AudioDevice
/* A thread to feed the audio device */ /* A thread to feed the audio device */
SDL_Thread *thread; SDL_Thread *thread;
Uint32 threadid; SDL_threadID threadid;
/* * * */ /* * * */
/* Data private to this driver */ /* Data private to this driver */
......
...@@ -62,7 +62,7 @@ static struct ...@@ -62,7 +62,7 @@ static struct
/* Thread functions */ /* Thread functions */
static SDL_Thread *SDL_EventThread = NULL; /* Thread handle */ 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 void
SDL_Lock_EventThread(void) SDL_Lock_EventThread(void)
...@@ -183,7 +183,7 @@ SDL_StopEventThread(void) ...@@ -183,7 +183,7 @@ SDL_StopEventThread(void)
} }
} }
Uint32 SDL_threadID
SDL_EventThreadID(void) SDL_EventThreadID(void)
{ {
return (event_thread); return (event_thread);
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
/* Useful functions and variables from SDL_events.c */ /* Useful functions and variables from SDL_events.c */
#include "SDL_events.h" #include "SDL_events.h"
#include "SDL_thread.h"
#include "SDL_mouse_c.h" #include "SDL_mouse_c.h"
#include "SDL_keyboard_c.h" #include "SDL_keyboard_c.h"
#include "SDL_windowevents_c.h" #include "SDL_windowevents_c.h"
...@@ -34,7 +35,7 @@ extern void SDL_QuitInterrupt(void); ...@@ -34,7 +35,7 @@ extern void SDL_QuitInterrupt(void);
extern void SDL_Lock_EventThread(void); extern void SDL_Lock_EventThread(void);
extern void SDL_Unlock_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); extern int SDL_SendSysWMEvent(SDL_SysWMmsg * message);
......
...@@ -159,7 +159,7 @@ SDL_GetErrBuf(void) ...@@ -159,7 +159,7 @@ SDL_GetErrBuf(void)
errbuf = &SDL_global_error; errbuf = &SDL_global_error;
if (SDL_Threads) { if (SDL_Threads) {
int i; int i;
Uint32 this_thread; SDL_threadID this_thread;
this_thread = SDL_ThreadID(); this_thread = SDL_ThreadID();
SDL_mutexP(thread_lock); SDL_mutexP(thread_lock);
...@@ -292,17 +292,17 @@ SDL_WaitThread(SDL_Thread * thread, int *status) ...@@ -292,17 +292,17 @@ SDL_WaitThread(SDL_Thread * thread, int *status)
} }
} }
Uint32 SDL_threadID
SDL_GetThreadID(SDL_Thread * thread) SDL_GetThreadID(SDL_Thread * thread)
{ {
Uint32 id; SDL_threadID id;
if (thread) { if (thread) {
id = thread->threadid; id = thread->threadid;
} else { } else {
id = SDL_ThreadID(); id = SDL_ThreadID();
} }
return (id); return id;
} }
void void
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
/* This is the system-independent thread info structure */ /* This is the system-independent thread info structure */
struct SDL_Thread struct SDL_Thread
{ {
Uint32 threadid; SDL_threadID threadid;
SYS_ThreadHandle handle; SYS_ThreadHandle handle;
int status; int status;
SDL_error errbuf; SDL_error errbuf;
......
...@@ -84,10 +84,10 @@ SDL_SYS_SetupThread(void) ...@@ -84,10 +84,10 @@ SDL_SYS_SetupThread(void)
SDL_MaskSignals(NULL); SDL_MaskSignals(NULL);
} }
Uint32 SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return ((Uint32) find_thread(NULL)); return ((SDL_threadID) find_thread(NULL));
} }
void void
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
struct SDL_mutex struct SDL_mutex
{ {
int recursive; int recursive;
Uint32 owner; SDL_threadID owner;
SDL_sem *sem; SDL_sem *sem;
}; };
...@@ -76,7 +76,7 @@ SDL_mutexP(SDL_mutex * mutex) ...@@ -76,7 +76,7 @@ SDL_mutexP(SDL_mutex * mutex)
#if SDL_THREADS_DISABLED #if SDL_THREADS_DISABLED
return 0; return 0;
#else #else
Uint32 this_thread; SDL_threadID this_thread;
if (mutex == NULL) { if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex"); SDL_SetError("Passed a NULL mutex");
......
...@@ -39,7 +39,7 @@ SDL_SYS_SetupThread(void) ...@@ -39,7 +39,7 @@ SDL_SYS_SetupThread(void)
return; return;
} }
Uint32 SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return (0); return (0);
......
...@@ -64,14 +64,12 @@ SDL_SYS_SetupThread(void) ...@@ -64,14 +64,12 @@ SDL_SYS_SetupThread(void)
sigprocmask(SIG_BLOCK, &mask, NULL); sigprocmask(SIG_BLOCK, &mask, NULL);
} }
/* WARNING: This may not work for systems with 64-bit pid_t */ SDL_threadID
Uint32
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return ((Uint32) getpid()); return ((SDL_threadID) getpid());
} }
/* WARNING: This may not work for systems with 64-bit pid_t */
void void
SDL_WaitThread(SDL_Thread * thread, int *status) SDL_WaitThread(SDL_Thread * thread, int *status)
{ {
......
...@@ -38,7 +38,7 @@ static char rcsid = ...@@ -38,7 +38,7 @@ static char rcsid =
struct SDL_mutex struct SDL_mutex
{ {
int recursive; int recursive;
Uint32 owner; SDL_threadID owner;
SDL_sem *sem; SDL_sem *sem;
}; };
...@@ -84,7 +84,7 @@ SDL_mutexP(SDL_mutex * mutex) ...@@ -84,7 +84,7 @@ SDL_mutexP(SDL_mutex * mutex)
#ifdef DISABLE_THREADS #ifdef DISABLE_THREADS
return 0; return 0;
#else #else
Uint32 this_thread; SDL_threadID this_thread;
if (mutex == NULL) { if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex"); SDL_SetError("Passed a NULL mutex");
......
...@@ -44,7 +44,7 @@ SDL_SYS_SetupThread(void) ...@@ -44,7 +44,7 @@ SDL_SYS_SetupThread(void)
return; return;
} }
Uint32 SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return (0); return (0);
......
...@@ -88,11 +88,10 @@ SDL_SYS_SetupThread(void) ...@@ -88,11 +88,10 @@ SDL_SYS_SetupThread(void)
pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate); pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
} }
/* WARNING: This may not work for systems with 64-bit pid_t */ SDL_threadID
Uint32
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return ((Uint32) pth_self()); return ((SDL_threadID) pth_self());
} }
void void
......
...@@ -38,7 +38,7 @@ static const int sig_list[] = { ...@@ -38,7 +38,7 @@ static const int sig_list[] = {
/* RISC OS needs to know the main thread for /* RISC OS needs to know the main thread for
* it's timer and event processing. */ * it's timer and event processing. */
int riscos_using_threads = 0; int riscos_using_threads = 0;
Uint32 riscos_main_thread = 0; /* Thread running events */ SDL_threadID riscos_main_thread = 0; /* Thread running events */
#endif #endif
...@@ -99,11 +99,10 @@ SDL_SYS_SetupThread(void) ...@@ -99,11 +99,10 @@ SDL_SYS_SetupThread(void)
#endif #endif
} }
/* WARNING: This may not work for systems with 64-bit pid_t */ SDL_threadID
Uint32
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return ((Uint32) pthread_self()); return ((SDL_threadID) pthread_self());
} }
void void
......
...@@ -24,4 +24,5 @@ ...@@ -24,4 +24,5 @@
#include <pthread.h> #include <pthread.h>
typedef pthread_t SYS_ThreadHandle; typedef pthread_t SYS_ThreadHandle;
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */
...@@ -42,7 +42,7 @@ SDL_SYS_SetupThread(void) ...@@ -42,7 +42,7 @@ SDL_SYS_SetupThread(void)
return; return;
} }
Uint32 SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return (0); return (0);
......
...@@ -151,10 +151,10 @@ SDL_SYS_SetupThread(void) ...@@ -151,10 +151,10 @@ SDL_SYS_SetupThread(void)
return; return;
} }
Uint32 SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)
{ {
return ((Uint32) GetCurrentThreadId()); return ((SDL_threadID) GetCurrentThreadId());
} }
void void
......
...@@ -25,4 +25,5 @@ ...@@ -25,4 +25,5 @@
#include <windows.h> #include <windows.h>
typedef HANDLE SYS_ThreadHandle; typedef HANDLE SYS_ThreadHandle;
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */
...@@ -128,7 +128,7 @@ SDL_ThreadedTimerCheck(void) ...@@ -128,7 +128,7 @@ SDL_ThreadedTimerCheck(void)
t->last_alarm = now; t->last_alarm = now;
} }
#ifdef DEBUG_TIMERS #ifdef DEBUG_TIMERS
printf("Executing timer %p (thread = %d)\n", t, SDL_ThreadID()); printf("Executing timer %p (thread = %lu)\n", t, SDL_ThreadID());
#endif #endif
timer = *t; timer = *t;
SDL_mutexV(SDL_timer_mutex); SDL_mutexV(SDL_timer_mutex);
...@@ -235,7 +235,7 @@ SDL_RemoveTimer(SDL_TimerID id) ...@@ -235,7 +235,7 @@ SDL_RemoveTimer(SDL_TimerID id)
} }
} }
#ifdef DEBUG_TIMERS #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()); (Uint32) id, removed, SDL_timer_running, SDL_ThreadID());
#endif #endif
SDL_mutexV(SDL_timer_mutex); SDL_mutexV(SDL_timer_mutex);
......
...@@ -40,10 +40,10 @@ static Uint32 timerStart; ...@@ -40,10 +40,10 @@ static Uint32 timerStart;
void RISCOS_CheckTimer(); void RISCOS_CheckTimer();
#else #else
#include <pthread.h> #include <pthread.h>
extern Uint32 riscos_main_thread; extern SDL_threadID riscos_main_thread;
extern int riscos_using_threads; extern int riscos_using_threads;
extern Uint32 SDL_ThreadID(); extern SDL_threadID SDL_ThreadID();
extern Uint32 SDL_EventThreadID(void); extern SDL_threadID SDL_EventThreadID(void);
#endif #endif
......
...@@ -22,7 +22,7 @@ int SDLCALL ...@@ -22,7 +22,7 @@ int SDLCALL
ThreadFunc(void *data) ThreadFunc(void *data)
{ {
/* Set the child thread error string */ /* 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"); (char *) data, SDL_ThreadID(), "nevermind");
while (alive) { while (alive) {
printf("Thread '%s' is alive!\n", (char *) data); printf("Thread '%s' is alive!\n", (char *) data);
......
...@@ -21,7 +21,7 @@ quit(int rc) ...@@ -21,7 +21,7 @@ quit(int rc)
int SDLCALL int SDLCALL
ThreadFunc(void *data) 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()); (char *) data, SDL_ThreadID());
while (alive) { while (alive) {
printf("Thread '%s' is alive!\n", (char *) data); printf("Thread '%s' is alive!\n", (char *) data);
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "SDL_thread.h" #include "SDL_thread.h"
static SDL_mutex *mutex = NULL; static SDL_mutex *mutex = NULL;
static Uint32 mainthread; static SDL_threadID mainthread;
static SDL_Thread *threads[6]; static SDL_Thread *threads[6];
static volatile int doterminate = 0; static volatile int doterminate = 0;
...@@ -28,7 +28,7 @@ SDL_Quit_Wrapper(void) ...@@ -28,7 +28,7 @@ SDL_Quit_Wrapper(void)
void void
printid(void) printid(void)
{ {
printf("Process %u: exiting\n", SDL_ThreadID()); printf("Process %lu: exiting\n", SDL_ThreadID());
} }
void void
...@@ -41,9 +41,9 @@ terminate(int sig) ...@@ -41,9 +41,9 @@ terminate(int sig)
void void
closemutex(int sig) closemutex(int sig)
{ {
Uint32 id = SDL_ThreadID(); SDL_threadID id = SDL_ThreadID();
int i; int i;
printf("Process %u: Cleaning up...\n", id == mainthread ? 0 : id); printf("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
doterminate = 1; doterminate = 1;
for (i = 0; i < 6; ++i) for (i = 0; i < 6; ++i)
SDL_WaitThread(threads[i], NULL); SDL_WaitThread(threads[i], NULL);
...@@ -57,14 +57,14 @@ Run(void *data) ...@@ -57,14 +57,14 @@ Run(void *data)
if (SDL_ThreadID() == mainthread) if (SDL_ThreadID() == mainthread)
signal(SIGTERM, closemutex); signal(SIGTERM, closemutex);
while (!doterminate) { 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) { if (SDL_mutexP(mutex) < 0) {
fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError()); fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
exit(1); exit(1);
} }
printf("Process %u, working!\n", SDL_ThreadID()); printf("Process %lu, working!\n", SDL_ThreadID());
SDL_Delay(1 * 1000); SDL_Delay(1 * 1000);
printf("Process %u, done!\n", SDL_ThreadID()); printf("Process %lu, done!\n", SDL_ThreadID());
if (SDL_mutexV(mutex) < 0) { if (SDL_mutexV(mutex) < 0) {
fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError()); fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
exit(1); exit(1);
...@@ -73,7 +73,7 @@ Run(void *data) ...@@ -73,7 +73,7 @@ Run(void *data)
SDL_Delay(10); SDL_Delay(10);
} }
if (SDL_ThreadID() == mainthread && doterminate) { if (SDL_ThreadID() == mainthread && doterminate) {
printf("Process %u: raising SIGTERM\n", SDL_ThreadID()); printf("Process %lu: raising SIGTERM\n", SDL_ThreadID());
raise(SIGTERM); raise(SIGTERM);
} }
return (0); return (0);
...@@ -98,7 +98,7 @@ main(int argc, char *argv[]) ...@@ -98,7 +98,7 @@ main(int argc, char *argv[])
} }
mainthread = SDL_ThreadID(); mainthread = SDL_ThreadID();
printf("Main thread: %u\n", mainthread); printf("Main thread: %lu\n", mainthread);
atexit(printid); atexit(printid);
for (i = 0; i < maxproc; ++i) { for (i = 0; i < maxproc; ++i) {
if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL) if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment