ysr@777: /* coleenp@4037: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_implementation/shared/concurrentGCThread.hpp" stefank@2314: #include "oops/instanceRefKlass.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: ysr@777: // CopyrightVersion 1.2 ysr@777: ysr@777: int ConcurrentGCThread::_CGC_flag = CGC_nil; ysr@777: ysr@777: SuspendibleThreadSet ConcurrentGCThread::_sts; ysr@777: iveresov@1229: ConcurrentGCThread::ConcurrentGCThread() : iveresov@1229: _should_terminate(false), _has_terminated(false) { ysr@777: _sts.initialize(); ysr@777: }; ysr@777: ysr@777: void ConcurrentGCThread::safepoint_synchronize() { ysr@777: _sts.suspend_all(); ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::safepoint_desynchronize() { ysr@777: _sts.resume_all(); ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::create_and_start() { ysr@777: if (os::create_thread(this, os::cgc_thread)) { ysr@777: // XXX: need to set this to low priority ysr@777: // unless "agressive mode" set; priority ysr@777: // should be just less than that of VMThread. ysr@777: os::set_priority(this, NearMaxPriority); ysr@777: if (!_should_terminate && !DisableStartThread) { ysr@777: os::start_thread(this); ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::initialize_in_thread() { ysr@777: this->record_stack_base_and_size(); ysr@777: this->initialize_thread_local_storage(); ysr@777: this->set_active_handles(JNIHandleBlock::allocate_block()); ysr@777: // From this time Thread::current() should be working. ysr@777: assert(this == Thread::current(), "just checking"); ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::wait_for_universe_init() { ysr@777: MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); ysr@777: while (!is_init_completed() && !_should_terminate) { ysr@777: CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200); ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::terminate() { ysr@777: // Signal that it is terminated ysr@777: { ysr@777: MutexLockerEx mu(Terminator_lock, ysr@777: Mutex::_no_safepoint_check_flag); ysr@777: _has_terminated = true; ysr@777: Terminator_lock->notify(); ysr@777: } ysr@777: ysr@777: // Thread destructor usually does this.. ysr@777: ThreadLocalStorage::set_thread(NULL); ysr@777: } ysr@777: ysr@777: ysr@777: void SuspendibleThreadSet::initialize_work() { ysr@777: MutexLocker x(STS_init_lock); ysr@777: if (!_initialized) { ysr@777: _m = new Monitor(Mutex::leaf, ysr@777: "SuspendibleThreadSetLock", true); ysr@777: _async = 0; ysr@777: _async_stop = false; ysr@777: _async_stopped = 0; ysr@777: _initialized = true; ysr@777: } ysr@777: } ysr@777: ysr@777: void SuspendibleThreadSet::join() { ysr@777: initialize(); ysr@777: MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); ysr@777: while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag); ysr@777: _async++; ysr@777: assert(_async > 0, "Huh."); ysr@777: } ysr@777: ysr@777: void SuspendibleThreadSet::leave() { ysr@777: assert(_initialized, "Must be initialized."); ysr@777: MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); ysr@777: _async--; ysr@777: assert(_async >= 0, "Huh."); ysr@777: if (_async_stop) _m->notify_all(); ysr@777: } ysr@777: ysr@777: void SuspendibleThreadSet::yield(const char* id) { ysr@777: assert(_initialized, "Must be initialized."); ysr@777: if (_async_stop) { ysr@777: MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); ysr@777: if (_async_stop) { ysr@777: _async_stopped++; ysr@777: assert(_async_stopped > 0, "Huh."); ysr@777: if (_async_stopped == _async) { ysr@777: if (ConcGCYieldTimeout > 0) { ysr@777: double now = os::elapsedTime(); ysr@777: guarantee((now - _suspend_all_start) * 1000.0 < ysr@777: (double)ConcGCYieldTimeout, ysr@777: "Long delay; whodunit?"); ysr@777: } ysr@777: } ysr@777: _m->notify_all(); ysr@777: while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag); ysr@777: _async_stopped--; ysr@777: assert(_async >= 0, "Huh"); ysr@777: _m->notify_all(); ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void SuspendibleThreadSet::suspend_all() { ysr@777: initialize(); // If necessary. ysr@777: if (ConcGCYieldTimeout > 0) { ysr@777: _suspend_all_start = os::elapsedTime(); ysr@777: } ysr@777: MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); ysr@777: assert(!_async_stop, "Only one at a time."); ysr@777: _async_stop = true; ysr@777: while (_async_stopped < _async) _m->wait(Mutex::_no_safepoint_check_flag); ysr@777: } ysr@777: ysr@777: void SuspendibleThreadSet::resume_all() { ysr@777: assert(_initialized, "Must be initialized."); ysr@777: MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); ysr@777: assert(_async_stopped == _async, "Huh."); ysr@777: _async_stop = false; ysr@777: _m->notify_all(); ysr@777: } ysr@777: ysr@777: static void _sltLoop(JavaThread* thread, TRAPS) { ysr@777: SurrogateLockerThread* slt = (SurrogateLockerThread*)thread; ysr@777: slt->loop(); ysr@777: } ysr@777: ysr@777: SurrogateLockerThread::SurrogateLockerThread() : ysr@777: JavaThread(&_sltLoop), ysr@777: _monitor(Mutex::nonleaf, "SLTMonitor"), ysr@777: _buffer(empty) ysr@777: {} ysr@777: ysr@777: SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) { coleenp@4037: Klass* k = coleenp@2497: SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), ysr@777: true, CHECK_NULL); ysr@777: instanceKlassHandle klass (THREAD, k); ysr@777: instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL); ysr@777: tonyp@2197: const char thread_name[] = "Surrogate Locker Thread (Concurrent GC)"; ysr@777: Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL); ysr@777: ysr@777: // Initialize thread_oop to put it into the system threadGroup ysr@777: Handle thread_group (THREAD, Universe::system_thread_group()); ysr@777: JavaValue result(T_VOID); ysr@777: JavaCalls::call_special(&result, thread_oop, ysr@777: klass, coleenp@2497: vmSymbols::object_initializer_name(), coleenp@2497: vmSymbols::threadgroup_string_void_signature(), ysr@777: thread_group, ysr@777: string, ysr@777: CHECK_NULL); ysr@777: ysr@777: SurrogateLockerThread* res; ysr@777: { ysr@777: MutexLocker mu(Threads_lock); ysr@777: res = new SurrogateLockerThread(); ysr@777: ysr@777: // At this point it may be possible that no osthread was created for the ysr@777: // JavaThread due to lack of memory. We would have to throw an exception ysr@777: // in that case. However, since this must work and we do not allow ysr@777: // exceptions anyway, check and abort if this fails. ysr@777: if (res == NULL || res->osthread() == NULL) { ysr@777: vm_exit_during_initialization("java.lang.OutOfMemoryError", ysr@777: "unable to create new native thread"); ysr@777: } ysr@777: java_lang_Thread::set_thread(thread_oop(), res); ysr@777: java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); ysr@777: java_lang_Thread::set_daemon(thread_oop()); ysr@777: ysr@777: res->set_threadObj(thread_oop()); ysr@777: Threads::add(res); ysr@777: Thread::start(res); ysr@777: } ysr@777: os::yield(); // This seems to help with initial start-up of SLT ysr@777: return res; ysr@777: } ysr@777: ysr@777: void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) { ysr@777: MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag); ysr@777: assert(_buffer == empty, "Should be empty"); ysr@777: assert(msg != empty, "empty message"); johnc@3218: assert(!Heap_lock->owned_by_self(), "Heap_lock owned by requesting thread"); johnc@3218: ysr@777: _buffer = msg; ysr@777: while (_buffer != empty) { ysr@777: _monitor.notify(); ysr@777: _monitor.wait(Mutex::_no_safepoint_check_flag); ysr@777: } ysr@777: } ysr@777: ysr@777: // ======= Surrogate Locker Thread ============= ysr@777: ysr@777: void SurrogateLockerThread::loop() { ysr@777: BasicLock pll_basic_lock; ysr@777: SLT_msg_type msg; ysr@777: debug_only(unsigned int owned = 0;) ysr@777: ysr@777: while (/* !isTerminated() */ 1) { ysr@777: { ysr@777: MutexLocker x(&_monitor); ysr@777: // Since we are a JavaThread, we can't be here at a safepoint. ysr@777: assert(!SafepointSynchronize::is_at_safepoint(), ysr@777: "SLT is a JavaThread"); ysr@777: // wait for msg buffer to become non-empty ysr@777: while (_buffer == empty) { ysr@777: _monitor.notify(); ysr@777: _monitor.wait(); ysr@777: } ysr@777: msg = _buffer; ysr@777: } ysr@777: switch(msg) { ysr@777: case acquirePLL: { coleenp@4047: InstanceRefKlass::acquire_pending_list_lock(&pll_basic_lock); ysr@777: debug_only(owned++;) ysr@777: break; ysr@777: } ysr@777: case releaseAndNotifyPLL: { ysr@777: assert(owned > 0, "Don't have PLL"); coleenp@4047: InstanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock); ysr@777: debug_only(owned--;) ysr@777: break; ysr@777: } ysr@777: case empty: ysr@777: default: { ysr@777: guarantee(false,"Unexpected message in _buffer"); ysr@777: break; ysr@777: } ysr@777: } ysr@777: { ysr@777: MutexLocker x(&_monitor); ysr@777: // Since we are a JavaThread, we can't be here at a safepoint. ysr@777: assert(!SafepointSynchronize::is_at_safepoint(), ysr@777: "SLT is a JavaThread"); ysr@777: _buffer = empty; ysr@777: _monitor.notify(); ysr@777: } ysr@777: } ysr@777: assert(!_monitor.owned_by_self(), "Should unlock before exit."); ysr@777: } ysr@777: ysr@777: ysr@777: // ===== STS Access From Outside CGCT ===== ysr@777: ysr@777: void ConcurrentGCThread::stsYield(const char* id) { ysr@777: assert( Thread::current()->is_ConcurrentGC_thread(), ysr@777: "only a conc GC thread can call this" ); ysr@777: _sts.yield(id); ysr@777: } ysr@777: ysr@777: bool ConcurrentGCThread::stsShouldYield() { ysr@777: assert( Thread::current()->is_ConcurrentGC_thread(), ysr@777: "only a conc GC thread can call this" ); ysr@777: return _sts.should_yield(); ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::stsJoin() { ysr@777: assert( Thread::current()->is_ConcurrentGC_thread(), ysr@777: "only a conc GC thread can call this" ); ysr@777: _sts.join(); ysr@777: } ysr@777: ysr@777: void ConcurrentGCThread::stsLeave() { ysr@777: assert( Thread::current()->is_ConcurrentGC_thread(), ysr@777: "only a conc GC thread can call this" ); ysr@777: _sts.leave(); ysr@777: }