5 #include "src/assert-scope.h" 7 #include "src/base/lazy-instance.h" 8 #include "src/base/platform/platform.h" 9 #include "src/isolate.h" 10 #include "src/utils.h" 17 struct PerThreadAssertKeyConstructTrait final {
18 static void Construct(
void* key_arg) {
19 auto key =
reinterpret_cast<base::Thread::LocalStorageKey*
>(key_arg);
20 *key = base::Thread::CreateThreadLocalKey();
25 typedef base::LazyStaticInstance<base::Thread::LocalStorageKey,
26 PerThreadAssertKeyConstructTrait>::type
30 PerThreadAssertKey kPerThreadAssertKey;
38 for (
int i = 0;
i < LAST_PER_THREAD_ASSERT_TYPE;
i++) {
39 assert_states_[
i] =
true;
44 for (
int i = 0;
i < LAST_PER_THREAD_ASSERT_TYPE; ++
i) {
45 DCHECK(assert_states_[
i]);
49 bool Get(PerThreadAssertType
type)
const {
return assert_states_[
type]; }
50 void Set(PerThreadAssertType
type,
bool x) { assert_states_[
type] = x; }
52 void IncrementLevel() { ++nesting_level_; }
53 bool DecrementLevel() {
return --nesting_level_ == 0; }
57 base::Thread::GetThreadLocal(kPerThreadAssertKey.Get()));
60 base::Thread::SetThreadLocal(kPerThreadAssertKey.Get(), data);
64 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE];
70 template <PerThreadAssertType kType,
bool kAllow>
73 if (current_data ==
nullptr) {
75 PerThreadAssertData::SetCurrent(current_data);
77 data_and_old_state_.update(current_data, current_data->Get(kType));
78 current_data->IncrementLevel();
79 current_data->Set(kType, kAllow);
82 template <PerThreadAssertType kType,
bool kAllow>
83 PerThreadAssertScope<kType, kAllow>::~PerThreadAssertScope() {
84 if (data() ==
nullptr)
return;
88 template <PerThreadAssertType kType,
bool kAllow>
89 void PerThreadAssertScope<kType, kAllow>::Release() {
90 auto* current_data = data();
91 DCHECK_NOT_NULL(current_data);
92 current_data->Set(kType, old_state());
93 if (current_data->DecrementLevel()) {
94 PerThreadAssertData::SetCurrent(
nullptr);
101 template <PerThreadAssertType kType,
bool kAllow>
102 bool PerThreadAssertScope<kType, kAllow>::IsAllowed() {
103 PerThreadAssertData* current_data = PerThreadAssertData::GetCurrent();
104 return current_data ==
nullptr || current_data->Get(kType);
107 template <PerIsolateAssertType kType,
bool kAllow>
109 :
public BitField<bool, kType, 1> {};
112 template <PerIsolateAssertType kType,
bool kAllow>
114 : isolate_(isolate), old_data_(isolate->per_isolate_assert_data()) {
115 DCHECK_NOT_NULL(isolate);
116 STATIC_ASSERT(kType < 32);
117 isolate_->set_per_isolate_assert_data(DataBit::update(old_data_, kAllow));
121 template <PerIsolateAssertType kType,
bool kAllow>
123 isolate_->set_per_isolate_assert_data(old_data_);
128 template <PerIsolateAssertType kType,
bool kAllow>
129 bool PerIsolateAssertScope<kType, kAllow>::IsAllowed(Isolate* isolate) {
130 return DataBit::decode(isolate->per_isolate_assert_data());
137 template class PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false>;
138 template class PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true>;
139 template class PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false>;
140 template class PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true>;
141 template class PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false>;
142 template class PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true>;
143 template class PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>;
144 template class PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>;
145 template class PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, false>;
146 template class PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, true>;
148 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>;
149 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>;
150 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>;
151 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>;
152 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, false>;
153 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_DUMP, true>;
154 template class PerIsolateAssertScope<DEOPTIMIZATION_ASSERT, false>;
155 template class PerIsolateAssertScope<DEOPTIMIZATION_ASSERT, true>;
156 template class PerIsolateAssertScope<COMPILATION_ASSERT, false>;
157 template class PerIsolateAssertScope<COMPILATION_ASSERT, true>;
158 template class PerIsolateAssertScope<NO_EXCEPTION_ASSERT, false>;
159 template class PerIsolateAssertScope<NO_EXCEPTION_ASSERT, true>;