Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

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.
  • Loading branch information
slouken committed Dec 16, 2009
1 parent 755b845 commit f44a591e61bdab2082ba8dc16cc1f7f1810b06a9
@@ -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.
@@ -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 */
@@ -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);
@@ -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);

@@ -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
@@ -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;
@@ -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
@@ -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");
@@ -39,7 +39,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
@@ -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)
{
@@ -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");
@@ -44,7 +44,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
@@ -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
@@ -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
@@ -24,4 +24,5 @@
#include <pthread.h>

typedef pthread_t SYS_ThreadHandle;

/* vi: set ts=4 sw=4 expandtab: */
@@ -42,7 +42,7 @@ SDL_SYS_SetupThread(void)
return;
}

Uint32
SDL_threadID
SDL_ThreadID(void)
{
return (0);
@@ -151,10 +151,10 @@ SDL_SYS_SetupThread(void)
return;
}

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

void
@@ -25,4 +25,5 @@
#include <windows.h>

typedef HANDLE SYS_ThreadHandle;

/* vi: set ts=4 sw=4 expandtab: */
@@ -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);
@@ -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


@@ -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);
@@ -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);
@@ -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)

0 comments on commit f44a591

Please sign in to comment.