1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_REFLECTIVE_HANDLE_SCOPE_INL_H_
18 #define ART_RUNTIME_REFLECTIVE_HANDLE_SCOPE_INL_H_
19
20 #include "android-base/thread_annotations.h"
21 #include "base/mutex.h"
22 #include "reflective_handle.h"
23 #include "reflective_handle_scope.h"
24 #include "thread-current-inl.h"
25
26 namespace art {
27
28 template <size_t kNumFields, size_t kNumMethods>
StackReflectiveHandleScope(Thread * self)29 StackReflectiveHandleScope<kNumFields, kNumMethods>::StackReflectiveHandleScope(Thread* self) : field_pos_(0), method_pos_(0) {
30 DCHECK_EQ(self, Thread::Current());
31 PushScope(self);
32 }
33
34 template <size_t kNumFields, size_t kNumMethods>
VisitTargets(ReflectiveValueVisitor * visitor)35 void StackReflectiveHandleScope<kNumFields, kNumMethods>::VisitTargets(
36 ReflectiveValueVisitor* visitor) {
37 Thread* self = Thread::Current();
38 DCHECK(GetThread() == self ||
39 Locks::mutator_lock_->IsExclusiveHeld(self))
40 << *GetThread() << " on thread " << *self;
41 auto visit_one = [&](auto& rv) NO_THREAD_SAFETY_ANALYSIS {
42 Locks::mutator_lock_->AssertSharedHeld(self);
43 if (!rv.IsNull()) {
44 rv.Assign((*visitor)(rv.Ptr(), ReflectiveHandleScopeSourceInfo(this)));
45 }
46 };
47 std::for_each(fields_.begin(), fields_.begin() + field_pos_, visit_one);
48 std::for_each(methods_.begin(), methods_.begin() + method_pos_, visit_one);
49 }
50
51 template <size_t kNumFields, size_t kNumMethods>
~StackReflectiveHandleScope()52 StackReflectiveHandleScope<kNumFields, kNumMethods>::~StackReflectiveHandleScope() {
53 PopScope();
54 }
55
PushScope(Thread * self)56 void BaseReflectiveHandleScope::PushScope(Thread* self) {
57 DCHECK_EQ(self, Thread::Current());
58 self_ = self;
59 link_ = self_->GetTopReflectiveHandleScope();
60 self_->PushReflectiveHandleScope(this);
61 }
62
PopScope()63 void BaseReflectiveHandleScope::PopScope() {
64 auto* prev = self_->PopReflectiveHandleScope();
65 CHECK_EQ(prev, this);
66 link_ = nullptr;
67 }
68
69 } // namespace art
70
71 #endif // ART_RUNTIME_REFLECTIVE_HANDLE_SCOPE_INL_H_
72