Fixed bug #741
authorSam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 04:48:11 +0000
changeset 35780d1b16ee0bca
parent 3577 72024425b437
child 3579 3427271a2d75
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
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
     1.1 --- a/include/SDL_thread.h	Wed Dec 16 03:02:31 2009 +0000
     1.2 +++ b/include/SDL_thread.h	Wed Dec 16 04:48:11 2009 +0000
     1.3 @@ -47,6 +47,9 @@
     1.4  struct SDL_Thread;
     1.5  typedef struct SDL_Thread SDL_Thread;
     1.6  
     1.7 +/* The SDL thread ID */
     1.8 +typedef unsigned long SDL_threadID;
     1.9 +
    1.10  #if defined(__WIN32__) && !defined(HAVE_LIBC)
    1.11  /**
    1.12   *  \file SDL_thread.h
    1.13 @@ -127,16 +130,16 @@
    1.14  #endif
    1.15  
    1.16  /**
    1.17 - *  Get the 32-bit thread identifier for the current thread.
    1.18 + *  Get the thread identifier for the current thread.
    1.19   */
    1.20 -extern DECLSPEC Uint32 SDLCALL SDL_ThreadID(void);
    1.21 +extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
    1.22  
    1.23  /**
    1.24 - *  Get the 32-bit thread identifier for the specified thread.
    1.25 + *  Get the thread identifier for the specified thread.
    1.26   *  
    1.27   *  Equivalent to SDL_ThreadID() if the specified thread is NULL.
    1.28   */
    1.29 -extern DECLSPEC Uint32 SDLCALL SDL_GetThreadID(SDL_Thread * thread);
    1.30 +extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
    1.31  
    1.32  /**
    1.33   *  Wait for a thread to finish.
     2.1 --- a/src/audio/SDL_sysaudio.h	Wed Dec 16 03:02:31 2009 +0000
     2.2 +++ b/src/audio/SDL_sysaudio.h	Wed Dec 16 04:48:11 2009 +0000
     2.3 @@ -108,7 +108,7 @@
     2.4  
     2.5      /* A thread to feed the audio device */
     2.6      SDL_Thread *thread;
     2.7 -    Uint32 threadid;
     2.8 +    SDL_threadID threadid;
     2.9  
    2.10      /* * * */
    2.11      /* Data private to this driver */
     3.1 --- a/src/events/SDL_events.c	Wed Dec 16 03:02:31 2009 +0000
     3.2 +++ b/src/events/SDL_events.c	Wed Dec 16 04:48:11 2009 +0000
     3.3 @@ -62,7 +62,7 @@
     3.4  
     3.5  /* Thread functions */
     3.6  static SDL_Thread *SDL_EventThread = NULL;      /* Thread handle */
     3.7 -static Uint32 event_thread;     /* The event thread id */
     3.8 +static SDL_threadID event_thread;     /* The event thread id */
     3.9  
    3.10  void
    3.11  SDL_Lock_EventThread(void)
    3.12 @@ -183,7 +183,7 @@
    3.13      }
    3.14  }
    3.15  
    3.16 -Uint32
    3.17 +SDL_threadID
    3.18  SDL_EventThreadID(void)
    3.19  {
    3.20      return (event_thread);
     4.1 --- a/src/events/SDL_events_c.h	Wed Dec 16 03:02:31 2009 +0000
     4.2 +++ b/src/events/SDL_events_c.h	Wed Dec 16 04:48:11 2009 +0000
     4.3 @@ -23,6 +23,7 @@
     4.4  
     4.5  /* Useful functions and variables from SDL_events.c */
     4.6  #include "SDL_events.h"
     4.7 +#include "SDL_thread.h"
     4.8  #include "SDL_mouse_c.h"
     4.9  #include "SDL_keyboard_c.h"
    4.10  #include "SDL_windowevents_c.h"
    4.11 @@ -34,7 +35,7 @@
    4.12  
    4.13  extern void SDL_Lock_EventThread(void);
    4.14  extern void SDL_Unlock_EventThread(void);
    4.15 -extern Uint32 SDL_EventThreadID(void);
    4.16 +extern SDL_threadID SDL_EventThreadID(void);
    4.17  
    4.18  extern int SDL_SendSysWMEvent(SDL_SysWMmsg * message);
    4.19  
     5.1 --- a/src/thread/SDL_thread.c	Wed Dec 16 03:02:31 2009 +0000
     5.2 +++ b/src/thread/SDL_thread.c	Wed Dec 16 04:48:11 2009 +0000
     5.3 @@ -159,7 +159,7 @@
     5.4      errbuf = &SDL_global_error;
     5.5      if (SDL_Threads) {
     5.6          int i;
     5.7 -        Uint32 this_thread;
     5.8 +        SDL_threadID this_thread;
     5.9  
    5.10          this_thread = SDL_ThreadID();
    5.11          SDL_mutexP(thread_lock);
    5.12 @@ -292,17 +292,17 @@
    5.13      }
    5.14  }
    5.15  
    5.16 -Uint32
    5.17 +SDL_threadID
    5.18  SDL_GetThreadID(SDL_Thread * thread)
    5.19  {
    5.20 -    Uint32 id;
    5.21 +    SDL_threadID id;
    5.22  
    5.23      if (thread) {
    5.24          id = thread->threadid;
    5.25      } else {
    5.26          id = SDL_ThreadID();
    5.27      }
    5.28 -    return (id);
    5.29 +    return id;
    5.30  }
    5.31  
    5.32  void
     6.1 --- a/src/thread/SDL_thread_c.h	Wed Dec 16 03:02:31 2009 +0000
     6.2 +++ b/src/thread/SDL_thread_c.h	Wed Dec 16 04:48:11 2009 +0000
     6.3 @@ -50,7 +50,7 @@
     6.4  /* This is the system-independent thread info structure */
     6.5  struct SDL_Thread
     6.6  {
     6.7 -    Uint32 threadid;
     6.8 +    SDL_threadID threadid;
     6.9      SYS_ThreadHandle handle;
    6.10      int status;
    6.11      SDL_error errbuf;
     7.1 --- a/src/thread/beos/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
     7.2 +++ b/src/thread/beos/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
     7.3 @@ -84,10 +84,10 @@
     7.4      SDL_MaskSignals(NULL);
     7.5  }
     7.6  
     7.7 -Uint32
     7.8 +SDL_threadID
     7.9  SDL_ThreadID(void)
    7.10  {
    7.11 -    return ((Uint32) find_thread(NULL));
    7.12 +    return ((SDL_threadID) find_thread(NULL));
    7.13  }
    7.14  
    7.15  void
     8.1 --- a/src/thread/generic/SDL_sysmutex.c	Wed Dec 16 03:02:31 2009 +0000
     8.2 +++ b/src/thread/generic/SDL_sysmutex.c	Wed Dec 16 04:48:11 2009 +0000
     8.3 @@ -30,7 +30,7 @@
     8.4  struct SDL_mutex
     8.5  {
     8.6      int recursive;
     8.7 -    Uint32 owner;
     8.8 +    SDL_threadID owner;
     8.9      SDL_sem *sem;
    8.10  };
    8.11  
    8.12 @@ -76,7 +76,7 @@
    8.13  #if SDL_THREADS_DISABLED
    8.14      return 0;
    8.15  #else
    8.16 -    Uint32 this_thread;
    8.17 +    SDL_threadID this_thread;
    8.18  
    8.19      if (mutex == NULL) {
    8.20          SDL_SetError("Passed a NULL mutex");
     9.1 --- a/src/thread/generic/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
     9.2 +++ b/src/thread/generic/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
     9.3 @@ -39,7 +39,7 @@
     9.4      return;
     9.5  }
     9.6  
     9.7 -Uint32
     9.8 +SDL_threadID
     9.9  SDL_ThreadID(void)
    9.10  {
    9.11      return (0);
    10.1 --- a/src/thread/irix/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    10.2 +++ b/src/thread/irix/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    10.3 @@ -64,14 +64,12 @@
    10.4      sigprocmask(SIG_BLOCK, &mask, NULL);
    10.5  }
    10.6  
    10.7 -/* WARNING:  This may not work for systems with 64-bit pid_t */
    10.8 -Uint32
    10.9 +SDL_threadID
   10.10  SDL_ThreadID(void)
   10.11  {
   10.12 -    return ((Uint32) getpid());
   10.13 +    return ((SDL_threadID) getpid());
   10.14  }
   10.15  
   10.16 -/* WARNING:  This may not work for systems with 64-bit pid_t */
   10.17  void
   10.18  SDL_WaitThread(SDL_Thread * thread, int *status)
   10.19  {
    11.1 --- a/src/thread/nds/SDL_sysmutex.c	Wed Dec 16 03:02:31 2009 +0000
    11.2 +++ b/src/thread/nds/SDL_sysmutex.c	Wed Dec 16 04:48:11 2009 +0000
    11.3 @@ -38,7 +38,7 @@
    11.4  struct SDL_mutex
    11.5  {
    11.6      int recursive;
    11.7 -    Uint32 owner;
    11.8 +    SDL_threadID owner;
    11.9      SDL_sem *sem;
   11.10  };
   11.11  
   11.12 @@ -84,7 +84,7 @@
   11.13  #ifdef DISABLE_THREADS
   11.14      return 0;
   11.15  #else
   11.16 -    Uint32 this_thread;
   11.17 +    SDL_threadID this_thread;
   11.18  
   11.19      if (mutex == NULL) {
   11.20          SDL_SetError("Passed a NULL mutex");
    12.1 --- a/src/thread/nds/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    12.2 +++ b/src/thread/nds/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    12.3 @@ -44,7 +44,7 @@
    12.4      return;
    12.5  }
    12.6  
    12.7 -Uint32
    12.8 +SDL_threadID
    12.9  SDL_ThreadID(void)
   12.10  {
   12.11      return (0);
    13.1 --- a/src/thread/pth/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    13.2 +++ b/src/thread/pth/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    13.3 @@ -88,11 +88,10 @@
    13.4      pth_cancel_state(PTH_CANCEL_ASYNCHRONOUS, &oldstate);
    13.5  }
    13.6  
    13.7 -/* WARNING:  This may not work for systems with 64-bit pid_t */
    13.8 -Uint32
    13.9 +SDL_threadID
   13.10  SDL_ThreadID(void)
   13.11  {
   13.12 -    return ((Uint32) pth_self());
   13.13 +    return ((SDL_threadID) pth_self());
   13.14  }
   13.15  
   13.16  void
    14.1 --- a/src/thread/pthread/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    14.2 +++ b/src/thread/pthread/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    14.3 @@ -38,7 +38,7 @@
    14.4  /* RISC OS needs to know the main thread for
    14.5   * it's timer and event processing. */
    14.6  int riscos_using_threads = 0;
    14.7 -Uint32 riscos_main_thread = 0;  /* Thread running events */
    14.8 +SDL_threadID riscos_main_thread = 0;  /* Thread running events */
    14.9  #endif
   14.10  
   14.11  
   14.12 @@ -99,11 +99,10 @@
   14.13  #endif
   14.14  }
   14.15  
   14.16 -/* WARNING:  This may not work for systems with 64-bit pid_t */
   14.17 -Uint32
   14.18 +SDL_threadID
   14.19  SDL_ThreadID(void)
   14.20  {
   14.21 -    return ((Uint32) pthread_self());
   14.22 +    return ((SDL_threadID) pthread_self());
   14.23  }
   14.24  
   14.25  void
    15.1 --- a/src/thread/pthread/SDL_systhread_c.h	Wed Dec 16 03:02:31 2009 +0000
    15.2 +++ b/src/thread/pthread/SDL_systhread_c.h	Wed Dec 16 04:48:11 2009 +0000
    15.3 @@ -24,4 +24,5 @@
    15.4  #include <pthread.h>
    15.5  
    15.6  typedef pthread_t SYS_ThreadHandle;
    15.7 +
    15.8  /* vi: set ts=4 sw=4 expandtab: */
    16.1 --- a/src/thread/riscos/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    16.2 +++ b/src/thread/riscos/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    16.3 @@ -42,7 +42,7 @@
    16.4      return;
    16.5  }
    16.6  
    16.7 -Uint32
    16.8 +SDL_threadID
    16.9  SDL_ThreadID(void)
   16.10  {
   16.11      return (0);
    17.1 --- a/src/thread/win32/SDL_systhread.c	Wed Dec 16 03:02:31 2009 +0000
    17.2 +++ b/src/thread/win32/SDL_systhread.c	Wed Dec 16 04:48:11 2009 +0000
    17.3 @@ -151,10 +151,10 @@
    17.4      return;
    17.5  }
    17.6  
    17.7 -Uint32
    17.8 +SDL_threadID
    17.9  SDL_ThreadID(void)
   17.10  {
   17.11 -    return ((Uint32) GetCurrentThreadId());
   17.12 +    return ((SDL_threadID) GetCurrentThreadId());
   17.13  }
   17.14  
   17.15  void
    18.1 --- a/src/thread/win32/SDL_systhread_c.h	Wed Dec 16 03:02:31 2009 +0000
    18.2 +++ b/src/thread/win32/SDL_systhread_c.h	Wed Dec 16 04:48:11 2009 +0000
    18.3 @@ -25,4 +25,5 @@
    18.4  #include <windows.h>
    18.5  
    18.6  typedef HANDLE SYS_ThreadHandle;
    18.7 +
    18.8  /* vi: set ts=4 sw=4 expandtab: */
    19.1 --- a/src/timer/SDL_timer.c	Wed Dec 16 03:02:31 2009 +0000
    19.2 +++ b/src/timer/SDL_timer.c	Wed Dec 16 04:48:11 2009 +0000
    19.3 @@ -128,7 +128,7 @@
    19.4                  t->last_alarm = now;
    19.5              }
    19.6  #ifdef DEBUG_TIMERS
    19.7 -            printf("Executing timer %p (thread = %d)\n", t, SDL_ThreadID());
    19.8 +            printf("Executing timer %p (thread = %lu)\n", t, SDL_ThreadID());
    19.9  #endif
   19.10              timer = *t;
   19.11              SDL_mutexV(SDL_timer_mutex);
   19.12 @@ -235,7 +235,7 @@
   19.13          }
   19.14      }
   19.15  #ifdef DEBUG_TIMERS
   19.16 -    printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n",
   19.17 +    printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %lu\n",
   19.18             (Uint32) id, removed, SDL_timer_running, SDL_ThreadID());
   19.19  #endif
   19.20      SDL_mutexV(SDL_timer_mutex);
    20.1 --- a/src/timer/riscos/SDL_systimer.c	Wed Dec 16 03:02:31 2009 +0000
    20.2 +++ b/src/timer/riscos/SDL_systimer.c	Wed Dec 16 04:48:11 2009 +0000
    20.3 @@ -40,10 +40,10 @@
    20.4  void RISCOS_CheckTimer();
    20.5  #else
    20.6  #include <pthread.h>
    20.7 -extern Uint32 riscos_main_thread;
    20.8 +extern SDL_threadID riscos_main_thread;
    20.9  extern int riscos_using_threads;
   20.10 -extern Uint32 SDL_ThreadID();
   20.11 -extern Uint32 SDL_EventThreadID(void);
   20.12 +extern SDL_threadID SDL_ThreadID();
   20.13 +extern SDL_threadID SDL_EventThreadID(void);
   20.14  #endif
   20.15  
   20.16  
    21.1 --- a/test/testerror.c	Wed Dec 16 03:02:31 2009 +0000
    21.2 +++ b/test/testerror.c	Wed Dec 16 04:48:11 2009 +0000
    21.3 @@ -22,7 +22,7 @@
    21.4  ThreadFunc(void *data)
    21.5  {
    21.6      /* Set the child thread error string */
    21.7 -    SDL_SetError("Thread %s (%d) had a problem: %s",
    21.8 +    SDL_SetError("Thread %s (%lu) had a problem: %s",
    21.9                   (char *) data, SDL_ThreadID(), "nevermind");
   21.10      while (alive) {
   21.11          printf("Thread '%s' is alive!\n", (char *) data);
    22.1 --- a/test/testhread.c	Wed Dec 16 03:02:31 2009 +0000
    22.2 +++ b/test/testhread.c	Wed Dec 16 04:48:11 2009 +0000
    22.3 @@ -21,7 +21,7 @@
    22.4  int SDLCALL
    22.5  ThreadFunc(void *data)
    22.6  {
    22.7 -    printf("Started thread %s: My thread id is %u\n",
    22.8 +    printf("Started thread %s: My thread id is %lu\n",
    22.9             (char *) data, SDL_ThreadID());
   22.10      while (alive) {
   22.11          printf("Thread '%s' is alive!\n", (char *) data);
    23.1 --- a/test/testlock.c	Wed Dec 16 03:02:31 2009 +0000
    23.2 +++ b/test/testlock.c	Wed Dec 16 04:48:11 2009 +0000
    23.3 @@ -11,7 +11,7 @@
    23.4  #include "SDL_thread.h"
    23.5  
    23.6  static SDL_mutex *mutex = NULL;
    23.7 -static Uint32 mainthread;
    23.8 +static SDL_threadID mainthread;
    23.9  static SDL_Thread *threads[6];
   23.10  static volatile int doterminate = 0;
   23.11  
   23.12 @@ -28,7 +28,7 @@
   23.13  void
   23.14  printid(void)
   23.15  {
   23.16 -    printf("Process %u:  exiting\n", SDL_ThreadID());
   23.17 +    printf("Process %lu:  exiting\n", SDL_ThreadID());
   23.18  }
   23.19  
   23.20  void
   23.21 @@ -41,9 +41,9 @@
   23.22  void
   23.23  closemutex(int sig)
   23.24  {
   23.25 -    Uint32 id = SDL_ThreadID();
   23.26 +    SDL_threadID id = SDL_ThreadID();
   23.27      int i;
   23.28 -    printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
   23.29 +    printf("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
   23.30      doterminate = 1;
   23.31      for (i = 0; i < 6; ++i)
   23.32          SDL_WaitThread(threads[i], NULL);
   23.33 @@ -57,14 +57,14 @@
   23.34      if (SDL_ThreadID() == mainthread)
   23.35          signal(SIGTERM, closemutex);
   23.36      while (!doterminate) {
   23.37 -        printf("Process %u ready to work\n", SDL_ThreadID());
   23.38 +        printf("Process %lu ready to work\n", SDL_ThreadID());
   23.39          if (SDL_mutexP(mutex) < 0) {
   23.40              fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
   23.41              exit(1);
   23.42          }
   23.43 -        printf("Process %u, working!\n", SDL_ThreadID());
   23.44 +        printf("Process %lu, working!\n", SDL_ThreadID());
   23.45          SDL_Delay(1 * 1000);
   23.46 -        printf("Process %u, done!\n", SDL_ThreadID());
   23.47 +        printf("Process %lu, done!\n", SDL_ThreadID());
   23.48          if (SDL_mutexV(mutex) < 0) {
   23.49              fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
   23.50              exit(1);
   23.51 @@ -73,7 +73,7 @@
   23.52          SDL_Delay(10);
   23.53      }
   23.54      if (SDL_ThreadID() == mainthread && doterminate) {
   23.55 -        printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
   23.56 +        printf("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
   23.57          raise(SIGTERM);
   23.58      }
   23.59      return (0);
   23.60 @@ -98,7 +98,7 @@
   23.61      }
   23.62  
   23.63      mainthread = SDL_ThreadID();
   23.64 -    printf("Main thread: %u\n", mainthread);
   23.65 +    printf("Main thread: %lu\n", mainthread);
   23.66      atexit(printid);
   23.67      for (i = 0; i < maxproc; ++i) {
   23.68          if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)