TestThreadThreadPool.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include "TestThread.h"
  5. #include <EATest/EATest.h>
  6. #include <eathread/eathread_pool.h>
  7. #include <eathread/eathread_atomic.h>
  8. #include <stdlib.h>
  9. using namespace EA::Thread;
  10. const int kMaxConcurrentThreadCount = EATHREAD_MAX_CONCURRENT_THREAD_COUNT;
  11. struct TPWorkData
  12. {
  13. int mnWorkItem;
  14. TPWorkData(int nWorkItem) : mnWorkItem(nWorkItem) {}
  15. };
  16. static AtomicInt32 gWorkItemsCreated = 0;
  17. static AtomicInt32 gWorkItemsProcessed = 0;
  18. static intptr_t WorkerFunction(void* pvWorkData)
  19. {
  20. TPWorkData* pWorkData = (TPWorkData*)pvWorkData;
  21. ThreadId threadId = GetThreadId();
  22. EA::UnitTest::ReportVerbosity(1, "Work %4d starting for thread %s.\n", pWorkData->mnWorkItem, EAThreadThreadIdToString(threadId));
  23. EA::UnitTest::ThreadSleepRandom(200, 600);
  24. ++gWorkItemsProcessed;
  25. EA::UnitTest::ReportVerbosity(1, "Work %4d ending for thread %s.\n", pWorkData->mnWorkItem, EAThreadThreadIdToString(threadId));
  26. delete pWorkData;
  27. return 0;
  28. }
  29. int TestThreadThreadPool()
  30. {
  31. int nErrorCount(0);
  32. #if EA_THREADS_AVAILABLE
  33. {
  34. ThreadPoolParameters tpp;
  35. tpp.mnMinCount = kMaxConcurrentThreadCount - 1;
  36. tpp.mnMaxCount = kMaxConcurrentThreadCount - 1;
  37. tpp.mnInitialCount = 0;
  38. tpp.mnIdleTimeoutMilliseconds = EA::Thread::kTimeoutNone; // left in to test the usage of this kTimeout* defines.
  39. tpp.mnIdleTimeoutMilliseconds = 20000;
  40. ThreadPool threadPool(&tpp);
  41. int nResult;
  42. for(unsigned int i = 0; i < gTestLengthSeconds * 3; i++)
  43. {
  44. const int nWorkItem = (int)gWorkItemsCreated++;
  45. TPWorkData* const pWorkData = new TPWorkData(nWorkItem);
  46. EA::UnitTest::ReportVerbosity(1, "Work %4d created.\n", nWorkItem);
  47. nResult = threadPool.Begin(WorkerFunction, pWorkData, NULL, true);
  48. EATEST_VERIFY_MSG(nResult != ThreadPool::kResultError, "Thread pool failure in Begin.");
  49. EA::UnitTest::ThreadSleepRandom(300, 700);
  50. //Todo: If the pool task length gets too long, wait some more.
  51. }
  52. EA::UnitTest::ReportVerbosity(1, "Shutting down thread pool.\n");
  53. bool bShutdownResult = threadPool.Shutdown(ThreadPool::kJobWaitAll, GetThreadTime() + 60000);
  54. EATEST_VERIFY_MSG(bShutdownResult, "Thread pool failure in Shutdown (waiting for jobs to complete).");
  55. }
  56. EATEST_VERIFY_MSG(gWorkItemsCreated == gWorkItemsProcessed, "Thread pool failure: gWorkItemsCreated != gWorkItemsProcessed.");
  57. #endif
  58. return nErrorCount;
  59. }