Commit f8ca50c3 authored by Benoit Lize's avatar Benoit Lize Committed by Commit Bot

base/allocator: Convert a SpinLock to base::Lock.

The hooks' spinlock in PartitionAlloc is not performance-sensitive, and
is likely used as PartitionAlloc used not to rely on base/. Removing it
to reduce the use of custom locking (see the attached bug for
motivations).

Bug: 1061437
Change-Id: Id1d74ac9f108bdc462fd0d5586e89b0be056b67a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2102532
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarChris Palmer <palmer@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750521}
parent 988d8989
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/allocator/partition_allocator/spin_lock.h" #include "base/allocator/partition_allocator/spin_lock.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/synchronization/lock.h"
namespace base { namespace base {
...@@ -62,9 +63,13 @@ subtle::SpinLock& GetLock() { ...@@ -62,9 +63,13 @@ subtle::SpinLock& GetLock() {
} }
static bool g_initialized = false; static bool g_initialized = false;
Lock& GetHooksLock() {
static NoDestructor<Lock> lock;
return *lock;
}
OomFunction internal::PartitionRootBase::g_oom_handling_function = nullptr; OomFunction internal::PartitionRootBase::g_oom_handling_function = nullptr;
std::atomic<bool> PartitionAllocHooks::hooks_enabled_(false); std::atomic<bool> PartitionAllocHooks::hooks_enabled_(false);
subtle::SpinLock PartitionAllocHooks::set_hooks_lock_;
std::atomic<PartitionAllocHooks::AllocationObserverHook*> std::atomic<PartitionAllocHooks::AllocationObserverHook*>
PartitionAllocHooks::allocation_observer_hook_(nullptr); PartitionAllocHooks::allocation_observer_hook_(nullptr);
std::atomic<PartitionAllocHooks::FreeObserverHook*> std::atomic<PartitionAllocHooks::FreeObserverHook*>
...@@ -78,7 +83,7 @@ std::atomic<PartitionAllocHooks::ReallocOverrideHook*> ...@@ -78,7 +83,7 @@ std::atomic<PartitionAllocHooks::ReallocOverrideHook*>
void PartitionAllocHooks::SetObserverHooks(AllocationObserverHook* alloc_hook, void PartitionAllocHooks::SetObserverHooks(AllocationObserverHook* alloc_hook,
FreeObserverHook* free_hook) { FreeObserverHook* free_hook) {
subtle::SpinLock::Guard guard(set_hooks_lock_); AutoLock guard(GetHooksLock());
// Chained hooks are not supported. Registering a non-null hook when a // Chained hooks are not supported. Registering a non-null hook when a
// non-null hook is already registered indicates somebody is trying to // non-null hook is already registered indicates somebody is trying to
...@@ -95,7 +100,7 @@ void PartitionAllocHooks::SetObserverHooks(AllocationObserverHook* alloc_hook, ...@@ -95,7 +100,7 @@ void PartitionAllocHooks::SetObserverHooks(AllocationObserverHook* alloc_hook,
void PartitionAllocHooks::SetOverrideHooks(AllocationOverrideHook* alloc_hook, void PartitionAllocHooks::SetOverrideHooks(AllocationOverrideHook* alloc_hook,
FreeOverrideHook* free_hook, FreeOverrideHook* free_hook,
ReallocOverrideHook realloc_hook) { ReallocOverrideHook realloc_hook) {
subtle::SpinLock::Guard guard(set_hooks_lock_); AutoLock guard(GetHooksLock());
CHECK((!allocation_override_hook_ && !free_override_hook_ && CHECK((!allocation_override_hook_ && !free_override_hook_ &&
!realloc_override_hook_) || !realloc_override_hook_) ||
......
...@@ -282,8 +282,6 @@ class BASE_EXPORT PartitionAllocHooks { ...@@ -282,8 +282,6 @@ class BASE_EXPORT PartitionAllocHooks {
static std::atomic<bool> hooks_enabled_; static std::atomic<bool> hooks_enabled_;
// Lock used to synchronize Set*Hooks calls. // Lock used to synchronize Set*Hooks calls.
static subtle::SpinLock set_hooks_lock_;
static std::atomic<AllocationObserverHook*> allocation_observer_hook_; static std::atomic<AllocationObserverHook*> allocation_observer_hook_;
static std::atomic<FreeObserverHook*> free_observer_hook_; static std::atomic<FreeObserverHook*> free_observer_hook_;
......
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