Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug #741
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Dec 16, 2009
1 parent 755b845 commit f44a591
Show file tree
Hide file tree
Showing 23 changed files with 53 additions and 51 deletions.
11 changes: 7 additions & 4 deletions include/SDL_thread.h
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/audio/SDL_sysaudio.h
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/events/SDL_events.c
Expand Up @@ -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)
Expand Down Expand Up @@ -183,7 +183,7 @@ SDL_StopEventThread(void)
}
}

Uint32
SDL_threadID
SDL_EventThreadID(void)
{
return (event_thread);
Expand Down
3 changes: 2 additions & 1 deletion src/events/SDL_events_c.h
Expand Up @@ -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"
Expand All @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions src/thread/SDL_thread.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/thread/SDL_thread_c.h
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/thread/beos/SDL_systhread.c
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/thread/generic/SDL_sysmutex.c
Expand Up @@ -30,7 +30,7 @@
struct SDL_mutex
{
int recursive;
Uint32 owner;
SDL_threadID owner;
SDL_sem *sem;
};

Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/thread/generic/SDL_systhread.c
Expand Up @@ -39,7 +39,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
Expand Down
6 changes: 2 additions & 4 deletions src/thread/irix/SDL_systhread.c
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/thread/nds/SDL_sysmutex.c
Expand Up @@ -38,7 +38,7 @@ static char rcsid =
struct SDL_mutex
{
int recursive;
Uint32 owner;
SDL_threadID owner;
SDL_sem *sem;
};

Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/thread/nds/SDL_systhread.c
Expand Up @@ -44,7 +44,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
Expand Down
5 changes: 2 additions & 3 deletions src/thread/pth/SDL_systhread.c
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/thread/pthread/SDL_systhread.c
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/thread/pthread/SDL_systhread_c.h
Expand Up @@ -24,4 +24,5 @@
#include <pthread.h>

typedef pthread_t SYS_ThreadHandle;

/* vi: set ts=4 sw=4 expandtab: */
2 changes: 1 addition & 1 deletion src/thread/riscos/SDL_systhread.c
Expand Up @@ -42,7 +42,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
Expand Down
4 changes: 2 additions & 2 deletions src/thread/win32/SDL_systhread.c
Expand Up @@ -151,10 +151,10 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return ((Uint32) GetCurrentThreadId());
return ((SDL_threadID) GetCurrentThreadId());
}

void
Expand Down
1 change: 1 addition & 0 deletions src/thread/win32/SDL_systhread_c.h
Expand Up @@ -25,4 +25,5 @@
#include <windows.h>

typedef HANDLE SYS_ThreadHandle;

/* vi: set ts=4 sw=4 expandtab: */
4 changes: 2 additions & 2 deletions src/timer/SDL_timer.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/timer/riscos/SDL_systimer.c
Expand Up @@ -40,10 +40,10 @@ static Uint32 timerStart;
void RISCOS_CheckTimer();
#else
#include <pthread.h>
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


Expand Down
2 changes: 1 addition & 1 deletion test/testerror.c
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/testhread.c
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions test/testlock.c
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit f44a591

Please sign in to comment.