0

[gin] Introduce gin::MaybeShutdownCppgc()

crrev.com/875699 introduced gin::InitializeCppgcFromV8Platform(), which
centralized initialization of cppgc in Gin for the case of multiple
cppgc users in the same process.

Introduce gin::MaybeShutdownCppgc(), which centralizes the shutdown of
cppgc in Gin. This will help in guaranteeing that cppgc is shutdown only
after all users in the same process are done using it.

Bug: 1056170, 1111024
Change-Id: I15aebd3bea78a7033cf00743dbd4e286b1a30711
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3011899
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#899637}
This commit is contained in:
Daniel Hosseinian
2021-07-08 18:35:10 +00:00
committed by Chromium LUCI CQ
parent b4a10a9100
commit 554068b7a9
3 changed files with 29 additions and 7 deletions

@ -3,19 +3,33 @@
// found in the LICENSE file.
#include "gin/public/cppgc.h"
#include "base/check_op.h"
#include "gin/public/v8_platform.h"
#include "v8/include/cppgc/platform.h"
namespace gin {
namespace {
int g_init_count = 0;
} // namespace
void InitializeCppgcFromV8Platform() {
static bool cppgc_is_initialized = false;
if (cppgc_is_initialized)
DCHECK_GE(g_init_count, 0);
if (g_init_count++ > 0)
return;
cppgc::InitializeProcess(gin::V8Platform::Get()->GetPageAllocator());
}
cppgc_is_initialized = true;
void MaybeShutdownCppgc() {
DCHECK_GT(g_init_count, 0);
if (--g_init_count > 0)
return;
cppgc::ShutdownProcess();
}
} // namespace gin

@ -9,8 +9,17 @@
namespace gin {
// A wrapper around `cppgc::InitializeProcess()` which helps to guarantee that
// cppgc is initialized only once when there are multiple users of cppgc in same
// process.
GIN_EXPORT void InitializeCppgcFromV8Platform();
// Calls `cppgc::ShutdownProcess()` only after being called as many times as
// `InitializeCppgcFromV8Platform()`. Helps to guarantee that cppgc is shutdown
// only after all users in the same process are done using it. Number of calls
// cannot exceed that of `InitializeCppgcFromV8Platform()`.
GIN_EXPORT void MaybeShutdownCppgc();
} // namespace gin
#endif // GIN_PUBLIC_CPPGC_H_
#endif // GIN_PUBLIC_CPPGC_H_

@ -31,7 +31,6 @@
#include "base/time/time.h"
#include "build/build_config.h"
#include "gin/array_buffer.h"
#include "gin/public/cppgc.h"
#include "gin/public/gin_embedders.h"
#include "gin/public/isolate_holder.h"
#include "gin/public/v8_platform.h"
@ -81,7 +80,7 @@
#include "v8/include/v8.h"
#if defined(PDF_ENABLE_XFA)
#include "v8/include/cppgc/platform.h"
#include "gin/public/cppgc.h"
#endif
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
@ -266,7 +265,7 @@ void SetUpV8() {
void TearDownV8() {
#if defined(PDF_ENABLE_XFA)
cppgc::ShutdownProcess();
gin::MaybeShutdownCppgc();
#endif
delete g_isolate_holder;