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

128 lines
3.4 KiB

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