testthread.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (C) 1997-2024 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. static SDL_TLSID tls;
  17. static SDL_Thread *thread = NULL;
  18. static SDL_AtomicInt alive;
  19. static int testprio = 0;
  20. static SDLTest_CommonState *state;
  21. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  22. static void
  23. quit(int rc)
  24. {
  25. SDL_Quit();
  26. SDLTest_CommonDestroyState(state);
  27. /* Let 'main()' return normally */
  28. if (rc != 0) {
  29. exit(rc);
  30. }
  31. }
  32. static const char *
  33. getprioritystr(SDL_ThreadPriority priority)
  34. {
  35. switch (priority) {
  36. case SDL_THREAD_PRIORITY_LOW:
  37. return "SDL_THREAD_PRIORITY_LOW";
  38. case SDL_THREAD_PRIORITY_NORMAL:
  39. return "SDL_THREAD_PRIORITY_NORMAL";
  40. case SDL_THREAD_PRIORITY_HIGH:
  41. return "SDL_THREAD_PRIORITY_HIGH";
  42. case SDL_THREAD_PRIORITY_TIME_CRITICAL:
  43. return "SDL_THREAD_PRIORITY_TIME_CRITICAL";
  44. }
  45. return "???";
  46. }
  47. static int SDLCALL
  48. ThreadFunc(void *data)
  49. {
  50. SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
  51. SDL_SetTLS(&tls, "baby thread", NULL);
  52. SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s\n",
  53. (char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls));
  54. while (SDL_GetAtomicInt(&alive)) {
  55. SDL_Log("Thread '%s' is alive!\n", (char *)data);
  56. if (testprio) {
  57. SDL_Log("SDL_SetCurrentThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetCurrentThreadPriority(prio));
  58. if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  59. prio = SDL_THREAD_PRIORITY_LOW;
  60. }
  61. }
  62. SDL_Delay(1 * 1000);
  63. }
  64. SDL_Log("Thread '%s' exiting!\n", (char *)data);
  65. return 0;
  66. }
  67. static void
  68. killed(int sig)
  69. {
  70. SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
  71. SDL_Delay(5 * 1000);
  72. SDL_SetAtomicInt(&alive, 0);
  73. SDL_WaitThread(thread, NULL);
  74. quit(0);
  75. }
  76. int main(int argc, char *argv[])
  77. {
  78. int i;
  79. /* Initialize test framework */
  80. state = SDLTest_CommonCreateState(argv, 0);
  81. if (!state) {
  82. return 1;
  83. }
  84. /* Parse commandline */
  85. for (i = 1; i < argc;) {
  86. int consumed;
  87. consumed = SDLTest_CommonArg(state, i);
  88. if (!consumed) {
  89. if (SDL_strcmp("--prio", argv[i]) == 0) {
  90. testprio = 1;
  91. consumed = 1;
  92. }
  93. }
  94. if (consumed <= 0) {
  95. static const char *options[] = { "[--prio]", NULL };
  96. SDLTest_CommonLogUsage(state, argv[0], options);
  97. exit(1);
  98. }
  99. i += consumed;
  100. }
  101. /* Load the SDL library */
  102. if (!SDL_Init(0)) {
  103. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  104. return 1;
  105. }
  106. if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
  107. SDL_Log("Not running slower tests");
  108. SDL_Quit();
  109. return 0;
  110. }
  111. SDL_SetTLS(&tls, "main thread", NULL);
  112. SDL_Log("Main thread data initially: %s\n", (const char *)SDL_GetTLS(&tls));
  113. SDL_SetAtomicInt(&alive, 1);
  114. thread = SDL_CreateThread(ThreadFunc, "One", "#1");
  115. if (!thread) {
  116. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  117. quit(1);
  118. }
  119. SDL_Delay(5 * 1000);
  120. SDL_Log("Waiting for thread #1\n");
  121. SDL_SetAtomicInt(&alive, 0);
  122. SDL_WaitThread(thread, NULL);
  123. SDL_Log("Main thread data finally: %s\n", (const char *)SDL_GetTLS(&tls));
  124. SDL_SetAtomicInt(&alive, 1);
  125. (void)signal(SIGTERM, killed);
  126. thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
  127. if (!thread) {
  128. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  129. quit(1);
  130. }
  131. (void)raise(SIGTERM);
  132. SDL_Quit(); /* Never reached */
  133. return 0; /* Never reached */
  134. }