torturethread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (C) 1997-2025 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. #define NUMTHREADS 10
  17. static SDL_AtomicInt 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. /* Let 'main()' return normally */
  24. if (rc != 0) {
  25. exit(rc);
  26. }
  27. }
  28. static int SDLCALL
  29. SubThreadFunc(void *data)
  30. {
  31. SDL_AtomicInt *flag = (SDL_AtomicInt *)data;
  32. while (!SDL_GetAtomicInt(flag)) {
  33. SDL_Delay(10);
  34. }
  35. return 0;
  36. }
  37. static int SDLCALL
  38. ThreadFunc(void *data)
  39. {
  40. SDL_Thread *sub_threads[NUMTHREADS];
  41. SDL_AtomicInt flags[NUMTHREADS];
  42. int i;
  43. int tid = (int)(uintptr_t)data;
  44. SDL_Log("Creating Thread %d", tid);
  45. for (i = 0; i < NUMTHREADS; i++) {
  46. char name[64];
  47. (void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
  48. SDL_SetAtomicInt(&flags[i], 0);
  49. sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
  50. }
  51. SDL_Log("Thread '%d' waiting for signal", tid);
  52. while (SDL_GetAtomicInt(&time_for_threads_to_die[tid]) != 1) {
  53. ; /* do nothing */
  54. }
  55. SDL_Log("Thread '%d' sending signals to subthreads", tid);
  56. for (i = 0; i < NUMTHREADS; i++) {
  57. SDL_SetAtomicInt(&flags[i], 1);
  58. SDL_WaitThread(sub_threads[i], NULL);
  59. }
  60. SDL_Log("Thread '%d' exiting!", tid);
  61. return 0;
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. SDL_Thread *threads[NUMTHREADS];
  66. int i;
  67. SDLTest_CommonState *state;
  68. /* Initialize test framework */
  69. state = SDLTest_CommonCreateState(argv, 0);
  70. if (!state) {
  71. return 1;
  72. }
  73. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  74. SDL_Quit();
  75. SDLTest_CommonDestroyState(state);
  76. return 1;
  77. }
  78. (void)signal(SIGSEGV, SIG_DFL);
  79. for (i = 0; i < NUMTHREADS; i++) {
  80. char name[64];
  81. (void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
  82. SDL_SetAtomicInt(&time_for_threads_to_die[i], 0);
  83. threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
  84. if (threads[i] == NULL) {
  85. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
  86. quit(1);
  87. }
  88. }
  89. for (i = 0; i < NUMTHREADS; i++) {
  90. SDL_SetAtomicInt(&time_for_threads_to_die[i], 1);
  91. }
  92. for (i = 0; i < NUMTHREADS; i++) {
  93. SDL_WaitThread(threads[i], NULL);
  94. }
  95. SDL_Quit();
  96. SDLTest_CommonDestroyState(state);
  97. return 0;
  98. }