Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_THREAD_INL_H_ |
| 18 | #define ART_RUNTIME_THREAD_INL_H_ |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 19 | |
| 20 | #include "thread.h" |
| 21 | |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 22 | #include "arch/instruction_set.h" |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 23 | #include "base/aborting.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 24 | #include "base/casts.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 25 | #include "base/mutex-inl.h" |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 26 | #include "base/time_utils.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 27 | #include "jni/jni_env_ext.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 28 | #include "managed_stack-inl.h" |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 29 | #include "obj_ptr.h" |
Andreas Gampe | 0c2313c | 2019-05-14 09:47:00 -0700 | [diff] [blame] | 30 | #include "suspend_reason.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 31 | #include "thread-current-inl.h" |
Mathieu Chartier | 3cf2253 | 2015-07-09 15:15:09 -0700 | [diff] [blame] | 32 | #include "thread_pool.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 36 | // Quickly access the current thread from a JNIEnv. |
| 37 | static inline Thread* ThreadForEnv(JNIEnv* env) { |
| 38 | JNIEnvExt* full_env(down_cast<JNIEnvExt*>(env)); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 39 | return full_env->GetSelf(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 42 | inline void Thread::AllowThreadSuspension() { |
| 43 | DCHECK_EQ(Thread::Current(), this); |
| 44 | if (UNLIKELY(TestAllFlags())) { |
| 45 | CheckSuspend(); |
| 46 | } |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 47 | // Invalidate the current thread's object pointers (ObjPtr) to catch possible moving GC bugs due |
| 48 | // to missing handles. |
Mathieu Chartier | 3f7f03c | 2016-09-26 11:39:52 -0700 | [diff] [blame] | 49 | PoisonObjectPointers(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | inline void Thread::CheckSuspend() { |
| 53 | DCHECK_EQ(Thread::Current(), this); |
| 54 | for (;;) { |
| 55 | if (ReadFlag(kCheckpointRequest)) { |
| 56 | RunCheckpointFunction(); |
| 57 | } else if (ReadFlag(kSuspendRequest)) { |
| 58 | FullSuspendCheck(); |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 59 | } else if (ReadFlag(kEmptyCheckpointRequest)) { |
| 60 | RunEmptyCheckpoint(); |
| 61 | } else { |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 67 | inline void Thread::CheckEmptyCheckpointFromWeakRefAccess(BaseMutex* cond_var_mutex) { |
| 68 | Thread* self = Thread::Current(); |
| 69 | DCHECK_EQ(self, this); |
| 70 | for (;;) { |
| 71 | if (ReadFlag(kEmptyCheckpointRequest)) { |
| 72 | RunEmptyCheckpoint(); |
| 73 | // Check we hold only an expected mutex when accessing weak ref. |
| 74 | if (kIsDebugBuild) { |
| 75 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
| 76 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 77 | if (held_mutex != nullptr && |
| 78 | held_mutex != Locks::mutator_lock_ && |
| 79 | held_mutex != cond_var_mutex) { |
Hiroshi Yamauchi | 8a43324 | 2017-03-07 14:39:22 -0800 | [diff] [blame] | 80 | CHECK(Locks::IsExpectedOnWeakRefAccess(held_mutex)) |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 81 | << "Holding unexpected mutex " << held_mutex->GetName() |
| 82 | << " when accessing weak ref"; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | inline void Thread::CheckEmptyCheckpointFromMutex() { |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 93 | DCHECK_EQ(Thread::Current(), this); |
| 94 | for (;;) { |
| 95 | if (ReadFlag(kEmptyCheckpointRequest)) { |
| 96 | RunEmptyCheckpoint(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 97 | } else { |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 103 | inline ThreadState Thread::SetState(ThreadState new_state) { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 104 | // Should only be used to change between suspended states. |
| 105 | // Cannot use this code to change into or from Runnable as changing to Runnable should |
| 106 | // fail if old_state_and_flags.suspend_request is true and changing from Runnable might |
| 107 | // miss passing an active suspend barrier. |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 108 | DCHECK_NE(new_state, kRunnable); |
Andreas Gampe | ef048f6 | 2014-11-25 22:12:27 -0800 | [diff] [blame] | 109 | if (kIsDebugBuild && this != Thread::Current()) { |
| 110 | std::string name; |
| 111 | GetThreadName(name); |
| 112 | LOG(FATAL) << "Thread \"" << name << "\"(" << this << " != Thread::Current()=" |
| 113 | << Thread::Current() << ") changing state to " << new_state; |
| 114 | } |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 115 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 116 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Mathieu Chartier | dc9d6a0 | 2020-03-17 12:39:02 -0700 | [diff] [blame] | 117 | CHECK_NE(old_state_and_flags.as_struct.state, kRunnable) << new_state << " " << *this << " " |
| 118 | << *Thread::Current(); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 119 | tls32_.state_and_flags.as_struct.state = new_state; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 120 | return static_cast<ThreadState>(old_state_and_flags.as_struct.state); |
| 121 | } |
| 122 | |
Mathieu Chartier | 10b218d | 2016-07-25 17:48:52 -0700 | [diff] [blame] | 123 | inline bool Thread::IsThreadSuspensionAllowable() const { |
| 124 | if (tls32_.no_thread_suspension != 0) { |
| 125 | return false; |
| 126 | } |
| 127 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 128 | if (i != kMutatorLock && |
| 129 | i != kUserCodeSuspensionLock && |
| 130 | GetHeldMutex(static_cast<LockLevel>(i)) != nullptr) { |
Mathieu Chartier | 10b218d | 2016-07-25 17:48:52 -0700 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 134 | // Thread autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we |
| 135 | // have the mutex meaning we need to do this hack. |
| 136 | auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS { |
| 137 | return tls32_.user_code_suspend_count != 0; |
| 138 | }; |
| 139 | if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) { |
| 140 | return false; |
| 141 | } |
Mathieu Chartier | 10b218d | 2016-07-25 17:48:52 -0700 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 145 | inline void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 146 | if (kIsDebugBuild) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 147 | if (gAborting == 0) { |
| 148 | CHECK_EQ(0u, tls32_.no_thread_suspension) << tlsPtr_.last_no_thread_suspension_cause; |
| 149 | } |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 150 | if (check_locks) { |
| 151 | bool bad_mutexes_held = false; |
| 152 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 153 | // We expect no locks except the mutator_lock_. User code suspension lock is OK as long as |
| 154 | // we aren't going to be held suspended due to SuspendReason::kForUserCode. |
| 155 | if (i != kMutatorLock && i != kUserCodeSuspensionLock) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 156 | BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 157 | if (held_mutex != nullptr) { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 158 | LOG(ERROR) << "holding \"" << held_mutex->GetName() |
| 159 | << "\" at point where thread suspension is expected"; |
| 160 | bad_mutexes_held = true; |
| 161 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 164 | // Make sure that if we hold the user_code_suspension_lock_ we aren't suspending due to |
| 165 | // user_code_suspend_count which would prevent the thread from ever waking up. Thread |
| 166 | // autoanalysis isn't able to understand that the GetHeldMutex(...) or AssertHeld means we |
| 167 | // have the mutex meaning we need to do this hack. |
| 168 | auto is_suspending_for_user_code = [this]() NO_THREAD_SAFETY_ANALYSIS { |
| 169 | return tls32_.user_code_suspend_count != 0; |
| 170 | }; |
| 171 | if (GetHeldMutex(kUserCodeSuspensionLock) != nullptr && is_suspending_for_user_code()) { |
| 172 | LOG(ERROR) << "suspending due to user-code while holding \"" |
| 173 | << Locks::user_code_suspension_lock_->GetName() << "\"! Thread would never " |
| 174 | << "wake up."; |
| 175 | bad_mutexes_held = true; |
| 176 | } |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 177 | if (gAborting == 0) { |
| 178 | CHECK(!bad_mutexes_held); |
| 179 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 180 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 181 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 184 | inline void Thread::TransitionToSuspendedAndRunCheckpoints(ThreadState new_state) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 185 | DCHECK_NE(new_state, kRunnable); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 186 | DCHECK_EQ(GetState(), kRunnable); |
| 187 | union StateAndFlags old_state_and_flags; |
| 188 | union StateAndFlags new_state_and_flags; |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 189 | while (true) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 190 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 191 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kCheckpointRequest) != 0)) { |
| 192 | RunCheckpointFunction(); |
| 193 | continue; |
| 194 | } |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 195 | if (UNLIKELY((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest) != 0)) { |
| 196 | RunEmptyCheckpoint(); |
| 197 | continue; |
| 198 | } |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 199 | // Change the state but keep the current flags (kCheckpointRequest is clear). |
| 200 | DCHECK_EQ((old_state_and_flags.as_struct.flags & kCheckpointRequest), 0); |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 201 | DCHECK_EQ((old_state_and_flags.as_struct.flags & kEmptyCheckpointRequest), 0); |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 202 | new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 203 | new_state_and_flags.as_struct.state = new_state; |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 204 | |
Hans Boehm | 891cb88 | 2020-07-31 12:06:58 -0700 | [diff] [blame] | 205 | // CAS the value, ensuring that prior memory operations are visible to any thread |
| 206 | // that observes that we are suspended. |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 207 | bool done = |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 208 | tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakRelease(old_state_and_flags.as_int, |
Ian Rogers | b8e087e | 2014-07-09 21:12:06 -0700 | [diff] [blame] | 209 | new_state_and_flags.as_int); |
| 210 | if (LIKELY(done)) { |
Dave Allison | 0f5f6bb | 2013-11-22 17:39:19 -0800 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 214 | } |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 215 | |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 216 | inline void Thread::PassActiveSuspendBarriers() { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 217 | while (true) { |
| 218 | uint16_t current_flags = tls32_.state_and_flags.as_struct.flags; |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 219 | if (LIKELY((current_flags & |
| 220 | (kCheckpointRequest | kEmptyCheckpointRequest | kActiveSuspendBarrier)) == 0)) { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 221 | break; |
| 222 | } else if ((current_flags & kActiveSuspendBarrier) != 0) { |
| 223 | PassActiveSuspendBarriers(this); |
| 224 | } else { |
| 225 | // Impossible |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 226 | LOG(FATAL) << "Fatal, thread transitioned into suspended without running the checkpoint"; |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 227 | } |
| 228 | } |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 231 | inline void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { |
| 232 | AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 233 | PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 8ac9c91 | 2015-10-01 15:58:41 -0700 | [diff] [blame] | 234 | DCHECK_EQ(this, Thread::Current()); |
| 235 | // Change to non-runnable state, thereby appearing suspended to the system. |
| 236 | TransitionToSuspendedAndRunCheckpoints(new_state); |
| 237 | // Mark the release of the share of the mutator_lock_. |
| 238 | Locks::mutator_lock_->TransitionFromRunnableToSuspended(this); |
| 239 | // Once suspended - check the active suspend barrier flag |
| 240 | PassActiveSuspendBarriers(); |
| 241 | } |
| 242 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 243 | inline ThreadState Thread::TransitionFromSuspendedToRunnable() { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 244 | union StateAndFlags old_state_and_flags; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 245 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 246 | int16_t old_state = old_state_and_flags.as_struct.state; |
| 247 | DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); |
| 248 | do { |
| 249 | Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 250 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 251 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 252 | if (LIKELY(old_state_and_flags.as_struct.flags == 0)) { |
| 253 | // Optimize for the return from native code case - this is the fast path. |
| 254 | // Atomically change from suspended to runnable if no suspend request pending. |
| 255 | union StateAndFlags new_state_and_flags; |
| 256 | new_state_and_flags.as_int = old_state_and_flags.as_int; |
| 257 | new_state_and_flags.as_struct.state = kRunnable; |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 258 | |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 259 | // CAS the value with a memory barrier. |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 260 | if (LIKELY(tls32_.state_and_flags.as_atomic_int.CompareAndSetWeakAcquire( |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 261 | old_state_and_flags.as_int, |
| 262 | new_state_and_flags.as_int))) { |
| 263 | // Mark the acquisition of a share of the mutator_lock_. |
| 264 | Locks::mutator_lock_->TransitionFromSuspendedToRunnable(this); |
| 265 | break; |
| 266 | } |
| 267 | } else if ((old_state_and_flags.as_struct.flags & kActiveSuspendBarrier) != 0) { |
| 268 | PassActiveSuspendBarriers(this); |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 269 | } else if ((old_state_and_flags.as_struct.flags & |
| 270 | (kCheckpointRequest | kEmptyCheckpointRequest)) != 0) { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 271 | // Impossible |
Mathieu Chartier | dabdccc | 2015-10-01 14:46:29 -0700 | [diff] [blame] | 272 | LOG(FATAL) << "Transitioning to runnable with checkpoint flag, " |
| 273 | << " flags=" << old_state_and_flags.as_struct.flags |
| 274 | << " state=" << old_state_and_flags.as_struct.state; |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 275 | } else if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 276 | // Wait while our suspend count is non-zero. |
Nicolas Geoffray | 9f5f8ac | 2016-06-29 14:39:59 +0100 | [diff] [blame] | 277 | |
| 278 | // We pass null to the MutexLock as we may be in a situation where the |
| 279 | // runtime is shutting down. Guarding ourselves from that situation |
| 280 | // requires to take the shutdown lock, which is undesirable here. |
| 281 | Thread* thread_to_pass = nullptr; |
| 282 | if (kIsDebugBuild && !IsDaemon()) { |
| 283 | // We know we can make our debug locking checks on non-daemon threads, |
| 284 | // so re-enable them on debug builds. |
| 285 | thread_to_pass = this; |
| 286 | } |
| 287 | MutexLock mu(thread_to_pass, *Locks::thread_suspend_count_lock_); |
Hiroshi Yamauchi | ee23582 | 2016-08-19 17:03:27 -0700 | [diff] [blame] | 288 | ScopedTransitioningToRunnable scoped_transitioning_to_runnable(this); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 289 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 290 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 291 | while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { |
| 292 | // Re-check when Thread::resume_cond_ is notified. |
Nicolas Geoffray | 9f5f8ac | 2016-06-29 14:39:59 +0100 | [diff] [blame] | 293 | Thread::resume_cond_->Wait(thread_to_pass); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 294 | old_state_and_flags.as_int = tls32_.state_and_flags.as_int; |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 295 | DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); |
| 296 | } |
| 297 | DCHECK_EQ(GetSuspendCount(), 0); |
| 298 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 299 | } while (true); |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 300 | // Run the flip function, if set. |
| 301 | Closure* flip_func = GetFlipFunction(); |
| 302 | if (flip_func != nullptr) { |
| 303 | flip_func->Run(this); |
| 304 | } |
| 305 | return static_cast<ThreadState>(old_state); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 308 | inline mirror::Object* Thread::AllocTlab(size_t bytes) { |
| 309 | DCHECK_GE(TlabSize(), bytes); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 310 | ++tlsPtr_.thread_local_objects; |
| 311 | mirror::Object* ret = reinterpret_cast<mirror::Object*>(tlsPtr_.thread_local_pos); |
| 312 | tlsPtr_.thread_local_pos += bytes; |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 313 | return ret; |
| 314 | } |
| 315 | |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 316 | inline bool Thread::PushOnThreadLocalAllocationStack(mirror::Object* obj) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 317 | DCHECK_LE(tlsPtr_.thread_local_alloc_stack_top, tlsPtr_.thread_local_alloc_stack_end); |
| 318 | if (tlsPtr_.thread_local_alloc_stack_top < tlsPtr_.thread_local_alloc_stack_end) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 319 | // There's room. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 320 | DCHECK_LE(reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_top) + |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 321 | sizeof(StackReference<mirror::Object>), |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 322 | reinterpret_cast<uint8_t*>(tlsPtr_.thread_local_alloc_stack_end)); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 323 | DCHECK(tlsPtr_.thread_local_alloc_stack_top->AsMirrorPtr() == nullptr); |
| 324 | tlsPtr_.thread_local_alloc_stack_top->Assign(obj); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 325 | ++tlsPtr_.thread_local_alloc_stack_top; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 326 | return true; |
| 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 331 | inline void Thread::SetThreadLocalAllocationStack(StackReference<mirror::Object>* start, |
| 332 | StackReference<mirror::Object>* end) { |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 333 | DCHECK(Thread::Current() == this) << "Should be called by self"; |
| 334 | DCHECK(start != nullptr); |
| 335 | DCHECK(end != nullptr); |
Mathieu Chartier | cb535da | 2015-01-23 13:50:03 -0800 | [diff] [blame] | 336 | DCHECK_ALIGNED(start, sizeof(StackReference<mirror::Object>)); |
| 337 | DCHECK_ALIGNED(end, sizeof(StackReference<mirror::Object>)); |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 338 | DCHECK_LT(start, end); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 339 | tlsPtr_.thread_local_alloc_stack_end = end; |
| 340 | tlsPtr_.thread_local_alloc_stack_top = start; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | inline void Thread::RevokeThreadLocalAllocationStack() { |
| 344 | if (kIsDebugBuild) { |
| 345 | // Note: self is not necessarily equal to this thread since thread may be suspended. |
| 346 | Thread* self = Thread::Current(); |
| 347 | DCHECK(this == self || IsSuspended() || GetState() == kWaitingPerformingGc) |
| 348 | << GetState() << " thread " << this << " self " << self; |
| 349 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 350 | tlsPtr_.thread_local_alloc_stack_end = nullptr; |
| 351 | tlsPtr_.thread_local_alloc_stack_top = nullptr; |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 354 | inline void Thread::PoisonObjectPointersIfDebug() { |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 355 | if (kObjPtrPoisoning) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 356 | Thread::Current()->PoisonObjectPointers(); |
| 357 | } |
| 358 | } |
| 359 | |
Hiroshi Yamauchi | 02e7f1a | 2016-10-03 15:32:01 -0700 | [diff] [blame] | 360 | inline bool Thread::ModifySuspendCount(Thread* self, |
| 361 | int delta, |
| 362 | AtomicInteger* suspend_barrier, |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 363 | SuspendReason reason) { |
Hiroshi Yamauchi | 02e7f1a | 2016-10-03 15:32:01 -0700 | [diff] [blame] | 364 | if (delta > 0 && ((kUseReadBarrier && this != self) || suspend_barrier != nullptr)) { |
| 365 | // When delta > 0 (requesting a suspend), ModifySuspendCountInternal() may fail either if |
| 366 | // active_suspend_barriers is full or we are in the middle of a thread flip. Retry in a loop. |
| 367 | while (true) { |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 368 | if (LIKELY(ModifySuspendCountInternal(self, delta, suspend_barrier, reason))) { |
Hiroshi Yamauchi | 02e7f1a | 2016-10-03 15:32:01 -0700 | [diff] [blame] | 369 | return true; |
| 370 | } else { |
| 371 | // Failure means the list of active_suspend_barriers is full or we are in the middle of a |
| 372 | // thread flip, we should release the thread_suspend_count_lock_ (to avoid deadlock) and |
| 373 | // wait till the target thread has executed or Thread::PassActiveSuspendBarriers() or the |
| 374 | // flip function. Note that we could not simply wait for the thread to change to a suspended |
| 375 | // state, because it might need to run checkpoint function before the state change or |
| 376 | // resumes from the resume_cond_, which also needs thread_suspend_count_lock_. |
| 377 | // |
| 378 | // The list of active_suspend_barriers is very unlikely to be full since more than |
| 379 | // kMaxSuspendBarriers threads need to execute SuspendAllInternal() simultaneously, and |
| 380 | // target thread stays in kRunnable in the mean time. |
| 381 | Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
| 382 | NanoSleep(100000); |
| 383 | Locks::thread_suspend_count_lock_->ExclusiveLock(self); |
| 384 | } |
| 385 | } |
| 386 | } else { |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 387 | return ModifySuspendCountInternal(self, delta, suspend_barrier, reason); |
Hiroshi Yamauchi | 02e7f1a | 2016-10-03 15:32:01 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 391 | inline ShadowFrame* Thread::PushShadowFrame(ShadowFrame* new_top_frame) { |
Nicolas Geoffray | 893147c | 2018-10-29 14:49:30 +0000 | [diff] [blame] | 392 | new_top_frame->CheckConsistentVRegs(); |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 393 | return tlsPtr_.managed_stack.PushShadowFrame(new_top_frame); |
| 394 | } |
| 395 | |
| 396 | inline ShadowFrame* Thread::PopShadowFrame() { |
| 397 | return tlsPtr_.managed_stack.PopShadowFrame(); |
| 398 | } |
| 399 | |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 400 | inline uint8_t* Thread::GetStackEndForInterpreter(bool implicit_overflow_check) const { |
| 401 | uint8_t* end = tlsPtr_.stack_end + (implicit_overflow_check |
| 402 | ? GetStackOverflowReservedBytes(kRuntimeISA) |
| 403 | : 0); |
| 404 | if (kIsDebugBuild) { |
| 405 | // In a debuggable build, but especially under ASAN, the access-checks interpreter has a |
| 406 | // potentially humongous stack size. We don't want to take too much of the stack regularly, |
| 407 | // so do not increase the regular reserved size (for compiled code etc) and only report the |
| 408 | // virtually smaller stack to the interpreter here. |
| 409 | end += GetStackOverflowReservedBytes(kRuntimeISA); |
| 410 | } |
| 411 | return end; |
| 412 | } |
| 413 | |
| 414 | inline void Thread::ResetDefaultStackEnd() { |
| 415 | // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room |
| 416 | // to throw a StackOverflowError. |
| 417 | tlsPtr_.stack_end = tlsPtr_.stack_begin + GetStackOverflowReservedBytes(kRuntimeISA); |
| 418 | } |
| 419 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 420 | } // namespace art |
| 421 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 422 | #endif // ART_RUNTIME_THREAD_INL_H_ |