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

113 lines
2.8 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. /* Simple test of the SDL threading code */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include "SDL.h"
  16. #define NUMTHREADS 10
  17. static SDL_atomic_t time_for_threads_to_die[NUMTHREADS];
  18. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  19. static void
  20. quit(int rc)
  21. {
  22. SDL_Quit();
  23. exit(rc);
  24. }
  25. int SDLCALL
  26. SubThreadFunc(void *data)
  27. {
  28. while (!*(int volatile *) data) {
  29. ; /* SDL_Delay(10); *//* do nothing */
  30. }
  31. return 0;
  32. }
  33. int SDLCALL
  34. ThreadFunc(void *data)
  35. {
  36. SDL_Thread *sub_threads[NUMTHREADS];
  37. int flags[NUMTHREADS];
  38. int i;
  39. int tid = (int) (uintptr_t) data;
  40. SDL_Log("Creating Thread %d\n", tid);
  41. for (i = 0; i < NUMTHREADS; i++) {
  42. char name[64];
  43. SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
  44. flags[i] = 0;
  45. sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
  46. }
  47. SDL_Log("Thread '%d' waiting for signal\n", tid);
  48. while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
  49. ; /* do nothing */
  50. }
  51. SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
  52. for (i = 0; i < NUMTHREADS; i++) {
  53. flags[i] = 1;
  54. SDL_WaitThread(sub_threads[i], NULL);
  55. }
  56. SDL_Log("Thread '%d' exiting!\n", tid);
  57. return 0;
  58. }
  59. int
  60. main(int argc, char *argv[])
  61. {
  62. SDL_Thread *threads[NUMTHREADS];
  63. int i;
  64. /* Enable standard application logging */
  65. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  66. /* Load the SDL library */
  67. if (SDL_Init(0) < 0) {
  68. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  69. return (1);
  70. }
  71. signal(SIGSEGV, SIG_DFL);
  72. for (i = 0; i < NUMTHREADS; i++) {
  73. char name[64];
  74. SDL_snprintf(name, sizeof (name), "Parent%d", i);
  75. SDL_AtomicSet(&time_for_threads_to_die[i], 0);
  76. threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
  77. if (threads[i] == NULL) {
  78. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  79. quit(1);
  80. }
  81. }
  82. for (i = 0; i < NUMTHREADS; i++) {
  83. SDL_AtomicSet(&time_for_threads_to_die[i], 1);
  84. }
  85. for (i = 0; i < NUMTHREADS; i++) {
  86. SDL_WaitThread(threads[i], NULL);
  87. }
  88. SDL_Quit();
  89. return (0);
  90. }