🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.6 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 "SDL.h"
  15. static SDL_TLSID tls;
  16. static int alive = 0;
  17. static int testprio = 0;
  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. static const char *
  26. getprioritystr(SDL_ThreadPriority priority)
  27. {
  28. switch (priority) {
  29. case SDL_THREAD_PRIORITY_LOW:
  30. return "SDL_THREAD_PRIORITY_LOW";
  31. case SDL_THREAD_PRIORITY_NORMAL:
  32. return "SDL_THREAD_PRIORITY_NORMAL";
  33. case SDL_THREAD_PRIORITY_HIGH:
  34. return "SDL_THREAD_PRIORITY_HIGH";
  35. case SDL_THREAD_PRIORITY_TIME_CRITICAL:
  36. return "SDL_THREAD_PRIORITY_TIME_CRITICAL";
  37. }
  38. return "???";
  39. }
  40. int SDLCALL
  41. ThreadFunc(void *data)
  42. {
  43. SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
  44. SDL_TLSSet(tls, "baby thread", NULL);
  45. SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
  46. (char *)data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
  47. while (alive) {
  48. SDL_Log("Thread '%s' is alive!\n", (char *)data);
  49. if (testprio) {
  50. SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio));
  51. if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  52. prio = SDL_THREAD_PRIORITY_LOW;
  53. }
  54. }
  55. SDL_Delay(1 * 1000);
  56. }
  57. SDL_Log("Thread '%s' exiting!\n", (char *)data);
  58. return 0;
  59. }
  60. static void
  61. killed(int sig)
  62. {
  63. SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
  64. SDL_Delay(5 * 1000);
  65. alive = 0;
  66. quit(0);
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. int arg = 1;
  71. SDL_Thread *thread;
  72. /* Enable standard application logging */
  73. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  74. /* Load the SDL library */
  75. if (SDL_Init(0) < 0) {
  76. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  77. return 1;
  78. }
  79. if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
  80. SDL_Log("Not running slower tests");
  81. SDL_Quit();
  82. return 0;
  83. }
  84. while (argv[arg] && *argv[arg] == '-') {
  85. if (SDL_strcmp(argv[arg], "--prio") == 0) {
  86. testprio = 1;
  87. }
  88. ++arg;
  89. }
  90. tls = SDL_TLSCreate();
  91. SDL_assert(tls);
  92. SDL_TLSSet(tls, "main thread", NULL);
  93. SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
  94. alive = 1;
  95. thread = SDL_CreateThread(ThreadFunc, "One", "#1");
  96. if (thread == NULL) {
  97. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  98. quit(1);
  99. }
  100. SDL_Delay(5 * 1000);
  101. SDL_Log("Waiting for thread #1\n");
  102. alive = 0;
  103. SDL_WaitThread(thread, NULL);
  104. SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
  105. alive = 1;
  106. (void)signal(SIGTERM, killed);
  107. thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
  108. if (thread == NULL) {
  109. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  110. quit(1);
  111. }
  112. (void)raise(SIGTERM);
  113. SDL_Quit(); /* Never reached */
  114. return 0; /* Never reached */
  115. }