blob: d4469f4357a45d10e8c460af99e94729b0c84f3d [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.
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070086 return ObjPtr<T, kPoison>::DownCast(Self()->DecodeJObject(obj));
Mathieu Chartier0795f232016-09-27 18:43:30 -070087}
88
Mathieu Chartier0795f232016-09-27 18:43:30 -070089inline bool ScopedObjectAccessAlreadyRunnable::IsRunnable() const {
90 return self_->GetState() == kRunnable;
91}
92
93inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
94 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {}
95
96inline ScopedObjectAccessAlreadyRunnable::ScopedObjectAccessAlreadyRunnable(Thread* self)
97 : self_(self),
98 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
99 vm_(env_ != nullptr ? env_->vm : nullptr) {}
100
101inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(JNIEnv* env)
102 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
103 Self()->VerifyStack();
104 Locks::mutator_lock_->AssertSharedHeld(Self());
105}
106
107inline ScopedObjectAccessUnchecked::ScopedObjectAccessUnchecked(Thread* self)
108 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
109 Self()->VerifyStack();
110 Locks::mutator_lock_->AssertSharedHeld(Self());
111}
112
113inline ScopedThreadSuspension::ScopedThreadSuspension(Thread* self, ThreadState suspended_state)
114 : self_(self), suspended_state_(suspended_state) {
115 DCHECK(self_ != nullptr);
116 self_->TransitionFromRunnableToSuspended(suspended_state);
117}
118
119inline ScopedThreadSuspension::~ScopedThreadSuspension() {
120 DCHECK_EQ(self_->GetState(), suspended_state_);
121 self_->TransitionFromSuspendedToRunnable();
122}
123
124} // namespace art
125
126#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_INL_H_