testthread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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 CheckMainThread(void *data)
  48. {
  49. bool *thread_is_main = (bool *)data;
  50. *thread_is_main = SDL_IsMainThread();
  51. return 0;
  52. }
  53. static int SDLCALL ThreadFunc(void *data)
  54. {
  55. SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
  56. SDL_SetTLS(&tls, "baby thread", NULL);
  57. SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s",
  58. (char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls));
  59. while (SDL_GetAtomicInt(&alive)) {
  60. SDL_Log("Thread '%s' is alive!", (char *)data);
  61. if (testprio) {
  62. SDL_Log("SDL_SetCurrentThreadPriority(%s):%d", getprioritystr(prio), SDL_SetCurrentThreadPriority(prio));
  63. if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  64. prio = SDL_THREAD_PRIORITY_LOW;
  65. }
  66. }
  67. SDL_Delay(1 * 1000);
  68. }
  69. SDL_Log("Thread '%s' exiting!", (char *)data);
  70. return 0;
  71. }
  72. static void
  73. killed(int sig)
  74. {
  75. SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit");
  76. SDL_Delay(5 * 1000);
  77. SDL_SetAtomicInt(&alive, 0);
  78. SDL_WaitThread(thread, NULL);
  79. quit(0);
  80. }
  81. int main(int argc, char *argv[])
  82. {
  83. int i;
  84. bool child_is_main = true;
  85. /* Initialize test framework */
  86. state = SDLTest_CommonCreateState(argv, 0);
  87. if (!state) {
  88. return 1;
  89. }
  90. /* Parse commandline */
  91. for (i = 1; i < argc;) {
  92. int consumed;
  93. consumed = SDLTest_CommonArg(state, i);
  94. if (!consumed) {
  95. if (SDL_strcmp("--prio", argv[i]) == 0) {
  96. testprio = 1;
  97. consumed = 1;
  98. }
  99. }
  100. if (consumed <= 0) {
  101. static const char *options[] = { "[--prio]", NULL };
  102. SDLTest_CommonLogUsage(state, argv[0], options);
  103. quit(1);
  104. }
  105. i += consumed;
  106. }
  107. /* Check main thread */
  108. if (!SDL_IsMainThread()) {
  109. SDL_Log("SDL_IsMainThread() returned false for the main thread");
  110. quit(1);
  111. }
  112. thread = SDL_CreateThread(CheckMainThread, "CheckMainThread", &child_is_main);
  113. if (!thread) {
  114. SDL_Log("Couldn't create thread: %s", SDL_GetError());
  115. quit(1);
  116. }
  117. SDL_WaitThread(thread, NULL);
  118. if (child_is_main) {
  119. SDL_Log("SDL_IsMainThread() returned true for a child thread");
  120. quit(1);
  121. }
  122. if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
  123. SDL_Log("Not running slower tests");
  124. quit(0);
  125. return 0;
  126. }
  127. SDL_SetTLS(&tls, "main thread", NULL);
  128. SDL_Log("Main thread data initially: %s", (const char *)SDL_GetTLS(&tls));
  129. SDL_SetAtomicInt(&alive, 1);
  130. thread = SDL_CreateThread(ThreadFunc, "One", "#1");
  131. if (!thread) {
  132. SDL_Log("Couldn't create thread: %s", SDL_GetError());
  133. quit(1);
  134. }
  135. SDL_Delay(5 * 1000);
  136. SDL_Log("Waiting for thread #1");
  137. SDL_SetAtomicInt(&alive, 0);
  138. SDL_WaitThread(thread, NULL);
  139. SDL_Log("Main thread data finally: %s", (const char *)SDL_GetTLS(&tls));
  140. SDL_SetAtomicInt(&alive, 1);
  141. (void)signal(SIGTERM, killed);
  142. thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
  143. if (!thread) {
  144. SDL_Log("Couldn't create thread: %s", SDL_GetError());
  145. quit(1);
  146. }
  147. (void)raise(SIGTERM);
  148. SDL_Quit(); /* Never reached */
  149. return 0; /* Never reached */
  150. }