testlock.c 5.9 KB

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