testerror.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (C) 1997-2025 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 and error handling */
  11. #include <stdlib.h>
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. #include <SDL3/SDL_test.h>
  15. static int alive = 0;
  16. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  17. static void
  18. quit(int rc)
  19. {
  20. SDL_Quit();
  21. /* Let 'main()' return normally */
  22. if (rc != 0) {
  23. exit(rc);
  24. }
  25. }
  26. static int SDLCALL
  27. ThreadFunc(void *data)
  28. {
  29. /* Set the child thread error string */
  30. SDL_SetError("Thread %s (%" SDL_PRIu64 ") had a problem: %s",
  31. (char *)data, SDL_GetCurrentThreadID(), "nevermind");
  32. while (alive) {
  33. SDL_Log("Thread '%s' is alive!", (char *)data);
  34. SDL_Delay(1 * 1000);
  35. }
  36. SDL_Log("Child thread error string: %s", SDL_GetError());
  37. return 0;
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41. SDL_Thread *thread;
  42. SDLTest_CommonState *state;
  43. int i;
  44. bool enable_threads = true;
  45. /* Initialize test framework */
  46. state = SDLTest_CommonCreateState(argv, 0);
  47. if (!state) {
  48. return 1;
  49. }
  50. /* Parse commandline */
  51. for (i = 1; i < argc;) {
  52. int consumed;
  53. consumed = SDLTest_CommonArg(state, i);
  54. if (consumed == 0) {
  55. consumed = -1;
  56. if (SDL_strcasecmp(argv[i], "--no-threads") == 0) {
  57. enable_threads = false;
  58. consumed = 1;
  59. }
  60. }
  61. if (consumed < 0) {
  62. static const char *options[] = {
  63. "[--no-threads]",
  64. NULL
  65. };
  66. SDLTest_CommonLogUsage(state, argv[0], options);
  67. return 1;
  68. }
  69. i += consumed;
  70. }
  71. /* Load the SDL library */
  72. if (!SDL_Init(0)) {
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
  74. return 1;
  75. }
  76. /* Set the error value for the main thread */
  77. SDL_SetError("No worries");
  78. if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
  79. SDL_Log("Not running slower tests");
  80. SDL_Quit();
  81. return 0;
  82. }
  83. if (enable_threads) {
  84. alive = 1;
  85. thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
  86. if (!thread) {
  87. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
  88. quit(1);
  89. }
  90. SDL_Delay(5 * 1000);
  91. SDL_Log("Waiting for thread #1");
  92. alive = 0;
  93. SDL_WaitThread(thread, NULL);
  94. }
  95. SDL_Log("Main thread error string: %s", SDL_GetError());
  96. SDL_Quit();
  97. SDLTest_CommonDestroyState(state);
  98. return 0;
  99. }