SDL_systhread.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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.h"
  22. #include "../SDL_thread_c.h"
  23. #include "../SDL_systhread.h"
  24. }
  25. #include <mutex>
  26. #include <thread>
  27. #include <system_error>
  28. #ifdef __WINRT__
  29. #include <Windows.h>
  30. #endif
  31. static void RunThread(void *args)
  32. {
  33. SDL_RunThread((SDL_Thread *)args);
  34. }
  35. extern "C" int
  36. SDL_SYS_CreateThread(SDL_Thread *thread)
  37. {
  38. try {
  39. // !!! FIXME: no way to set a thread stack size here.
  40. std::thread cpp_thread(RunThread, thread);
  41. thread->handle = (void *)new std::thread(std::move(cpp_thread));
  42. return 0;
  43. } catch (std::system_error &ex) {
  44. return SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
  45. } catch (std::bad_alloc &) {
  46. return SDL_OutOfMemory();
  47. }
  48. }
  49. extern "C" void
  50. SDL_SYS_SetupThread(const char *name)
  51. {
  52. // Make sure a thread ID gets assigned ASAP, for debugging purposes:
  53. SDL_ThreadID();
  54. return;
  55. }
  56. extern "C" SDL_threadID
  57. SDL_ThreadID(void)
  58. {
  59. #ifdef __WINRT__
  60. return GetCurrentThreadId();
  61. #else
  62. // HACK: Mimick a thread ID, if one isn't otherwise available.
  63. static thread_local SDL_threadID current_thread_id = 0;
  64. static SDL_threadID next_thread_id = 1;
  65. static std::mutex next_thread_id_mutex;
  66. if (current_thread_id == 0) {
  67. std::lock_guard<std::mutex> lock(next_thread_id_mutex);
  68. current_thread_id = next_thread_id;
  69. ++next_thread_id;
  70. }
  71. return current_thread_id;
  72. #endif
  73. }
  74. extern "C" int
  75. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  76. {
  77. #ifdef __WINRT__
  78. int value;
  79. if (priority == SDL_THREAD_PRIORITY_LOW) {
  80. value = THREAD_PRIORITY_LOWEST;
  81. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  82. value = THREAD_PRIORITY_HIGHEST;
  83. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  84. // FIXME: WinRT does not support TIME_CRITICAL! -flibit
  85. SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "TIME_CRITICAL unsupported, falling back to HIGHEST");
  86. value = THREAD_PRIORITY_HIGHEST;
  87. } else {
  88. value = THREAD_PRIORITY_NORMAL;
  89. }
  90. if (!SetThreadPriority(GetCurrentThread(), value)) {
  91. return WIN_SetError("SetThreadPriority()");
  92. }
  93. return 0;
  94. #else
  95. return SDL_Unsupported();
  96. #endif
  97. }
  98. extern "C" void
  99. SDL_SYS_WaitThread(SDL_Thread *thread)
  100. {
  101. if (!thread) {
  102. return;
  103. }
  104. try {
  105. std::thread *cpp_thread = (std::thread *)thread->handle;
  106. if (cpp_thread->joinable()) {
  107. cpp_thread->join();
  108. }
  109. } catch (std::system_error &) {
  110. // An error occurred when joining the thread. SDL_WaitThread does not,
  111. // however, seem to provide a means to report errors to its callers
  112. // though!
  113. }
  114. }
  115. extern "C" void
  116. SDL_SYS_DetachThread(SDL_Thread *thread)
  117. {
  118. if (!thread) {
  119. return;
  120. }
  121. try {
  122. std::thread *cpp_thread = (std::thread *)thread->handle;
  123. if (cpp_thread->joinable()) {
  124. cpp_thread->detach();
  125. }
  126. } catch (std::system_error &) {
  127. // An error occurred when detaching the thread. SDL_DetachThread does not,
  128. // however, seem to provide a means to report errors to its callers
  129. // though!
  130. }
  131. }
  132. extern "C" SDL_TLSData *
  133. SDL_SYS_GetTLSData(void)
  134. {
  135. return SDL_Generic_GetTLSData();
  136. }
  137. extern "C" int
  138. SDL_SYS_SetTLSData(SDL_TLSData *data)
  139. {
  140. return SDL_Generic_SetTLSData(data);
  141. }
  142. /* vi: set ts=4 sw=4 expandtab: */