SDL_systhread.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. /* Thread management routines for SDL */
  20. extern "C" {
  21. #include "../SDL_thread_c.h"
  22. #include "../SDL_systhread.h"
  23. }
  24. #include <mutex>
  25. #include <thread>
  26. #include <system_error>
  27. #ifdef SDL_PLATFORM_WINRT
  28. #include <Windows.h>
  29. #endif
  30. static void RunThread(void *args)
  31. {
  32. SDL_RunThread((SDL_Thread *)args);
  33. }
  34. extern "C" int
  35. SDL_SYS_CreateThread(SDL_Thread *thread)
  36. {
  37. try {
  38. // !!! FIXME: no way to set a thread stack size here.
  39. std::thread cpp_thread(RunThread, thread);
  40. thread->handle = (void *)new std::thread(std::move(cpp_thread));
  41. return 0;
  42. } catch (std::system_error &ex) {
  43. return SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
  44. } catch (std::bad_alloc &) {
  45. return SDL_OutOfMemory();
  46. }
  47. }
  48. extern "C" void
  49. SDL_SYS_SetupThread(const char *name)
  50. {
  51. // Make sure a thread ID gets assigned ASAP, for debugging purposes:
  52. SDL_GetCurrentThreadID();
  53. return;
  54. }
  55. extern "C" SDL_ThreadID
  56. SDL_GetCurrentThreadID(void)
  57. {
  58. #ifdef SDL_PLATFORM_WINRT
  59. return GetCurrentThreadId();
  60. #else
  61. // HACK: Mimic a thread ID, if one isn't otherwise available.
  62. static thread_local SDL_ThreadID current_thread_id = 0;
  63. static SDL_ThreadID next_thread_id = 1;
  64. static std::mutex next_thread_id_mutex;
  65. if (current_thread_id == 0) {
  66. std::lock_guard<std::mutex> lock(next_thread_id_mutex);
  67. current_thread_id = next_thread_id;
  68. ++next_thread_id;
  69. }
  70. return current_thread_id;
  71. #endif
  72. }
  73. extern "C" int
  74. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  75. {
  76. #ifdef SDL_PLATFORM_WINRT
  77. int value;
  78. if (priority == SDL_THREAD_PRIORITY_LOW) {
  79. value = THREAD_PRIORITY_LOWEST;
  80. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  81. value = THREAD_PRIORITY_HIGHEST;
  82. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  83. // FIXME: WinRT does not support TIME_CRITICAL! -flibit
  84. SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "TIME_CRITICAL unsupported, falling back to HIGHEST");
  85. value = THREAD_PRIORITY_HIGHEST;
  86. } else {
  87. value = THREAD_PRIORITY_NORMAL;
  88. }
  89. if (!SetThreadPriority(GetCurrentThread(), value)) {
  90. return WIN_SetError("SetThreadPriority()");
  91. }
  92. return 0;
  93. #else
  94. return SDL_Unsupported();
  95. #endif
  96. }
  97. extern "C" void
  98. SDL_SYS_WaitThread(SDL_Thread *thread)
  99. {
  100. if (!thread) {
  101. return;
  102. }
  103. try {
  104. std::thread *cpp_thread = (std::thread *)thread->handle;
  105. if (cpp_thread->joinable()) {
  106. cpp_thread->join();
  107. }
  108. } catch (std::system_error &) {
  109. // An error occurred when joining the thread. SDL_WaitThread does not,
  110. // however, seem to provide a means to report errors to its callers
  111. // though!
  112. }
  113. }
  114. extern "C" void
  115. SDL_SYS_DetachThread(SDL_Thread *thread)
  116. {
  117. if (!thread) {
  118. return;
  119. }
  120. try {
  121. std::thread *cpp_thread = (std::thread *)thread->handle;
  122. if (cpp_thread->joinable()) {
  123. cpp_thread->detach();
  124. }
  125. } catch (std::system_error &) {
  126. // An error occurred when detaching the thread. SDL_DetachThread does not,
  127. // however, seem to provide a means to report errors to its callers
  128. // though!
  129. }
  130. }
  131. extern "C" SDL_TLSData *
  132. SDL_SYS_GetTLSData(void)
  133. {
  134. return SDL_Generic_GetTLSData();
  135. }
  136. extern "C" int
  137. SDL_SYS_SetTLSData(SDL_TLSData *data)
  138. {
  139. return SDL_Generic_SetTLSData(data);
  140. }