< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page




 274   _suspend_flags = 0;
 275 
 276   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
 277   _hashStateX = os::random();
 278   _hashStateY = 842502087;
 279   _hashStateZ = 0x8767;    // (int)(3579807591LL & 0xffff) ;
 280   _hashStateW = 273326509;
 281 
 282   _OnTrap   = 0;
 283   _Stalled  = 0;
 284   _TypeTag  = 0x2BAD;
 285 
 286   // Many of the following fields are effectively final - immutable
 287   // Note that nascent threads can't use the Native Monitor-Mutex
 288   // construct until the _MutexEvent is initialized ...
 289   // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
 290   // we might instead use a stack of ParkEvents that we could provision on-demand.
 291   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
 292   // and ::Release()
 293   _ParkEvent   = ParkEvent::Allocate(this);
 294   _SleepEvent  = ParkEvent::Allocate(this);
 295   _MuxEvent    = ParkEvent::Allocate(this);
 296 
 297 #ifdef CHECK_UNHANDLED_OOPS
 298   if (CheckUnhandledOops) {
 299     _unhandled_oops = new UnhandledOops(this);
 300   }
 301 #endif // CHECK_UNHANDLED_OOPS
 302 #ifdef ASSERT
 303   if (UseBiasedLocking) {
 304     assert(is_aligned(this, markWord::biased_lock_alignment), "forced alignment of thread object failed");
 305     assert(this == _real_malloc_address ||
 306            this == align_up(_real_malloc_address, markWord::biased_lock_alignment),
 307            "bug in forced alignment of thread objects");
 308   }
 309 #endif // ASSERT
 310 
 311   // Notify the barrier set that a thread is being created. The initial
 312   // thread is created before the barrier set is available.  The call to
 313   // BarrierSet::on_thread_create() for this thread is therefore deferred
 314   // to BarrierSet::set_barrier_set().


 439 #if INCLUDE_NMT
 440   if (_stack_base != NULL) {
 441     MemTracker::release_thread_stack(stack_end(), stack_size());
 442 #ifdef ASSERT
 443     set_stack_base(NULL);
 444 #endif
 445   }
 446 #endif // INCLUDE_NMT
 447 
 448   // deallocate data structures
 449   delete resource_area();
 450   // since the handle marks are using the handle area, we have to deallocated the root
 451   // handle mark before deallocating the thread's handle area,
 452   assert(last_handle_mark() != NULL, "check we have an element");
 453   delete last_handle_mark();
 454   assert(last_handle_mark() == NULL, "check we have reached the end");
 455 
 456   // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
 457   // We NULL out the fields for good hygiene.
 458   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;
 459   ParkEvent::Release(_SleepEvent); _SleepEvent  = NULL;
 460   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 461 
 462   delete handle_area();
 463   delete metadata_handles();
 464 
 465   // SR_handler uses this as a termination indicator -
 466   // needs to happen before os::free_thread()
 467   delete _SR_lock;
 468   _SR_lock = NULL;
 469 
 470   // osthread() can be NULL, if creation of thread failed.
 471   if (osthread() != NULL) os::free_thread(osthread());
 472 
 473   // Clear Thread::current if thread is deleting itself and it has not
 474   // already been done. This must be done before the memory is deallocated.
 475   // Needed to ensure JNI correctly detects non-attached threads.
 476   if (this == Thread::current_or_null()) {
 477     Thread::clear_thread_current();
 478   }
 479 


1683     resize_counters(0, (int) JVMCICounterSize);
1684   }
1685 #endif // INCLUDE_JVMCI
1686   _reserved_stack_activation = NULL;  // stack base not known yet
1687   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1688   _exception_pc  = 0;
1689   _exception_handler_pc = 0;
1690   _is_method_handle_return = 0;
1691   _jvmti_thread_state= NULL;
1692   _should_post_on_exceptions_flag = JNI_FALSE;
1693   _interp_only_mode    = 0;
1694   _special_runtime_exit_condition = _no_async_condition;
1695   _pending_async_exception = NULL;
1696   _thread_stat = NULL;
1697   _thread_stat = new ThreadStatistics();
1698   _jni_active_critical = 0;
1699   _pending_jni_exception_check_fn = NULL;
1700   _do_not_unlock_if_synchronized = false;
1701   _cached_monitor_info = NULL;
1702   _parker = Parker::Allocate(this);
1703 
1704   // Setup safepoint state info for this thread
1705   ThreadSafepointState::create(this);
1706 
1707   debug_only(_java_call_counter = 0);
1708 
1709   // JVMTI PopFrame support
1710   _popframe_condition = popframe_inactive;
1711   _popframe_preserved_args = NULL;
1712   _popframe_preserved_args_size = 0;
1713   _frames_to_pop_failed_realloc = 0;
1714 
1715   if (SafepointMechanism::uses_thread_local_poll()) {
1716     SafepointMechanism::initialize_header(this);
1717   }
1718 
1719   _class_to_be_initialized = NULL;
1720 
1721   pd_initialize();
1722 }
1723 


