blob: ac2575715eee597a2b64614fb4e2eac55a91463a [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>
Mathieu Chartier8778c522016-10-04 19:06:30 -070075inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(ObjPtr<mirror::Object> obj) const {
Mathieu Chartier0795f232016-09-27 18:43:30 -070076 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
Mathieu Chartier0795f232016-09-27 18:43:30 -070082template<typename T, bool kPoison>
83inline ObjPtr<T, kPoison> ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const {
84 Locks::mutator_lock_->AssertSharedHeld(Self());
85 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
86 return down_cast<T*>(Self()->DecodeJObject(obj));
87}
88
89inline ArtField* ScopedObjectAccessAlreadyRunnable::DecodeField(jfieldID fid) const {
90 Locks::mutator_lock_->AssertSharedHeld(Self());
91 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
92 return reinterpret_cast<ArtField*>(fid);
93}
94
95inline jfieldID ScopedObjectAccessAlreadyRunnable::EncodeField(ArtField* field) const {
96 Locks::mutator_lock_->AssertSharedHeld(Self());
97 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
98 return reinterpret_cast<jfieldID>(field);
99}
100
101inline ArtMethod* ScopedObjectAccessAlreadyRunnable::DecodeMethod(jmethodID mid) const {
102 Locks::mutator_lock_->AssertSharedHeld(Self());
103 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
104 return reinterpret_cast<ArtMethod*>(mid);
105}
106
107inline jmethodID ScopedObjectAccessAlreadyRunnable::EncodeMethod(ArtMethod* method) const {
108 Locks::mutator_lock_->AssertSharedHeld(Self());
109 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
110 return reinterpret_cast<jmethodID>(method);
111}
112
113inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
114 return self_->GetState() == kRunnable;
115}
116
117inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
118 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {}
119
120inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
121 : self_(self),
122 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
123 vm_(env_ != nullptr ? env_->vm : nullptr) {}
124
125inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
126 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
127 Self()->VerifyStack();
128 Locks::mutator_lock_->AssertSharedHeld(Self());
129}
130
131inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
132 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
133 Self()->VerifyStack();
134 Locks::mutator_lock_->AssertSharedHeld(Self());
135}
136
137inline ScopedThreadSuspension::ScopedThreadSuspension(Thread* self, ThreadState suspended_state)
138 : self_(self), suspended_state_(suspended_state) {
139 DCHECK(self_ != nullptr);
140 self_->TransitionFromRunnableToSuspended(suspended_state);
141}
142
143inline ScopedThreadSuspension::~ScopedThreadSuspension() {
144 DCHECK_EQ(self_->GetState(), suspended_state_);
145 self_->TransitionFromSuspendedToRunnable();
146}
147
148} // namespace art
149
150#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_