< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page

 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "jvm.h"
 28 #include "classfile/javaClasses.hpp"
 29 #include "classfile/javaThreadStatus.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "jfr/jfrEvents.hpp"
 32 #include "jvmtifiles/jvmtiEnv.hpp"
 33 #include "logging/log.hpp"
 34 #include "memory/allocation.inline.hpp"
 35 #include "memory/iterator.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "runtime/atomic.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/javaThread.inline.hpp"

 41 #include "runtime/nonJavaThread.hpp"
 42 #include "runtime/orderAccess.hpp"
 43 #include "runtime/osThread.hpp"
 44 #include "runtime/safepoint.hpp"
 45 #include "runtime/safepointMechanism.inline.hpp"
 46 #include "runtime/thread.inline.hpp"
 47 #include "runtime/threadSMR.inline.hpp"
 48 #include "services/memTracker.hpp"
 49 #include "utilities/macros.hpp"
 50 #include "utilities/spinYield.hpp"
 51 #if INCLUDE_JFR
 52 #include "jfr/jfr.hpp"
 53 #endif
 54 
 55 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 56 // Current thread is maintained as a thread-local variable
 57 THREAD_LOCAL Thread* Thread::_thr_current = NULL;
 58 #endif
 59 
 60 // ======= Thread ========
 61 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
 62   return throw_excpt ? AllocateHeap(size, flags, CURRENT_PC)
 63                        : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 64 }
 65 
 66 void Thread::operator delete(void* p) {
 67   FreeHeap(p);
 68 }
 69 
 70 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 71 // JavaThread
 72 
 73 DEBUG_ONLY(Thread* Thread::_starting_thread = NULL;)
 74 
 75 Thread::Thread() {

 76 
 77   DEBUG_ONLY(_run_state = PRE_CALL_RUN;)
 78 
 79   // stack and get_thread
 80   set_stack_base(NULL);
 81   set_stack_size(0);
 82   set_lgrp_id(-1);
 83   DEBUG_ONLY(clear_suspendible_thread();)
 84 
 85   // allocated data structures
 86   set_osthread(NULL);
 87   set_resource_area(new (mtThread)ResourceArea());
 88   DEBUG_ONLY(_current_resource_mark = NULL;)
 89   set_handle_area(new (mtThread) HandleArea(NULL));
 90   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, mtClass));
 91   set_last_handle_mark(NULL);
 92   DEBUG_ONLY(_missed_ic_stub_refill_verifier = NULL);
 93 
 94   // Initial value of zero ==> never claimed.
 95   _threads_do_token = 0;

