Fixed Bugzilla bug #205
authorSam Lantinga <slouken@libsdl.org>
Mon, 24 Nov 2008 00:18:42 +0000
changeset 27794436464c4f51
parent 2778 38dfc890ee6b
child 2780 cc15254e44b6
Fixed Bugzilla bug #205

Removed SDL_KillThread() from the API, as it isn't safe on many platforms.
include/SDL_thread.h
src/thread/SDL_systhread.h
src/thread/SDL_thread.c
src/thread/beos/SDL_systhread.c
src/thread/dc/SDL_systhread.c
src/thread/generic/SDL_systhread.c
src/thread/irix/SDL_systhread.c
src/thread/os2/SDL_systhread.c
src/thread/pth/SDL_systhread.c
src/thread/pthread/SDL_systhread.c
src/thread/riscos/SDL_systhread.c
src/thread/win32/SDL_systhread.c
test/testhread.c
test/testlock.c
     1.1 --- a/include/SDL_thread.h	Sat Nov 22 06:59:22 2008 +0000
     1.2 +++ b/include/SDL_thread.h	Mon Nov 24 00:18:42 2008 +0000
     1.3 @@ -122,7 +122,12 @@
     1.4   */
     1.5  extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
     1.6  
     1.7 -/* Forcefully kill a thread without worrying about its state */
     1.8 +/* This function is here for binary compatibility with legacy apps, but
     1.9 +   in SDL 1.3 and later, it's a no-op. You cannot forcibly kill a thread
    1.10 +   in a safe manner on many platforms. You should instead find a way to
    1.11 +   alert your thread that it is time to terminate, and then have it gracefully
    1.12 +   exit on its own. Do not ever call this function!
    1.13 + */
    1.14  extern DECLSPEC void SDLCALL SDL_KillThread(SDL_Thread * thread);
    1.15  
    1.16  
     2.1 --- a/src/thread/SDL_systhread.h	Sat Nov 22 06:59:22 2008 +0000
     2.2 +++ b/src/thread/SDL_systhread.h	Mon Nov 24 00:18:42 2008 +0000
     2.3 @@ -48,8 +48,5 @@
     2.4   */
     2.5  extern void SDL_SYS_WaitThread(SDL_Thread * thread);
     2.6  
     2.7 -/* This function kills the thread and returns */
     2.8 -extern void SDL_SYS_KillThread(SDL_Thread * thread);
     2.9 -
    2.10  #endif /* _SDL_systhread_h */
    2.11  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/thread/SDL_thread.c	Sat Nov 22 06:59:22 2008 +0000
     3.2 +++ b/src/thread/SDL_thread.c	Mon Nov 24 00:18:42 2008 +0000
     3.3 @@ -306,10 +306,7 @@
     3.4  void
     3.5  SDL_KillThread(SDL_Thread * thread)
     3.6  {
     3.7 -    if (thread) {
     3.8 -        SDL_SYS_KillThread(thread);
     3.9 -        SDL_WaitThread(thread, NULL);
    3.10 -    }
    3.11 +    /* This is a no-op in SDL 1.3 and later. */
    3.12  }
    3.13  
    3.14  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/thread/beos/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     4.2 +++ b/src/thread/beos/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     4.3 @@ -98,10 +98,4 @@
     4.4      wait_for_thread(thread->handle, &the_status);
     4.5  }
     4.6  
     4.7 -void
     4.8 -SDL_SYS_KillThread(SDL_Thread * thread)
     4.9 -{
    4.10 -    kill_thread(thread->handle);
    4.11 -}
    4.12 -
    4.13  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/thread/dc/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     5.2 +++ b/src/thread/dc/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     5.3 @@ -58,10 +58,4 @@
     5.4      thd_wait(thread->handle);
     5.5  }
     5.6  
     5.7 -void
     5.8 -SDL_SYS_KillThread(SDL_Thread * thread)
     5.9 -{
    5.10 -    thd_destroy(thread->handle);
    5.11 -}
    5.12 -
    5.13  /* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/thread/generic/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     6.2 +++ b/src/thread/generic/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     6.3 @@ -51,10 +51,4 @@
     6.4      return;
     6.5  }
     6.6  
     6.7 -void
     6.8 -SDL_SYS_KillThread(SDL_Thread * thread)
     6.9 -{
    6.10 -    return;
    6.11 -}
    6.12 -
    6.13  /* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/thread/irix/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     7.2 +++ b/src/thread/irix/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     7.3 @@ -81,11 +81,4 @@
     7.4      }
     7.5  }
     7.6  
     7.7 -/* WARNING:  This may not work for systems with 64-bit pid_t */
     7.8 -void
     7.9 -SDL_KillThread(SDL_Thread * thread)
    7.10 -{
    7.11 -    kill(thread->handle, SIGKILL);
    7.12 -}
    7.13 -
    7.14  /* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/src/thread/os2/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     8.2 +++ b/src/thread/os2/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     8.3 @@ -102,14 +102,4 @@
     8.4      DosWaitThread(&tid, DCWW_WAIT);
     8.5  }
     8.6  
     8.7 -/* WARNING: This function is really a last resort.
     8.8 - * Threads should be signaled and then exit by themselves.
     8.9 - * TerminateThread() doesn't perform stack and DLL cleanup.
    8.10 - */
    8.11 -void
    8.12 -SDL_SYS_KillThread(SDL_Thread * thread)
    8.13 -{
    8.14 -    DosKillThread(thread->handle);
    8.15 -}
    8.16 -
    8.17  /* vi: set ts=4 sw=4 expandtab: */
     9.1 --- a/src/thread/pth/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
     9.2 +++ b/src/thread/pth/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
     9.3 @@ -101,11 +101,4 @@
     9.4      pth_join(thread->handle, NULL);
     9.5  }
     9.6  
     9.7 -void
     9.8 -SDL_SYS_KillThread(SDL_Thread * thread)
     9.9 -{
    9.10 -    pth_cancel(thread->handle);
    9.11 -    pth_join(thread->handle, NULL);
    9.12 -}
    9.13 -
    9.14  /* vi: set ts=4 sw=4 expandtab: */
    10.1 --- a/src/thread/pthread/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
    10.2 +++ b/src/thread/pthread/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
    10.3 @@ -112,17 +112,4 @@
    10.4      pthread_join(thread->handle, 0);
    10.5  }
    10.6  
    10.7 -void
    10.8 -SDL_SYS_KillThread(SDL_Thread * thread)
    10.9 -{
   10.10 -#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   10.11 -    pthread_cancel(thread->handle);
   10.12 -#else
   10.13 -#ifdef __FREEBSD__
   10.14 -#warning For some reason, this doesnt actually kill a thread - FreeBSD 3.2
   10.15 -#endif
   10.16 -    pthread_kill(thread->handle, SIGKILL);
   10.17 -#endif
   10.18 -}
   10.19 -
   10.20  /* vi: set ts=4 sw=4 expandtab: */
    11.1 --- a/src/thread/riscos/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
    11.2 +++ b/src/thread/riscos/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
    11.3 @@ -54,12 +54,6 @@
    11.4      return;
    11.5  }
    11.6  
    11.7 -void
    11.8 -SDL_SYS_KillThread(SDL_Thread * thread)
    11.9 -{
   11.10 -    return;
   11.11 -}
   11.12 -
   11.13  #else
   11.14  
   11.15  #include <signal.h>
   11.16 @@ -143,15 +137,5 @@
   11.17      pthread_join(thread->handle, 0);
   11.18  }
   11.19  
   11.20 -void
   11.21 -SDL_SYS_KillThread(SDL_Thread * thread)
   11.22 -{
   11.23 -#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
   11.24 -    pthread_cancel(thread->handle);
   11.25 -#else
   11.26 -    pthread_kill(thread->handle, SIGKILL);
   11.27 -#endif
   11.28 -}
   11.29 -
   11.30  #endif
   11.31  /* vi: set ts=4 sw=4 expandtab: */
    12.1 --- a/src/thread/win32/SDL_systhread.c	Sat Nov 22 06:59:22 2008 +0000
    12.2 +++ b/src/thread/win32/SDL_systhread.c	Mon Nov 24 00:18:42 2008 +0000
    12.3 @@ -164,14 +164,4 @@
    12.4      CloseHandle(thread->handle);
    12.5  }
    12.6  
    12.7 -/* WARNING: This function is really a last resort.
    12.8 - * Threads should be signaled and then exit by themselves.
    12.9 - * TerminateThread() doesn't perform stack and DLL cleanup.
   12.10 - */
   12.11 -void
   12.12 -SDL_SYS_KillThread(SDL_Thread * thread)
   12.13 -{
   12.14 -    TerminateThread(thread->handle, FALSE);
   12.15 -}
   12.16 -
   12.17  /* vi: set ts=4 sw=4 expandtab: */
    13.1 --- a/test/testhread.c	Sat Nov 22 06:59:22 2008 +0000
    13.2 +++ b/test/testhread.c	Mon Nov 24 00:18:42 2008 +0000
    13.3 @@ -63,22 +63,12 @@
    13.4      SDL_WaitThread(thread, NULL);
    13.5  
    13.6      alive = 1;
    13.7 +    signal(SIGTERM, killed);
    13.8      thread = SDL_CreateThread(ThreadFunc, "#2");
    13.9      if (thread == NULL) {
   13.10          fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
   13.11          quit(1);
   13.12      }
   13.13 -    SDL_Delay(5 * 1000);
   13.14 -    printf("Killing thread #2\n");
   13.15 -    SDL_KillThread(thread);
   13.16 -
   13.17 -    alive = 1;
   13.18 -    signal(SIGTERM, killed);
   13.19 -    thread = SDL_CreateThread(ThreadFunc, "#3");
   13.20 -    if (thread == NULL) {
   13.21 -        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
   13.22 -        quit(1);
   13.23 -    }
   13.24      raise(SIGTERM);
   13.25  
   13.26      SDL_Quit();                 /* Never reached */
    14.1 --- a/test/testlock.c	Sat Nov 22 06:59:22 2008 +0000
    14.2 +++ b/test/testlock.c	Mon Nov 24 00:18:42 2008 +0000
    14.3 @@ -44,8 +44,9 @@
    14.4      Uint32 id = SDL_ThreadID();
    14.5      int i;
    14.6      printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
    14.7 +    doterminate = 1;
    14.8      for (i = 0; i < 6; ++i)
    14.9 -        SDL_KillThread(threads[i]);
   14.10 +        SDL_WaitThread(threads[i], NULL);
   14.11      SDL_DestroyMutex(mutex);
   14.12      exit(sig);
   14.13  }
   14.14 @@ -55,7 +56,7 @@
   14.15  {
   14.16      if (SDL_ThreadID() == mainthread)
   14.17          signal(SIGTERM, closemutex);
   14.18 -    while (1) {
   14.19 +    while (!doterminate) {
   14.20          printf("Process %u ready to work\n", SDL_ThreadID());
   14.21          if (SDL_mutexP(mutex) < 0) {
   14.22              fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
   14.23 @@ -70,10 +71,10 @@
   14.24          }
   14.25          /* If this sleep isn't done, then threads may starve */
   14.26          SDL_Delay(10);
   14.27 -        if (SDL_ThreadID() == mainthread && doterminate) {
   14.28 -            printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
   14.29 -            raise(SIGTERM);
   14.30 -        }
   14.31 +    }
   14.32 +    if (SDL_ThreadID() == mainthread && doterminate) {
   14.33 +        printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
   14.34 +        raise(SIGTERM);
   14.35      }
   14.36      return (0);
   14.37  }