torturethread.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (C) 1997-2026 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 <stdlib.h>
  12. #include <signal.h>
  13. #include <SDL3/SDL.h>
  14. #include <SDL3/SDL_main.h>
  15. #include <SDL3/SDL_test.h>
  16. #ifdef SDL_PLATFORM_DOS
  17. #define NUMTHREADS 3 /* DOS cooperative scheduler has limited thread slots */
  18. #else
  19. #define NUMTHREADS 10
  20. #endif
  21. static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS];
  22. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  23. static void
  24. quit(int rc)
  25. {
  26. SDL_Quit();
  27. /* Let 'main()' return normally */
  28. if (rc != 0) {
  29. exit(rc);
  30. }
  31. }
  32. static int SDLCALL
  33. SubThreadFunc(void *data)
  34. {
  35. SDL_AtomicInt *flag = (SDL_AtomicInt *)data;
  36. while (!SDL_GetAtomicInt(flag)) {
  37. SDL_Delay(10);
  38. }
  39. return 0;
  40. }
  41. static int SDLCALL
  42. ThreadFunc(void *data)
  43. {
  44. SDL_Thread *sub_threads[NUMTHREADS];
  45. SDL_AtomicInt flags[NUMTHREADS];
  46. int i;
  47. int tid = (int)(uintptr_t)data;
  48. SDL_Log("Creating Thread %d", tid);
  49. for (i = 0; i < NUMTHREADS; i++) {
  50. char name[64];
  51. (void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
  52. SDL_SetAtomicInt(&flags[i], 0);
  53. sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
  54. }
  55. SDL_Log("Thread '%d' waiting for signal", tid);
  56. while (SDL_GetAtomicInt(&time_for_threads_to_die[tid]) != 1) {
  57. #ifdef SDL_PLATFORM_DOS
  58. SDL_Delay(0); /* Yield for cooperative threading */
  59. #endif
  60. }
  61. SDL_Log("Thread '%d' sending signals to subthreads", tid);
  62. for (i = 0; i < NUMTHREADS; i++) {
  63. SDL_SetAtomicInt(&flags[i], 1);
  64. SDL_WaitThread(sub_threads[i], NULL);
  65. }
  66. SDL_Log("Thread '%d' exiting!", tid);
  67. return 0;
  68. }
  69. int main(int argc, char *argv[])
  70. {
  71. SDL_Thread *threads[NUMTHREADS];
  72. int i;
  73. SDLTest_CommonState *state;
  74. /* Initialize test framework */
  75. state = SDLTest_CommonCreateState(argv, 0);
  76. if (!state) {
  77. return 1;
  78. }
  79. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  80. SDL_Quit();
  81. SDLTest_CommonDestroyState(state);
  82. return 1;
  83. }
  84. (void)signal(SIGSEGV, SIG_DFL);
  85. for (i = 0; i < NUMTHREADS; i++) {
  86. char name[64];
  87. (void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
  88. SDL_SetAtomicInt(&time_for_threads_to_die[i], 0);
  89. threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
  90. if (threads[i] == NULL) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
  92. quit(1);
  93. }
  94. }
  95. for (i = 0; i < NUMTHREADS; i++) {
  96. SDL_SetAtomicInt(&time_for_threads_to_die[i], 1);
  97. }
  98. for (i = 0; i < NUMTHREADS; i++) {
  99. SDL_WaitThread(threads[i], NULL);
  100. }
  101. SDL_Quit();
  102. SDLTest_CommonDestroyState(state);
  103. return 0;
  104. }