blob: ebc545250a630413f8497354a0929fdfbdfcf357 [file] [log] [blame]
Ian Rogers00f7d0e2012-07-19 15:28:27 -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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_
18#define ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_
Ian Rogers00f7d0e2012-07-19 15:28:27 -070019
Elliott Hughes1aa246d2012-12-13 09:29:36 -080020#include "base/casts.h"
Ian Rogers693ff612013-02-01 10:56:12 -080021#include "thread-inl.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080022#include "verify_object.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023
24namespace art {
25
26// Scoped change into and out of a particular state. Handles Runnable transitions that require
27// more complicated suspension checking. The subclasses ScopedObjectAccessUnchecked and
28// ScopedObjectAccess are used to handle the change into Runnable to get direct access to objects,
29// the unchecked variant doesn't aid annotalysis.
30class ScopedThreadStateChange {
31 public:
32 ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
Ian Rogers1ffa32f2013-02-05 18:29:08 -080033 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080035 if (UNLIKELY(self_ == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 // Value chosen arbitrarily and won't be used in the destructor since thread_ == NULL.
37 old_thread_state_ = kTerminated;
Ian Rogers120f1c72012-09-28 17:17:10 -070038 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -070039 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 } else {
41 bool runnable_transition;
Ian Rogers22f454c2012-09-08 11:06:29 -070042 DCHECK_EQ(self, Thread::Current());
43 // Read state without locks, ok as state is effectively thread local and we're not interested
44 // in the suspend count (this will be handled in the runnable transitions).
Ian Rogers474b6da2012-09-25 00:20:38 -070045 old_thread_state_ = self->GetState();
Ian Rogers22f454c2012-09-08 11:06:29 -070046 runnable_transition = old_thread_state_ == kRunnable || new_thread_state == kRunnable;
47 if (!runnable_transition) {
48 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
Ian Rogers474b6da2012-09-25 00:20:38 -070049 self_->SetState(new_thread_state);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 }
Mathieu Chartier858f1c52012-10-17 17:45:55 -070051
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052 if (runnable_transition && old_thread_state_ != new_thread_state) {
53 if (new_thread_state == kRunnable) {
54 self_->TransitionFromSuspendedToRunnable();
55 } else {
56 DCHECK_EQ(old_thread_state_, kRunnable);
57 self_->TransitionFromRunnableToSuspended(new_thread_state);
58 }
59 }
60 }
61 }
62
Ian Rogers1ffa32f2013-02-05 18:29:08 -080063 ~ScopedThreadStateChange() LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE {
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080064 if (UNLIKELY(self_ == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 if (!expected_has_no_thread_) {
Ian Rogers120f1c72012-09-28 17:17:10 -070066 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -070067 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(nullptr);
Ian Rogers120f1c72012-09-28 17:17:10 -070068 CHECK(shutting_down);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 }
70 } else {
71 if (old_thread_state_ != thread_state_) {
72 if (old_thread_state_ == kRunnable) {
73 self_->TransitionFromSuspendedToRunnable();
74 } else if (thread_state_ == kRunnable) {
75 self_->TransitionFromRunnableToSuspended(old_thread_state_);
76 } else {
Ian Rogers22f454c2012-09-08 11:06:29 -070077 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
Ian Rogers474b6da2012-09-25 00:20:38 -070078 self_->SetState(old_thread_state_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 }
80 }
81 }
82 }
83
84 Thread* Self() const {
85 return self_;
86 }
87
88 protected:
89 // Constructor used by ScopedJniThreadState for an unattached thread that has access to the VM*.
90 ScopedThreadStateChange()
91 : self_(NULL), thread_state_(kTerminated), old_thread_state_(kTerminated),
92 expected_has_no_thread_(true) {}
93
94 Thread* const self_;
95 const ThreadState thread_state_;
96
97 private:
98 ThreadState old_thread_state_;
99 const bool expected_has_no_thread_;
100
101 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
102};
103
104// Entry/exit processing for transitions from Native to Runnable (ie within JNI functions).
105//
106// This class performs the necessary thread state switching to and from Runnable and lets us
107// amortize the cost of working out the current thread. Additionally it lets us check (and repair)
108// apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects
109// into jobjects via methods of this class. Performing this here enforces the Runnable thread state
110// for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code
111// is also manipulating the Object.
112//
113// The destructor transitions back to the previous thread state, typically Native. In this state
114// GC and thread suspension may occur.
115//
116// For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of
117// the mutator_lock_ will be acquired on construction.
118class ScopedObjectAccessUnchecked : public ScopedThreadStateChange {
119 public:
120 explicit ScopedObjectAccessUnchecked(JNIEnv* env)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800121 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 : ScopedThreadStateChange(ThreadForEnv(env), kRunnable),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700123 env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 self_->VerifyStack();
Ian Rogers98379392014-02-24 16:53:16 -0800125 Locks::mutator_lock_->AssertSharedHeld(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 }
127
128 explicit ScopedObjectAccessUnchecked(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 : ScopedThreadStateChange(self, kRunnable),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700131 env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 vm_(env_ != NULL ? env_->vm : NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 self_->VerifyStack();
Ian Rogers98379392014-02-24 16:53:16 -0800134 Locks::mutator_lock_->AssertSharedHeld(self_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 }
136
137 // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't
138 // change into Runnable or acquire a share on the mutator_lock_.
139 explicit ScopedObjectAccessUnchecked(JavaVM* vm)
Ian Rogers1eb512d2013-10-18 15:42:20 -0700140 : ScopedThreadStateChange(), env_(NULL), vm_(down_cast<JavaVMExt*>(vm)) {}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800142 // Here purely to force inlining.
143 ~ScopedObjectAccessUnchecked() ALWAYS_INLINE {
Ian Rogers98379392014-02-24 16:53:16 -0800144 Locks::mutator_lock_->AssertSharedHeld(self_);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800145 }
146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 JNIEnvExt* Env() const {
148 return env_;
149 }
150
151 JavaVMExt* Vm() const {
152 return vm_;
153 }
154
155 /*
156 * Add a local reference for an object to the indirect reference table associated with the
157 * current stack frame. When the native function returns, the reference will be discarded.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 *
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700159 * We need to allow the same reference to be added multiple times, and cope with NULL.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 *
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700161 * This will be called on otherwise unreferenced objects. We cannot do GC allocations here, and
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 * it's best if we don't grab a mutex.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 */
164 template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 T AddLocalReference(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700166 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
168 if (obj == NULL) {
169 return NULL;
170 }
171
172 DCHECK_NE((reinterpret_cast<uintptr_t>(obj) & 0xffff0000), 0xebad0000);
173
Ian Rogers53b8b092014-03-13 23:45:53 -0700174 return Env()->AddLocalReference<T>(obj, Vm()->work_around_app_jni_bugs);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175 }
176
177 template<typename T>
178 T Decode(jobject obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700180 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
182 return down_cast<T>(Self()->DecodeJObject(obj));
183 }
184
Brian Carlstromea46f952013-07-30 01:26:50 -0700185 mirror::ArtField* DecodeField(jfieldID fid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700187 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700189 CHECK(!kMovingFields);
Brian Carlstromea46f952013-07-30 01:26:50 -0700190 return reinterpret_cast<mirror::ArtField*>(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 }
192
Brian Carlstromea46f952013-07-30 01:26:50 -0700193 jfieldID EncodeField(mirror::ArtField* field) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700195 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700196 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700197 CHECK(!kMovingFields);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 return reinterpret_cast<jfieldID>(field);
199 }
200
Brian Carlstromea46f952013-07-30 01:26:50 -0700201 mirror::ArtMethod* DecodeMethod(jmethodID mid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700203 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205 CHECK(!kMovingMethods);
Brian Carlstromea46f952013-07-30 01:26:50 -0700206 return reinterpret_cast<mirror::ArtMethod*>(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 }
208
Brian Carlstromea46f952013-07-30 01:26:50 -0700209 jmethodID EncodeMethod(mirror::ArtMethod* method) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700211 Locks::mutator_lock_->AssertSharedHeld(Self());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 DCHECK_EQ(thread_state_, kRunnable); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700213 CHECK(!kMovingMethods);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 return reinterpret_cast<jmethodID>(method);
215 }
216
217 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 // The full JNIEnv.
219 JNIEnvExt* const env_;
220 // The full JavaVM.
221 JavaVMExt* const vm_;
222
223 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked);
224};
225
226// Annotalysis helping variant of the above.
227class ScopedObjectAccess : public ScopedObjectAccessUnchecked {
228 public:
229 explicit ScopedObjectAccess(JNIEnv* env)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700230 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800231 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 : ScopedObjectAccessUnchecked(env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 }
234
235 explicit ScopedObjectAccess(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
237 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 : ScopedObjectAccessUnchecked(self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 }
240
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800241 ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 // Base class will release share of lock. Invoked after this destructor.
243 }
244
245 private:
246 // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that
247 // routines operating with just a VM are sound, they are not, but when you have just a VM
248 // you cannot call the unsound routines.
249 explicit ScopedObjectAccess(JavaVM* vm)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 : ScopedObjectAccessUnchecked(vm) {}
252
253 friend class ScopedCheck;
254 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess);
255};
256
257} // namespace art
258
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700259#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_