changeset 3578:0d1b16ee0bca

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.
author Sam Lantinga <slouken@libsdl.org>
date Wed, 16 Dec 2009 04:48:11 +0000
parents 72024425b437
children 3427271a2d75
files include/SDL_thread.h src/audio/SDL_sysaudio.h src/events/SDL_events.c src/events/SDL_events_c.h src/thread/SDL_thread.c src/thread/SDL_thread_c.h src/thread/beos/SDL_systhread.c src/thread/generic/SDL_sysmutex.c src/thread/generic/SDL_systhread.c src/thread/irix/SDL_systhread.c src/thread/nds/SDL_sysmutex.c src/thread/nds/SDL_systhread.c src/thread/pth/SDL_systhread.c src/thread/pthread/SDL_systhread.c src/thread/pthread/SDL_systhread_c.h src/thread/riscos/SDL_systhread.c src/thread/win32/SDL_systhread.c src/thread/win32/SDL_systhread_c.h src/timer/SDL_timer.c src/timer/riscos/SDL_systimer.c test/testerror.c test/testhread.c test/testlock.c
diffstat 23 files changed, 53 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_thread.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/include/SDL_thread.h	Wed Dec 16 04:48:11 2009 +0000
@@ -47,6 +47,9 @@
 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 @@
 #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.
--- a/src/audio/SDL_sysaudio.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/audio/SDL_sysaudio.h	Wed Dec 16 04:48:11 2009 +0000
@@ -108,7 +108,7 @@
 
     /* A thread to feed the audio device */
     SDL_Thread *thread;
-    Uint32 threadid;
+    SDL_threadID threadid;
 
     /* * * */
     /* Data private to this driver */
--- a/src/events/SDL_events.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/events/SDL_events.c	Wed Dec 16 04:48:11 2009 +0000
@@ -62,7 +62,7 @@
 
 /* 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 @@
     }
 }
 
-Uint32
+SDL_threadID
 SDL_EventThreadID(void)
 {
     return (event_thread);
--- a/src/events/SDL_events_c.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/events/SDL_events_c.h	Wed Dec 16 04:48:11 2009 +0000
@@ -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_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);
 
--- a/src/thread/SDL_thread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/SDL_thread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -159,7 +159,7 @@
     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 @@
     }
 }
 
-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
--- a/src/thread/SDL_thread_c.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/SDL_thread_c.h	Wed Dec 16 04:48:11 2009 +0000
@@ -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;
--- a/src/thread/beos/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/beos/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -84,10 +84,10 @@
     SDL_MaskSignals(NULL);
 }
 
-Uint32
+SDL_threadID
 SDL_ThreadID(void)
 {
-    return ((Uint32) find_thread(NULL));
+    return ((SDL_threadID) find_thread(NULL));
 }
 
 void
--- a/src/thread/generic/SDL_sysmutex.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/generic/SDL_sysmutex.c	Wed Dec 16 04:48:11 2009 +0000
@@ -30,7 +30,7 @@
 struct SDL_mutex
 {
     int recursive;
-    Uint32 owner;
+    SDL_threadID owner;
     SDL_sem *sem;
 };
 
@@ -76,7 +76,7 @@
 #if SDL_THREADS_DISABLED
     return 0;
 #else
-    Uint32 this_thread;
+    SDL_threadID this_thread;
 
     if (mutex == NULL) {
         SDL_SetError("Passed a NULL mutex");
--- a/src/thread/generic/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/generic/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -39,7 +39,7 @@
     return;
 }
 
-Uint32
+SDL_threadID
 SDL_ThreadID(void)
 {
     return (0);
--- a/src/thread/irix/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/irix/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -64,14 +64,12 @@
     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)
 {
--- a/src/thread/nds/SDL_sysmutex.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/nds/SDL_sysmutex.c	Wed Dec 16 04:48:11 2009 +0000
@@ -38,7 +38,7 @@
 struct SDL_mutex
 {
     int recursive;
-    Uint32 owner;
+    SDL_threadID owner;
     SDL_sem *sem;
 };
 
@@ -84,7 +84,7 @@
 #ifdef DISABLE_THREADS
     return 0;
 #else
-    Uint32 this_thread;
+    SDL_threadID this_thread;
 
     if (mutex == NULL) {
         SDL_SetError("Passed a NULL mutex");
--- a/src/thread/nds/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/nds/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -44,7 +44,7 @@
     return;
 }
 
-Uint32
+SDL_threadID
 SDL_ThreadID(void)
 {
     return (0);
--- a/src/thread/pth/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/pth/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -88,11 +88,10 @@
     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
--- a/src/thread/pthread/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/pthread/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -38,7 +38,7 @@
 /* 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 @@
 #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
--- a/src/thread/pthread/SDL_systhread_c.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/pthread/SDL_systhread_c.h	Wed Dec 16 04:48:11 2009 +0000
@@ -24,4 +24,5 @@
 #include <pthread.h>
 
 typedef pthread_t SYS_ThreadHandle;
+
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/riscos/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/riscos/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -42,7 +42,7 @@
     return;
 }
 
-Uint32
+SDL_threadID
 SDL_ThreadID(void)
 {
     return (0);
--- a/src/thread/win32/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/win32/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -151,10 +151,10 @@
     return;
 }
 
-Uint32
+SDL_threadID
 SDL_ThreadID(void)
 {
-    return ((Uint32) GetCurrentThreadId());
+    return ((SDL_threadID) GetCurrentThreadId());
 }
 
 void
--- a/src/thread/win32/SDL_systhread_c.h	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/thread/win32/SDL_systhread_c.h	Wed Dec 16 04:48:11 2009 +0000
@@ -25,4 +25,5 @@
 #include <windows.h>
 
 typedef HANDLE SYS_ThreadHandle;
+
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/timer/SDL_timer.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/timer/SDL_timer.c	Wed Dec 16 04:48:11 2009 +0000
@@ -128,7 +128,7 @@
                 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 @@
         }
     }
 #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);
--- a/src/timer/riscos/SDL_systimer.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/src/timer/riscos/SDL_systimer.c	Wed Dec 16 04:48:11 2009 +0000
@@ -40,10 +40,10 @@
 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
 
 
--- a/test/testerror.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/test/testerror.c	Wed Dec 16 04:48:11 2009 +0000
@@ -22,7 +22,7 @@
 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);
--- a/test/testhread.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/test/testhread.c	Wed Dec 16 04:48:11 2009 +0000
@@ -21,7 +21,7 @@
 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);
--- a/test/testlock.c	Wed Dec 16 03:02:31 2009 +0000
+++ b/test/testlock.c	Wed Dec 16 04:48:11 2009 +0000
@@ -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 @@
 void
 printid(void)
 {
-    printf("Process %u:  exiting\n", SDL_ThreadID());
+    printf("Process %lu:  exiting\n", SDL_ThreadID());
 }
 
 void
@@ -41,9 +41,9 @@
 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 @@
     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 @@
         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 @@
     }
 
     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)