eathread_cpp11.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EABase/eabase.h>
  5. #include "eathread/eathread.h"
  6. #include "eathread/eathread_thread.h"
  7. #include <cstring>
  8. #include <sstream>
  9. #include <type_traits>
  10. namespace EA
  11. {
  12. namespace Thread
  13. {
  14. EA::Thread::AssertionFailureFunction gpAssertionFailureFunction = NULL;
  15. void* gpAssertionFailureContext = NULL;
  16. EATHREADLIB_API ThreadId EA::Thread::GetThreadId()
  17. {
  18. return std::this_thread::get_id();
  19. }
  20. EATHREADLIB_API ThreadId EA::Thread::GetThreadId(EA::Thread::SysThreadId id)
  21. {
  22. EAThreadDynamicData* const pTDD = EA::Thread::FindThreadDynamicData(id);
  23. if(pTDD)
  24. {
  25. return pTDD->mpComp->mThread.get_id();
  26. }
  27. return EA::Thread::kThreadIdInvalid;
  28. }
  29. EATHREADLIB_API SysThreadId EA::Thread::GetSysThreadId(ThreadId threadId)
  30. {
  31. EAThreadDynamicData* tdd = EA::Thread::FindThreadDynamicData(threadId);
  32. if (tdd && tdd->mpComp)
  33. return tdd->mpComp->mThread.native_handle();
  34. ThreadId threadIdCurrent = GetThreadId();
  35. if(threadId == threadIdCurrent)
  36. {
  37. #if defined(EA_PLATFORM_MICROSOFT)
  38. std::thread::id stdId = std::this_thread::get_id();
  39. EAT_COMPILETIME_ASSERT(sizeof(_Thrd_t) == sizeof(std::thread::id));
  40. return ((_Thrd_t&)stdId)._Hnd;
  41. #elif EA_POSIX_THREADS_AVAILABLE && defined(_YVALS)
  42. std::thread::id stdId = std::this_thread::get_id();
  43. EAT_COMPILETIME_ASSERT(sizeof(_Thrd_t) == sizeof(std::thread::id));
  44. return reinterpret_cast<_Thrd_t>(stdId);
  45. #else
  46. #error Platform not supported yet.
  47. #endif
  48. }
  49. EAT_ASSERT_MSG(false, "Failed to find associated EAThreadDynamicData for this thread.\n");
  50. return SysThreadId();
  51. }
  52. EATHREADLIB_API SysThreadId EA::Thread::GetSysThreadId()
  53. {
  54. // There currently isn't a means to directly get the current SysThreadId, so we do it indirectly:
  55. return GetSysThreadId(std::this_thread::get_id());
  56. }
  57. EATHREADLIB_API ThreadTime EA::Thread::GetThreadTime()
  58. {
  59. using namespace std::chrono;
  60. auto nowMs = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
  61. return nowMs.count();
  62. }
  63. EATHREADLIB_API int GetThreadPriority()
  64. {
  65. // No way to query or set thread priority through standard C++11 thread library.
  66. // On some platforms this could be implemented through platform specific APIs
  67. return kThreadPriorityDefault;
  68. }
  69. EATHREADLIB_API bool SetThreadPriority(int nPriority)
  70. {
  71. // No way to query or set thread priority through standard C++11 thread library.
  72. // On some platforms this could be implemented through platform specific APIs
  73. return false;
  74. }
  75. EATHREADLIB_API void SetThreadProcessor(int nProcessor)
  76. {
  77. // No way to query or set thread processor through standard C++11 thread library.
  78. // On some platforms this could be implemented through platform specific APIs
  79. }
  80. EATHREADLIB_API int GetThreadProcessor()
  81. {
  82. // No way to query or set thread processor through standard C++11 thread library.
  83. // On some platforms this could be implemented through platform specific APIs
  84. return 0;
  85. }
  86. EATHREADLIB_API int GetProcessorCount()
  87. {
  88. return static_cast<int>(std::thread::hardware_concurrency());
  89. }
  90. EATHREADLIB_API void ThreadSleep(const ThreadTime& timeRelative)
  91. {
  92. std::this_thread::sleep_for(std::chrono::milliseconds(timeRelative));
  93. }
  94. void ThreadEnd(intptr_t threadReturnValue)
  95. {
  96. // No way to end a thread through standard C++11 thread library.
  97. // On some platforms this could be implemented through platform specific APIs
  98. EAT_ASSERT_MSG(false, "ThreadEnd is not implemented for C++11 threads.\n");
  99. }
  100. EATHREADLIB_API void EA::Thread::SetThreadAffinityMask(const EA::Thread::ThreadId& id, ThreadAffinityMask nAffinityMask)
  101. {
  102. // Update the affinity mask in the thread dynamic data cache.
  103. EAThreadDynamicData* const pTDD = FindThreadDynamicData(id);
  104. if(pTDD)
  105. {
  106. pTDD->mnThreadAffinityMask = nAffinityMask;
  107. }
  108. #if EATHREAD_THREAD_AFFINITY_MASK_SUPPORTED
  109. // Call the Windows library function.
  110. #endif
  111. }
  112. EATHREADLIB_API EA::Thread::ThreadAffinityMask EA::Thread::GetThreadAffinityMask(const EA::Thread::ThreadId& id)
  113. {
  114. // Update the affinity mask in the thread dynamic data cache.
  115. EAThreadDynamicData* const pTDD = FindThreadDynamicData(id);
  116. if(pTDD)
  117. {
  118. return pTDD->mnThreadAffinityMask;
  119. }
  120. return kThreadAffinityMaskAny;
  121. }
  122. EATHREADLIB_API void SetAssertionFailureFunction(AssertionFailureFunction pAssertionFailureFunction, void* pContext)
  123. {
  124. gpAssertionFailureFunction = pAssertionFailureFunction;
  125. gpAssertionFailureContext = pContext;
  126. }
  127. EATHREADLIB_API void AssertionFailure(const char* pExpression)
  128. {
  129. if(gpAssertionFailureFunction)
  130. gpAssertionFailureFunction(pExpression, gpAssertionFailureContext);
  131. }
  132. void* GetThreadStackBase()
  133. {
  134. return nullptr;
  135. }
  136. // This can be removed once all remaining synchronization primitives are implemented in terms of C++11 APIs
  137. uint32_t EA::Thread::RelativeTimeoutFromAbsoluteTimeout(ThreadTime timeoutAbsolute)
  138. {
  139. EAT_ASSERT((timeoutAbsolute == kTimeoutImmediate) || (timeoutAbsolute > EATHREAD_MIN_ABSOLUTE_TIME)); // Assert that the user didn't make the mistake of treating time as relative instead of absolute.
  140. DWORD timeoutRelative = 0;
  141. if (timeoutAbsolute == kTimeoutNone)
  142. {
  143. timeoutRelative = 0xffffffff;
  144. }
  145. else if (timeoutAbsolute == kTimeoutImmediate)
  146. {
  147. timeoutRelative = 0;
  148. }
  149. else
  150. {
  151. ThreadTime timeCurrent(GetThreadTime());
  152. timeoutRelative = (timeoutAbsolute > timeCurrent) ? static_cast<DWORD>(timeoutAbsolute - timeCurrent) : 0;
  153. }
  154. EAT_ASSERT((timeoutRelative == 0xffffffff) || (timeoutRelative < 100000000)); // Assert that the timeout is a sane value and didn't wrap around.
  155. return timeoutRelative;
  156. }
  157. // Implement native_handle_type comparison as a memcmp() - may need platform specific implementations on some future platforms.
  158. bool Equals(const SysThreadId& a, const SysThreadId& b)
  159. {
  160. static_assert((std::is_fundamental<SysThreadId>::value || std::is_pointer<SysThreadId>::value || std::is_pod<SysThreadId>::value),
  161. "SysThreadId should be comparable using memcmp()");
  162. return memcmp(&a, &b, sizeof(SysThreadId)) == 0;
  163. }
  164. namespace detail
  165. {
  166. // Override the default EAThreadToString implementation
  167. #define EAThreadIdToString_CUSTOM_IMPLEMENTATION
  168. ThreadIdToStringBuffer::ThreadIdToStringBuffer(EA::Thread::ThreadId threadId)
  169. {
  170. std::stringstream formatStream;
  171. formatStream << threadId;
  172. strncpy(mBuf, formatStream.str().c_str(), BufSize - 1);
  173. mBuf[BufSize - 1] = '\0';
  174. }
  175. SysThreadIdToStringBuffer::SysThreadIdToStringBuffer(EA::Thread::SysThreadId sysThreadId)
  176. {
  177. strncpy(mBuf, "Unknown", BufSize - 1);
  178. mBuf[BufSize - 1] = '\0';
  179. }
  180. }
  181. }
  182. }