blob: cf020d0617ce40d5a94b905b2d6bde40a03e5bfb [file] [log] [blame]
Mathieu Chartier0795f232016-09-27 18:43:30 -07001/*
2 * Copyright (C) 2012 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_SCOPED_THREAD_STATE_CHANGE_INL_H_
18#define ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_
19
20#include "scoped_thread_state_change.h"
21
22#include "jni_env_ext-inl.h"
23#include "obj_ptr-inl.h"
24#include "thread-inl.h"
25
26namespace art {
27
28inline ScopedThreadStateChange::ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
29 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
30 if (UNLIKELY(self_ == nullptr)) {
31 // Value chosen arbitrarily and won't be used in the destructor since thread_ == null.
32 old_thread_state_ = kTerminated;
33 Runtime* runtime = Runtime::Current();
34 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
35 } else {
36 DCHECK_EQ(self, Thread::Current());
37 // Read state without locks, ok as state is effectively thread local and we're not interested
38 // in the suspend count (this will be handled in the runnable transitions).
39 old_thread_state_ = self->GetState();
40 if (old_thread_state_ != new_thread_state) {
41 if (new_thread_state == kRunnable) {
42 self_->TransitionFromSuspendedToRunnable();
43 } else if (old_thread_state_ == kRunnable) {
44 self_->TransitionFromRunnableToSuspended(new_thread_state);
45 } else {
46 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
47 self_->SetState(new_thread_state);
48 }
49 }
50 }
51}
52
53inline ScopedThreadStateChange::~ScopedThreadStateChange() {
54 if (UNLIKELY(self_ == nullptr)) {
55 if (!expected_has_no_thread_) {
56 Runtime* runtime = Runtime::Current();
57 bool shutting_down = (runtime == nullptr) || runtime->IsShuttingDown(nullptr);
58 CHECK(shutting_down);
59 }
60 } else {
61 if (old_thread_state_ != thread_state_) {
62 if (old_thread_state_ == kRunnable) {
63 self_->TransitionFromSuspendedToRunnable();
64 } else if (thread_state_ == kRunnable) {
65 self_->TransitionFromRunnableToSuspended(old_thread_state_);
66 } else {
67 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
68 self_->SetState(old_thread_state_);
69 }
70 }
71 }
72}
73
74template<typename T>
75inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(mirror::Object* obj) const {
76 Locks::mutator_lock_->AssertSharedHeld(Self());
77 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
78 DCHECK_NE(obj, Runtime::Current()->GetClearedJniWeakGlobal());
79 return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj);
80}
81
82template<typename T, typename MirrorType, bool kPoison>
83inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(
84 ObjPtr<MirrorType, kPoison> obj) const {
85 return AddLocalReference<T>(obj.Decode());
86}
87
88template<typename T, bool kPoison>
89inline ObjPtr<T, kPoison> ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const {
90 Locks::mutator_lock_->AssertSharedHeld(Self());
91 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
92 return down_cast<T*>(Self()->DecodeJObject(obj));
93}
94
95inline ArtField* ScopedObjectAccessAlreadyRunnable::DecodeField(jfieldID fid) const {
96 Locks::mutator_lock_->AssertSharedHeld(Self());
97 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
98 return reinterpret_cast<ArtField*>(fid);
99}
100
101inline jfieldID ScopedObjectAccessAlreadyRunnable::EncodeField(ArtField* field) const {
102 Locks::mutator_lock_->AssertSharedHeld(Self());
103 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
104 return reinterpret_cast<jfieldID>(field);
105}
106
107inline ArtMethod* ScopedObjectAccessAlreadyRunnable::DecodeMethod(jmethodID mid) const {
108 Locks::mutator_lock_->AssertSharedHeld(Self());
109 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
110 return reinterpret_cast<ArtMethod*>(mid);
111}
112
113inline jmethodID ScopedObjectAccessAlreadyRunnable::EncodeMethod(ArtMethod* method) const {
114 Locks::mutator_lock_->AssertSharedHeld(Self());
115 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
116 return reinterpret_cast<jmethodID>(method);
117}
118
119inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
120 return self_->GetState() == kRunnable;
121}
122
123inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
124 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {}
125
126inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
127 : self_(self),
128 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
129 vm_(env_ != nullptr ? env_->vm : nullptr) {}
130
131inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
132 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
133 Self()->VerifyStack();
134 Locks::mutator_lock_->AssertSharedHeld(Self());
135}
136
137inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
138 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
139 Self()->VerifyStack();
140 Locks::mutator_lock_->AssertSharedHeld(Self());
141}
142
143inline ScopedThreadSuspension::ScopedThreadSuspension(Thread* self, ThreadState suspended_state)
144 : self_(self), suspended_state_(suspended_state) {
145 DCHECK(self_ != nullptr);
146 self_->TransitionFromRunnableToSuspended(suspended_state);
147}
148
149inline ScopedThreadSuspension::~ScopedThreadSuspension() {
150 DCHECK_EQ(self_->GetState(), suspended_state_);
151 self_->TransitionFromSuspendedToRunnable();
152}
153
154} // namespace art
155
156#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_