Lines Matching refs:thread
36 #include "thread.h"
46 thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
51 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
52 // We need to detach the current thread here in case there's another thread waiting to join with
56 Thread* self = Thread::Current();
65 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
66 // Thread::Init.
70 bool ThreadList::Contains(Thread* thread) {
71 return find(list_.begin(), list_.end(), thread) != list_.end();
75 for (const auto& thread : list_) {
76 if (thread->GetTid() == tid) {
88 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
89 for (const auto& thread : list_) {
90 os << "DUMPING THREAD " << thread->GetTid() << "\n";
91 DumpNativeStack(os, thread->GetTid(), "\t");
98 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
105 // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
107 Thread::DumpState(os, NULL, tid);
123 Thread* self = Thread::Current();
144 for (const auto& thread : list_) {
145 thread->Dump(os);
150 void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
153 for (const auto& thread : list_) {
154 if (thread != ignore1 && thread != ignore2) {
155 CHECK(thread->IsSuspended())
156 << "\nUnsuspended thread: <<" << *thread << "\n"
157 << "self: <<" << *Thread::Current();
163 // Attempt to rectify locks so that we dump thread list with required locks before exiting.
168 ss << "Thread suspend timeout\n";
176 // individual thread requires polling. delay_us is the requested sleep and total_delay_us
179 static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us) {
196 Thread* self = Thread::Current();
204 std::vector<Thread*> suspended_count_modified_threads;
207 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
211 for (const auto& thread : list_) {
212 if (thread != self) {
214 if (thread->RequestCheckpoint(checkpoint_function)) {
215 // This thread will run its checkpoint some time in the near future.
220 // The thread switched back to runnable.
221 if (thread->GetState() == kRunnable) {
225 thread->ModifySuspendCount(self, +1, false);
226 suspended_count_modified_threads.push_back(thread);
238 for (const auto& thread : suspended_count_modified_threads) {
239 if (!thread->IsSuspended()) {
240 // Wait until the thread is suspended.
245 } while (!thread->IsSuspended());
249 LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!";
252 // We know for sure that the thread is suspended at this point.
253 checkpoint_function->Run(thread);
256 thread->ModifySuspendCount(self, -1, false);
261 // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
264 Thread::resume_cond_->Broadcast(self);
274 Thread* self = Thread::Current();
284 // Call a checkpoint function for each non-suspended thread.
287 for (const auto& thread : list_) {
288 if (thread != self) {
289 if (thread->RequestCheckpoint(checkpoint_function)) {
290 // This thread will run its checkpoint some time in the near future.
302 Thread* self = Thread::Current();
307 VLOG(threads) << "Thread[null] SuspendAll starting...";
324 for (const auto& thread : list_) {
325 if (thread == self) {
328 VLOG(threads) << "requesting thread suspend: " << *thread;
329 thread->ModifySuspendCount(self, +1, false);
359 VLOG(threads) << "Thread[null] SuspendAll complete";
364 Thread* self = Thread::Current();
369 VLOG(threads) << "Thread[null] ResumeAll starting";
387 for (const auto& thread : list_) {
388 if (thread == self) {
391 thread->ModifySuspendCount(self, -1, false);
399 VLOG(threads) << "Thread[null] ResumeAll waking others";
401 Thread::resume_cond_->Broadcast(self);
408 VLOG(threads) << "Thread[null] ResumeAll complete";
412 void ThreadList::Resume(Thread* thread, bool for_debugger) {
413 Thread* self = Thread::Current();
414 DCHECK_NE(thread, self);
415 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..."
423 DCHECK(thread->IsSuspended());
424 if (!Contains(thread)) {
425 // We only expect threads within the thread-list to have been suspended otherwise we can't
427 LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
428 << ") thread not within thread list";
431 thread->ModifySuspendCount(self, -1, for_debugger);
435 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") waking others";
437 Thread::resume_cond_->Broadcast(self);
440 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") complete";
443 static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) {
457 Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension,
464 Thread* self = Thread::Current();
467 Thread* thread;
469 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
471 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
472 // than request thread suspension, to avoid potential cycles in threads requesting each other
476 thread = Thread::FromManagedThread(soa, peer);
477 if (thread == nullptr) {
478 ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer);
481 if (!Contains(thread)) {
482 VLOG(threads) << "SuspendThreadByPeer failed for unattached thread: "
483 << reinterpret_cast<void*>(thread);
486 VLOG(threads) << "SuspendThreadByPeer found thread: " << *thread;
490 thread
495 CHECK_GT(thread->GetSuspendCount(), 0);
497 // IsSuspended on the current thread will fail as the current thread is changed into
498 // Runnable above. As the suspend count is now raised if this is the current thread
500 // to just explicitly handle the current thread in the callers to this code.
501 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
502 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
503 // count, or else we've waited and it has self suspended) or is the current thread, we're
505 if (thread->IsSuspended()) {
506 VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread;
507 return thread;
510 ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer);
512 thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
520 VLOG(threads) << "SuspendThreadByPeer sleeping to allow thread chance to suspend";
529 Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension,
535 Thread* suspended_thread = nullptr;
536 Thread* self = Thread::Current();
541 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
543 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
544 // than request thread suspension, to avoid potential cycles in threads requesting each other
548 Thread* thread = nullptr;
551 thread = it;
555 if (thread == nullptr) {
556 CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
557 << " no longer in thread list";
559 ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id);
562 VLOG(threads) << "SuspendThreadByThreadId found thread: " << *thread;
563 DCHECK(Contains(thread));
567 thread->ModifySuspendCount(self, +1, debug_suspension);
568 suspended_thread = thread;
570 CHECK_EQ(suspended_thread, thread);
572 CHECK_GT(thread->GetSuspendCount(), 0);
574 // IsSuspended on the current thread will fail as the current thread is changed into
575 // Runnable above. As the suspend count is now raised if this is the current thread
577 // to just explicitly handle the current thread in the callers to this code.
578 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
579 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
580 // count, or else we've waited and it has self suspended) or is the current thread, we're
582 if (thread->IsSuspended()) {
583 VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread;
584 return thread;
587 ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id);
589 thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
597 VLOG(threads) << "SuspendThreadByThreadId sleeping to allow thread chance to suspend";
602 Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) {
603 Thread* self = Thread::Current();
605 for (const auto& thread : list_) {
606 if (thread->GetThreadId() == thin_lock_id) {
607 CHECK(thread == self || thread->IsSuspended());
608 return thread;
615 Thread* self = Thread::Current();
616 Thread* debug_thread = Dbg::GetDebugThread();
628 for (const auto& thread : list_) {
629 if (thread == self || thread == debug_thread) {
632 VLOG(threads) << "requesting thread suspend: " << *thread;
633 thread->ModifySuspendCount(self, +1, true);
657 Thread* self = Thread::Current();
659 // The debugger thread must not suspend itself due to debugger activity!
660 Thread* debug_thread = Dbg::GetDebugThread();
689 // Tell JDWP that we've completed suspension. The JDWP thread can't
697 Thread::resume_cond_->Wait(self);
702 // also happen if the debugger lets go while a SIGQUIT thread
704 // just long enough to try to grab the thread-suspend lock).
717 Thread* self = Thread::Current();
728 for (const auto& thread : list_) {
729 if (thread == self || thread->GetDebugSuspendCount() == 0) {
732 thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true);
738 Thread::resume_cond_->Broadcast(self);
745 Thread* self = Thread::Current();
757 for (const auto& thread : list_) {
758 if (thread != self && !thread->IsDaemon()) {
764 // Wait for another thread to exit before re-checking.
771 Thread* self = Thread::Current();
775 for (const auto& thread : list_) {
778 CHECK(thread->IsDaemon()) << *thread;
779 if (thread != self) {
780 thread->ModifySuspendCount(self, +1, false);
789 for (const auto& thread : list_) {
790 if (thread != self && thread->GetState() == kRunnable) {
792 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
804 void ThreadList::Register(Thread* self) {
805 DCHECK_EQ(self, Thread::Current());
813 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
830 void ThreadList::Unregister(Thread* self) {
831 DCHECK_EQ(self, Thread::Current());
836 // suspend and so on, must happen at this point, and not in ~Thread.
841 // Remove and delete the Thread* while holding the thread_list_lock_ and
842 // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
848 LOG(ERROR) << "Request to unregister unattached thread\n" << os.str();
851 // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other
852 // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount).
861 // Release the thread ID after the thread is finished and deleted to avoid cases where we can
862 // temporarily have multiple threads with the same thread id. When this occurs, it causes
866 // Clear the TLS data, so that the underlying native thread is recognizably detached.
868 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
870 // Signal that a thread just detached.
875 void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
876 for (const auto& thread : list_) {
877 callback(thread, context);
882 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
883 for (const auto& thread : list_) {
884 thread->VisitRoots(callback, arg);
904 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
905 for (const auto& thread : list_) {
906 thread->VisitRoots(VerifyRootWrapperCallback, &wrapper);
910 uint32_t ThreadList::AllocThreadId(Thread* self) {
918 LOG(FATAL) << "Out of internal thread ids";
922 void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {