testsem.c 7.9 KB

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