Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |
| 18 | #define ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 20 | #include "base/casts.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 21 | #include "java_vm_ext.h" |
| 22 | #include "jni_env_ext-inl.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 23 | #include "art_field.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 24 | #include "read_barrier.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 25 | #include "thread-inl.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 26 | #include "verify_object.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | // Scoped change into and out of a particular state. Handles Runnable transitions that require |
| 31 | // more complicated suspension checking. The subclasses ScopedObjectAccessUnchecked and |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 32 | // ScopedObjectAccess are used to handle the change into Runnable to Get direct access to objects, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | // the unchecked variant doesn't aid annotalysis. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 34 | class ScopedThreadStateChange : public ValueObject { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 35 | public: |
| 36 | ScopedThreadStateChange(Thread* self, ThreadState new_thread_state) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 37 | REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 39 | if (UNLIKELY(self_ == nullptr)) { |
| 40 | // Value chosen arbitrarily and won't be used in the destructor since thread_ == null. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | old_thread_state_ = kTerminated; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 42 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 43 | CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDown(self_)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | } else { |
Ian Rogers | 22f454c | 2012-09-08 11:06:29 -0700 | [diff] [blame] | 45 | DCHECK_EQ(self, Thread::Current()); |
| 46 | // Read state without locks, ok as state is effectively thread local and we're not interested |
| 47 | // in the suspend count (this will be handled in the runnable transitions). |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 48 | old_thread_state_ = self->GetState(); |
Mathieu Chartier | 92b7889 | 2014-04-24 16:14:43 -0700 | [diff] [blame] | 49 | if (old_thread_state_ != new_thread_state) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | if (new_thread_state == kRunnable) { |
| 51 | self_->TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 92b7889 | 2014-04-24 16:14:43 -0700 | [diff] [blame] | 52 | } else if (old_thread_state_ == kRunnable) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 53 | self_->TransitionFromRunnableToSuspended(new_thread_state); |
Mathieu Chartier | 92b7889 | 2014-04-24 16:14:43 -0700 | [diff] [blame] | 54 | } else { |
| 55 | // A suspended transition to another effectively suspended transition, ok to use Unsafe. |
| 56 | self_->SetState(new_thread_state); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | ~ScopedThreadStateChange() REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 63 | if (UNLIKELY(self_ == nullptr)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | if (!expected_has_no_thread_) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 65 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 66 | bool shutting_down = (runtime == nullptr) || runtime->IsShuttingDown(nullptr); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 67 | CHECK(shutting_down); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | } |
| 69 | } else { |
| 70 | if (old_thread_state_ != thread_state_) { |
| 71 | if (old_thread_state_ == kRunnable) { |
| 72 | self_->TransitionFromSuspendedToRunnable(); |
| 73 | } else if (thread_state_ == kRunnable) { |
| 74 | self_->TransitionFromRunnableToSuspended(old_thread_state_); |
| 75 | } else { |
Ian Rogers | 22f454c | 2012-09-08 11:06:29 -0700 | [diff] [blame] | 76 | // A suspended transition to another effectively suspended transition, ok to use Unsafe. |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 77 | self_->SetState(old_thread_state_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Thread* Self() const { |
| 84 | return self_; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | // Constructor used by ScopedJniThreadState for an unattached thread that has access to the VM*. |
| 89 | ScopedThreadStateChange() |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 90 | : self_(nullptr), thread_state_(kTerminated), old_thread_state_(kTerminated), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | expected_has_no_thread_(true) {} |
| 92 | |
| 93 | Thread* const self_; |
| 94 | const ThreadState thread_state_; |
| 95 | |
| 96 | private: |
| 97 | ThreadState old_thread_state_; |
| 98 | const bool expected_has_no_thread_; |
| 99 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 100 | friend class ScopedObjectAccessUnchecked; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange); |
| 102 | }; |
| 103 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 104 | // Assumes we are already runnable. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 105 | class ScopedObjectAccessAlreadyRunnable : public ValueObject { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 106 | public: |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 107 | Thread* Self() const { |
| 108 | return self_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 111 | JNIEnvExt* Env() const { |
| 112 | return env_; |
| 113 | } |
| 114 | |
| 115 | JavaVMExt* Vm() const { |
| 116 | return vm_; |
| 117 | } |
| 118 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 119 | bool ForceCopy() const { |
| 120 | return vm_->ForceCopy(); |
| 121 | } |
| 122 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Add a local reference for an object to the indirect reference table associated with the |
| 125 | * current stack frame. When the native function returns, the reference will be discarded. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 126 | * |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 127 | * We need to allow the same reference to be added multiple times, and cope with nullptr. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 128 | * |
Elliott Hughes | 9dcd45c | 2013-07-29 14:40:52 -0700 | [diff] [blame] | 129 | * This will be called on otherwise unreferenced objects. We cannot do GC allocations here, and |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 130 | * it's best if we don't grab a mutex. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | */ |
| 132 | template<typename T> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 133 | T AddLocalReference(mirror::Object* obj) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 134 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 135 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 136 | DCHECK_NE(obj, Runtime::Current()->GetClearedJniWeakGlobal()); |
Mathieu Chartier | e1a0153 | 2015-05-04 11:46:25 -0700 | [diff] [blame] | 137 | return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | template<typename T> |
| 141 | T Decode(jobject obj) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 142 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 143 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 144 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | return down_cast<T>(Self()->DecodeJObject(obj)); |
| 146 | } |
| 147 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 148 | ArtField* DecodeField(jfieldID fid) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 149 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 150 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 151 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 152 | return reinterpret_cast<ArtField*>(fid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 155 | jfieldID EncodeField(ArtField* field) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 156 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 157 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | return reinterpret_cast<jfieldID>(field); |
| 159 | } |
| 160 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 161 | ArtMethod* DecodeMethod(jmethodID mid) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 162 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 163 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 164 | return reinterpret_cast<ArtMethod*>(mid); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 167 | jmethodID EncodeMethod(ArtMethod* method) const SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 168 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 169 | DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 170 | return reinterpret_cast<jmethodID>(method); |
| 171 | } |
| 172 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 173 | bool IsRunnable() const { |
| 174 | return self_->GetState() == kRunnable; |
| 175 | } |
| 176 | |
| 177 | protected: |
| 178 | explicit ScopedObjectAccessAlreadyRunnable(JNIEnv* env) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 179 | REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 180 | : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) { |
| 181 | } |
| 182 | |
| 183 | explicit ScopedObjectAccessAlreadyRunnable(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 184 | REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 185 | : self_(self), env_(down_cast<JNIEnvExt*>(self->GetJniEnv())), |
| 186 | vm_(env_ != nullptr ? env_->vm : nullptr) { |
| 187 | } |
| 188 | |
| 189 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 190 | // change into Runnable or acquire a share on the mutator_lock_. |
| 191 | explicit ScopedObjectAccessAlreadyRunnable(JavaVM* vm) |
| 192 | : self_(nullptr), env_(nullptr), vm_(down_cast<JavaVMExt*>(vm)) {} |
| 193 | |
| 194 | // Here purely to force inlining. |
| 195 | ~ScopedObjectAccessAlreadyRunnable() ALWAYS_INLINE { |
| 196 | } |
| 197 | |
| 198 | // Self thread, can be null. |
| 199 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 200 | // The full JNIEnv. |
| 201 | JNIEnvExt* const env_; |
| 202 | // The full JavaVM. |
| 203 | JavaVMExt* const vm_; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | // Entry/exit processing for transitions from Native to Runnable (ie within JNI functions). |
| 207 | // |
| 208 | // This class performs the necessary thread state switching to and from Runnable and lets us |
| 209 | // amortize the cost of working out the current thread. Additionally it lets us check (and repair) |
| 210 | // apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects |
| 211 | // into jobjects via methods of this class. Performing this here enforces the Runnable thread state |
| 212 | // for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code |
| 213 | // is also manipulating the Object. |
| 214 | // |
| 215 | // The destructor transitions back to the previous thread state, typically Native. In this state |
| 216 | // GC and thread suspension may occur. |
| 217 | // |
| 218 | // For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of |
| 219 | // the mutator_lock_ will be acquired on construction. |
| 220 | class ScopedObjectAccessUnchecked : public ScopedObjectAccessAlreadyRunnable { |
| 221 | public: |
| 222 | explicit ScopedObjectAccessUnchecked(JNIEnv* env) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 223 | REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 224 | : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) { |
| 225 | Self()->VerifyStack(); |
| 226 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
| 227 | } |
| 228 | |
| 229 | explicit ScopedObjectAccessUnchecked(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 230 | REQUIRES(!Locks::thread_suspend_count_lock_) ALWAYS_INLINE |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 231 | : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) { |
| 232 | Self()->VerifyStack(); |
| 233 | Locks::mutator_lock_->AssertSharedHeld(Self()); |
| 234 | } |
| 235 | |
| 236 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 237 | // change into Runnable or acquire a share on the mutator_lock_. |
| 238 | explicit ScopedObjectAccessUnchecked(JavaVM* vm) ALWAYS_INLINE |
| 239 | : ScopedObjectAccessAlreadyRunnable(vm), tsc_() {} |
| 240 | |
| 241 | private: |
| 242 | // The scoped thread state change makes sure that we are runnable and restores the thread state |
| 243 | // in the destructor. |
| 244 | const ScopedThreadStateChange tsc_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 245 | |
| 246 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked); |
| 247 | }; |
| 248 | |
| 249 | // Annotalysis helping variant of the above. |
| 250 | class ScopedObjectAccess : public ScopedObjectAccessUnchecked { |
| 251 | public: |
| 252 | explicit ScopedObjectAccess(JNIEnv* env) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 253 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 254 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 255 | : ScopedObjectAccessUnchecked(env) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | explicit ScopedObjectAccess(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 259 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 260 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 261 | : ScopedObjectAccessUnchecked(self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 264 | ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | // Base class will release share of lock. Invoked after this destructor. |
| 266 | } |
| 267 | |
| 268 | private: |
| 269 | // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that |
| 270 | // routines operating with just a VM are sound, they are not, but when you have just a VM |
| 271 | // you cannot call the unsound routines. |
| 272 | explicit ScopedObjectAccess(JavaVM* vm) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 273 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 274 | : ScopedObjectAccessUnchecked(vm) {} |
| 275 | |
| 276 | friend class ScopedCheck; |
| 277 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess); |
| 278 | }; |
| 279 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 280 | // Annotalysis helper for going to a suspended state from runnable. |
| 281 | class ScopedThreadSuspension : public ValueObject { |
| 282 | public: |
| 283 | explicit ScopedThreadSuspension(Thread* self, ThreadState suspended_state) |
| 284 | REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_) |
| 285 | UNLOCK_FUNCTION(Locks::mutator_lock_) |
| 286 | ALWAYS_INLINE |
| 287 | : self_(self), suspended_state_(suspended_state) { |
| 288 | DCHECK(self_ != nullptr); |
| 289 | self_->TransitionFromRunnableToSuspended(suspended_state); |
| 290 | } |
| 291 | |
| 292 | ~ScopedThreadSuspension() SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE { |
| 293 | DCHECK_EQ(self_->GetState(), suspended_state_); |
| 294 | self_->TransitionFromSuspendedToRunnable(); |
| 295 | } |
| 296 | |
| 297 | private: |
| 298 | Thread* const self_; |
| 299 | const ThreadState suspended_state_; |
| 300 | DISALLOW_COPY_AND_ASSIGN(ScopedThreadSuspension); |
| 301 | }; |
| 302 | |
| 303 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 304 | } // namespace art |
| 305 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 306 | #endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |