testlock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright (C) 1997-2023 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. /* 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 %lu: exiting\n", SDL_ThreadID());
  38. }
  39. static void terminate(int sig)
  40. {
  41. (void)signal(SIGINT, terminate);
  42. SDL_AtomicSet(&doterminate, 1);
  43. }
  44. static void closemutex(int sig)
  45. {
  46. SDL_threadID id = SDL_ThreadID();
  47. int i;
  48. SDL_Log("Thread %lu: Cleaning up...\n", id == mainthread ? 0 : id);
  49. SDL_AtomicSet(&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. if (SDL_ThreadID() == mainthread) {
  67. (void)signal(SIGTERM, closemutex);
  68. }
  69. SDL_Log("Thread %lu: starting up", SDL_ThreadID());
  70. while (!SDL_AtomicGet(&doterminate)) {
  71. SDL_Log("Thread %lu: ready to work\n", SDL_ThreadID());
  72. if (SDL_LockMutex(mutex) < 0) {
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
  74. exit(1);
  75. }
  76. SDL_Log("Thread %lu: start work!\n", SDL_ThreadID());
  77. SDL_Delay(1 * worktime);
  78. SDL_Log("Thread %lu: work done!\n", SDL_ThreadID());
  79. if (SDL_UnlockMutex(mutex) < 0) {
  80. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
  81. exit(1);
  82. }
  83. /* If this sleep isn't done, then threads may starve */
  84. SDL_Delay(10);
  85. }
  86. if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
  87. SDL_Log("Thread %lu: raising SIGTERM\n", SDL_ThreadID());
  88. (void)raise(SIGTERM);
  89. }
  90. SDL_Log("Thread %lu: exiting!\n", SDL_ThreadID());
  91. return 0;
  92. }
  93. #ifndef _WIN32
  94. static Uint32 hit_timeout(Uint32 interval, void *param) {
  95. SDL_Log("Hit timeout! Sending SIGINT!");
  96. kill(0, SIGINT);
  97. return 0;
  98. }
  99. #endif
  100. int main(int argc, char *argv[])
  101. {
  102. int i;
  103. #ifndef _WIN32
  104. int timeout = 0;
  105. #endif
  106. /* Initialize test framework */
  107. state = SDLTest_CommonCreateState(argv, 0);
  108. if (state == NULL) {
  109. return 1;
  110. }
  111. /* Enable standard application logging */
  112. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  113. /* Parse commandline */
  114. for (i = 1; i < argc;) {
  115. int consumed;
  116. consumed = SDLTest_CommonArg(state, i);
  117. if (!consumed) {
  118. if (SDL_strcmp(argv[i], "--nbthreads") == 0) {
  119. if (argv[i + 1]) {
  120. char *endptr;
  121. nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
  122. if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
  123. consumed = 2;
  124. }
  125. }
  126. } else if (SDL_strcmp(argv[i], "--worktime") == 0) {
  127. if (argv[i + 1]) {
  128. char *endptr;
  129. nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
  130. if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
  131. consumed = 2;
  132. }
  133. }
  134. #ifndef _WIN32
  135. } else if (SDL_strcmp(argv[i], "--timeout") == 0) {
  136. if (argv[i + 1]) {
  137. char *endptr;
  138. timeout = SDL_strtol(argv[i + 1], &endptr, 0);
  139. if (endptr != argv[i + 1] && *endptr == '\0' && timeout > 0) {
  140. consumed = 2;
  141. }
  142. }
  143. #endif
  144. }
  145. }
  146. if (consumed <= 0) {
  147. static const char *options[] = {
  148. "[--nbthreads NB]",
  149. "[--worktime ms]",
  150. #ifndef _WIN32
  151. "[--timeout ms]",
  152. #endif
  153. NULL,
  154. };
  155. SDLTest_CommonLogUsage(state, argv[0], options);
  156. exit(1);
  157. }
  158. i += consumed;
  159. }
  160. threads = SDL_malloc(nb_threads * sizeof(SDL_Thread*));
  161. /* Load the SDL library */
  162. if (SDL_Init(0) < 0) {
  163. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  164. exit(1);
  165. }
  166. (void)atexit(SDL_Quit_Wrapper);
  167. SDL_AtomicSet(&doterminate, 0);
  168. mutex = SDL_CreateMutex();
  169. if (mutex == NULL) {
  170. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
  171. exit(1);
  172. }
  173. mainthread = SDL_ThreadID();
  174. SDL_Log("Main thread: %lu\n", mainthread);
  175. (void)atexit(printid);
  176. for (i = 0; i < nb_threads; ++i) {
  177. char name[64];
  178. (void)SDL_snprintf(name, sizeof(name), "Worker%d", i);
  179. threads[i] = SDL_CreateThread(Run, name, NULL);
  180. if (threads[i] == NULL) {
  181. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
  182. }
  183. }
  184. #ifndef _WIN32
  185. if (timeout) {
  186. SDL_AddTimer(timeout, hit_timeout, NULL);
  187. }
  188. #endif
  189. (void)signal(SIGINT, terminate);
  190. Run(NULL);
  191. return 0; /* Never reached */
  192. }