OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "src/assert-scope.h" |
5 | 6 |
6 #include "src/assert-scope.h" | 7 #include "src/base/lazy-instance.h" |
7 #include "src/v8.h" | 8 #include "src/base/platform/platform.h" |
| 9 #include "src/isolate-inl.h" |
| 10 #include "src/utils.h" |
8 | 11 |
9 namespace v8 { | 12 namespace v8 { |
10 namespace internal { | 13 namespace internal { |
11 | 14 |
12 uint32_t PerIsolateAssertBase::GetData(Isolate* isolate) { | 15 namespace { |
13 return isolate->per_isolate_assert_data(); | 16 |
| 17 struct PerThreadAssertKeyConstructTrait FINAL { |
| 18 static void Construct(base::Thread::LocalStorageKey* key) { |
| 19 *key = base::Thread::CreateThreadLocalKey(); |
| 20 } |
| 21 }; |
| 22 |
| 23 |
| 24 typedef base::LazyStaticInstance<base::Thread::LocalStorageKey, |
| 25 PerThreadAssertKeyConstructTrait>::type |
| 26 PerThreadAssertKey; |
| 27 |
| 28 |
| 29 PerThreadAssertKey kPerThreadAssertKey; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 |
| 34 class PerThreadAssertData FINAL { |
| 35 public: |
| 36 PerThreadAssertData() : nesting_level_(0) { |
| 37 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; i++) { |
| 38 assert_states_[i] = true; |
| 39 } |
| 40 } |
| 41 |
| 42 ~PerThreadAssertData() { |
| 43 for (int i = 0; i < LAST_PER_THREAD_ASSERT_TYPE; ++i) { |
| 44 DCHECK(assert_states_[i]); |
| 45 } |
| 46 } |
| 47 |
| 48 bool Get(PerThreadAssertType type) const { return assert_states_[type]; } |
| 49 void Set(PerThreadAssertType type, bool x) { assert_states_[type] = x; } |
| 50 |
| 51 void IncrementLevel() { ++nesting_level_; } |
| 52 bool DecrementLevel() { return --nesting_level_ == 0; } |
| 53 |
| 54 static PerThreadAssertData* GetCurrent() { |
| 55 return reinterpret_cast<PerThreadAssertData*>( |
| 56 base::Thread::GetThreadLocal(kPerThreadAssertKey.Get())); |
| 57 } |
| 58 static void SetCurrent(PerThreadAssertData* data) { |
| 59 base::Thread::SetThreadLocal(kPerThreadAssertKey.Get(), data); |
| 60 } |
| 61 |
| 62 private: |
| 63 bool assert_states_[LAST_PER_THREAD_ASSERT_TYPE]; |
| 64 int nesting_level_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(PerThreadAssertData); |
| 67 }; |
| 68 |
| 69 |
| 70 template <PerThreadAssertType kType, bool kAllow> |
| 71 PerThreadAssertScope<kType, kAllow>::PerThreadAssertScope() |
| 72 : data_(PerThreadAssertData::GetCurrent()) { |
| 73 if (data_ == NULL) { |
| 74 data_ = new PerThreadAssertData(); |
| 75 PerThreadAssertData::SetCurrent(data_); |
| 76 } |
| 77 data_->IncrementLevel(); |
| 78 old_state_ = data_->Get(kType); |
| 79 data_->Set(kType, kAllow); |
14 } | 80 } |
15 | 81 |
16 | 82 |
17 void PerIsolateAssertBase::SetData(Isolate* isolate, uint32_t data) { | 83 template <PerThreadAssertType kType, bool kAllow> |
18 isolate->set_per_isolate_assert_data(data); | 84 PerThreadAssertScope<kType, kAllow>::~PerThreadAssertScope() { |
| 85 DCHECK_NOT_NULL(data_); |
| 86 data_->Set(kType, old_state_); |
| 87 if (data_->DecrementLevel()) { |
| 88 PerThreadAssertData::SetCurrent(NULL); |
| 89 delete data_; |
| 90 } |
19 } | 91 } |
20 | 92 |
21 } } // namespace v8::internal | 93 |
| 94 // static |
| 95 template <PerThreadAssertType kType, bool kAllow> |
| 96 bool PerThreadAssertScope<kType, kAllow>::IsAllowed() { |
| 97 PerThreadAssertData* data = PerThreadAssertData::GetCurrent(); |
| 98 return data == NULL || data->Get(kType); |
| 99 } |
| 100 |
| 101 |
| 102 template <PerIsolateAssertType kType, bool kAllow> |
| 103 class PerIsolateAssertScope<kType, kAllow>::DataBit |
| 104 : public BitField<bool, kType, 1> {}; |
| 105 |
| 106 |
| 107 template <PerIsolateAssertType kType, bool kAllow> |
| 108 PerIsolateAssertScope<kType, kAllow>::PerIsolateAssertScope(Isolate* isolate) |
| 109 : isolate_(isolate), old_data_(isolate->per_isolate_assert_data()) { |
| 110 DCHECK_NOT_NULL(isolate); |
| 111 STATIC_ASSERT(kType < 32); |
| 112 isolate_->set_per_isolate_assert_data(DataBit::update(old_data_, kAllow)); |
| 113 } |
| 114 |
| 115 |
| 116 template <PerIsolateAssertType kType, bool kAllow> |
| 117 PerIsolateAssertScope<kType, kAllow>::~PerIsolateAssertScope() { |
| 118 isolate_->set_per_isolate_assert_data(old_data_); |
| 119 } |
| 120 |
| 121 |
| 122 // static |
| 123 template <PerIsolateAssertType kType, bool kAllow> |
| 124 bool PerIsolateAssertScope<kType, kAllow>::IsAllowed(Isolate* isolate) { |
| 125 return DataBit::decode(isolate->per_isolate_assert_data()); |
| 126 } |
| 127 |
| 128 |
| 129 // ----------------------------------------------------------------------------- |
| 130 // Instantiations. |
| 131 |
| 132 template class PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, false>; |
| 133 template class PerThreadAssertScope<HEAP_ALLOCATION_ASSERT, true>; |
| 134 template class PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, false>; |
| 135 template class PerThreadAssertScope<HANDLE_ALLOCATION_ASSERT, true>; |
| 136 template class PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, false>; |
| 137 template class PerThreadAssertScope<HANDLE_DEREFERENCE_ASSERT, true>; |
| 138 template class PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, false>; |
| 139 template class PerThreadAssertScope<DEFERRED_HANDLE_DEREFERENCE_ASSERT, true>; |
| 140 template class PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, false>; |
| 141 template class PerThreadAssertScope<CODE_DEPENDENCY_CHANGE_ASSERT, true>; |
| 142 |
| 143 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, false>; |
| 144 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_ASSERT, true>; |
| 145 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, false>; |
| 146 template class PerIsolateAssertScope<JAVASCRIPT_EXECUTION_THROWS, true>; |
| 147 template class PerIsolateAssertScope<ALLOCATION_FAILURE_ASSERT, false>; |
| 148 template class PerIsolateAssertScope<ALLOCATION_FAILURE_ASSERT, true>; |
| 149 template class PerIsolateAssertScope<DEOPTIMIZATION_ASSERT, false>; |
| 150 template class PerIsolateAssertScope<DEOPTIMIZATION_ASSERT, true>; |
| 151 template class PerIsolateAssertScope<COMPILATION_ASSERT, false>; |
| 152 template class PerIsolateAssertScope<COMPILATION_ASSERT, true>; |
| 153 |
| 154 } // namespace internal |
| 155 } // namespace v8 |
OLD | NEW |