torturethread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (C) 1997-2019 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. #define NUMTHREADS 10
  17. static SDL_atomic_t time_for_threads_to_die[NUMTHREADS];
  18. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  19. static void
  20. quit(int rc)
  21. {
  22. SDL_Quit();
  23. exit(rc);
  24. }
  25. int SDLCALL
  26. SubThreadFunc(void *data)
  27. {
  28. while (!*(int volatile *) data) {
  29. ; /* SDL_Delay(10); *//* do nothing */
  30. }
  31. return 0;
  32. }
  33. int SDLCALL
  34. ThreadFunc(void *data)
  35. {
  36. SDL_Thread *sub_threads[NUMTHREADS];
  37. int flags[NUMTHREADS];
  38. int i;
  39. int tid = (int) (uintptr_t) data;
  40. SDL_Log("Creating Thread %d\n", tid);
  41. for (i = 0; i < NUMTHREADS; i++) {
  42. char name[64];
  43. SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
  44. flags[i] = 0;
  45. sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
  46. }
  47. SDL_Log("Thread '%d' waiting for signal\n", tid);
  48. while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
  49. ; /* do nothing */
  50. }
  51. SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
  52. for (i = 0; i < NUMTHREADS; i++) {
  53. flags[i] = 1;
  54. SDL_WaitThread(sub_threads[i], NULL);
  55. }
  56. SDL_Log("Thread '%d' exiting!\n", tid);
  57. return 0;
  58. }
  59. int
  60. main(int argc, char *argv[])
  61. {
  62. SDL_Thread *threads[NUMTHREADS];
  63. int i;
  64. /* Enable standard application logging */
  65. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  66. /* Load the SDL library */
  67. if (SDL_Init(0) < 0) {
  68. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  69. return (1);
  70. }
  71. signal(SIGSEGV, SIG_DFL);
  72. for (i = 0; i < NUMTHREADS; i++) {
  73. char name[64];
  74. SDL_snprintf(name, sizeof (name), "Parent%d", i);
  75. SDL_AtomicSet(&time_for_threads_to_die[i], 0);
  76. threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
  77. if (threads[i] == NULL) {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  79. quit(1);
  80. }
  81. }
  82. for (i = 0; i < NUMTHREADS; i++) {
  83. SDL_AtomicSet(&time_for_threads_to_die[i], 1);
  84. }
  85. for (i = 0; i < NUMTHREADS; i++) {
  86. SDL_WaitThread(threads[i], NULL);
  87. }
  88. SDL_Quit();
  89. return (0);
  90. }