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

285 lines
8.0 KiB

  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. /* Simple test of the SDL semaphore code */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include "SDL.h"
  15. #define NUM_THREADS 10
  16. /* This value should be smaller than the maximum count of the */
  17. /* semaphore implementation: */
  18. #define NUM_OVERHEAD_OPS 10000
  19. #define NUM_OVERHEAD_OPS_MULT 10
  20. static SDL_sem *sem;
  21. int alive;
  22. typedef struct Thread_State
  23. {
  24. SDL_Thread *thread;
  25. int number;
  26. SDL_bool flag;
  27. int loop_count;
  28. int content_count;
  29. } Thread_State;
  30. static void
  31. killed(int sig)
  32. {
  33. alive = 0;
  34. }
  35. static int SDLCALL
  36. ThreadFuncRealWorld(void *data)
  37. {
  38. Thread_State *state = (Thread_State *)data;
  39. while (alive) {
  40. SDL_SemWait(sem);
  41. SDL_Log("Thread number %d has got the semaphore (value = %" SDL_PRIu32 ")!\n",
  42. state->number, SDL_SemValue(sem));
  43. SDL_Delay(200);
  44. SDL_SemPost(sem);
  45. SDL_Log("Thread number %d has released the semaphore (value = %" SDL_PRIu32 ")!\n",
  46. state->number, SDL_SemValue(sem));
  47. ++state->loop_count;
  48. SDL_Delay(1); /* For the scheduler */
  49. }
  50. SDL_Log("Thread number %d exiting.\n", state->number);
  51. return 0;
  52. }
  53. static void
  54. TestRealWorld(int init_sem)
  55. {
  56. Thread_State thread_states[NUM_THREADS] = { { 0 } };
  57. int i;
  58. int loop_count;
  59. sem = SDL_CreateSemaphore(init_sem);
  60. SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS,
  61. init_sem);
  62. alive = 1;
  63. /* Create all the threads */
  64. for (i = 0; i < NUM_THREADS; ++i) {
  65. char name[64];
  66. (void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i);
  67. thread_states[i].number = i;
  68. thread_states[i].thread = SDL_CreateThread(ThreadFuncRealWorld, name, (void *)&thread_states[i]);
  69. }
  70. /* Wait 10 seconds */
  71. SDL_Delay(10 * 1000);
  72. /* Wait for all threads to finish */
  73. SDL_Log("Waiting for threads to finish\n");
  74. alive = 0;
  75. loop_count = 0;
  76. for (i = 0; i < NUM_THREADS; ++i) {
  77. SDL_WaitThread(thread_states[i].thread, NULL);
  78. loop_count += thread_states[i].loop_count;
  79. }
  80. SDL_Log("Finished waiting for threads, ran %d loops in total\n\n", loop_count);
  81. SDL_DestroySemaphore(sem);
  82. }
  83. static void
  84. TestWaitTimeout(void)
  85. {
  86. Uint32 start_ticks;
  87. Uint32 end_ticks;
  88. Uint32 duration;
  89. int retval;
  90. sem = SDL_CreateSemaphore(0);
  91. SDL_Log("Waiting 2 seconds on semaphore\n");
  92. start_ticks = SDL_GetTicks();
  93. retval = SDL_SemWaitTimeout(sem, 2000);
  94. end_ticks = SDL_GetTicks();
  95. duration = end_ticks - start_ticks;
  96. /* Accept a little offset in the effective wait */
  97. SDL_assert(duration > 1900 && duration < 2050);
  98. SDL_Log("Wait took %" SDL_PRIu32 " milliseconds\n\n", duration);
  99. /* Check to make sure the return value indicates timed out */
  100. if (retval != SDL_MUTEX_TIMEDOUT) {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n\n", retval, SDL_MUTEX_TIMEDOUT);
  102. }
  103. SDL_DestroySemaphore(sem);
  104. }
  105. static void
  106. TestOverheadUncontended(void)
  107. {
  108. Uint32 start_ticks;
  109. Uint32 end_ticks;
  110. Uint32 duration;
  111. int i, j;
  112. sem = SDL_CreateSemaphore(0);
  113. SDL_Log("Doing %d uncontended Post/Wait operations on semaphore\n", NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT);
  114. start_ticks = SDL_GetTicks();
  115. for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) {
  116. for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
  117. SDL_SemPost(sem);
  118. }
  119. for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
  120. SDL_SemWait(sem);
  121. }
  122. }
  123. end_ticks = SDL_GetTicks();
  124. duration = end_ticks - start_ticks;
  125. SDL_Log("Took %" SDL_PRIu32 " milliseconds\n\n", duration);
  126. SDL_DestroySemaphore(sem);
  127. }
  128. static int SDLCALL
  129. ThreadFuncOverheadContended(void *data)
  130. {
  131. Thread_State *state = (Thread_State *)data;
  132. if (state->flag) {
  133. while (alive) {
  134. if (SDL_SemTryWait(sem) == SDL_MUTEX_TIMEDOUT) {
  135. ++state->content_count;
  136. }
  137. ++state->loop_count;
  138. }
  139. } else {
  140. while (alive) {
  141. /* Timeout needed to allow check on alive flag */
  142. if (SDL_SemWaitTimeout(sem, 50) == SDL_MUTEX_TIMEDOUT) {
  143. ++state->content_count;
  144. }
  145. ++state->loop_count;
  146. }
  147. }
  148. return 0;
  149. }
  150. static void
  151. TestOverheadContended(SDL_bool try_wait)
  152. {
  153. Uint32 start_ticks;
  154. Uint32 end_ticks;
  155. Uint32 duration;
  156. Thread_State thread_states[NUM_THREADS] = { { 0 } };
  157. char textBuffer[1024];
  158. int loop_count;
  159. int content_count;
  160. int i, j;
  161. size_t len;
  162. sem = SDL_CreateSemaphore(0);
  163. SDL_Log("Doing %d contended %s operations on semaphore using %d threads\n",
  164. NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT, try_wait ? "Post/TryWait" : "Post/WaitTimeout", NUM_THREADS);
  165. alive = 1;
  166. /* Create multiple threads to starve the semaphore and cause contention */
  167. for (i = 0; i < NUM_THREADS; ++i) {
  168. char name[64];
  169. (void)SDL_snprintf(name, sizeof(name), "Thread%u", (unsigned int)i);
  170. thread_states[i].flag = try_wait;
  171. thread_states[i].thread = SDL_CreateThread(ThreadFuncOverheadContended, name, (void *)&thread_states[i]);
  172. }
  173. start_ticks = SDL_GetTicks();
  174. for (i = 0; i < NUM_OVERHEAD_OPS_MULT; i++) {
  175. for (j = 0; j < NUM_OVERHEAD_OPS; j++) {
  176. SDL_SemPost(sem);
  177. }
  178. /* Make sure threads consumed everything */
  179. while (SDL_SemValue(sem)) {
  180. /* Friendlier with cooperative threading models */
  181. SDL_Delay(1);
  182. }
  183. }
  184. end_ticks = SDL_GetTicks();
  185. alive = 0;
  186. loop_count = 0;
  187. content_count = 0;
  188. for (i = 0; i < NUM_THREADS; ++i) {
  189. SDL_WaitThread(thread_states[i].thread, NULL);
  190. loop_count += thread_states[i].loop_count;
  191. content_count += thread_states[i].content_count;
  192. }
  193. SDL_assert_release((loop_count - content_count) == NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT);
  194. duration = end_ticks - start_ticks;
  195. SDL_Log("Took %" SDL_PRIu32 " milliseconds, threads %s %d out of %d times in total (%.2f%%)\n",
  196. duration, try_wait ? "where contended" : "timed out", content_count,
  197. loop_count, ((float)content_count * 100) / loop_count);
  198. /* Print how many semaphores where consumed per thread */
  199. (void)SDL_snprintf(textBuffer, sizeof(textBuffer), "{ ");
  200. for (i = 0; i < NUM_THREADS; ++i) {
  201. if (i > 0) {
  202. len = SDL_strlen(textBuffer);
  203. (void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", ");
  204. }
  205. len = SDL_strlen(textBuffer);
  206. (void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", thread_states[i].loop_count - thread_states[i].content_count);
  207. }
  208. len = SDL_strlen(textBuffer);
  209. (void)SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
  210. SDL_Log("%s\n", textBuffer);
  211. SDL_DestroySemaphore(sem);
  212. }
  213. int main(int argc, char **argv)
  214. {
  215. int init_sem;
  216. /* Enable standard application logging */
  217. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  218. if (argc < 2) {
  219. SDL_Log("Usage: %s init_value\n", argv[0]);
  220. return 1;
  221. }
  222. /* Load the SDL library */
  223. if (SDL_Init(0) < 0) {
  224. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  225. return 1;
  226. }
  227. (void)signal(SIGTERM, killed);
  228. (void)signal(SIGINT, killed);
  229. init_sem = SDL_atoi(argv[1]);
  230. if (init_sem > 0) {
  231. TestRealWorld(init_sem);
  232. }
  233. TestWaitTimeout();
  234. TestOverheadUncontended();
  235. TestOverheadContended(SDL_FALSE);
  236. TestOverheadContended(SDL_TRUE);
  237. SDL_Quit();
  238. return 0;
  239. }