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