Commit aca727f6 authored by Michael Lippautz's avatar Michael Lippautz Committed by V8 LUCI CQ

heap: Remove OneShotBarrier

The code is dead since migrating to jobs API.

Change-Id: Icdcc3523ffe5830ef5851cf4ea86e579841f543c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3540103Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79573}
parent d0b49f66
......@@ -1365,7 +1365,6 @@ filegroup(
"src/heap/allocation-stats.h",
"src/heap/array-buffer-sweeper.cc",
"src/heap/array-buffer-sweeper.h",
"src/heap/barrier.h",
"src/heap/base-space.cc",
"src/heap/base-space.h",
"src/heap/basic-memory-chunk.cc",
......
......@@ -2964,7 +2964,6 @@ v8_header_set("v8_internal_headers") {
"src/heap/allocation-result.h",
"src/heap/allocation-stats.h",
"src/heap/array-buffer-sweeper.h",
"src/heap/barrier.h",
"src/heap/base-space.h",
"src/heap/basic-memory-chunk.h",
"src/heap/code-object-registry.h",
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_HEAP_BARRIER_H_
#define V8_HEAP_BARRIER_H_
#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
#include "src/base/platform/time.h"
namespace v8 {
namespace internal {
// Barrier that can be used once to synchronize a dynamic number of tasks
// working concurrently.
//
// The barrier takes a timeout which is used to avoid waiting for too long. If
// any of the users ever reach the timeout they will disable the barrier and
// signal others to fall through.
//
// Usage:
// void RunConcurrently(OneShotBarrier* shared_barrier) {
// shared_barrier->Start();
// do {
// {
// /* process work and create new work */
// barrier->NotifyAll();
// /* process work and create new work */
// }
// } while(!shared_barrier->Wait());
// }
//
// Note: If Start() is not called in time, e.g., because the first concurrent
// task is already done processing all work, then Done() will return true
// immediately.
class OneshotBarrier {
public:
explicit OneshotBarrier(base::TimeDelta timeout) : timeout_(timeout) {}
void Start() {
base::MutexGuard guard(&mutex_);
tasks_++;
}
void NotifyAll() {
base::MutexGuard guard(&mutex_);
if (waiting_ > 0) condition_.NotifyAll();
}
bool Wait() {
base::MutexGuard guard(&mutex_);
if (done_) return true;
DCHECK_LE(waiting_, tasks_);
waiting_++;
if (waiting_ == tasks_) {
done_ = true;
condition_.NotifyAll();
} else {
// Spurious wakeup is ok here.
if (!condition_.WaitFor(&mutex_, timeout_)) {
// If predefined timeout was reached, Stop waiting and signal being done
// also to other tasks.
done_ = true;
}
}
waiting_--;
return done_;
}
// Only valid to be called in a sequential setting.
bool DoneForTesting() const { return done_; }
private:
base::ConditionVariable condition_;
base::Mutex mutex_;
base::TimeDelta timeout_;
int tasks_ = 0;
int waiting_ = 0;
bool done_ = false;
};
} // namespace internal
} // namespace v8
#endif // V8_HEAP_BARRIER_H_
......@@ -34,7 +34,6 @@
#include "src/execution/vm-state-inl.h"
#include "src/handles/global-handles-inl.h"
#include "src/heap/array-buffer-sweeper.h"
#include "src/heap/barrier.h"
#include "src/heap/base/stack.h"
#include "src/heap/basic-memory-chunk.h"
#include "src/heap/code-object-registry.h"
......
......@@ -7,7 +7,6 @@
#include "src/common/globals.h"
#include "src/handles/global-handles.h"
#include "src/heap/array-buffer-sweeper.h"
#include "src/heap/barrier.h"
#include "src/heap/concurrent-allocator.h"
#include "src/heap/gc-tracer.h"
#include "src/heap/heap-inl.h"
......
......@@ -16,7 +16,6 @@
namespace v8 {
namespace internal {
class OneshotBarrier;
class RootScavengeVisitor;
class Scavenger;
......
......@@ -313,7 +313,6 @@ v8_source_set("unittests_sources") {
"diagnostics/gdb-jit-unittest.cc",
"execution/microtask-queue-unittest.cc",
"heap/allocation-observer-unittest.cc",
"heap/barrier-unittest.cc",
"heap/bitmap-test-utils.h",
"heap/bitmap-unittest.cc",
"heap/code-object-registry-unittest.cc",
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/heap/barrier.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
namespace heap {
namespace {
// Large timeout that will not trigger in tests.
constexpr base::TimeDelta test_timeout = base::TimeDelta::FromHours(3);
} // namespace
TEST(OneshotBarrier, InitializeNotDone) {
OneshotBarrier barrier(test_timeout);
EXPECT_FALSE(barrier.DoneForTesting());
}
TEST(OneshotBarrier, DoneAfterWait_Sequential) {
OneshotBarrier barrier(test_timeout);
barrier.Start();
barrier.Wait();
EXPECT_TRUE(barrier.DoneForTesting());
}
namespace {
class ThreadWaitingOnBarrier final : public base::Thread {
public:
ThreadWaitingOnBarrier()
: base::Thread(Options("ThreadWaitingOnBarrier")), barrier_(nullptr) {}
void Initialize(OneshotBarrier* barrier) { barrier_ = barrier; }
void Run() final { barrier_->Wait(); }
private:
OneshotBarrier* barrier_;
};
} // namespace
TEST(OneshotBarrier, DoneAfterWait_Concurrent) {
const int kThreadCount = 2;
OneshotBarrier barrier(test_timeout);
ThreadWaitingOnBarrier threads[kThreadCount];
for (int i = 0; i < kThreadCount; i++) {
threads[i].Initialize(&barrier);
// All threads need to call Wait() to be done.
barrier.Start();
}
for (int i = 0; i < kThreadCount; i++) {
CHECK(threads[i].Start());
}
for (int i = 0; i < kThreadCount; i++) {
threads[i].Join();
}
EXPECT_TRUE(barrier.DoneForTesting());
}
TEST(OneshotBarrier, EarlyFinish_Concurrent) {
const int kThreadCount = 2;
OneshotBarrier barrier(test_timeout);
ThreadWaitingOnBarrier threads[kThreadCount];
// Test that one thread that actually finishes processing work before other
// threads call Start() will move the barrier in Done state.
barrier.Start();
barrier.Wait();
EXPECT_TRUE(barrier.DoneForTesting());
for (int i = 0; i < kThreadCount; i++) {
threads[i].Initialize(&barrier);
// All threads need to call Wait() to be done.
barrier.Start();
}
for (int i = 0; i < kThreadCount; i++) {
CHECK(threads[i].Start());
}
for (int i = 0; i < kThreadCount; i++) {
threads[i].Join();
}
EXPECT_TRUE(barrier.DoneForTesting());
}
namespace {
class CountingThread final : public base::Thread {
public:
CountingThread(OneshotBarrier* barrier, base::Mutex* mutex, size_t* work)
: base::Thread(Options("CountingThread")),
barrier_(barrier),
mutex_(mutex),
work_(work),
processed_work_(0) {}
void Run() final {
do {
ProcessWork();
} while (!barrier_->Wait());
// Main thread is not processing work, so we need one last step.
ProcessWork();
}
size_t processed_work() const { return processed_work_; }
private:
void ProcessWork() {
base::MutexGuard guard(mutex_);
processed_work_ += *work_;
*work_ = 0;
}
OneshotBarrier* const barrier_;
base::Mutex* const mutex_;
size_t* const work_;
size_t processed_work_;
};
} // namespace
TEST(OneshotBarrier, Processing_Concurrent) {
const size_t kWorkCounter = 173173;
OneshotBarrier barrier(test_timeout);
base::Mutex mutex;
size_t work = 0;
CountingThread counting_thread(&barrier, &mutex, &work);
barrier.Start();
barrier.Start();
EXPECT_FALSE(barrier.DoneForTesting());
CHECK(counting_thread.Start());
for (size_t i = 0; i < kWorkCounter; i++) {
{
base::MutexGuard guard(&mutex);
work++;
}
barrier.NotifyAll();
}
barrier.Wait();
counting_thread.Join();
EXPECT_TRUE(barrier.DoneForTesting());
EXPECT_EQ(kWorkCounter, counting_thread.processed_work());
}
} // namespace heap
} // namespace internal
} // namespace v8
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