1 /*
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 "base/casts.h"
23 #include "jni_env_ext-inl.h"
24 #include "obj_ptr-inl.h"
25 #include "runtime.h"
26 #include "thread-inl.h"
27
28 namespace art {
29
ScopedThreadStateChange(Thread * self,ThreadState new_thread_state)30 inline ScopedThreadStateChange::ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
31 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
32 if (UNLIKELY(self_ == nullptr)) {
33 // Value chosen arbitrarily and won't be used in the destructor since thread_ == null.
34 old_thread_state_ = kTerminated;
35 Runtime* runtime = Runtime::Current();
36 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
37 } else {
38 DCHECK_EQ(self, Thread::Current());
39 // Read state without locks, ok as state is effectively thread local and we're not interested
40 // in the suspend count (this will be handled in the runnable transitions).
41 old_thread_state_ = self->GetState();
42 if (old_thread_state_ != new_thread_state) {
43 if (new_thread_state == kRunnable) {
44 self_->TransitionFromSuspendedToRunnable();
45 } else if (old_thread_state_ == kRunnable) {
46 self_->TransitionFromRunnableToSuspended(new_thread_state);
47 } else {
48 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
49 self_->SetState(new_thread_state);
50 }
51 }
52 }
53 }
54
~ScopedThreadStateChange()55 inline ScopedThreadStateChange::~ScopedThreadStateChange() {
56 if (UNLIKELY(self_ == nullptr)) {
57 if (!expected_has_no_thread_) {
58 Runtime* runtime = Runtime::Current();
59 bool shutting_down = (runtime == nullptr) || runtime->IsShuttingDown(nullptr);
60 CHECK(shutting_down);
61 }
62 } else {
63 if (old_thread_state_ != thread_state_) {
64 if (old_thread_state_ == kRunnable) {
65 self_->TransitionFromSuspendedToRunnable();
66 } else if (thread_state_ == kRunnable) {
67 self_->TransitionFromRunnableToSuspended(old_thread_state_);
68 } else {
69 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
70 self_->SetState(old_thread_state_);
71 }
72 }
73 }
74 }
75
76 template<typename T>
AddLocalReference(ObjPtr<mirror::Object> obj)77 inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(ObjPtr<mirror::Object> obj) const {
78 Locks::mutator_lock_->AssertSharedHeld(Self());
79 if (kIsDebugBuild) {
80 CHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
81 DCheckObjIsNotClearedJniWeakGlobal(obj);
82 }
83 return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj);
84 }
85
86 template<typename T>
Decode(jobject obj)87 inline ObjPtr<T> ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const {
88 Locks::mutator_lock_->AssertSharedHeld(Self());
89 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
90 return ObjPtr<T>::DownCast(Self()->DecodeJObject(obj));
91 }
92
IsRunnable()93 inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
94 return self_->GetState() == kRunnable;
95 }
96
ScopedObjectAccessAlreadyRunnable(JNIEnv * env)97 inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
98 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {}
99
ScopedObjectAccessAlreadyRunnable(Thread * self)100 inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
101 : self_(self),
102 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
103 vm_(env_ != nullptr ? env_->vm : nullptr) {}
104
ScopedObjectAccessUnchecked(JNIEnv * env)105 inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
106 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
107 Self()->VerifyStack();
108 Locks::mutator_lock_->AssertSharedHeld(Self());
109 }
110
ScopedObjectAccessUnchecked(Thread * self)111 inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
112 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
113 Self()->VerifyStack();
114 Locks::mutator_lock_->AssertSharedHeld(Self());
115 }
116
ScopedObjectAccess(JNIEnv * env)117 inline ScopedObjectAccess::ScopedObjectAccess(JNIEnv* env) : ScopedObjectAccessUnchecked(env) {}
ScopedObjectAccess(Thread * self)118 inline ScopedObjectAccess::ScopedObjectAccess(Thread* self) : ScopedObjectAccessUnchecked(self) {}
~ScopedObjectAccess()119 inline ScopedObjectAccess::~ScopedObjectAccess() {}
120
ScopedThreadSuspension(Thread * self,ThreadState suspended_state)121 inline ScopedThreadSuspension::ScopedThreadSuspension(Thread* self, ThreadState suspended_state)
122 : self_(self), suspended_state_(suspended_state) {
123 DCHECK(self_ != nullptr);
124 self_->TransitionFromRunnableToSuspended(suspended_state);
125 }
126
~ScopedThreadSuspension()127 inline ScopedThreadSuspension::~ScopedThreadSuspension() {
128 DCHECK_EQ(self_->GetState(), suspended_state_);
129 self_->TransitionFromSuspendedToRunnable();
130 }
131
132 } // namespace art
133
134 #endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_
135