testlock.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /* Test the thread and mutex locking functions
  11. Also exercises the system's signal/thread interaction
  12. */
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h> /* for atexit() */
  16. #include "SDL.h"
  17. #include "SDL_mutex.h"
  18. #include "SDL_thread.h"
  19. static SDL_mutex *mutex = NULL;
  20. static SDL_threadID mainthread;
  21. static SDL_Thread *threads[6];
  22. static volatile int doterminate = 0;
  23. /*
  24. * SDL_Quit() shouldn't be used with atexit() directly because
  25. * calling conventions may differ...
  26. */
  27. static void
  28. SDL_Quit_Wrapper(void)
  29. {
  30. SDL_Quit();
  31. }
  32. void
  33. printid(void)
  34. {
  35. SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
  36. }
  37. void
  38. terminate(int sig)
  39. {
  40. signal(SIGINT, terminate);
  41. doterminate = 1;
  42. }
  43. void
  44. closemutex(int sig)
  45. {
  46. SDL_threadID id = SDL_ThreadID();
  47. int i;
  48. SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
  49. doterminate = 1;
  50. for (i = 0; i < 6; ++i)
  51. SDL_WaitThread(threads[i], NULL);
  52. SDL_DestroyMutex(mutex);
  53. exit(sig);
  54. }
  55. int SDLCALL
  56. Run(void *data)
  57. {
  58. if (SDL_ThreadID() == mainthread)
  59. signal(SIGTERM, closemutex);
  60. while (!doterminate) {
  61. SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
  62. if (SDL_LockMutex(mutex) < 0) {
  63. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
  64. exit(1);
  65. }
  66. SDL_Log("Process %lu, working!\n", SDL_ThreadID());
  67. SDL_Delay(1 * 1000);
  68. SDL_Log("Process %lu, done!\n", SDL_ThreadID());
  69. if (SDL_UnlockMutex(mutex) < 0) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
  71. exit(1);
  72. }
  73. /* If this sleep isn't done, then threads may starve */
  74. SDL_Delay(10);
  75. }
  76. if (SDL_ThreadID() == mainthread && doterminate) {
  77. SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
  78. raise(SIGTERM);
  79. }
  80. return (0);
  81. }
  82. int
  83. main(int argc, char *argv[])
  84. {
  85. int i;
  86. int maxproc = 6;
  87. /* Enable standard application logging */
  88. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  89. /* Load the SDL library */
  90. if (SDL_Init(0) < 0) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
  92. exit(1);
  93. }
  94. atexit(SDL_Quit_Wrapper);
  95. if ((mutex = SDL_CreateMutex()) == NULL) {
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
  97. exit(1);
  98. }
  99. mainthread = SDL_ThreadID();
  100. SDL_Log("Main thread: %lu\n", mainthread);
  101. atexit(printid);
  102. for (i = 0; i < maxproc; ++i) {
  103. char name[64];
  104. SDL_snprintf(name, sizeof (name), "Worker%d", i);
  105. if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
  106. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
  107. }
  108. signal(SIGINT, terminate);
  109. Run(NULL);
  110. return (0); /* Never reached */
  111. }