397   os::start_thread(thread);
398 }
399 
400 // GC Support
401 bool Thread::claim_par_threads_do(uintx claim_token) {
402   uintx token = _threads_do_token;
403   if (token != claim_token) {
404     uintx res = Atomic::cmpxchg(&_threads_do_token, token, claim_token);
405     if (res == token) {
406       return true;
407     }
408     guarantee(res == claim_token, "invariant");
409   }
410   return false;
411 }
412 
413 void Thread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) {
414   // Do oop for ThreadShadow
415   f->do_oop((oop*)&_pending_exception);
416   handle_area()->oops_do(f);



417 }
418 
419 // If the caller is a NamedThread, then remember, in the current scope,
420 // the given JavaThread in its _processed_thread field.
421 class RememberProcessedThread: public StackObj {
422   NamedThread* _cur_thr;
423 public:
424   RememberProcessedThread(Thread* thread) {
425     Thread* self = Thread::current();
426     if (self->is_Named_thread()) {
427       _cur_thr = (NamedThread *)self;
428       assert(_cur_thr->processed_thread() == NULL, "nesting not supported");
429       _cur_thr->set_processed_thread(thread);
430     } else {
431       _cur_thr = NULL;
432     }
433   }
434 
435   ~RememberProcessedThread() {
436     if (_cur_thr) {

524 void Thread::print_owned_locks_on(outputStream* st) const {
525   Mutex* cur = _owned_locks;
526   if (cur == NULL) {
527     st->print(" (no locks) ");
528   } else {
529     st->print_cr(" Locks owned:");
530     while (cur) {
531       cur->print_on(st);
532       cur = cur->next();
533     }
534   }
535 }
536 #endif // ASSERT
537 
538 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
539 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
540 // used for compilation in the future. If that change is made, the need for these methods
541 // should be revisited, and they should be removed if possible.
542 
543 bool Thread::is_lock_owned(address adr) const {
544   return is_in_full_stack(adr);

545 }
546 
547 bool Thread::set_as_starting_thread() {
548   assert(_starting_thread == NULL, "already initialized: "
549          "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
550   // NOTE: this must be called inside the main thread.
551   DEBUG_ONLY(_starting_thread = this;)
552   return os::create_main_thread(JavaThread::cast(this));
553 }
554 
555 // Ad-hoc mutual exclusion primitives: SpinLock
556 //
557 // We employ SpinLocks _only for low-contention, fixed-length
558 // short-duration critical sections where we're concerned
559 // about native mutex_t or HotSpot Mutex:: latency.
560 //
561 // TODO-FIXME: ListLock should be of type SpinLock.
562 // We should make this a 1st-class type, integrated into the lock
563 // hierarchy as leaf-locks.  Critically, the SpinLock structure
564 // should have sufficient padding to avoid false-sharing and excessive

 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "jvm.h"
 28 #include "classfile/javaClasses.hpp"
 29 #include "classfile/javaThreadStatus.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "jfr/jfrEvents.hpp"
 32 #include "jvmtifiles/jvmtiEnv.hpp"
 33 #include "logging/log.hpp"
 34 #include "memory/allocation.inline.hpp"
 35 #include "memory/iterator.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "runtime/atomic.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/javaThread.inline.hpp"
 41 #include "runtime/lockStack.inline.hpp"
 42 #include "runtime/nonJavaThread.hpp"
 43 #include "runtime/orderAccess.hpp"
 44 #include "runtime/osThread.hpp"
 45 #include "runtime/safepoint.hpp"
 46 #include "runtime/safepointMechanism.inline.hpp"
 47 #include "runtime/thread.inline.hpp"
 48 #include "runtime/threadSMR.inline.hpp"
 49 #include "services/memTracker.hpp"
 50 #include "utilities/macros.hpp"
 51 #include "utilities/spinYield.hpp"
 52 #if INCLUDE_JFR
 53 #include "jfr/jfr.hpp"
 54 #endif
 55 
 56 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 57 // Current thread is maintained as a thread-local variable
 58 THREAD_LOCAL Thread* Thread::_thr_current = NULL;
 59 #endif
 60 
 61 // ======= Thread ========
 62 void* Thread::allocate(size_t size, bool throw_excpt, MEMFLAGS flags) {
 63   return throw_excpt ? AllocateHeap(size, flags, CURRENT_PC)
 64                        : AllocateHeap(size, flags, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 65 }
 66 
 67 void Thread::operator delete(void* p) {
 68   FreeHeap(p);
 69 }
 70 
 71 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 72 // JavaThread
 73 
 74 DEBUG_ONLY(Thread* Thread::_starting_thread = NULL;)
 75 
 76 Thread::Thread():
 77   _lock_stack() {
 78 
 79   DEBUG_ONLY(_run_state = PRE_CALL_RUN;)
 80 
 81   // stack and get_thread
 82   set_stack_base(NULL);
 83   set_stack_size(0);
 84   set_lgrp_id(-1);
 85   DEBUG_ONLY(clear_suspendible_thread();)
 86 
 87   // allocated data structures
 88   set_osthread(NULL);
 89   set_resource_area(new (mtThread)ResourceArea());
 90   DEBUG_ONLY(_current_resource_mark = NULL;)
 91   set_handle_area(new (mtThread) HandleArea(NULL));
 92   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(30, mtClass));
 93   set_last_handle_mark(NULL);
 94   DEBUG_ONLY(_missed_ic_stub_refill_verifier = NULL);
 95 
 96   // Initial value of zero ==> never claimed.
 97   _threads_do_token = 0;

399   os::start_thread(thread);
400 }
401 
402 // GC Support
403 bool Thread::claim_par_threads_do(uintx claim_token) {
404   uintx token = _threads_do_token;
405   if (token != claim_token) {
406     uintx res = Atomic::cmpxchg(&_threads_do_token, token, claim_token);
407     if (res == token) {
408       return true;
409     }
410     guarantee(res == claim_token, "invariant");
411   }
412   return false;
413 }
414 
415 void Thread::oops_do_no_frames(OopClosure* f, CodeBlobClosure* cf) {
416   // Do oop for ThreadShadow
417   f->do_oop((oop*)&_pending_exception);
418   handle_area()->oops_do(f);
419   if (!UseHeavyMonitors) {
420     lock_stack().oops_do(f);
421   }
422 }
423 
424 // If the caller is a NamedThread, then remember, in the current scope,
425 // the given JavaThread in its _processed_thread field.
426 class RememberProcessedThread: public StackObj {
427   NamedThread* _cur_thr;
428 public:
429   RememberProcessedThread(Thread* thread) {
430     Thread* self = Thread::current();
431     if (self->is_Named_thread()) {
432       _cur_thr = (NamedThread *)self;
433       assert(_cur_thr->processed_thread() == NULL, "nesting not supported");
434       _cur_thr->set_processed_thread(thread);
435     } else {
436       _cur_thr = NULL;
437     }
438   }
439 
440   ~RememberProcessedThread() {
441     if (_cur_thr) {

529 void Thread::print_owned_locks_on(outputStream* st) const {
530   Mutex* cur = _owned_locks;
531   if (cur == NULL) {
532     st->print(" (no locks) ");
533   } else {
534     st->print_cr(" Locks owned:");
535     while (cur) {
536       cur->print_on(st);
537       cur = cur->next();
538     }
539   }
540 }
541 #endif // ASSERT
542 
543 // We had to move these methods here, because vm threads get into ObjectSynchronizer::enter
544 // However, there is a note in JavaThread::is_lock_owned() about the VM threads not being
545 // used for compilation in the future. If that change is made, the need for these methods
546 // should be revisited, and they should be removed if possible.
547 
548 bool Thread::is_lock_owned(address adr) const {
549   assert(adr != ANONYMOUS_OWNER, "must convert to lock object");
550   return !UseHeavyMonitors && lock_stack().contains(cast_to_oop(adr));
551 }
552 
553 bool Thread::set_as_starting_thread() {
554   assert(_starting_thread == NULL, "already initialized: "
555          "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
556   // NOTE: this must be called inside the main thread.
557   DEBUG_ONLY(_starting_thread = this;)
558   return os::create_main_thread(JavaThread::cast(this));
559 }
560 
561 // Ad-hoc mutual exclusion primitives: SpinLock
562 //
563 // We employ SpinLocks _only for low-contention, fixed-length
564 // short-duration critical sections where we're concerned
565 // about native mutex_t or HotSpot Mutex:: latency.
566 //
567 // TODO-FIXME: ListLock should be of type SpinLock.
568 // We should make this a 1st-class type, integrated into the lock
569 // hierarchy as leaf-locks.  Critically, the SpinLock structure
570 // should have sufficient padding to avoid false-sharing and excessive
< prev index next >