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

129 lines
3.4 KiB

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