class BarrierTest : public CommonRuntimeTest { public:
BarrierTest() {
use_boot_image_ = true; // Make the Runtime creation cheaper.
}
static int32_t num_threads;
};
int32_t BarrierTest::num_threads = 4;
// Check that barrier wait and barrier increment work.
TEST_F(BarrierTest, CheckWait) {
Thread* self = Thread::Current();
std::unique_ptr<ThreadPool> thread_pool(
ThreadPool::Create("Barrier test thread pool", num_threads));
Barrier barrier(num_threads + 1); // One extra Wait() in main thread.
Barrier timeout_barrier(0); // Only used for sleeping on timeout.
AtomicInteger count1(0);
AtomicInteger count2(0); for (int32_t i = 0; i < num_threads; ++i) {
thread_pool->AddTask(self, new CheckWaitTask(&barrier, &count1, &count2));
}
thread_pool->StartWorkers(self); while (count1.load(std::memory_order_relaxed) != num_threads) {
timeout_barrier.Increment(self, 1, 100); // sleep 100 msecs
} // Count 2 should still be zero since no thread should have gone past the barrier.
EXPECT_EQ(0, count2.load(std::memory_order_relaxed)); // Perform one additional Wait(), allowing pool threads to proceed.
barrier.Wait(self); // Wait for all the threads to finish.
thread_pool->Wait(self, true, false); // Both counts should be equal to num_threads now.
EXPECT_EQ(count1.load(std::memory_order_relaxed), num_threads);
EXPECT_EQ(count2.load(std::memory_order_relaxed), num_threads);
timeout_barrier.Init(self, 0); // Reset to zero for destruction.
}
class CheckPassTask : public Task { public:
CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
: barrier_(barrier),
count_(count),
subtasks_(subtasks) {}
void Run(Thread* self) override { for (size_t i = 0; i < subtasks_; ++i) {
++*count_; // Pass through to next subtask.
barrier_->Pass(self);
}
}
// Check that barrier pass through works.
TEST_F(BarrierTest, CheckPass) {
Thread* self = Thread::Current();
std::unique_ptr<ThreadPool> thread_pool(
ThreadPool::Create("Barrier test thread pool", num_threads));
Barrier barrier(0);
AtomicInteger count(0); const int32_t num_tasks = num_threads * 4; const int32_t num_sub_tasks = 128; for (int32_t i = 0; i < num_tasks; ++i) {
thread_pool->AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
}
thread_pool->StartWorkers(self); const int32_t expected_total_tasks = num_sub_tasks * num_tasks; // Wait for all the tasks to complete using the barrier.
barrier.Increment(self, expected_total_tasks); // The total number of completed tasks should be equal to expected_total_tasks.
EXPECT_EQ(count.load(std::memory_order_relaxed), expected_total_tasks);
}
} // namespace art
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.