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

82 lines
2.1 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. /* Simple test of the SDL threading code and error handling */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "SDL.h"
  14. static int alive = 0;
  15. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  16. static void
  17. quit(int rc)
  18. {
  19. SDL_Quit();
  20. exit(rc);
  21. }
  22. int SDLCALL
  23. ThreadFunc(void *data)
  24. {
  25. /* Set the child thread error string */
  26. SDL_SetError("Thread %s (%lu) had a problem: %s",
  27. (char *) data, SDL_ThreadID(), "nevermind");
  28. while (alive) {
  29. SDL_Log("Thread '%s' is alive!\n", (char *) data);
  30. SDL_Delay(1 * 1000);
  31. }
  32. SDL_Log("Child thread error string: %s\n", SDL_GetError());
  33. return (0);
  34. }
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. SDL_Thread *thread;
  39. /* Enable standard application logging */
  40. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  41. /* Load the SDL library */
  42. if (SDL_Init(0) < 0) {
  43. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  44. return (1);
  45. }
  46. /* Set the error value for the main thread */
  47. SDL_SetError("No worries");
  48. if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
  49. SDL_Log("Not running slower tests");
  50. SDL_Quit();
  51. return 0;
  52. }
  53. alive = 1;
  54. thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
  55. if (thread == NULL) {
  56. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
  57. quit(1);
  58. }
  59. SDL_Delay(5 * 1000);
  60. SDL_Log("Waiting for thread #1\n");
  61. alive = 0;
  62. SDL_WaitThread(thread, NULL);
  63. SDL_Log("Main thread error string: %s\n", SDL_GetError());
  64. SDL_Quit();
  65. return (0);
  66. }