Commit 15805b2d authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

[heap] Fix ItemParallelJobTest.DistributeItemsMultipleTasks

TBR=ulan@chromium.org

Bug: chromium:651354
Change-Id: Ib3a53a62e048e438bc31cbfd2ea44d17fd6a3b94
Reviewed-on: https://chromium-review.googlesource.com/506204Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45360}
parent 68b81ff4
...@@ -57,28 +57,48 @@ class EagerTask : public ItemParallelJob::Task { ...@@ -57,28 +57,48 @@ class EagerTask : public ItemParallelJob::Task {
} }
}; };
class OneShotBarrier {
public:
explicit OneShotBarrier(size_t counter) : counter_(counter) {
DCHECK_GE(counter_, 0);
}
void Wait() {
DCHECK_NE(counter_, 0);
mutex_.Lock();
counter_--;
if (counter_ == 0) {
condition_.NotifyAll();
} else {
while (counter_ > 0) {
condition_.Wait(&mutex_);
}
}
mutex_.Unlock();
}
private:
base::Mutex mutex_;
base::ConditionVariable condition_;
size_t counter_;
};
class TaskProcessingOneItem : public ItemParallelJob::Task { class TaskProcessingOneItem : public ItemParallelJob::Task {
public: public:
explicit TaskProcessingOneItem(Isolate* isolate, explicit TaskProcessingOneItem(Isolate* isolate, OneShotBarrier* barrier)
base::AtomicNumber<size_t>* count, : ItemParallelJob::Task(isolate), barrier_(barrier) {}
size_t finish)
: ItemParallelJob::Task(isolate), count_(count), finish_(finish) {}
void RunInParallel() override { void RunInParallel() override {
SimpleItem* item = GetItem<SimpleItem>(); SimpleItem* item = GetItem<SimpleItem>();
EXPECT_NE(nullptr, item); EXPECT_NE(nullptr, item);
item->Process(); item->Process();
item->MarkFinished(); item->MarkFinished();
// Avoid canceling the remaining task if it has no started by // Avoid canceling the remaining tasks with a simple barrier.
// busy looping. barrier_->Wait();
count_->Increment(1);
while (count_->Value() != finish_) {
}
} }
private: private:
base::AtomicNumber<size_t>* count_; OneShotBarrier* barrier_;
size_t finish_;
}; };
class TaskForDifferentItems; class TaskForDifferentItems;
...@@ -155,21 +175,21 @@ TEST_F(ItemParallelJobTest, FinishAllItems) { ...@@ -155,21 +175,21 @@ TEST_F(ItemParallelJobTest, FinishAllItems) {
} }
} }
TEST_F(ItemParallelJobTest, DistributeItems) { TEST_F(ItemParallelJobTest, DistributeItemsMultipleTasks) {
const int kItems = 4; const int kItemsAndTasks = 2; // Main thread + additional task.
bool was_processed[kItems]; bool was_processed[kItemsAndTasks];
base::AtomicNumber<size_t> count; OneShotBarrier barrier(kItemsAndTasks);
for (int i = 0; i < kItems; i++) { for (int i = 0; i < kItemsAndTasks; i++) {
was_processed[i] = false; was_processed[i] = false;
} }
ItemParallelJob job(i_isolate()->cancelable_task_manager(), ItemParallelJob job(i_isolate()->cancelable_task_manager(),
parallel_job_semaphore()); parallel_job_semaphore());
for (int i = 0; i < kItems; i++) { for (int i = 0; i < kItemsAndTasks; i++) {
job.AddItem(new SimpleItem(&was_processed[i])); job.AddItem(new SimpleItem(&was_processed[i]));
job.AddTask(new TaskProcessingOneItem(i_isolate(), &count, kItems)); job.AddTask(new TaskProcessingOneItem(i_isolate(), &barrier));
} }
job.Run(); job.Run();
for (int i = 0; i < kItems; i++) { for (int i = 0; i < kItemsAndTasks; i++) {
EXPECT_TRUE(was_processed[i]); EXPECT_TRUE(was_processed[i]);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment