1.1 --- a/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentGCThread.cpp Fri Nov 14 14:23:05 2008 -0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,314 +0,0 @@ 1.4 -/* 1.5 - * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. 1.11 - * 1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 - * version 2 for more details (a copy is included in the LICENSE file that 1.16 - * accompanied this code). 1.17 - * 1.18 - * You should have received a copy of the GNU General Public License version 1.19 - * 2 along with this work; if not, write to the Free Software Foundation, 1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 - * 1.22 - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 - * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 - * have any questions. 1.25 - * 1.26 - */ 1.27 - 1.28 -// CopyrightVersion 1.2 1.29 - 1.30 -# include "incls/_precompiled.incl" 1.31 -# include "incls/_concurrentGCThread.cpp.incl" 1.32 - 1.33 -bool ConcurrentGCThread::_should_terminate = false; 1.34 -bool ConcurrentGCThread::_has_terminated = false; 1.35 -int ConcurrentGCThread::_CGC_flag = CGC_nil; 1.36 - 1.37 -SuspendibleThreadSet ConcurrentGCThread::_sts; 1.38 - 1.39 -ConcurrentGCThread::ConcurrentGCThread() { 1.40 - _sts.initialize(); 1.41 -}; 1.42 - 1.43 -void ConcurrentGCThread::stopWorldAndDo(VoidClosure* op) { 1.44 - MutexLockerEx x(Heap_lock, 1.45 - Mutex::_no_safepoint_check_flag); 1.46 - // warning("CGC: about to try stopping world"); 1.47 - SafepointSynchronize::begin(); 1.48 - // warning("CGC: successfully stopped world"); 1.49 - op->do_void(); 1.50 - SafepointSynchronize::end(); 1.51 - // warning("CGC: successfully restarted world"); 1.52 -} 1.53 - 1.54 -void ConcurrentGCThread::safepoint_synchronize() { 1.55 - _sts.suspend_all(); 1.56 -} 1.57 - 1.58 -void ConcurrentGCThread::safepoint_desynchronize() { 1.59 - _sts.resume_all(); 1.60 -} 1.61 - 1.62 -void ConcurrentGCThread::create_and_start() { 1.63 - if (os::create_thread(this, os::cgc_thread)) { 1.64 - // XXX: need to set this to low priority 1.65 - // unless "agressive mode" set; priority 1.66 - // should be just less than that of VMThread. 1.67 - os::set_priority(this, NearMaxPriority); 1.68 - if (!_should_terminate && !DisableStartThread) { 1.69 - os::start_thread(this); 1.70 - } 1.71 - } 1.72 -} 1.73 - 1.74 -void ConcurrentGCThread::initialize_in_thread() { 1.75 - this->record_stack_base_and_size(); 1.76 - this->initialize_thread_local_storage(); 1.77 - this->set_active_handles(JNIHandleBlock::allocate_block()); 1.78 - // From this time Thread::current() should be working. 1.79 - assert(this == Thread::current(), "just checking"); 1.80 -} 1.81 - 1.82 -void ConcurrentGCThread::wait_for_universe_init() { 1.83 - MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); 1.84 - while (!is_init_completed() && !_should_terminate) { 1.85 - CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200); 1.86 - } 1.87 -} 1.88 - 1.89 -void ConcurrentGCThread::terminate() { 1.90 - // Signal that it is terminated 1.91 - { 1.92 - MutexLockerEx mu(Terminator_lock, 1.93 - Mutex::_no_safepoint_check_flag); 1.94 - _has_terminated = true; 1.95 - Terminator_lock->notify(); 1.96 - } 1.97 - 1.98 - // Thread destructor usually does this.. 1.99 - ThreadLocalStorage::set_thread(NULL); 1.100 -} 1.101 - 1.102 - 1.103 -void SuspendibleThreadSet::initialize_work() { 1.104 - MutexLocker x(STS_init_lock); 1.105 - if (!_initialized) { 1.106 - _m = new Monitor(Mutex::leaf, 1.107 - "SuspendibleThreadSetLock", true); 1.108 - _async = 0; 1.109 - _async_stop = false; 1.110 - _async_stopped = 0; 1.111 - _initialized = true; 1.112 - } 1.113 -} 1.114 - 1.115 -void SuspendibleThreadSet::join() { 1.116 - initialize(); 1.117 - MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 1.118 - while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag); 1.119 - _async++; 1.120 - assert(_async > 0, "Huh."); 1.121 -} 1.122 - 1.123 -void SuspendibleThreadSet::leave() { 1.124 - assert(_initialized, "Must be initialized."); 1.125 - MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 1.126 - _async--; 1.127 - assert(_async >= 0, "Huh."); 1.128 - if (_async_stop) _m->notify_all(); 1.129 -} 1.130 - 1.131 -void SuspendibleThreadSet::yield(const char* id) { 1.132 - assert(_initialized, "Must be initialized."); 1.133 - if (_async_stop) { 1.134 - MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 1.135 - if (_async_stop) { 1.136 - _async_stopped++; 1.137 - assert(_async_stopped > 0, "Huh."); 1.138 - if (_async_stopped == _async) { 1.139 - if (ConcGCYieldTimeout > 0) { 1.140 - double now = os::elapsedTime(); 1.141 - guarantee((now - _suspend_all_start) * 1000.0 < 1.142 - (double)ConcGCYieldTimeout, 1.143 - "Long delay; whodunit?"); 1.144 - } 1.145 - } 1.146 - _m->notify_all(); 1.147 - while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag); 1.148 - _async_stopped--; 1.149 - assert(_async >= 0, "Huh"); 1.150 - _m->notify_all(); 1.151 - } 1.152 - } 1.153 -} 1.154 - 1.155 -void SuspendibleThreadSet::suspend_all() { 1.156 - initialize(); // If necessary. 1.157 - if (ConcGCYieldTimeout > 0) { 1.158 - _suspend_all_start = os::elapsedTime(); 1.159 - } 1.160 - MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 1.161 - assert(!_async_stop, "Only one at a time."); 1.162 - _async_stop = true; 1.163 - while (_async_stopped < _async) _m->wait(Mutex::_no_safepoint_check_flag); 1.164 -} 1.165 - 1.166 -void SuspendibleThreadSet::resume_all() { 1.167 - assert(_initialized, "Must be initialized."); 1.168 - MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 1.169 - assert(_async_stopped == _async, "Huh."); 1.170 - _async_stop = false; 1.171 - _m->notify_all(); 1.172 -} 1.173 - 1.174 -static void _sltLoop(JavaThread* thread, TRAPS) { 1.175 - SurrogateLockerThread* slt = (SurrogateLockerThread*)thread; 1.176 - slt->loop(); 1.177 -} 1.178 - 1.179 -SurrogateLockerThread::SurrogateLockerThread() : 1.180 - JavaThread(&_sltLoop), 1.181 - _monitor(Mutex::nonleaf, "SLTMonitor"), 1.182 - _buffer(empty) 1.183 -{} 1.184 - 1.185 -SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) { 1.186 - klassOop k = 1.187 - SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(), 1.188 - true, CHECK_NULL); 1.189 - instanceKlassHandle klass (THREAD, k); 1.190 - instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL); 1.191 - 1.192 - const char thread_name[] = "Surrogate Locker Thread (CMS)"; 1.193 - Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL); 1.194 - 1.195 - // Initialize thread_oop to put it into the system threadGroup 1.196 - Handle thread_group (THREAD, Universe::system_thread_group()); 1.197 - JavaValue result(T_VOID); 1.198 - JavaCalls::call_special(&result, thread_oop, 1.199 - klass, 1.200 - vmSymbolHandles::object_initializer_name(), 1.201 - vmSymbolHandles::threadgroup_string_void_signature(), 1.202 - thread_group, 1.203 - string, 1.204 - CHECK_NULL); 1.205 - 1.206 - SurrogateLockerThread* res; 1.207 - { 1.208 - MutexLocker mu(Threads_lock); 1.209 - res = new SurrogateLockerThread(); 1.210 - 1.211 - // At this point it may be possible that no osthread was created for the 1.212 - // JavaThread due to lack of memory. We would have to throw an exception 1.213 - // in that case. However, since this must work and we do not allow 1.214 - // exceptions anyway, check and abort if this fails. 1.215 - if (res == NULL || res->osthread() == NULL) { 1.216 - vm_exit_during_initialization("java.lang.OutOfMemoryError", 1.217 - "unable to create new native thread"); 1.218 - } 1.219 - java_lang_Thread::set_thread(thread_oop(), res); 1.220 - java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 1.221 - java_lang_Thread::set_daemon(thread_oop()); 1.222 - 1.223 - res->set_threadObj(thread_oop()); 1.224 - Threads::add(res); 1.225 - Thread::start(res); 1.226 - } 1.227 - os::yield(); // This seems to help with initial start-up of SLT 1.228 - return res; 1.229 -} 1.230 - 1.231 -void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) { 1.232 - MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag); 1.233 - assert(_buffer == empty, "Should be empty"); 1.234 - assert(msg != empty, "empty message"); 1.235 - _buffer = msg; 1.236 - while (_buffer != empty) { 1.237 - _monitor.notify(); 1.238 - _monitor.wait(Mutex::_no_safepoint_check_flag); 1.239 - } 1.240 -} 1.241 - 1.242 -// ======= Surrogate Locker Thread ============= 1.243 - 1.244 -void SurrogateLockerThread::loop() { 1.245 - BasicLock pll_basic_lock; 1.246 - SLT_msg_type msg; 1.247 - debug_only(unsigned int owned = 0;) 1.248 - 1.249 - while (/* !isTerminated() */ 1) { 1.250 - { 1.251 - MutexLocker x(&_monitor); 1.252 - // Since we are a JavaThread, we can't be here at a safepoint. 1.253 - assert(!SafepointSynchronize::is_at_safepoint(), 1.254 - "SLT is a JavaThread"); 1.255 - // wait for msg buffer to become non-empty 1.256 - while (_buffer == empty) { 1.257 - _monitor.notify(); 1.258 - _monitor.wait(); 1.259 - } 1.260 - msg = _buffer; 1.261 - } 1.262 - switch(msg) { 1.263 - case acquirePLL: { 1.264 - instanceRefKlass::acquire_pending_list_lock(&pll_basic_lock); 1.265 - debug_only(owned++;) 1.266 - break; 1.267 - } 1.268 - case releaseAndNotifyPLL: { 1.269 - assert(owned > 0, "Don't have PLL"); 1.270 - instanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock); 1.271 - debug_only(owned--;) 1.272 - break; 1.273 - } 1.274 - case empty: 1.275 - default: { 1.276 - guarantee(false,"Unexpected message in _buffer"); 1.277 - break; 1.278 - } 1.279 - } 1.280 - { 1.281 - MutexLocker x(&_monitor); 1.282 - // Since we are a JavaThread, we can't be here at a safepoint. 1.283 - assert(!SafepointSynchronize::is_at_safepoint(), 1.284 - "SLT is a JavaThread"); 1.285 - _buffer = empty; 1.286 - _monitor.notify(); 1.287 - } 1.288 - } 1.289 - assert(!_monitor.owned_by_self(), "Should unlock before exit."); 1.290 -} 1.291 - 1.292 - 1.293 -// ===== STS Access From Outside CGCT ===== 1.294 - 1.295 -void ConcurrentGCThread::stsYield(const char* id) { 1.296 - assert( Thread::current()->is_ConcurrentGC_thread(), 1.297 - "only a conc GC thread can call this" ); 1.298 - _sts.yield(id); 1.299 -} 1.300 - 1.301 -bool ConcurrentGCThread::stsShouldYield() { 1.302 - assert( Thread::current()->is_ConcurrentGC_thread(), 1.303 - "only a conc GC thread can call this" ); 1.304 - return _sts.should_yield(); 1.305 -} 1.306 - 1.307 -void ConcurrentGCThread::stsJoin() { 1.308 - assert( Thread::current()->is_ConcurrentGC_thread(), 1.309 - "only a conc GC thread can call this" ); 1.310 - _sts.join(); 1.311 -} 1.312 - 1.313 -void ConcurrentGCThread::stsLeave() { 1.314 - assert( Thread::current()->is_ConcurrentGC_thread(), 1.315 - "only a conc GC thread can call this" ); 1.316 - _sts.leave(); 1.317 -}