testlock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (C) 1997-2025 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. /* Test the thread and mutex locking functions
  11. Also exercises the system's signal/thread interaction
  12. */
  13. #include <signal.h>
  14. #include <stdlib.h> /* for atexit() */
  15. #include <SDL3/SDL.h>
  16. #include <SDL3/SDL_main.h>
  17. #include <SDL3/SDL_test.h>
  18. static SDL_Mutex *mutex = NULL;
  19. static SDL_ThreadID mainthread;
  20. static SDL_AtomicInt doterminate;
  21. static int nb_threads = 6;
  22. static SDL_Thread **threads;
  23. static int worktime = 1000;
  24. static SDLTest_CommonState *state;
  25. /**
  26. * SDL_Quit() shouldn't be used with atexit() directly because
  27. * calling conventions may differ...
  28. */
  29. static void
  30. SDL_Quit_Wrapper(void)
  31. {
  32. SDL_Quit();
  33. SDLTest_CommonDestroyState(state);
  34. }
  35. static void printid(void)
  36. {
  37. SDL_Log("Thread %" SDL_PRIu64 ": exiting", SDL_GetCurrentThreadID());
  38. }
  39. static void terminate(int sig)
  40. {
  41. (void)signal(SIGINT, terminate);
  42. SDL_SetAtomicInt(&doterminate, 1);
  43. }
  44. static void closemutex(int sig)
  45. {
  46. SDL_ThreadID id = SDL_GetCurrentThreadID();
  47. int i;
  48. SDL_Log("Thread %" SDL_PRIu64 ": Cleaning up...", id == mainthread ? 0 : id);
  49. SDL_SetAtomicInt(&doterminate, 1);
  50. if (threads) {
  51. for (i = 0; i < nb_threads; ++i) {
  52. SDL_WaitThread(threads[i], NULL);
  53. }
  54. SDL_free(threads);
  55. threads = NULL;
  56. }
  57. SDL_DestroyMutex(mutex);
  58. /* Let 'main()' return normally */
  59. if (sig != 0) {
  60. exit(sig);
  61. }
  62. }
  63. static int SDLCALL
  64. Run(void *data)
  65. {
  66. SDL_ThreadID current_thread = SDL_GetCurrentThreadID();
  67. if (current_thread == mainthread) {
  68. (void)signal(SIGTERM, closemutex);
  69. }
  70. SDL_Log("Thread %" SDL_PRIu64 ": starting up", current_thread);
  71. while (!SDL_GetAtomicInt(&doterminate)) {
  72. SDL_Log("Thread %" SDL_PRIu64 ": ready to work", current_thread);
  73. SDL_LockMutex(mutex);
  74. SDL_Log("Thread %" SDL_PRIu64 ": start work!", current_thread);
  75. SDL_Delay(1 * worktime);
  76. SDL_Log("Thread %" SDL_PRIu64 ": work done!", current_thread);
  77. SDL_UnlockMutex(mutex);
  78. /* If this sleep isn't done, then threads may starve */
  79. SDL_Delay(10);
  80. }
  81. if (current_thread == mainthread && SDL_GetAtomicInt(&doterminate)) {
  82. SDL_Log("Thread %" SDL_PRIu64 ": raising SIGTERM", current_thread);
  83. (void)raise(SIGTERM);
  84. }
  85. SDL_Log("Thread %" SDL_PRIu64 ": exiting!", current_thread);
  86. return 0;
  87. }
  88. #ifndef _WIN32
  89. static Uint32 hit_timeout(void *param, SDL_TimerID timerID, Uint32 interval) {
  90. SDL_Log("Hit timeout! Sending SIGINT!");
  91. (void)raise(SIGINT);
  92. return 0;
  93. }
  94. #endif
  95. int main(int argc, char *argv[])
  96. {
  97. int i;
  98. #ifndef _WIN32
  99. int timeout = 0;
  100. #endif
  101. /* Initialize test framework */
  102. state = SDLTest_CommonCreateState(argv, 0);
  103. if (!state) {
  104. return 1;
  105. }
  106. /* Parse commandline */
  107. for (i = 1; i < argc;) {
  108. int consumed;
  109. consumed = SDLTest_CommonArg(state, i);
  110. if (!consumed) {
  111. if (SDL_strcmp(argv[i], "--nbthreads") == 0) {
  112. if (argv[i + 1]) {
  113. char *endptr;
  114. nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
  115. if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
  116. consumed = 2;
  117. }
  118. }
  119. } else if (SDL_strcmp(argv[i], "--worktime") == 0) {
  120. if (argv[i + 1]) {
  121. char *endptr;
  122. nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
  123. if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
  124. consumed = 2;
  125. }
  126. }
  127. #ifndef _WIN32
  128. } else if (SDL_strcmp(argv[i], "--timeout") == 0) {
  129. if (argv[i + 1]) {
  130. char *endptr;
  131. timeout = SDL_strtol(argv[i + 1], &endptr, 0);
  132. if (endptr != argv[i + 1] && *endptr == '\0' && timeout > 0) {
  133. consumed = 2;
  134. }
  135. }
  136. #endif
  137. }
  138. }
  139. if (consumed <= 0) {
  140. static const char *options[] = {
  141. "[--nbthreads NB]",
  142. "[--worktime ms]",
  143. #ifndef _WIN32
  144. "[--timeout ms]",
  145. #endif
  146. NULL,
  147. };
  148. SDLTest_CommonLogUsage(state, argv[0], options);
  149. exit(1);
  150. }
  151. i += consumed;
  152. }
  153. threads = SDL_malloc(nb_threads * sizeof(SDL_Thread*));
  154. /* Load the SDL library */
  155. if (!SDL_Init(0)) {
  156. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", SDL_GetError());
  157. exit(1);
  158. }
  159. (void)atexit(SDL_Quit_Wrapper);
  160. SDL_SetAtomicInt(&doterminate, 0);
  161. mutex = SDL_CreateMutex();
  162. if (!mutex) {
  163. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s", SDL_GetError());
  164. exit(1);
  165. }
  166. mainthread = SDL_GetCurrentThreadID();
  167. SDL_Log("Main thread: %" SDL_PRIu64, mainthread);
  168. (void)atexit(printid);
  169. for (i = 0; i < nb_threads; ++i) {
  170. char name[64];
  171. (void)SDL_snprintf(name, sizeof(name), "Worker%d", i);
  172. threads[i] = SDL_CreateThread(Run, name, NULL);
  173. if (threads[i] == NULL) {
  174. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!");
  175. }
  176. }
  177. #ifndef _WIN32
  178. if (timeout) {
  179. SDL_AddTimer(timeout, hit_timeout, NULL);
  180. }
  181. #endif
  182. (void)signal(SIGINT, terminate);
  183. Run(NULL);
  184. return 0; /* Never reached */
  185. }