testsem.c 3.3 KB

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