🛠️🐜 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.

136 lines
3.6 KiB

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