Commit 923d919c authored by Benoit Lize's avatar Benoit Lize Committed by Commit Bot

blink: Enforce ParkableString thread restrictions when DCHECK_IS_ON().

Bug: 877044
Change-Id: I6b3e7be16ade21bd9c511a6a4fda10930575efa4
Reviewed-on: https://chromium-review.googlesource.com/1210782
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: default avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589505}
parent e41ffb6c
...@@ -27,9 +27,19 @@ ParkableStringImpl::ParkableStringImpl(scoped_refptr<StringImpl>&& impl, ...@@ -27,9 +27,19 @@ ParkableStringImpl::ParkableStringImpl(scoped_refptr<StringImpl>&& impl,
lock_depth_(0), lock_depth_(0),
string_(std::move(impl)), string_(std::move(impl)),
is_parked_(false), is_parked_(false),
is_parkable_(parkable == ParkableState::kParkable) {} is_parkable_(parkable == ParkableState::kParkable)
#if DCHECK_IS_ON()
,
owning_thread_(CurrentThread())
#endif
{
}
ParkableStringImpl::~ParkableStringImpl() { ParkableStringImpl::~ParkableStringImpl() {
// Null strings (default-constructed) can be destroyed on a different thread.
if (!string_.IsNull())
AssertOnValidThread();
if (is_parkable_) if (is_parkable_)
ParkableStringManager::Instance().Remove(string_.Impl()); ParkableStringManager::Instance().Remove(string_.Impl());
} }
...@@ -46,24 +56,29 @@ void ParkableStringImpl::Unlock() { ...@@ -46,24 +56,29 @@ void ParkableStringImpl::Unlock() {
} }
bool ParkableStringImpl::Is8Bit() const { bool ParkableStringImpl::Is8Bit() const {
AssertOnValidThread();
return string_.Is8Bit(); return string_.Is8Bit();
} }
bool ParkableStringImpl::IsNull() const { bool ParkableStringImpl::IsNull() const {
AssertOnValidThread();
return string_.IsNull(); return string_.IsNull();
} }
const String& ParkableStringImpl::ToString() { const String& ParkableStringImpl::ToString() {
AssertOnValidThread();
MutexLocker locker(mutex_); MutexLocker locker(mutex_);
Unpark(); Unpark();
return string_; return string_;
} }
unsigned ParkableStringImpl::CharactersSizeInBytes() const { unsigned ParkableStringImpl::CharactersSizeInBytes() const {
AssertOnValidThread();
return string_.CharactersSizeInBytes(); return string_.CharactersSizeInBytes();
} }
bool ParkableStringImpl::Park() { bool ParkableStringImpl::Park() {
AssertOnValidThread();
MutexLocker locker(mutex_); MutexLocker locker(mutex_);
DCHECK(is_parkable_); DCHECK(is_parkable_);
...@@ -79,6 +94,7 @@ bool ParkableStringImpl::Park() { ...@@ -79,6 +94,7 @@ bool ParkableStringImpl::Park() {
} }
void ParkableStringImpl::Unpark() { void ParkableStringImpl::Unpark() {
AssertOnValidThread();
mutex_.AssertAcquired(); mutex_.AssertAcquired();
if (!is_parked_) if (!is_parked_)
return; return;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h" #include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"
#include "third_party/blink/renderer/platform/wtf/threading_primitives.h" #include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
// ParkableString represents a string that may be parked in memory, that it its // ParkableString represents a string that may be parked in memory, that it its
...@@ -87,6 +88,16 @@ class PLATFORM_EXPORT ParkableStringImpl final ...@@ -87,6 +88,16 @@ class PLATFORM_EXPORT ParkableStringImpl final
const bool is_parkable_; const bool is_parkable_;
#if DCHECK_IS_ON()
const ThreadIdentifier owning_thread_;
#endif
void AssertOnValidThread() const {
#if DCHECK_IS_ON()
DCHECK_EQ(owning_thread_, CurrentThread());
#endif
}
FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, Park); FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, Park);
FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, Unpark); FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, Unpark);
FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, LockUnlock); FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, LockUnlock);
......
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