testsem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (C) 1997-2019 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. static SDL_sem *sem;
  17. int alive = 1;
  18. int SDLCALL
  19. ThreadFunc(void *data)
  20. {
  21. int threadnum = (int) (uintptr_t) data;
  22. while (alive) {
  23. SDL_SemWait(sem);
  24. SDL_Log("Thread number %d has got the semaphore (value = %d)!\n",
  25. threadnum, SDL_SemValue(sem));
  26. SDL_Delay(200);
  27. SDL_SemPost(sem);
  28. SDL_Log("Thread number %d has released the semaphore (value = %d)!\n",
  29. threadnum, SDL_SemValue(sem));
  30. SDL_Delay(1); /* For the scheduler */
  31. }
  32. SDL_Log("Thread number %d exiting.\n", threadnum);
  33. return 0;
  34. }
  35. static void
  36. killed(int sig)
  37. {
  38. alive = 0;
  39. }
  40. static void
  41. TestWaitTimeout(void)
  42. {
  43. Uint32 start_ticks;
  44. Uint32 end_ticks;
  45. Uint32 duration;
  46. int retval;
  47. sem = SDL_CreateSemaphore(0);
  48. SDL_Log("Waiting 2 seconds on semaphore\n");
  49. start_ticks = SDL_GetTicks();
  50. retval = SDL_SemWaitTimeout(sem, 2000);
  51. end_ticks = SDL_GetTicks();
  52. duration = end_ticks - start_ticks;
  53. /* Accept a little offset in the effective wait */
  54. if (duration > 1900 && duration < 2050)
  55. SDL_Log("Wait done.\n");
  56. else
  57. SDL_Log("Wait took %d milliseconds\n", duration);
  58. /* Check to make sure the return value indicates timed out */
  59. if (retval != SDL_MUTEX_TIMEDOUT)
  60. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
  61. }
  62. int
  63. main(int argc, char **argv)
  64. {
  65. SDL_Thread *threads[NUM_THREADS];
  66. uintptr_t i;
  67. int init_sem;
  68. /* Enable standard application logging */
  69. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  70. if (argc < 2) {
  71. SDL_Log("Usage: %s init_value\n", argv[0]);
  72. return (1);
  73. }
  74. /* Load the SDL library */
  75. if (SDL_Init(0) < 0) {
  76. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  77. return (1);
  78. }
  79. signal(SIGTERM, killed);
  80. signal(SIGINT, killed);
  81. init_sem = atoi(argv[1]);
  82. sem = SDL_CreateSemaphore(init_sem);
  83. SDL_Log("Running %d threads, semaphore value = %d\n", NUM_THREADS,
  84. init_sem);
  85. /* Create all the threads */
  86. for (i = 0; i < NUM_THREADS; ++i) {
  87. char name[64];
  88. SDL_snprintf(name, sizeof (name), "Thread%u", (unsigned int) i);
  89. threads[i] = SDL_CreateThread(ThreadFunc, name, (void *) i);
  90. }
  91. /* Wait 10 seconds */
  92. SDL_Delay(10 * 1000);
  93. /* Wait for all threads to finish */
  94. SDL_Log("Waiting for threads to finish\n");
  95. alive = 0;
  96. for (i = 0; i < NUM_THREADS; ++i) {
  97. SDL_WaitThread(threads[i], NULL);
  98. }
  99. SDL_Log("Finished waiting for threads\n");
  100. SDL_DestroySemaphore(sem);
  101. TestWaitTimeout();
  102. SDL_Quit();
  103. return (0);
  104. }