diff --git a/unittests/VMRuntime/SamplingProfilerTest.cpp b/unittests/VMRuntime/SamplingProfilerTest.cpp index a2e63c1898f..0a73fbed2c3 100644 --- a/unittests/VMRuntime/SamplingProfilerTest.cpp +++ b/unittests/VMRuntime/SamplingProfilerTest.cpp @@ -65,78 +65,41 @@ TEST(SamplingProfilerTest, MultipleProfilers) { #endif TEST(SamplingProfilerTest, MultipleThreads) { - constexpr uint32_t kThreadCount = 3; - constexpr uint32_t kMaxEvals = 100; + constexpr uint32_t kThreadCount = 10; auto rt = makeRuntime(withSamplingProfilerEnabled); rt->samplingProfiler->enable(); // Take turns evaluating JavaScript on a different thread each time. - std::mutex mutex; - std::condition_variable condVar; - std::atomic evalCount(0); - std::atomic shutdown(false); - std::array threads; - auto evaluateLoop = [&](uint32_t threadNumber) { - while (true) { - { - std::unique_lock lock(mutex); - condVar.wait(lock, [&] { - bool myTurn = (evalCount < kMaxEvals) && - (evalCount % kThreadCount == threadNumber); - return myTurn || shutdown; - }); - } - - if (shutdown) { - break; - } - - rt->samplingProfiler->setRuntimeThread(); - auto bytecode = hermes::bytecodeForSource(R"( - (function count() { - let x = 0; - function inc() { - x++; - return (x < 100000); - } - while(inc()){} - })(); - )"); - std::shared_ptr bcProvider = - hermes::hbc::BCProviderFromBuffer::createBCProviderFromBuffer( - std::make_unique( - bytecode.data(), bytecode.size())) - .first; - RuntimeModuleFlags runtimeModuleFlags; - runtimeModuleFlags.persistent = false; - auto result = rt->runBytecode( - std::move(bcProvider), - runtimeModuleFlags, - "test.js.hbc", - Runtime::makeNullHandle()); - EXPECT_NE(result.getStatus(), ExecutionStatus::EXCEPTION); - - evalCount++; - condVar.notify_all(); - } + auto evaluate = [&]() { + rt->samplingProfiler->setRuntimeThread(); + auto bytecode = hermes::bytecodeForSource(R"( + (function count() { + let x = 0; + function inc() { + x++; + return (x < 100000); + } + while(inc()){} + })(); + )"); + std::shared_ptr bcProvider = + hermes::hbc::BCProviderFromBuffer::createBCProviderFromBuffer( + std::make_unique(bytecode.data(), bytecode.size())) + .first; + RuntimeModuleFlags runtimeModuleFlags; + runtimeModuleFlags.persistent = false; + auto result = rt->runBytecode( + std::move(bcProvider), + runtimeModuleFlags, + "test.js.hbc", + Runtime::makeNullHandle()); + EXPECT_NE(result.getStatus(), ExecutionStatus::EXCEPTION); }; for (uint32_t threadNumber = 0; threadNumber < kThreadCount; ++threadNumber) { - threads[threadNumber] = std::thread(evaluateLoop, threadNumber); + std::thread(evaluate).join(); } - // Wait for all evaluations to complete - { - std::unique_lock lock(mutex); - condVar.wait(lock, [&] { return evalCount == kMaxEvals; }); - } - - // Terminate threads. - shutdown = true; - condVar.notify_all(); - for (std::thread &thread : threads) { - thread.join(); - } rt->samplingProfiler->disable(); }