testthread.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (C) 1997-2019 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. while (argv[arg] && *argv[arg] == '-') {
  77. if (SDL_strcmp(argv[arg], "--prio") == 0) {
  78. testprio = 1;
  79. }
  80. ++arg;
  81. }
  82. tls = SDL_TLSCreate();
  83. SDL_assert(tls);
  84. SDL_TLSSet(tls, "main thread", NULL);
  85. SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
  86. alive = 1;
  87. thread = SDL_CreateThread(ThreadFunc, "One", "#1");
  88. if (thread == NULL) {
  89. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  90. quit(1);
  91. }
  92. SDL_Delay(5 * 1000);
  93. SDL_Log("Waiting for thread #1\n");
  94. alive = 0;
  95. SDL_WaitThread(thread, NULL);
  96. SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
  97. alive = 1;
  98. signal(SIGTERM, killed);
  99. thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
  100. if (thread == NULL) {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  102. quit(1);
  103. }
  104. raise(SIGTERM);
  105. SDL_Quit(); /* Never reached */
  106. return (0); /* Never reached */
  107. }