TestThread.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include "TestThread.h"
  5. #include <eathread/eathread.h>
  6. #include <eathread/eathread_callstack.h>
  7. #include <EATest/EATest.h>
  8. #include <EAStdC/EAString.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. //Prevents false positive memory leaks on GCC/Clang platforms
  13. #if defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)
  14. #define EA_MEMORY_GCC_USE_FINALIZE
  15. #endif
  16. #include <MemoryMan/MemoryMan.inl>
  17. #include <MemoryMan/CoreAllocator.inl>
  18. #include <coreallocator/icoreallocator_interface.h>
  19. #ifdef EA_DLL
  20. #include <MemoryMan/MemoryManDLL.h>
  21. #endif
  22. #if defined(EA_PLATFORM_MICROSOFT)
  23. EA_DISABLE_ALL_VC_WARNINGS()
  24. #include <Windows.h>
  25. EA_RESTORE_ALL_VC_WARNINGS()
  26. #endif
  27. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  28. EA_DISABLE_ALL_VC_WARNINGS()
  29. #include <crtdbg.h>
  30. EA_RESTORE_ALL_VC_WARNINGS()
  31. #endif
  32. #include <EAMain/EAEntryPointMain.inl>
  33. #include <EATest/EASTLVsnprintf.inl>
  34. #include <EAMain/EAEntryPointMain.inl>
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // gTestLengthSeconds
  37. //
  38. unsigned int gTestLengthSeconds = 2;
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // EAThreadFailure
  41. //
  42. // This is called by EAThread's assert failure function.
  43. //
  44. static void EAThreadFailure(const char* pMessage, void* /*pContext*/)
  45. {
  46. EA::UnitTest::IncrementGlobalErrorCount(1);
  47. EA::UnitTest::Report("Thread test failure (EAThread assert): %s\n", pMessage);
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // TestThreadMisc
  51. //
  52. // To do: Move this to its own file.
  53. //
  54. #if defined(EA_PLATFORM_APPLE)
  55. #include <eathread/apple/eathread_callstack_apple.h>
  56. #endif
  57. #if defined(EA_PLATFORM_POSIX)
  58. #include <unistd.h>
  59. #endif
  60. bool IsSuperUser()
  61. {
  62. #if defined(EA_PLATFORM_POSIX) && !defined(EA_PLATFORM_SONY) && !defined(CS_UNDEFINED_STRING) // PS4 is a POSIX machine but doesn't implement 'getuid'.
  63. // http://pubs.opengroup.org/onlinepubs/009695399/functions/geteuid.html
  64. // http://pubs.opengroup.org/onlinepubs/009695399/functions/getuid.html
  65. uid_t uid = getuid(), euid = geteuid();
  66. return (uid == 0 || euid == 0);
  67. #else
  68. return true;
  69. #endif
  70. }
  71. int TestThreadGetThreadTimeMin()
  72. {
  73. int nErrorCount(0);
  74. #if defined(EA_PLATFORM_MICROSOFT) || defined(EA_PLATFORM_KETTLE)
  75. EATEST_VERIFY_MSG(EA::Thread::GetThreadTime() >= EATHREAD_MIN_ABSOLUTE_TIME, "Reported GetThreadTime absolute time is less than EATHREAD_MIN_ABSOLUTE_TIME. You are going to have a bad time.");
  76. #endif
  77. return nErrorCount;
  78. }
  79. int TestThreadMisc()
  80. {
  81. int nErrorCount = 0;
  82. // this is here because its intended to be the first test run. Since it depends on tick count since title start.
  83. nErrorCount += TestThreadGetThreadTimeMin();
  84. #if defined(EA_PLATFORM_APPLE) && EATHREAD_APPLE_GETMODULEINFO_ENABLED
  85. if(!IsSuperUser())
  86. {
  87. EA::EAMain::Report("Skipping GetModuleInfoApple test because we don't have sufficient system privileges.\n");
  88. return nErrorCount;
  89. }
  90. EA::Thread::ModuleInfoApple moduleInfoApple[15];
  91. size_t n = EA::Thread::GetModuleInfoApple(moduleInfoApple, EAArrayCount(moduleInfoApple), NULL, true);
  92. #if defined(EA_PLATFORM_OSX)
  93. EATEST_VERIFY(n > 0);
  94. for(size_t i = 0; i < eastl::min(n, EAArrayCount(moduleInfoApple)); i++)
  95. EA::UnitTest::Report("%s\n", moduleInfoApple[i].mPath);
  96. #else
  97. EA_UNUSED(n);
  98. #endif
  99. #endif
  100. return nErrorCount;
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////
  103. // EAMain
  104. //
  105. int EAMain(int argc, char** argv)
  106. {
  107. using namespace EA::Thread;
  108. using namespace EA::UnitTest;
  109. int nErrorCount(0);
  110. EA::EAMain::PlatformStartup();
  111. gTestLengthSeconds = (unsigned int)(3 * EA::UnitTest::GetSystemSpeed());
  112. if(gTestLengthSeconds == 0)
  113. gTestLengthSeconds = 1;
  114. // Set EAThread to route its errors to our own error reporting function.
  115. EA::Thread::SetAssertionFailureFunction(EAThreadFailure, NULL);
  116. #if defined(EA_DLL) && defined(EA_MEMORY_ENABLED) && EA_MEMORY_ENABLED
  117. EA::Allocator::InitSharedDllAllocator();
  118. #endif
  119. //Set EAThread to use the Default Allocator to keep track of our memory usage
  120. EA::Thread::SetAllocator(EA::Allocator::ICoreAllocator::GetDefaultAllocator());
  121. EA::Thread::InitCallstack();
  122. // Print ThreadId for this primary thread.
  123. const ThreadId threadId = GetThreadId();
  124. ReportVerbosity(1, "Primary thread ThreadId: %s\n", EAThreadThreadIdToString(threadId));
  125. // Print SysThreadId for this primary thread.
  126. const SysThreadId sysThreadId = GetSysThreadId(threadId);
  127. ReportVerbosity(1, "Primary thread SysThreadId: %s\n", EAThreadSysThreadIdToString(sysThreadId));
  128. // Print thread priority for this primary thread.
  129. const int nPriority = EA::Thread::GetThreadPriority();
  130. ReportVerbosity(1, "Primary thread priority: %d\n", nPriority);
  131. const int nProcessorCount = EA::Thread::GetProcessorCount();
  132. ReportVerbosity(1, "Currently active virtual processor count: %d\n", nProcessorCount);
  133. #if defined(EA_PLATFORM_MICROSOFT) && !EA_POSIX_THREADS_AVAILABLE && !EA_USE_CPP11_CONCURRENCY
  134. const DWORD dwCurrentThreadId = GetCurrentThreadId(); // This is a system OS call.
  135. EATEST_VERIFY_F(sysThreadId == dwCurrentThreadId, "GetSysThreadId failed. SysThreadId = %u, sys thread id = %u.\n", (unsigned)sysThreadId, (unsigned)dwCurrentThreadId);
  136. #endif
  137. // Add the tests
  138. TestApplication testSuite("EAThread Unit Tests", argc, argv);
  139. testSuite.AddTest("Atomic", TestThreadAtomic);
  140. testSuite.AddTest("Barrier", TestThreadBarrier);
  141. testSuite.AddTest("Callstack", TestThreadCallstack);
  142. testSuite.AddTest("Condition", TestThreadCondition);
  143. testSuite.AddTest("EnumerateThreads", TestEnumerateThreads);
  144. testSuite.AddTest("Futex", TestThreadFutex);
  145. testSuite.AddTest("Misc", TestThreadMisc);
  146. testSuite.AddTest("Mutex", TestThreadMutex);
  147. testSuite.AddTest("RWMutex", TestThreadRWMutex);
  148. testSuite.AddTest("RWSemaphore", TestThreadRWSemaLock);
  149. testSuite.AddTest("RWSpinLock", TestThreadRWSpinLock);
  150. testSuite.AddTest("Semaphore", TestThreadSemaphore);
  151. testSuite.AddTest("SmartPtr", TestThreadSmartPtr);
  152. testSuite.AddTest("SpinLock", TestThreadSpinLock);
  153. testSuite.AddTest("Storage", TestThreadStorage);
  154. testSuite.AddTest("Sync", TestThreadSync);
  155. testSuite.AddTest("Thread", TestThreadThread);
  156. testSuite.AddTest("ThreadPool", TestThreadThreadPool);
  157. nErrorCount += testSuite.Run();
  158. #ifndef EA_USE_CPP11_CONCURRENCY
  159. // Verify the converted a EA::Thread::ThreadId to a EA::Thread::SysThreadId id matches this thread context id.
  160. const ThreadId convertedThreadId = GetThreadId(sysThreadId);
  161. EATEST_VERIFY_F(threadId == convertedThreadId , "GetThreadId failed to convert SysThreadId. ThreadId = %s, converted thread id = %s.\n", EAThreadThreadIdToString(threadId), EAThreadThreadIdToString(convertedThreadId));
  162. #endif
  163. EA::Thread::ShutdownCallstack();
  164. EA::EAMain::PlatformShutdown(nErrorCount);
  165. return nErrorCount;
  166. }