|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#include "vm/closure_functions_cache.h" |
| 6 | + |
| 7 | +#include "vm/compiler/jit/compiler.h" |
| 8 | +#include "vm/object.h" |
| 9 | +#include "vm/object_store.h" |
| 10 | + |
| 11 | +namespace dart { |
| 12 | + |
| 13 | +FunctionPtr ClosureFunctionsCache::LookupClosureFunction( |
| 14 | + const Class& owner, |
| 15 | + TokenPosition token_pos) { |
| 16 | + auto thread = Thread::Current(); |
| 17 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 18 | + return LookupClosureFunctionLocked(owner, token_pos); |
| 19 | +} |
| 20 | + |
| 21 | +FunctionPtr ClosureFunctionsCache::LookupClosureFunctionLocked( |
| 22 | + const Class& owner, |
| 23 | + TokenPosition token_pos) { |
| 24 | + auto thread = Thread::Current(); |
| 25 | + auto zone = thread->zone(); |
| 26 | + auto object_store = thread->isolate_group()->object_store(); |
| 27 | + |
| 28 | + DEBUG_ASSERT( |
| 29 | + thread->isolate_group()->program_lock()->IsCurrentThreadReader()); |
| 30 | + |
| 31 | + const auto& closures = |
| 32 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 33 | + auto& closure = Function::Handle(zone); |
| 34 | + intptr_t num_closures = closures.Length(); |
| 35 | + for (intptr_t i = 0; i < num_closures; i++) { |
| 36 | + closure ^= closures.At(i); |
| 37 | + if (closure.token_pos() == token_pos && closure.Owner() == owner.raw()) { |
| 38 | + return closure.raw(); |
| 39 | + } |
| 40 | + } |
| 41 | + return Function::null(); |
| 42 | +} |
| 43 | + |
| 44 | +FunctionPtr ClosureFunctionsCache::LookupClosureFunction( |
| 45 | + const Function& parent, |
| 46 | + TokenPosition token_pos) { |
| 47 | + auto thread = Thread::Current(); |
| 48 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 49 | + return LookupClosureFunctionLocked(parent, token_pos); |
| 50 | +} |
| 51 | + |
| 52 | +FunctionPtr ClosureFunctionsCache::LookupClosureFunctionLocked( |
| 53 | + const Function& parent, |
| 54 | + TokenPosition token_pos) { |
| 55 | + auto thread = Thread::Current(); |
| 56 | + auto zone = thread->zone(); |
| 57 | + auto object_store = thread->isolate_group()->object_store(); |
| 58 | + |
| 59 | + DEBUG_ASSERT( |
| 60 | + thread->isolate_group()->program_lock()->IsCurrentThreadReader()); |
| 61 | + |
| 62 | + const auto& closures = |
| 63 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 64 | + auto& closure = Function::Handle(zone); |
| 65 | + intptr_t num_closures = closures.Length(); |
| 66 | + for (intptr_t i = 0; i < num_closures; i++) { |
| 67 | + closure ^= closures.At(i); |
| 68 | + if (closure.token_pos() == token_pos && |
| 69 | + closure.parent_function() == parent.raw()) { |
| 70 | + return closure.raw(); |
| 71 | + } |
| 72 | + } |
| 73 | + return Function::null(); |
| 74 | +} |
| 75 | + |
| 76 | +void ClosureFunctionsCache::AddClosureFunctionLocked(const Function& function) { |
| 77 | + ASSERT(!Compiler::IsBackgroundCompilation()); |
| 78 | + |
| 79 | + auto thread = Thread::Current(); |
| 80 | + auto zone = thread->zone(); |
| 81 | + auto object_store = thread->isolate_group()->object_store(); |
| 82 | + |
| 83 | + DEBUG_ASSERT( |
| 84 | + thread->isolate_group()->program_lock()->IsCurrentThreadWriter()); |
| 85 | + |
| 86 | + const auto& closures = |
| 87 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 88 | + ASSERT(!closures.IsNull()); |
| 89 | + ASSERT(function.IsNonImplicitClosureFunction()); |
| 90 | + closures.Add(function, Heap::kOld); |
| 91 | +} |
| 92 | + |
| 93 | +intptr_t ClosureFunctionsCache::FindClosureIndex(const Function& needle) { |
| 94 | + auto thread = Thread::Current(); |
| 95 | + auto zone = thread->zone(); |
| 96 | + auto object_store = thread->isolate_group()->object_store(); |
| 97 | + |
| 98 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 99 | + |
| 100 | + const auto& closures_array = |
| 101 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 102 | + intptr_t num_closures = closures_array.Length(); |
| 103 | + for (intptr_t i = 0; i < num_closures; i++) { |
| 104 | + if (closures_array.At(i) == needle.raw()) { |
| 105 | + return i; |
| 106 | + } |
| 107 | + } |
| 108 | + return -1; |
| 109 | +} |
| 110 | + |
| 111 | +FunctionPtr ClosureFunctionsCache::ClosureFunctionFromIndex(intptr_t idx) { |
| 112 | + auto thread = Thread::Current(); |
| 113 | + auto zone = thread->zone(); |
| 114 | + auto object_store = thread->isolate_group()->object_store(); |
| 115 | + |
| 116 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 117 | + |
| 118 | + const auto& closures_array = |
| 119 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 120 | + if (idx < 0 || idx >= closures_array.Length()) { |
| 121 | + return Function::null(); |
| 122 | + } |
| 123 | + return Function::RawCast(closures_array.At(idx)); |
| 124 | +} |
| 125 | + |
| 126 | +FunctionPtr ClosureFunctionsCache::GetUniqueInnerClosure( |
| 127 | + const Function& outer) { |
| 128 | + auto thread = Thread::Current(); |
| 129 | + auto zone = thread->zone(); |
| 130 | + auto object_store = thread->isolate_group()->object_store(); |
| 131 | + |
| 132 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 133 | + |
| 134 | + const auto& closures = |
| 135 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 136 | + auto& entry = Function::Handle(zone); |
| 137 | + for (intptr_t i = (closures.Length() - 1); i >= 0; i--) { |
| 138 | + entry ^= closures.At(i); |
| 139 | + if (entry.parent_function() == outer.raw()) { |
| 140 | +#if defined(DEBUG) |
| 141 | + auto& other = Function::Handle(zone); |
| 142 | + for (intptr_t j = i - 1; j >= 0; j--) { |
| 143 | + other ^= closures.At(j); |
| 144 | + ASSERT(other.parent_function() != outer.raw()); |
| 145 | + } |
| 146 | +#endif |
| 147 | + return entry.raw(); |
| 148 | + } |
| 149 | + } |
| 150 | + return Function::null(); |
| 151 | +} |
| 152 | + |
| 153 | +void ClosureFunctionsCache::ForAllClosureFunctions( |
| 154 | + std::function<bool(const Function&)> callback) { |
| 155 | + auto thread = Thread::Current(); |
| 156 | + auto zone = thread->zone(); |
| 157 | + auto object_store = thread->isolate_group()->object_store(); |
| 158 | + |
| 159 | + auto& current_data = Array::Handle(zone); |
| 160 | + auto& entry = Function::Handle(zone); |
| 161 | + |
| 162 | + // NOTE: Inner functions may get added to the closures array while iterating - |
| 163 | + // we guarantee that any closure functions added on this thread by a |
| 164 | + // [callback] call will be visited as well. |
| 165 | + // |
| 166 | + // We avoid holding a lock while accessing the closures array, since often |
| 167 | + // times [callback] will do very heavy things (e.g. compiling the function). |
| 168 | + // |
| 169 | + // This means we can possibly miss a concurrently added closure function - |
| 170 | + // which the caller should be ok with (or it guarantees that this cannot |
| 171 | + // happen). |
| 172 | + const auto& closures = |
| 173 | + GrowableObjectArray::Handle(zone, object_store->closure_functions()); |
| 174 | + |
| 175 | + { |
| 176 | + // The empty read locker scope will implicitly issue an acquire memory |
| 177 | + // fence, which means any closure functions added so far will be visible and |
| 178 | + // iterated further down. |
| 179 | + SafepointReadRwLocker ml(thread, thread->isolate_group()->program_lock()); |
| 180 | + } |
| 181 | + |
| 182 | + // We have an outer loop to ensure any new closure functions added by |
| 183 | + // [callback] will be iterated as well. |
| 184 | + intptr_t i = 0; |
| 185 | + while (true) { |
| 186 | + intptr_t current_length = closures.Length(); |
| 187 | + if (i == current_length) break; |
| 188 | + |
| 189 | + current_data = closures.data(); |
| 190 | + if (current_data.Length() < current_length) { |
| 191 | + current_length = current_data.Length(); |
| 192 | + } |
| 193 | + |
| 194 | + for (; i < current_length; ++i) { |
| 195 | + entry ^= current_data.At(i); |
| 196 | + if (!callback(entry)) { |
| 197 | + return; // Stop iteration. |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace dart |
0 commit comments