TestThreadInterprocess.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include "TestThreadInterprocess.h"
  5. #include <EATest/EATest.h>
  6. #include <EAMain/EAMain.h>
  7. #include <eathread/eathread.h>
  8. #include <EAStdC/EAString.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <EAMain/EAEntryPointMain.inl>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // Globals
  15. //
  16. unsigned int gTestThreadCount = 4;
  17. unsigned int gTestLengthSeconds = 10;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // EAThreadFailure
  20. //
  21. // This is called by EAThread's assert failure function.
  22. //
  23. static void EAThreadFailure(const char* pMessage, void* /*pContext*/)
  24. {
  25. EA::UnitTest::Report("Thread test failure (EAThread assert): %s\n", pMessage);
  26. }
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // operator new
  29. // EASTL requires the following new operators to be defined.
  30. //
  31. void* operator new[](size_t size, const char*, int, unsigned, const char*, int)
  32. {
  33. return new char[size];
  34. }
  35. void* operator new[](size_t size, size_t, size_t, const char*, int, unsigned, const char*, int)
  36. {
  37. return new char[size];
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // main
  41. //
  42. int EAMain(int argc, char** argv)
  43. {
  44. using namespace EA::Thread;
  45. using namespace EA::UnitTest;
  46. using namespace EA::StdC;
  47. int nErrorCount = 0;
  48. bool bDebugMode = false;
  49. EA::EAMain::PlatformStartup();
  50. // Process possible command line parameters
  51. for(int i(0); i < argc; i++)
  52. {
  53. // Look for -?
  54. if(Stricmp(argv[i], "-?") == 0)
  55. {
  56. printf("Command line parameters:\n");
  57. printf(" -? Get Help.\n");
  58. printf(" -t <seconds> Run the tests for <seconds> seconds each.\n");
  59. printf(" -c <count> Specifies test thread count.\n");
  60. printf(" -d Debug mode. Causes app to wait for a debugger to connect.\n");
  61. continue;
  62. }
  63. // Run the tests for <seconds> seconds each.
  64. if((Stricmp(argv[i], "-t") == 0) && (i < (argc - 1)))
  65. {
  66. gTestLengthSeconds = (unsigned int) atoi(argv[i+1]);
  67. if(gTestLengthSeconds < 3)
  68. gTestLengthSeconds = 3;
  69. continue;
  70. }
  71. // Specifies test thread count. e.g. -c 10
  72. if((Stricmp(argv[i], "-c") == 0) && (i < (argc - 1)))
  73. {
  74. gTestThreadCount = (unsigned int) atoi(argv[i+1]);
  75. if(gTestThreadCount < 1)
  76. gTestThreadCount = 1;
  77. if(gTestThreadCount > 100)
  78. gTestThreadCount = 100;
  79. continue;
  80. }
  81. // Debug mode. Causes app to wait for a debugger to connect.
  82. if(Stricmp(argv[i], "-d") == 0)
  83. {
  84. bDebugMode = true;
  85. continue;
  86. }
  87. }
  88. // Set EAThread to route its errors to our own error reporting function.
  89. EA::Thread::SetAssertionFailureFunction(EAThreadFailure, NULL);
  90. ReportVerbosity(1, "Test time seconds: %u\n", gTestLengthSeconds);
  91. ReportVerbosity(1, "Thread count: %u\n", gTestThreadCount);
  92. // Print ThreadId for this primary thread.
  93. const ThreadId threadId = GetThreadId();
  94. ReportVerbosity(1, "Primary thread ThreadId: %08x\n", (int)(intptr_t)threadId);
  95. // Print SysThreadId for this primary thread.
  96. const SysThreadId sysThreadId = GetSysThreadId(threadId);
  97. ReportVerbosity(1, "Primary thread SysThreadId: %08d\n", (int)(intptr_t)sysThreadId);
  98. // Print thread priority for this primary thread.
  99. const int nPriority = EA::Thread::GetThreadPriority();
  100. ReportVerbosity(1, "Primary thread priority: %d\n", nPriority);
  101. const int nProcessorCount = EA::Thread::GetProcessorCount();
  102. ReportVerbosity(1, "Currently active virtual processor count: %d\n", nProcessorCount);
  103. if(bDebugMode)
  104. {
  105. Report("Debug mode activated. Waiting for debugger to attach.\n");
  106. while(bDebugMode)
  107. ThreadSleepRandom(500, 500, true);
  108. Report("Continuing.\n");
  109. }
  110. // Add the tests
  111. TestApplication testSuite("EAThread Interprocess Unit Tests", argc, argv);
  112. testSuite.AddTest("RWMutex", TestThreadRWMutex);
  113. nErrorCount += testSuite.Run();
  114. EA::EAMain::PlatformShutdown(nErrorCount);
  115. return nErrorCount;
  116. }