torturethread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple test of the SDL threading code */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include "SDL.h"
  16. #include "SDL_thread.h"
  17. #define NUMTHREADS 10
  18. static char volatile time_for_threads_to_die[NUMTHREADS];
  19. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  20. static void
  21. quit(int rc)
  22. {
  23. SDL_Quit();
  24. exit(rc);
  25. }
  26. int SDLCALL
  27. SubThreadFunc(void *data)
  28. {
  29. while (!*(int volatile *) data) {
  30. ; /* SDL_Delay(10); *//* do nothing */
  31. }
  32. return 0;
  33. }
  34. int SDLCALL
  35. ThreadFunc(void *data)
  36. {
  37. SDL_Thread *sub_threads[NUMTHREADS];
  38. int flags[NUMTHREADS];
  39. int i;
  40. int tid = (int) (uintptr_t) data;
  41. SDL_Log("Creating Thread %d\n", tid);
  42. for (i = 0; i < NUMTHREADS; i++) {
  43. char name[64];
  44. SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
  45. flags[i] = 0;
  46. sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
  47. }
  48. SDL_Log("Thread '%d' waiting for signal\n", tid);
  49. while (time_for_threads_to_die[tid] != 1) {
  50. ; /* do nothing */
  51. }
  52. SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
  53. for (i = 0; i < NUMTHREADS; i++) {
  54. flags[i] = 1;
  55. SDL_WaitThread(sub_threads[i], NULL);
  56. }
  57. SDL_Log("Thread '%d' exiting!\n", tid);
  58. return 0;
  59. }
  60. int
  61. main(int argc, char *argv[])
  62. {
  63. SDL_Thread *threads[NUMTHREADS];
  64. int i;
  65. /* Enable standard application logging */
  66. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  67. /* Load the SDL library */
  68. if (SDL_Init(0) < 0) {
  69. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  70. return (1);
  71. }
  72. signal(SIGSEGV, SIG_DFL);
  73. for (i = 0; i < NUMTHREADS; i++) {
  74. char name[64];
  75. SDL_snprintf(name, sizeof (name), "Parent%d", i);
  76. time_for_threads_to_die[i] = 0;
  77. threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
  78. if (threads[i] == NULL) {
  79. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  80. quit(1);
  81. }
  82. }
  83. for (i = 0; i < NUMTHREADS; i++) {
  84. time_for_threads_to_die[i] = 1;
  85. }
  86. for (i = 0; i < NUMTHREADS; i++) {
  87. SDL_WaitThread(threads[i], NULL);
  88. }
  89. SDL_Quit();
  90. return (0);
  91. }