Home | History | Annotate | Download | only in runtime

Lines Matching defs:thread

19 #include "thread.h"
67 #include "thread-inl.h"
76 bool Thread::is_started_ = false;
77 pthread_key_t Thread::pthread_key_self_;
78 ConditionVariable* Thread::resume_cond_ = nullptr;
79 const size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA);
81 static const char* kThreadNameDuringStartup = "<native thread without managed peer>";
83 void Thread::InitCardTable() {
94 void Thread::InitTlsEntryPoints() {
106 void Thread::ResetQuickAllocEntryPointsForThread() {
110 void Thread::SetDeoptimizationShadowFrame(ShadowFrame* sf) {
114 void Thread::SetDeoptimizationReturnValue(const JValue& ret_val) {
118 ShadowFrame* Thread::GetAndClearDeoptimizationShadowFrame(JValue* ret_val) {
125 void Thread::SetShadowFrameUnderConstruction(ShadowFrame* sf) {
130 void Thread::ClearShadowFrameUnderConstruction() {
135 void Thread::InitTid() {
139 void Thread::InitAfterFork() {
140 // One thread (us) survived the fork, but we have a new tid so we need to
141 // update the value stashed in this Thread*.
145 void* Thread::CreateCallback(void* arg) {
146 Thread* self = reinterpret_cast<Thread*>(arg);
149 LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self;
153 // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true
173 // Invoke the 'run' method of our java.lang.Thread.
184 Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
187 Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetLong(thread_peer)));
199 Thread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa,
227 stack_size += Thread::kStackOverflowImplicitCheckSize +
251 void Thread::InstallImplicitProtection() {
285 void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) {
287 Thread* self = static_cast<JNIEnvExt*>(env)->self;
290 // Atomically start the birth of the thread ensuring the runtime isn't shutting down.
302 env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown");
306 Thread* child_thread = new Thread(is_daemon);
307 // Use global JNI ref to hold peer live while child thread starts.
311 // Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing to
318 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
321 int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread);
322 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
330 // Manually delete the global reference since Thread::Init will not have been run.
335 // TODO: remove from thread group?
346 void Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) {
347 // This function does all the initialization that must be run by the native thread it applies to.
348 // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so
349 // we can handshake with the corresponding native thread when it's ready.) Check this native
350 // thread hasn't been through here already...
351 CHECK(Thread::Current() == nullptr);
358 // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this
359 // avoids pthread_self_ ever being invalid when discovered from Thread::Current().
362 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self");
363 DCHECK_EQ(Thread::Current(), this);
372 Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group,
374 Thread* self;
377 LOG(ERROR) << "Thread attaching to non-existent runtime: " << thread_name;
383 LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name;
387 self = new Thread(as_daemon);
396 // If we're the main thread, ClassLinker won't be created until after we're attached,
397 // so that thread needs a two-stage attach. Regular threads don't need this hack.
408 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
415 void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) {
442 Thread* self = this;
443 DCHECK_EQ(self, Thread::Current());
451 // The Thread constructor should have set the Thread.name to a
469 void Thread::InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group,
481 void Thread::SetThreadName(const char* name) {
487 void Thread::InitStackHwm() {
493 // This is included in the SIGQUIT output, but it's useful here for thread debugging.
504 // be 8+4+4 = 16K. The thread won't be able to do much with this stack even the GC takes
509 LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << read_stack_size
515 // If we're the main thread, check whether we were run with an unlimited stack. In that case,
552 // The thread might have protected region at the bottom. We need
568 void Thread::ShortDump(std::ostream& os) const {
569 os << "Thread[";
576 << ",Thread*=" << this
582 void Thread::Dump(std::ostream& os) const {
587 mirror::String* Thread::GetThreadName(const ScopedObjectAccessAlreadyRunnable& soa) const {
592 void Thread::GetThreadName(std::string& name) const {
596 uint64_t Thread::GetCpuMicroTime() const {
609 // Attempt to rectify locks so that we dump thread list with required locks before exiting.
610 static void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS {
611 LOG(ERROR) << *thread << " suspend count already zero.";
616 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
622 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
630 void Thread::ModifySuspendCount(Thread* self, int delta, bool for_debugger) {
658 void Thread::RunCheckpointFunction() {
688 bool Thread::RequestCheckpoint(Closure* function) {
692 return false; // Fail, thread is suspended and so can't run a checkpoint.
718 // The thread changed state before the checkpoint was installed.
728 void Thread::FullSuspendCheck() {
731 // Make thread appear suspended to other threads, release mutator_lock_.
739 void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) {
743 Thread* self = Thread::Current();
747 if (gAborting == 0 && self != nullptr && thread != nullptr && thread->tlsPtr_.opeer != nullptr) {
750 ->GetInt(thread->tlsPtr_.opeer);
752 ->GetBoolean(thread->tlsPtr_.opeer);
755 soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(thread->tlsPtr_.opeer);
773 if (thread != nullptr) {
774 os << '"' << *thread->tlsPtr_.name << '"';
779 << " tid=" << thread->GetThreadId()
780 << " " << thread->GetState();
781 if (thread->IsStillStarting()) {
791 if (thread != nullptr) {
794 << " sCount=" << thread->tls32_.suspend_count
795 << " dsCount=" << thread->tls32_.debug_suspend_count
796 << " obj=" << reinterpret_cast<void*>(thread->tlsPtr_.opeer)
797 << " self=" << reinterpret_cast<const void*>(thread) << "\n";
803 if (thread != nullptr) {
806 CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp),
809 << " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self);
813 // Grab the scheduler stats for this thread.
833 if (thread != nullptr) {
834 os << " | stack=" << reinterpret_cast<void*>(thread->tlsPtr_.stack_begin) << "-"
835 << reinterpret_cast<void*>(thread->tlsPtr_.stack_end) << " stackSize="
836 << PrettySize(thread->tlsPtr_.stack_size) << "\n";
841 BaseMutex* mutex = thread->GetHeldMutex(static_cast<LockLevel>(i));
859 void Thread::DumpState(std::ostream& os) const {
860 Thread::DumpState(os, this, GetTid());
864 StackDumpVisitor(std::ostream& os, Thread* thread, Context* context, bool can_allocate)
866 : StackVisitor(thread, context), os(os), thread(thread), can_allocate(can_allocate),
910 Monitor::DescribeWait(os, thread);
930 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
932 // current thread, which isn't safe if this is the only runnable thread.
943 const Thread* thread;
951 static bool ShouldShowNativeStack(const Thread* thread)
953 ThreadState state = thread->GetState();
960 // In an Object.wait variant or Thread.sleep? That's not interesting.
966 const ManagedStack* managed_stack = thread->GetManagedStack();
975 // thread-startup states if it's early enough in their life cycle (http://b/7432159).
976 mirror::ArtMethod* current_method = thread->GetCurrentMethod(nullptr);
980 void Thread::DumpJavaStack(std::ostream& os) const {
982 StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(),
987 void Thread::DumpStack(std::ostream& os) const {
988 // TODO: we call this code when dying but may not have suspended the thread ourself. The
992 bool safe_to_dump = (this == Thread::Current() || IsSuspended());
995 // thread's stack in debug builds where we'll hit the not suspended check in the stack walk.
1006 os << "Not able to dump stack of thread that isn't suspended";
1010 void Thread::ThreadExitCallback(void* arg) {
1011 Thread* self = reinterpret_cast<Thread*>(arg);
1013 LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's "
1016 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self");
1019 LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self;
1023 void Thread::Startup() {
1029 // Note we use nullptr for the thread because Thread::Current can
1031 // Thread::pthread_key_self_ is not yet initialized.
1034 resume_cond_ = new ConditionVariable("Thread resumption condition variable",
1039 CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key");
1047 void Thread::FinishStartup() {
1051 // Finish attaching the main thread.
1052 ScopedObjectAccess soa(Thread::Current());
1053 Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup());
1058 void Thread::Shutdown() {
1061 CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key");
1062 MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_);
1069 Thread::Thread(bool daemon) : tls32_(daemon), wait_monitor_(nullptr), interrupted_(false) {
1070 wait_mutex_ = new Mutex("a thread wait mutex");
1071 wait_cond_ = new ConditionVariable("a thread wait condition variable", *wait_mutex_);
1078 CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread);
1090 bool Thread::IsStillStarting() const {
1091 // You might think you can check whether the state is kStarting, but for much of thread startup,
1092 // the thread is in kNative; it might also be in kVmWait.
1095 // It turns out that the last thing to change is the thread name; that's a good proxy for "has
1096 // this thread _ever_ entered kRunnable".
1101 void Thread::AssertNoPendingException() const {
1103 ScopedObjectAccess soa(Thread::Current());
1109 void Thread::AssertNoPendingExceptionForNewException(const char* msg) const {
1111 ScopedObjectAccess soa(Thread::Current());
1121 Thread* self = reinterpret_cast<Thread*>(arg);
1126 << " left locked by native thread "
1127 << *Thread::Current() << " which is detaching";
1132 void Thread::Destroy() {
1133 Thread* self = this;
1134 DCHECK_EQ(self, Thread::Current());
1152 // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone
1156 // (This conditional is only needed for tests, where Thread.lock won't have been set.)
1165 // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
1171 Thread::~Thread() {
1179 bool initialized = (tlsPtr_.jni_env != nullptr); // Did Thread::Init run?
1190 // We may be deleting a still born thread.
1216 void Thread::HandleUncaughtExceptions(ScopedObjectAccess& soa) {
1227 // If the thread has its own handler, use that.
1232 // Otherwise use the thread group's default handler.
1246 void Thread::RemoveFromThreadGroup(ScopedObjectAccess& soa) {
1261 size_t Thread::NumHandleReferences() {
1269 bool Thread::HandleScopeContains(jobject obj) const {
1281 void Thread::HandleScopeVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id) {
1297 mirror::Object* Thread::DecodeJObject(jobject obj) const {
1322 result = vm->globals.SynchronizedGet(const_cast<Thread*>(this), &vm->globals_lock, ref);
1325 result = Runtime::Current()->GetJavaVM()->DecodeWeakGlobal(const_cast<Thread*>(this), ref);
1338 // Implements java.lang.Thread.interrupted.
1339 bool Thread::Interrupted() {
1340 MutexLock mu(Thread::Current(), *wait_mutex_);
1346 // Implements java.lang.Thread.isInterrupted.
1347 bool Thread::IsInterrupted() {
1348 MutexLock mu(Thread::Current(), *wait_mutex_);
1352 void Thread::Interrupt(Thread* self) {
1361 void Thread::Notify() {
1362 Thread* self = Thread::Current();
1367 void Thread::NotifyLocked(Thread* self) {
1375 explicit CountStackDepthVisitor(Thread* thread)
1377 : StackVisitor(thread, nullptr),
1416 explicit BuildInternalStackTraceVisitor(Thread* self, Thread* thread, int skip_depth)
1417 : StackVisitor(thread, nullptr), self_(self),
1438 // Set the Object*s and assert that no thread suspension is now possible.
1477 Thread* const self_;
1489 jobject Thread::CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const {
1491 CountStackDepthVisitor count_visitor(const_cast<Thread*>(this));
1498 const_cast<Thread*>(this),
1512 template jobject Thread::CreateInternalStackTrace<false>(
1514 template jobject Thread::CreateInternalStackTrace<true>(
1517 jobjectArray Thread::InternalStackTraceToStackTraceElementArray(
1600 void Thread::ThrowNewExceptionF(const ThrowLocation& throw_location,
1609 void Thread::ThrowNewExceptionV(const ThrowLocation& throw_location,
1617 void Thread::ThrowNewException(const ThrowLocation& throw_location, const char* exception_class_descriptor,
1624 void Thread::ThrowNewWrappedException(const ThrowLocation& throw_location,
1627 DCHECK_EQ(this, Thread::Current());
1742 void Thread::ThrowOutOfMemoryError(const char* msg) {
1756 Thread* Thread::CurrentFromGdb() {
1757 return Thread::Current();
1760 void Thread::DumpFromGdb() const {
1772 // Explicitly instantiate 32 and 64bit thread offset dumping support.
1773 template void Thread::DumpThreadOffset<4>(std::ostream& os, uint32_t offset);
1774 template void Thread::DumpThreadOffset<8>(std::ostream& os, uint32_t offset);
1777 void Thread::DumpThreadOffset(std::ostream& os, uint32_t offset) {
1914 void Thread::QuickDeliverException() {
1915 // Get exception from thread.
1935 Context* Thread::GetLongJumpContext() {
1949 CurrentMethodVisitor(Thread* thread, Context* context, bool abort_on_error)
1951 : StackVisitor(thread, context), this_object_(nullptr), method_(nullptr), dex_pc_(0),
1972 mirror::ArtMethod* Thread::GetCurrentMethod(uint32_t* dex_pc, bool abort_on_error) const {
1973 CurrentMethodVisitor visitor(const_cast<Thread*>(this), nullptr, abort_on_error);
1981 ThrowLocation Thread::GetCurrentLocationForThrow() {
1989 bool Thread::HoldsLock(mirror::Object* object) const {
2000 ReferenceMapVisitor(Thread* thread, Context* context, const RootVisitor& visitor)
2002 : StackVisitor(thread, context), visitor_(visitor) {}
2147 void Thread::SetClassLoaderOverride(mirror::ClassLoader* class_loader_override) {
2152 void Thread::VisitRoots(RootCallback* visitor, void* arg) {
2194 // Visit roots on this thread's stack
2214 void Thread::VerifyStackImpl() {
2222 void Thread::SetStackEndForStackOverflow() {
2243 void Thread::SetTlab(byte* start, byte* end) {
2251 bool Thread::HasTlab() const {
2261 std::ostream& operator<<(std::ostream& os, const Thread& thread) {
2262 thread.ShortDump(os);
2266 void Thread::ProtectStack() {
2276 bool Thread::UnprotectStack() {