blob: aa96871145e96661aa48f51567df7c85b82b2651 [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
Andreas Gampec15a2f42017-04-21 12:09:39 -070022#include "base/casts.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "jni_env_ext-inl.h"
24#include "obj_ptr-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070025#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "thread-inl.h"
27
28namespace art {
29
30inline 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
55inline 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
76template<typename T>
Mathieu Chartier8778c522016-10-04 19:06:30 -070077inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(ObjPtr<mirror::Object> obj) const {
Mathieu Chartier0795f232016-09-27 18:43:30 -070078 Locks::mutator_lock_->AssertSharedHeld(Self());
Andreas Gampec15a2f42017-04-21 12:09:39 -070079 if (kIsDebugBuild) {
80 CHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
81 DCheckObjIsNotClearedJniWeakGlobal(obj);
82 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070083 return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj);
84}
85
Andreas Gampec73cb642017-02-22 10:11:30 -080086template<typename T>
87inline ObjPtr<T> ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const {
Mathieu Chartier0795f232016-09-27 18:43:30 -070088 Locks::mutator_lock_->AssertSharedHeld(Self());
89 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Andreas Gampec73cb642017-02-22 10:11:30 -080090 return ObjPtr<T>::DownCast(Self()->DecodeJObject(obj));
Mathieu Chartier0795f232016-09-27 18:43:30 -070091}
92
Mathieu Chartier0795f232016-09-27 18:43:30 -070093inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
94 return self_->GetState() == kRunnable;
95}
96
97inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
98 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {}
99
100inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
101 : self_(self),
102 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
103 vm_(env_ != nullptr ? env_->vm : nullptr) {}
104
105inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
106 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
107 Self()->VerifyStack();
108 Locks::mutator_lock_->AssertSharedHeld(Self());
109}
110
111inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
112 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
113 Self()->VerifyStack();
114 Locks::mutator_lock_->AssertSharedHeld(Self());
115}
116
Andreas Gampe90b936d2017-01-31 08:58:55 -0800117inline ScopedObjectAccess::ScopedObjectAccess(JNIEnv* env) : ScopedObjectAccessUnchecked(env) {}
118inline ScopedObjectAccess::ScopedObjectAccess(Thread* self) : ScopedObjectAccessUnchecked(self) {}
119inline ScopedObjectAccess::~ScopedObjectAccess() {}
120
Mathieu Chartier0795f232016-09-27 18:43:30 -0700121inline 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
127inline 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_