--- old/src/share/vm/gc/cms/concurrentMarkSweepThread.cpp 2016-03-01 17:46:30.739510753 -0500 +++ new/src/share/vm/gc/cms/concurrentMarkSweepThread.cpp 2016-03-01 17:46:30.600512491 -0500 @@ -42,7 +42,6 @@ ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::_cmst = NULL; CMSCollector* ConcurrentMarkSweepThread::_collector = NULL; -bool ConcurrentMarkSweepThread::_should_terminate = false; int ConcurrentMarkSweepThread::_CMS_flag = CMS_nil; volatile jint ConcurrentMarkSweepThread::_pending_yields = 0; @@ -62,54 +61,34 @@ set_name("CMS Main Thread"); - if (os::create_thread(this, os::cgc_thread)) { - // An old comment here said: "Priority should be just less - // than that of VMThread". Since the VMThread runs at - // NearMaxPriority, the old comment was inaccurate, but - // changing the default priority to NearMaxPriority-1 - // could change current behavior, so the default of - // NearMaxPriority stays in place. - // - // Note that there's a possibility of the VMThread - // starving if UseCriticalCMSThreadPriority is on. - // That won't happen on Solaris for various reasons, - // but may well happen on non-Solaris platforms. - int native_prio; - if (UseCriticalCMSThreadPriority) { - native_prio = os::java_to_os_priority[CriticalPriority]; - } else { - native_prio = os::java_to_os_priority[NearMaxPriority]; - } - os::set_native_priority(this, native_prio); + // An old comment here said: "Priority should be just less + // than that of VMThread". Since the VMThread runs at + // NearMaxPriority, the old comment was inaccurate, but + // changing the default priority to NearMaxPriority-1 + // could change current behavior, so the default of + // NearMaxPriority stays in place. + // + // Note that there's a possibility of the VMThread + // starving if UseCriticalCMSThreadPriority is on. + // That won't happen on Solaris for various reasons, + // but may well happen on non-Solaris platforms. + create_and_start(UseCriticalCMSThreadPriority ? CriticalPriority : NearMaxPriority); - if (!DisableStartThread) { - os::start_thread(this); - } - } _sltMonitor = SLT_lock; } -void ConcurrentMarkSweepThread::run() { +void ConcurrentMarkSweepThread::run_service() { assert(this == cmst(), "just checking"); - initialize_in_thread(); - // From this time Thread::current() should be working. - assert(this == Thread::current(), "just checking"); if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) { warning("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread); } - // Wait until Universe::is_fully_initialized() + { - CMSLoopCountWarn loopX("CMS::run", "waiting for " - "Universe::is_fully_initialized()", 2); MutexLockerEx x(CGC_lock, true); set_CMS_flag(CMS_cms_wants_token); - // Wait until Universe is initialized and all initialization is completed. - while (!is_init_completed() && !Universe::is_fully_initialized() && - !_should_terminate) { - CGC_lock->wait(true, 200); - loopX.tick(); - } + assert(is_init_completed() && Universe::is_fully_initialized(), "ConcurrentGCThread::run() should have waited for this."); + // Wait until the surrogate locker thread that will do // pending list locking on our behalf has been created. // We cannot start the SLT thread ourselves since we need @@ -130,20 +109,12 @@ _collector->_full_gc_cause : GCCause::_cms_concurrent_mark; _collector->collect_in_background(cause); } - assert(_should_terminate, "just checking"); + // Check that the state of any protocol for synchronization // between background (CMS) and foreground collector is "clean" // (i.e. will not potentially block the foreground collector, // requiring action by us). verify_ok_to_terminate(); - // Signal that it is terminated - { - MutexLockerEx mu(Terminator_lock, - Mutex::_no_safepoint_check_flag); - assert(_cmst == this, "Weird!"); - _cmst = NULL; - Terminator_lock->notify(); - } } #ifndef PRODUCT @@ -157,39 +128,29 @@ // create and start a new ConcurrentMarkSweep Thread for given CMS generation ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) { - if (!_should_terminate) { - assert(cmst() == NULL, "start() called twice?"); - ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector); - assert(cmst() == th, "Where did the just-created CMS thread go?"); - return th; - } - return NULL; + guarantee(_cmst == NULL, "start() called twice!"); + ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector); + assert(_cmst == th, "Where did the just-created CMS thread go?"); + return th; } void ConcurrentMarkSweepThread::stop_all() { - // it is ok to take late safepoints here, if needed - { - MutexLockerEx x(Terminator_lock); - _should_terminate = true; - } - { // Now post a notify on CGC_lock so as to nudge - // CMS thread(s) that might be slumbering in - // sleepBeforeNextCycle. - MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); - CGC_lock->notify_all(); - } - { // Now wait until (all) CMS thread(s) have exited - MutexLockerEx x(Terminator_lock); - while(cmst() != NULL) { - Terminator_lock->wait(); - } - } + assert(_cmst != NULL, "stop_all should be called after initialization"); + _cmst->stop(); +} + +void ConcurrentMarkSweepThread::stop_service() { + // Now post a notify on CGC_lock so as to nudge + // CMS thread(s) that might be slumbering in + // sleepBeforeNextCycle. + MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); + CGC_lock->notify_all(); } void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) { assert(tc != NULL, "Null ThreadClosure"); - if (_cmst != NULL) { - tc->do_thread(_cmst); + if (cmst() != NULL) { + tc->do_thread(cmst()); } assert(Universe::is_fully_initialized(), "Called too early, make sure heap is fully initialized"); @@ -202,8 +163,8 @@ } void ConcurrentMarkSweepThread::print_all_on(outputStream* st) { - if (_cmst != NULL) { - _cmst->print_on(st); + if (cmst() != NULL) { + cmst()->print_on(st); st->cr(); } if (_collector != NULL) { --- old/src/share/vm/gc/cms/concurrentMarkSweepThread.hpp 2016-03-01 17:46:31.495501302 -0500 +++ new/src/share/vm/gc/cms/concurrentMarkSweepThread.hpp 2016-03-01 17:46:31.351503102 -0500 @@ -37,8 +37,6 @@ friend class VMStructs; friend class ConcurrentMarkSweepGeneration; // XXX should remove friendship friend class CMSCollector; - public: - virtual void run(); private: static ConcurrentMarkSweepThread* _cmst; @@ -47,8 +45,6 @@ static SurrogateLockerThread::SLT_msg_type _sltBuffer; static Monitor* _sltMonitor; - static bool _should_terminate; - enum CMS_flag_type { CMS_nil = NoBits, CMS_cms_wants_token = nth_bit(0), @@ -72,6 +68,9 @@ // debugging void verify_ok_to_terminate() const PRODUCT_RETURN; + void run_service(); + void stop_service(); + public: // Constructor ConcurrentMarkSweepThread(CMSCollector* collector); @@ -86,15 +85,19 @@ static void print_all() { print_all_on(tty); } // Returns the CMS Thread - static ConcurrentMarkSweepThread* cmst() { return _cmst; } + inline static ConcurrentMarkSweepThread* cmst() { + if (_cmst != NULL && !_cmst->_has_terminated) { + return _cmst; + } + return NULL; + } + static CMSCollector* collector() { return _collector; } // Create and start the CMS Thread, or stop it on shutdown static ConcurrentMarkSweepThread* start(CMSCollector* collector); static void stop_all(); - static bool should_terminate() { return _should_terminate; } - // ConcurrentMarkSweepThread uses it's own termination protocol, not ConcurrentGCThread's: - virtual void stop() { ShouldNotReachHere(); } + static bool should_terminate() { return _cmst != NULL && _cmst->_should_terminate; } // Synchronization using CMS token static void synchronize(bool is_cms_thread); --- old/src/share/vm/gc/shared/concurrentGCThread.cpp 2016-03-01 17:46:32.779485250 -0500 +++ new/src/share/vm/gc/shared/concurrentGCThread.cpp 2016-03-01 17:46:32.598487513 -0500 @@ -37,12 +37,12 @@ _should_terminate(false), _has_terminated(false) { }; -void ConcurrentGCThread::create_and_start() { +void ConcurrentGCThread::create_and_start(ThreadPriority prio) { if (os::create_thread(this, os::cgc_thread)) { // XXX: need to set this to low priority // unless "aggressive mode" set; priority // should be just less than that of VMThread. - os::set_priority(this, NearMaxPriority); + os::set_priority(this, prio); if (!_should_terminate && !DisableStartThread) { os::start_thread(this); } @@ -88,6 +88,8 @@ // it is ok to take late safepoints here, if needed { MutexLockerEx mu(Terminator_lock); + assert(!_has_terminated, "stop should only be called once"); + assert(!_should_terminate, "stop should only be called once"); _should_terminate = true; } --- old/src/share/vm/gc/shared/concurrentGCThread.hpp 2016-03-01 17:46:33.748473136 -0500 +++ new/src/share/vm/gc/shared/concurrentGCThread.hpp 2016-03-01 17:46:33.588475136 -0500 @@ -35,8 +35,8 @@ bool volatile _should_terminate; bool _has_terminated; - // Create and start the thread (setting it's priority high.) - void create_and_start(); + // Create and start the thread (setting it's priority.) + void create_and_start(ThreadPriority prio = NearMaxPriority); // Do initialization steps in the thread: record stack base and size, // init thread local storage, set JNI handle block. @@ -48,25 +48,21 @@ // Record that the current thread is terminating, and will do more // concurrent work. void terminate(); - - // Most implementations of ConcurrentGCThread use ConcurrentGCThread's termination protocol, - // run(), and stop() methods, and provide specific implementations of run_service() and stop_service. - // Otherwise run_service() and stop_service should not be called.) - + // Do the specific GC work. Called by run() after initialization complete. - virtual void run_service() { ShouldNotReachHere(); } - + virtual void run_service() = 0; + // Shut down the specific GC work. Called by stop() as part of termination protocol. - virtual void stop_service() { ShouldNotReachHere(); } + virtual void stop_service() = 0; public: ConcurrentGCThread(); // Tester bool is_ConcurrentGC_thread() const { return true; } - + virtual void run(); - + // shutdown following termination protocol virtual void stop(); };