ThreadTester.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <Tests/ThreadTester.h>
  10. #include <AzCore/std/parallel/thread.h>
  11. #include <AzCore/std/parallel/atomic.h>
  12. #include <AzCore/std/parallel/conditional_variable.h>
  13. #include <AzCore/std/containers/vector.h>
  14. namespace UnitTest
  15. {
  16. using namespace AZ;
  17. void ThreadTester::Dispatch(size_t threadCountMax, ThreadFunction threadFunction)
  18. {
  19. AZStd::mutex mutex;
  20. AZStd::vector<AZStd::thread> threads;
  21. AZStd::atomic<size_t> threadCount(threadCountMax);
  22. AZStd::condition_variable cv;
  23. for (size_t i = 0; i < threadCountMax; ++i)
  24. {
  25. threads.emplace_back([threadFunction, &threadCount, &cv, i]()
  26. {
  27. threadFunction(i);
  28. threadCount--;
  29. cv.notify_one();
  30. });
  31. }
  32. bool timedOut = false;
  33. // Used to detect a deadlock. The longer we wait for them to finish, the more likely it is
  34. // that we have instead deadlocked.
  35. while (threadCount > 0 && !timedOut)
  36. {
  37. AZStd::unique_lock<AZStd::mutex> lock(mutex);
  38. // This completes in about 2 seconds in profile. It is tempting to wait up to 5 seconds to make sure.
  39. // However, the user
  40. // * Might be running in debug (10x or more slower) - 2s could be as long as 20s
  41. // * Might be running with ASAN enabled or some other deep memory checker (also 2-5x slower) - 20s could end up being 100s
  42. // * Might have a slow machine with fewer actual physical cores for threads (2-5x slower) - 100s could be as long as 500s
  43. // * Might have a busy machine doing updates or other tasks in the background. (Unknown multiplier, could be huge)
  44. // As such, I'm going to wait for 500 seconds instead of just 5.
  45. // If the test passes, it will not actually wait for any longer than the base amount of time anyway
  46. // since this timeout unblocks as soon as all the threads are done anyway, so the only time it waits for the full 500
  47. // seconds is if it truly is deadlocked.
  48. // So if this fails you know with a very very high confidence that it is in fact deadlocked and not just running on a potato.
  49. timedOut = (AZStd::cv_status::timeout == cv.wait_until(lock, AZStd::chrono::steady_clock::now() + AZStd::chrono::seconds(500)));
  50. }
  51. EXPECT_TRUE(threadCount == 0);
  52. for (auto& thread : threads)
  53. {
  54. thread.join();
  55. }
  56. }
  57. }