SDL_systhread.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #include "SDL_log.h"
  25. }
  26. #include <mutex>
  27. #include <thread>
  28. #include <system_error>
  29. #ifdef __WINRT__
  30. #include <Windows.h>
  31. #endif
  32. static void
  33. RunThread(void *args)
  34. {
  35. SDL_RunThread(args);
  36. }
  37. extern "C"
  38. int
  39. SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
  40. {
  41. try {
  42. std::thread cpp_thread(RunThread, args);
  43. thread->handle = (void *) new std::thread(std::move(cpp_thread));
  44. return 0;
  45. } catch (std::system_error & ex) {
  46. SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
  47. return -1;
  48. } catch (std::bad_alloc &) {
  49. SDL_OutOfMemory();
  50. return -1;
  51. }
  52. }
  53. extern "C"
  54. void
  55. SDL_SYS_SetupThread(const char *name)
  56. {
  57. // Make sure a thread ID gets assigned ASAP, for debugging purposes:
  58. SDL_ThreadID();
  59. return;
  60. }
  61. extern "C"
  62. SDL_threadID
  63. SDL_ThreadID(void)
  64. {
  65. #ifdef __WINRT__
  66. return GetCurrentThreadId();
  67. #else
  68. // HACK: Mimick a thread ID, if one isn't otherwise available.
  69. static thread_local SDL_threadID current_thread_id = 0;
  70. static SDL_threadID next_thread_id = 1;
  71. static std::mutex next_thread_id_mutex;
  72. if (current_thread_id == 0) {
  73. std::lock_guard<std::mutex> lock(next_thread_id_mutex);
  74. current_thread_id = next_thread_id;
  75. ++next_thread_id;
  76. }
  77. return current_thread_id;
  78. #endif
  79. }
  80. extern "C"
  81. int
  82. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  83. {
  84. // Thread priorities do not look to be settable via C++11's thread
  85. // interface, at least as of this writing (Nov 2012). std::thread does
  86. // provide access to the OS' native handle, however, and some form of
  87. // priority-setting could, in theory, be done through this interface.
  88. //
  89. // WinRT: UPDATE (Aug 20, 2013): thread priorities cannot be changed
  90. // on WinRT, at least not for any thread that's already been created.
  91. // WinRT threads appear to be based off of the WinRT class,
  92. // ThreadPool, more info on which can be found at:
  93. // http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool.aspx
  94. //
  95. // For compatibility sake, 0 will be returned here.
  96. return (0);
  97. }
  98. extern "C"
  99. void
  100. SDL_SYS_WaitThread(SDL_Thread * thread)
  101. {
  102. if ( ! thread) {
  103. return;
  104. }
  105. try {
  106. std::thread * cpp_thread = (std::thread *) thread->handle;
  107. if (cpp_thread->joinable()) {
  108. cpp_thread->join();
  109. }
  110. } catch (std::system_error &) {
  111. // An error occurred when joining the thread. SDL_WaitThread does not,
  112. // however, seem to provide a means to report errors to its callers
  113. // though!
  114. }
  115. }
  116. extern "C"
  117. void
  118. SDL_SYS_DetachThread(SDL_Thread * thread)
  119. {
  120. if ( ! thread) {
  121. return;
  122. }
  123. try {
  124. std::thread * cpp_thread = (std::thread *) thread->handle;
  125. if (cpp_thread->joinable()) {
  126. cpp_thread->detach();
  127. }
  128. } catch (std::system_error &) {
  129. // An error occurred when detaching the thread. SDL_DetachThread does not,
  130. // however, seem to provide a means to report errors to its callers
  131. // though!
  132. }
  133. }
  134. extern "C"
  135. SDL_TLSData *
  136. SDL_SYS_GetTLSData()
  137. {
  138. return SDL_Generic_GetTLSData();
  139. }
  140. extern "C"
  141. int
  142. SDL_SYS_SetTLSData(SDL_TLSData *data)
  143. {
  144. return SDL_Generic_SetTLSData(data);
  145. }
  146. /* vi: set ts=4 sw=4 expandtab: */