Lines Matching defs:thread
48 #include "thread.h"
92 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
93 // We need to detach the current thread here in case there's another thread waiting to join with
96 Thread* self = Thread::Current();
110 heap->WaitForGcToComplete(gc::kGcCauseBackground, Thread::Current());
111 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
112 // Thread::Init.
118 bool ThreadList::Contains(Thread* thread) {
119 return find(list_.begin(), list_.end(), thread) != list_.end();
123 for (const auto& thread : list_) {
124 if (thread->GetTid() == tid) {
136 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
138 for (const auto& thread : list_) {
139 os << "DUMPING THREAD " << thread->GetTid() << "\n";
140 DumpNativeStack(os, thread->GetTid(), map.get(), "\t");
147 ScopedObjectAccess soa(Thread::Current());
162 // TODO: No thread safety analysis as DumpState with a null thread won't access fields, should
164 Thread::DumpState(os, nullptr, tid);
178 Thread* self = Thread::Current();
201 // A closure used by Thread::Dump.
206 // Avoid verifying count in case a thread doesn't end up passing through the barrier.
216 void Run(Thread* thread) override {
217 // Note thread and self may not be equal if thread was already suspended at the point of the
219 Thread* self = Thread::Current();
224 thread->Dump(local_os, dump_native_stack_, backtrace_map_.get());
235 Thread* self = Thread::Current();
257 Thread* self = Thread::Current();
278 void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
281 for (const auto& thread : list_) {
282 if (thread != ignore1 && thread != ignore2) {
283 CHECK(thread->IsSuspended())
284 << "\nUnsuspended thread: <<" << *thread << "\n"
285 << "self: <<" << *Thread::Current();
291 // Attempt to rectify locks so that we dump thread list with required locks before exiting.
293 // Increment gAborting before doing the thread list dump since we don't want any failures from
294 // AssertThreadSuspensionIsAllowable in cases where thread suspension is not allowed.
299 ss << "Thread suspend timeout\n";
310 // individual thread requires polling. delay_us is the requested sleep wait. If delay_us is 0 then
323 Thread* self = Thread::Current();
328 std::vector<Thread*> suspended_count_modified_threads;
331 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
336 for (const auto& thread : list_) {
337 if (thread != self) {
339 if (thread->RequestCheckpoint(checkpoint_function)) {
340 // This thread will run its checkpoint some time in the near future.
344 // The thread switched back to runnable.
345 if (thread->GetState() == kRunnable) {
349 bool updated = thread->ModifySuspendCount(self, +1, nullptr, SuspendReason::kInternal);
351 suspended_count_modified_threads.push_back(thread);
367 for (const auto& thread : suspended_count_modified_threads) {
368 if (!thread->IsSuspended()) {
371 thread->ShortDump(oss);
372 return std::string("Waiting for suspension of thread ") + oss.str();
374 // Busy wait until the thread is suspended.
378 } while (!thread->IsSuspended());
384 << *thread << " suspension!";
387 // We know for sure that the thread is suspended at this point.
388 checkpoint_function->Run(thread);
391 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
397 // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
400 Thread::resume_cond_->Broadcast(self);
407 Thread* self = Thread::Current();
418 for (Thread* thread : list_) {
419 if (thread != self) {
421 if (thread->RequestEmptyCheckpoint()) {
422 // This thread will run an empty checkpoint (decrement the empty checkpoint barrier)
426 runnable_thread_ids.push_back(thread->GetThreadId());
430 if (thread->GetState() != kRunnable) {
449 // Wake up the runnable threads blocked on the mutexes that another thread, which is blocked
450 // on a weak ref access, holds (indirectly blocking for weak ref access through another thread
451 // and a mutex.) This needs to be done periodically because the thread may be preempted
473 ss << "Runnable thread IDs";
486 for (Thread* thread : GetList()) {
487 uint32_t tid = thread->GetThreadId();
492 thread->ReadFlag(kEmptyCheckpointRequest)) {
493 // Found a runnable thread that hasn't responded to the empty checkpoint request.
495 thread->Dump(LOG_STREAM(FATAL_WITHOUT_ABORT),
512 // A checkpoint/suspend-all hybrid to switch thread roots from
521 Thread* self = Thread::Current();
549 std::vector<Thread*> other_threads;
555 for (const auto& thread : list_) {
556 // Set the flip function for all threads because Thread::DumpState/DumpJavaStack() (invoked by
557 // a checkpoint) may cause the flip function to be run for a runnable/suspended thread before
558 // a runnable thread runs it for itself or we run it for a suspended thread below.
559 thread->SetFlipFunction(thread_flip_visitor);
560 if (thread == self) {
563 // Resume early the threads that were runnable but are suspended just for this thread flip or
565 // runnable (both cases waiting inside Thread::TransitionFromSuspendedToRunnable), or waiting
566 // for the thread flip to end at the JNI critical section entry (kWaitingForGcThreadFlip),
567 ThreadState state = thread->GetState();
568 if ((state == kWaitingForGcThreadFlip || thread->IsTransitioningToRunnable()) &&
569 thread->GetSuspendCount() == 1) {
570 // The thread will resume right after the broadcast.
571 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
575 other_threads.push_back(thread);
578 Thread::resume_cond_->Broadcast(self);
587 for (const auto& thread : other_threads) {
588 Closure* flip_func = thread->GetFlipFunction();
590 flip_func->Run(thread);
604 for (const auto& thread : other_threads) {
605 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
608 Thread::resume_cond_->Broadcast(self);
615 Thread* self = Thread::Current();
620 VLOG(threads) << "Thread[null] SuspendAll for " << cause << " starting...";
627 // All threads are known to have suspended (but a thread may still own the mutator lock)
628 // Make sure this thread grabs exclusive access to the mutator lock and its protected data.
637 // could result in a thread suspend timeout.
665 VLOG(threads) << "Thread[null] SuspendAll complete";
670 // Debugger thread might be set to kRunnable for a short period of time after the
673 void ThreadList::SuspendAllInternal(Thread* self,
674 Thread* ignore1,
675 Thread* ignore2,
711 for (const auto& thread : list_) {
712 if (thread == ignore1 || thread == ignore2) {
715 VLOG(threads) << "requesting thread suspend: " << *thread;
716 bool updated = thread->ModifySuspendCount(self, +1, &pending_threads, reason);
719 // Must install the pending_threads counter first, then check thread->IsSuspend() and clear
720 // the counter. Otherwise there's a race with Thread::TransitionFromRunnableToSuspended()
721 // that can lead a thread to miss a call to PassActiveSuspendBarriers().
722 if (thread->IsSuspended()) {
723 // Only clear the counter for the current thread.
724 thread->ClearSuspendBarrier(&pending_threads);
752 for (const auto& thread : list_) {
753 if (thread == ignore1 || thread == ignore2) {
756 if (!thread->IsSuspended()) {
757 oss << std::endl << "Thread not suspended: " << *thread;
780 Thread* self = Thread::Current();
785 VLOG(threads) << "Thread[null] ResumeAll starting";
806 for (const auto& thread : list_) {
807 if (thread == self) {
810 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
819 VLOG(threads) << "Thread[null] ResumeAll waking others";
821 Thread::resume_cond_->Broadcast(self);
827 VLOG(threads) << "Thread[null] ResumeAll complete";
831 bool ThreadList::Resume(Thread* thread, SuspendReason reason) {
832 // This assumes there was an ATraceBegin when we suspended the thread.
835 Thread* self = Thread::Current();
836 DCHECK_NE(thread, self);
837 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..." << reason;
844 if (UNLIKELY(!thread->IsSuspended())) {
845 LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
846 << ") thread not suspended";
849 if (!Contains(thread)) {
850 // We only expect threads within the thread-list to have been suspended otherwise we can't
852 LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
853 << ") thread not within thread list";
856 if (UNLIKELY(!thread->ModifySuspendCount(self, -1, nullptr, reason))) {
857 LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
864 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") waking others";
866 Thread
869 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") complete";
873 static void ThreadSuspendByPeerWarning(Thread* self,
890 Thread* ThreadList::SuspendThreadByPeer(jobject peer,
897 Thread* const self = Thread::Current();
898 Thread* suspended_thread = nullptr;
901 Thread* thread;
903 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
905 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
906 // than request thread suspension, to avoid potential cycles in threads requesting each other
910 thread = Thread::FromManagedThread(soa, peer);
911 if (thread == nullptr) {
914 // If we incremented the suspend count but the thread reset its peer, we need to
925 "No such thread for suspend",
929 if (!Contains(thread)) {
931 VLOG(threads) << "SuspendThreadByPeer failed for unattached thread: "
932 << reinterpret_cast<void*>(thread);
935 VLOG(threads) << "SuspendThreadByPeer found thread: " << *thread;
940 // We hold the suspend count lock but another thread is trying to suspend us. Its not
941 // safe to try to suspend another thread in case we get a cycle. Start the loop again
942 // which will allow this thread to be suspended.
946 suspended_thread = thread;
952 CHECK_GT(thread->GetSuspendCount(), 0);
954 // IsSuspended on the current thread will fail as the current thread is changed into
955 // Runnable above. As the suspend count is now raised if this is the current thread
957 // to just explicitly handle the current thread in the callers to this code.
958 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
959 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
960 // count, or else we've waited and it has self suspended) or is the current thread, we're
962 if (thread->IsSuspended()) {
963 VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread;
966 thread->GetThreadName(name);
970 return thread;
976 "Thread suspension timed out",
979 CHECK_EQ(suspended_thread, thread);
997 VLOG(threads) << "SuspendThreadByPeer waiting to allow thread chance to suspend";
1011 Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id,
1017 Thread* suspended_thread = nullptr;
1018 Thread* const self = Thread::Current();
1023 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
1025 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
1026 // than request thread suspension, to avoid potential cycles in threads requesting each other
1030 Thread* thread = nullptr;
1033 thread = it;
1037 if (thread == nullptr) {
1038 CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
1039 << " no longer in thread list";
1042 "No such thread id for suspend",
1046 VLOG(threads) << "SuspendThreadByThreadId found thread: " << *thread;
1047 DCHECK(Contains(thread));
1052 // We hold the suspend count lock but another thread is trying to suspend us. Its not
1053 // safe to try to suspend another thread in case we get a cycle. Start the loop again
1054 // which will allow this thread to be suspended.
1057 bool updated = thread->ModifySuspendCount(self, +1, nullptr, reason);
1059 suspended_thread = thread;
1061 CHECK_EQ(suspended_thread, thread);
1063 CHECK_GT(thread->GetSuspendCount(), 0);
1065 // IsSuspended on the current thread will fail as the current thread is changed into
1066 // Runnable above. As the suspend count is now raised if this is the current thread
1068 // to just explicitly handle the current thread in the callers to this code.
1069 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
1070 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
1071 // count, or else we've waited and it has self suspended) or is the current thread, we're
1073 if (thread->IsSuspended()) {
1076 thread->GetThreadName(name);
1080 VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread;
1081 return thread;
1086 "Thread suspension timed out",
1089 bool updated = thread->ModifySuspendCount(soa.Self(), -1, nullptr, reason);
1103 VLOG(threads) << "SuspendThreadByThreadId waiting to allow thread chance to suspend";
1109 Thread* ThreadList::FindThreadByThreadId(uint32_t thread_id) {
1110 for (const auto& thread : list_) {
1111 if (thread->GetThreadId() == thread_id) {
1112 return thread;
1119 Thread* self = Thread::Current();
1120 Thread* debug_thread = Dbg::GetDebugThread();
1139 // Thread 1 calls SuspendAllForDebugger, gets preempted after pulsing the mutator lock.
1140 // Thread 2 calls SuspendAll and SetStateUnsafe (perhaps from Dbg::Disconnected).
1141 // Thread 1 fails assertion that all threads are suspended due to thread 2 being in a runnable
1149 Thread* const self = Thread::Current();
1152 // The debugger thread must not suspend itself due to debugger activity!
1153 Thread* debug_thread = Dbg::GetDebugThread();
1187 // Tell JDWP that we've completed suspension. The JDWP thread can't
1195 Thread::resume_cond_->Wait(self);
1200 // also happen if the debugger lets go while a SIGQUIT thread
1202 // just long enough to try to grab the thread-suspend lock).
1216 Thread* self = Thread::Current();
1217 Thread* debug_thread = Dbg::GetDebugThread();
1243 for (const auto& thread : list_) {
1244 if (thread == self || thread == debug_thread) {
1247 if (thread->GetDebugSuspendCount() == 0) {
1248 // This thread may have been individually resumed with ThreadReference.Resume.
1251 VLOG(threads) << "requesting thread resume: " << *thread;
1252 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kForDebugger);
1260 Thread::resume_cond_->Broadcast(self);
1267 Thread* self = Thread::Current();
1278 for (const auto& thread : list_) {
1279 if (thread == self || thread->GetDebugSuspendCount() == 0) {
1282 bool suspended = thread->ModifySuspendCount(self,
1283 -thread->GetDebugSuspendCount(),
1292 Thread::resume_cond_->Broadcast(self);
1300 Thread* self = Thread::Current();
1311 // threads access the thread list after it is deleted. TODO: This may not work for user daemon
1315 for (const auto& thread : list_) {
1316 if (thread != self && !thread->IsDaemon()) {
1325 // Wait for another thread to exit before re-checking.
1332 Thread* self = Thread::Current();
1338 for (const auto& thread : list_) {
1341 CHECK(thread->IsDaemon()) << *thread;
1342 if (thread != self) {
1343 bool updated = thread->ModifySuspendCount(self, +1, nullptr, SuspendReason::kInternal);
1349 thread->GetJniEnv()->SetFunctionsToRuntimeShutdownFunctions();
1368 for (const auto& thread : list_) {
1369 if (thread != self && thread->GetState() == kRunnable) {
1371 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
1386 void ThreadList::Register(Thread* self) {
1387 DCHECK_EQ(self, Thread::Current());
1396 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
1426 void ThreadList::Unregister(Thread* self) {
1427 DCHECK_EQ(self, Thread::Current());
1439 // suspend and so on, must happen at this point, and not in ~Thread. The self->Destroy is what
1441 // since we want the runtime to wait for the daemon threads to exit before deleting the thread
1445 // If tracing, remember thread id and name before thread exits.
1450 // Remove and delete the Thread* while holding the thread_list_lock_ and
1451 // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
1460 LOG(ERROR) << "Request to unregister unattached thread " << thread_name << "\n" << os.str();
1473 // We failed to remove the thread due to a suspend request, loop and try again.
1477 // Release the thread ID after the thread is finished and deleted to avoid cases where we can
1478 // temporarily have multiple threads with the same thread id. When this occurs, it causes
1482 // Clear the TLS data, so that the underlying native thread is recognizably detached.
1487 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, nullptr), "detach self");
1490 // Signal that a thread just detached.
1496 void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
1497 for (const auto& thread : list_) {
1498 callback(thread, context);
1503 Thread* const self = Thread::Current();
1504 std::vector<Thread*> threads_to_visit;
1510 for (Thread* thread : list_) {
1511 bool suspended = thread->ModifySuspendCount(self, +1, nullptr, SuspendReason::kInternal);
1513 if (thread == self || thread->IsSuspended()) {
1514 threads_to_visit.push_back(thread);
1516 bool resumed = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
1524 for (Thread* thread : threads_to_visit) {
1525 thread->VisitRoots(visitor, kVisitRootFlagAllRoots);
1531 for (Thread* thread : threads_to_visit) {
1532 bool updated = thread->ModifySuspendCount(self, -1, nullptr, SuspendReason::kInternal);
1539 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
1540 for (const auto& thread : list_) {
1541 thread->VisitRoots(visitor, flags);
1545 uint32_t ThreadList::AllocThreadId(Thread* self) {
1553 LOG(FATAL) << "Out of internal thread ids";
1557 void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {