blob: f95593209b00ac44d960c169394ddf198bf2ba1d [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 Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
Andreas Gampec15a2f42017-04-21 12:09:39 -070024#include "base/casts.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "jni_env_ext-inl.h"
26#include "obj_ptr-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070027#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "thread-inl.h"
29
30namespace art {
31
32inline ScopedThreadStateChange::ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
33 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
34 if (UNLIKELY(self_ == nullptr)) {
35 // Value chosen arbitrarily and won't be used in the destructor since thread_ == null.
36 old_thread_state_ = kTerminated;
37 Runtime* runtime = Runtime::Current();
38 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
39 } else {
40 DCHECK_EQ(self, Thread::Current());
41 // Read state without locks, ok as state is effectively thread local and we're not interested
42 // in the suspend count (this will be handled in the runnable transitions).
43 old_thread_state_ = self->GetState();
44 if (old_thread_state_ != new_thread_state) {
45 if (new_thread_state == kRunnable) {
46 self_->TransitionFromSuspendedToRunnable();
47 } else if (old_thread_state_ == kRunnable) {
48 self_->TransitionFromRunnableToSuspended(new_thread_state);
49 } else {
50 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
51 self_->SetState(new_thread_state);
52 }
53 }
54 }
55}
56
57inline ScopedThreadStateChange::~ScopedThreadStateChange() {
58 if (UNLIKELY(self_ == nullptr)) {
59 if (!expected_has_no_thread_) {
60 Runtime* runtime = Runtime::Current();
61 bool shutting_down = (runtime == nullptr) || runtime->IsShuttingDown(nullptr);
62 CHECK(shutting_down);
63 }
64 } else {
65 if (old_thread_state_ != thread_state_) {
66 if (old_thread_state_ == kRunnable) {
67 self_->TransitionFromSuspendedToRunnable();
68 } else if (thread_state_ == kRunnable) {
69 self_->TransitionFromRunnableToSuspended(old_thread_state_);
70 } else {
71 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
72 self_->SetState(old_thread_state_);
73 }
74 }
75 }
76}
77
78template<typename T>
Mathieu Chartier8778c522016-10-04 19:06:30 -070079inline T ScopedObjectAccessAlreadyRunnable::AddLocalReference(ObjPtr<mirror::Object> obj) const {
Mathieu Chartier0795f232016-09-27 18:43:30 -070080 Locks::mutator_lock_->AssertSharedHeld(Self());
Andreas Gampec15a2f42017-04-21 12:09:39 -070081 if (kIsDebugBuild) {
82 CHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
83 DCheckObjIsNotClearedJniWeakGlobal(obj);
84 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070085 return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj);
86}
87
Andreas Gampec73cb642017-02-22 10:11:30 -080088template<typename T>
89inline ObjPtr<T> ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const {
Mathieu Chartier0795f232016-09-27 18:43:30 -070090 Locks::mutator_lock_->AssertSharedHeld(Self());
91 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Andreas Gampec73cb642017-02-22 10:11:30 -080092 return ObjPtr<T>::DownCast(Self()->DecodeJObject(obj));
Mathieu Chartier0795f232016-09-27 18:43:30 -070093}
94
Mathieu Chartier0795f232016-09-27 18:43:30 -070095inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
96 return self_->GetState() == kRunnable;
97}
98
99inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
Ian Rogers55256cb2017-12-21 17:07:11 -0800100 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->GetVm()) {}
Mathieu Chartier0795f232016-09-27 18:43:30 -0700101
102inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
103 : self_(self),
104 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
Ian Rogers55256cb2017-12-21 17:07:11 -0800105 vm_(env_ != nullptr ? env_->GetVm() : nullptr) {}
Mathieu Chartier0795f232016-09-27 18:43:30 -0700106
107inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
108 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
109 Self()->VerifyStack();
110 Locks::mutator_lock_->AssertSharedHeld(Self());
111}
112
113inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
114 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
115 Self()->VerifyStack();
116 Locks::mutator_lock_->AssertSharedHeld(Self());
117}
118
Andreas Gampe90b936d2017-01-31 08:58:55 -0800119inline ScopedObjectAccess::ScopedObjectAccess(JNIEnv* env) : ScopedObjectAccessUnchecked(env) {}
120inline ScopedObjectAccess::ScopedObjectAccess(Thread* self) : ScopedObjectAccessUnchecked(self) {}
121inline ScopedObjectAccess::~ScopedObjectAccess() {}
122
Mathieu Chartier0795f232016-09-27 18:43:30 -0700123inline ScopedThreadSuspension::ScopedThreadSuspension(Thread* self, ThreadState suspended_state)
124 : self_(self), suspended_state_(suspended_state) {
125 DCHECK(self_ != nullptr);
126 self_->TransitionFromRunnableToSuspended(suspended_state);
127}
128
129inline ScopedThreadSuspension::~ScopedThreadSuspension() {
130 DCHECK_EQ(self_->GetState(), suspended_state_);
131 self_->TransitionFromSuspendedToRunnable();
132}
133
134} // namespace art
135
136#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_