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