1795                                                      os::java_thread;
1796   os::create_thread(this, thr_type, stack_sz);
1797   // The _osthread may be NULL here because we ran out of memory (too many threads active).
1798   // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1799   // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1800   // the exception consists of creating the exception object & initializing it, initialization
1801   // will leave the VM via a JavaCall and then all locks must be unlocked).
1802   //
1803   // The thread is still suspended when we reach here. Thread must be explicit started
1804   // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1805   // by calling Threads:add. The reason why this is not done here, is because the thread
1806   // object must be fully initialized (take a look at JVM_Start)
1807 }
1808 
1809 JavaThread::~JavaThread() {
1810 
1811   // JSR166 -- return the parker to the free list
1812   Parker::Release(_parker);
1813   _parker = NULL;
1814 




1815   // Free any remaining  previous UnrollBlock
1816   vframeArray* old_array = vframe_array_last();
1817 
1818   if (old_array != NULL) {
1819     Deoptimization::UnrollBlock* old_info = old_array->unroll_block();
1820     old_array->set_unroll_block(NULL);
1821     delete old_info;
1822     delete old_array;
1823   }
1824 
1825   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1826   if (deferred != NULL) {
1827     // This can only happen if thread is destroyed before deoptimization occurs.
1828     assert(deferred->length() != 0, "empty array!");
1829     do {
1830       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1831       deferred->remove_at(0);
1832       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1833       delete dlv;
1834     } while (deferred->length() != 0);


3325 
3326 
3327 javaVFrame* JavaThread::last_java_vframe(RegisterMap *reg_map) {
3328   assert(reg_map != NULL, "a map must be given");
3329   frame f = last_frame();
3330   for (vframe* vf = vframe::new_vframe(&f, reg_map, this); vf; vf = vf->sender()) {
3331     if (vf->is_java_frame()) return javaVFrame::cast(vf);
3332   }
3333   return NULL;
3334 }
3335 
3336 
3337 Klass* JavaThread::security_get_caller_class(int depth) {
3338   vframeStream vfst(this);
3339   vfst.security_get_caller_frame(depth);
3340   if (!vfst.at_end()) {
3341     return vfst.method()->method_holder();
3342   }
3343   return NULL;
3344 }

























































3345 
3346 static void compiler_thread_entry(JavaThread* thread, TRAPS) {
3347   assert(thread->is_Compiler_thread(), "must be compiler thread");
3348   CompileBroker::compiler_thread_loop();
3349 }
3350 
3351 static void sweeper_thread_entry(JavaThread* thread, TRAPS) {
3352   NMethodSweeper::sweeper_loop();
3353 }
3354 
3355 // Create a CompilerThread
3356 CompilerThread::CompilerThread(CompileQueue* queue,
3357                                CompilerCounters* counters)
3358                                : JavaThread(&compiler_thread_entry) {
3359   _env   = NULL;
3360   _log   = NULL;
3361   _task  = NULL;
3362   _queue = queue;
3363   _counters = counters;
3364   _buffer_blob = NULL;




 274   _suspend_flags = 0;
 275 
 276   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
 277   _hashStateX = os::random();
 278   _hashStateY = 842502087;
 279   _hashStateZ = 0x8767;    // (int)(3579807591LL & 0xffff) ;
 280   _hashStateW = 273326509;
 281 
 282   _OnTrap   = 0;
 283   _Stalled  = 0;
 284   _TypeTag  = 0x2BAD;
 285 
 286   // Many of the following fields are effectively final - immutable
 287   // Note that nascent threads can't use the Native Monitor-Mutex
 288   // construct until the _MutexEvent is initialized ...
 289   // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
 290   // we might instead use a stack of ParkEvents that we could provision on-demand.
 291   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
 292   // and ::Release()
 293   _ParkEvent   = ParkEvent::Allocate(this);

 294   _MuxEvent    = ParkEvent::Allocate(this);
 295 
 296 #ifdef CHECK_UNHANDLED_OOPS
 297   if (CheckUnhandledOops) {
 298     _unhandled_oops = new UnhandledOops(this);
 299   }
 300 #endif // CHECK_UNHANDLED_OOPS
 301 #ifdef ASSERT
 302   if (UseBiasedLocking) {
 303     assert(is_aligned(this, markWord::biased_lock_alignment), "forced alignment of thread object failed");
 304     assert(this == _real_malloc_address ||
 305            this == align_up(_real_malloc_address, markWord::biased_lock_alignment),
 306            "bug in forced alignment of thread objects");
 307   }
 308 #endif // ASSERT
 309 
 310   // Notify the barrier set that a thread is being created. The initial
 311   // thread is created before the barrier set is available.  The call to
 312   // BarrierSet::on_thread_create() for this thread is therefore deferred
 313   // to BarrierSet::set_barrier_set().


 438 #if INCLUDE_NMT
 439   if (_stack_base != NULL) {
 440     MemTracker::release_thread_stack(stack_end(), stack_size());
 441 #ifdef ASSERT
 442     set_stack_base(NULL);
 443 #endif
 444   }
 445 #endif // INCLUDE_NMT
 446 
 447   // deallocate data structures
 448   delete resource_area();
 449   // since the handle marks are using the handle area, we have to deallocated the root
 450   // handle mark before deallocating the thread's handle area,
 451   assert(last_handle_mark() != NULL, "check we have an element");
 452   delete last_handle_mark();
 453   assert(last_handle_mark() == NULL, "check we have reached the end");
 454 
 455   // It's possible we can encounter a null _ParkEvent, etc., in stillborn threads.
 456   // We NULL out the fields for good hygiene.
 457   ParkEvent::Release(_ParkEvent); _ParkEvent   = NULL;

 458   ParkEvent::Release(_MuxEvent); _MuxEvent    = NULL;
 459 
 460   delete handle_area();
 461   delete metadata_handles();
 462 
 463   // SR_handler uses this as a termination indicator -
 464   // needs to happen before os::free_thread()
 465   delete _SR_lock;
 466   _SR_lock = NULL;
 467 
 468   // osthread() can be NULL, if creation of thread failed.
 469   if (osthread() != NULL) os::free_thread(osthread());
 470 
 471   // Clear Thread::current if thread is deleting itself and it has not
 472   // already been done. This must be done before the memory is deallocated.
 473   // Needed to ensure JNI correctly detects non-attached threads.
 474   if (this == Thread::current_or_null()) {
 475     Thread::clear_thread_current();
 476   }
 477 


1681     resize_counters(0, (int) JVMCICounterSize);
1682   }
1683 #endif // INCLUDE_JVMCI
1684   _reserved_stack_activation = NULL;  // stack base not known yet
1685   (void)const_cast<oop&>(_exception_oop = oop(NULL));
1686   _exception_pc  = 0;
1687   _exception_handler_pc = 0;
1688   _is_method_handle_return = 0;
1689   _jvmti_thread_state= NULL;
1690   _should_post_on_exceptions_flag = JNI_FALSE;
1691   _interp_only_mode    = 0;
1692   _special_runtime_exit_condition = _no_async_condition;
1693   _pending_async_exception = NULL;
1694   _thread_stat = NULL;
1695   _thread_stat = new ThreadStatistics();
1696   _jni_active_critical = 0;
1697   _pending_jni_exception_check_fn = NULL;
1698   _do_not_unlock_if_synchronized = false;
1699   _cached_monitor_info = NULL;
1700   _parker = Parker::Allocate(this);
1701   _SleepEvent  = ParkEvent::Allocate(this);
1702   // Setup safepoint state info for this thread
1703   ThreadSafepointState::create(this);
1704 
1705   debug_only(_java_call_counter = 0);
1706 
1707   // JVMTI PopFrame support
1708   _popframe_condition = popframe_inactive;
1709   _popframe_preserved_args = NULL;
1710   _popframe_preserved_args_size = 0;
1711   _frames_to_pop_failed_realloc = 0;
1712 
1713   if (SafepointMechanism::uses_thread_local_poll()) {
1714     SafepointMechanism::initialize_header(this);
1715   }
1716 
1717   _class_to_be_initialized = NULL;
1718 
1719   pd_initialize();
1720 }
1721 


1793                                                      os::java_thread;
1794   os::create_thread(this, thr_type, stack_sz);
1795   // The _osthread may be NULL here because we ran out of memory (too many threads active).
1796   // We need to throw and OutOfMemoryError - however we cannot do this here because the caller
1797   // may hold a lock and all locks must be unlocked before throwing the exception (throwing
1798   // the exception consists of creating the exception object & initializing it, initialization
1799   // will leave the VM via a JavaCall and then all locks must be unlocked).
1800   //
1801   // The thread is still suspended when we reach here. Thread must be explicit started
1802   // by creator! Furthermore, the thread must also explicitly be added to the Threads list
1803   // by calling Threads:add. The reason why this is not done here, is because the thread
1804   // object must be fully initialized (take a look at JVM_Start)
1805 }
1806 
1807 JavaThread::~JavaThread() {
1808 
1809   // JSR166 -- return the parker to the free list
1810   Parker::Release(_parker);
1811   _parker = NULL;
1812 
1813   // Return the sleep event to the free list
1814   ParkEvent::Release(_SleepEvent);
1815   _SleepEvent  = NULL;
1816 
1817   // Free any remaining  previous UnrollBlock
1818   vframeArray* old_array = vframe_array_last();
1819 
1820   if (old_array != NULL) {
1821     Deoptimization::UnrollBlock* old_info = old_array->unroll_block();
1822     old_array->set_unroll_block(NULL);
1823     delete old_info;
1824     delete old_array;
1825   }
1826 
1827   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = deferred_locals();
1828   if (deferred != NULL) {
1829     // This can only happen if thread is destroyed before deoptimization occurs.
1830     assert(deferred->length() != 0, "empty array!");
1831     do {
1832       jvmtiDeferredLocalVariableSet* dlv = deferred->at(0);
1833       deferred->remove_at(0);
1834       // individual jvmtiDeferredLocalVariableSet are CHeapObj's
1835       delete dlv;
1836     } while (deferred->length() != 0);


3327 
3328 
3329 javaVFrame* JavaThread::last_java_vframe(RegisterMap *reg_map) {
3330   assert(reg_map != NULL, "a map must be given");
3331   frame f = last_frame();
3332   for (vframe* vf = vframe::new_vframe(&f, reg_map, this); vf; vf = vf->sender()) {
3333     if (vf->is_java_frame()) return javaVFrame::cast(vf);
3334   }
3335   return NULL;
3336 }
3337 
3338 
3339 Klass* JavaThread::security_get_caller_class(int depth) {
3340   vframeStream vfst(this);
3341   vfst.security_get_caller_frame(depth);
3342   if (!vfst.at_end()) {
3343     return vfst.method()->method_holder();
3344   }
3345   return NULL;
3346 }
3347 
3348 // java.lang.Thread.sleep support
3349 // Returns true if sleep time elapsed as expected, and false
3350 // if the thread was interrupted.
3351 bool JavaThread::sleep(jlong millis) {
3352   assert(this == Thread::current(),  "thread consistency check");
3353 
3354   ParkEvent * const slp = this->_SleepEvent;
3355   // Because there can be races with thread interruption sending an unpark()
3356   // to the event, we explicitly reset it here to avoid an immediate return.
3357   // The actual interrupt state will be checked before we park().
3358   slp->reset();
3359   // Thread interruption establishes a happens-before ordering in the
3360   // Java Memory Model, so we need to ensure we synchronize with the
3361   // interrupt state.
3362   OrderAccess::fence();
3363 
3364   jlong prevtime = os::javaTimeNanos();
3365 
3366   for (;;) {
3367     // interruption has precedence over timing out
3368     if (os::is_interrupted(this, true)) {
3369       return false;
3370     }
3371 
3372     if (millis <= 0) {
3373       return true;
3374     }
3375 
3376     {
3377       ThreadBlockInVM tbivm(this);
3378       OSThreadWaitState osts(this->osthread(), false /* not Object.wait() */);
3379 
3380       this->set_suspend_equivalent();
3381       // cleared by handle_special_suspend_equivalent_condition() or
3382       // java_suspend_self() via check_and_wait_while_suspended()
3383 
3384       slp->park(millis);
3385 
3386       // were we externally suspended while we were waiting?
3387       this->check_and_wait_while_suspended();
3388     }
3389 
3390     // Update elapsed time tracking
3391     jlong newtime = os::javaTimeNanos();
3392     if (newtime - prevtime < 0) {
3393       // time moving backwards, should only happen if no monotonic clock
3394       // not a guarantee() because JVM should not abort on kernel/glibc bugs
3395       assert(!os::supports_monotonic_clock(),
3396              "unexpected time moving backwards detected in os::sleep()");
3397     } else {
3398       millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
3399     }
3400     prevtime = newtime;
3401   }
3402 }
3403 
3404 
3405 static void compiler_thread_entry(JavaThread* thread, TRAPS) {
3406   assert(thread->is_Compiler_thread(), "must be compiler thread");
3407   CompileBroker::compiler_thread_loop();
3408 }
3409 
3410 static void sweeper_thread_entry(JavaThread* thread, TRAPS) {
3411   NMethodSweeper::sweeper_loop();
3412 }
3413 
3414 // Create a CompilerThread
3415 CompilerThread::CompilerThread(CompileQueue* queue,
3416                                CompilerCounters* counters)
3417                                : JavaThread(&compiler_thread_entry) {
3418   _env   = NULL;
3419   _log   = NULL;
3420   _task  = NULL;
3421   _queue = queue;
3422   _counters = counters;
3423   _buffer_blob = NULL;


< prev index next >