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