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