1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/memory/gcLocker.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,285 @@ 1.4 +/* 1.5 + * Copyright 1997-2007 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 +// The direct lock/unlock calls do not force a collection if an unlock 1.29 +// decrements the count to zero. Avoid calling these if at all possible. 1.30 + 1.31 +class GC_locker: public AllStatic { 1.32 + private: 1.33 + static volatile jint _jni_lock_count; // number of jni active instances 1.34 + static volatile jint _lock_count; // number of other active instances 1.35 + static volatile bool _needs_gc; // heap is filling, we need a GC 1.36 + // note: bool is typedef'd as jint 1.37 + static volatile bool _doing_gc; // unlock_critical() is doing a GC 1.38 + 1.39 + // Accessors 1.40 + static bool is_jni_active() { 1.41 + return _jni_lock_count > 0; 1.42 + } 1.43 + 1.44 + static void set_needs_gc() { 1.45 + assert(SafepointSynchronize::is_at_safepoint(), 1.46 + "needs_gc is only set at a safepoint"); 1.47 + _needs_gc = true; 1.48 + } 1.49 + 1.50 + static void clear_needs_gc() { 1.51 + assert_lock_strong(JNICritical_lock); 1.52 + _needs_gc = false; 1.53 + } 1.54 + 1.55 + static void jni_lock() { 1.56 + Atomic::inc(&_jni_lock_count); 1.57 + CHECK_UNHANDLED_OOPS_ONLY( 1.58 + if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; }) 1.59 + assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(), 1.60 + "locking failed"); 1.61 + } 1.62 + 1.63 + static void jni_unlock() { 1.64 + Atomic::dec(&_jni_lock_count); 1.65 + CHECK_UNHANDLED_OOPS_ONLY( 1.66 + if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; }) 1.67 + } 1.68 + 1.69 + static void jni_lock_slow(); 1.70 + static void jni_unlock_slow(); 1.71 + 1.72 + public: 1.73 + // Accessors 1.74 + static bool is_active(); 1.75 + static bool needs_gc() { return _needs_gc; } 1.76 + // Shorthand 1.77 + static bool is_active_and_needs_gc() { return is_active() && needs_gc();} 1.78 + 1.79 + // Calls set_needs_gc() if is_active() is true. Returns is_active(). 1.80 + static bool check_active_before_gc(); 1.81 + 1.82 + // Stalls the caller (who should not be in a jni critical section) 1.83 + // until needs_gc() clears. Note however that needs_gc() may be 1.84 + // set at a subsequent safepoint and/or cleared under the 1.85 + // JNICritical_lock, so the caller may not safely assert upon 1.86 + // return from this method that "!needs_gc()" since that is 1.87 + // not a stable predicate. 1.88 + static void stall_until_clear(); 1.89 + 1.90 + // Non-structured GC locking: currently needed for JNI. Use with care! 1.91 + static void lock(); 1.92 + static void unlock(); 1.93 + 1.94 + // The following two methods are used for JNI critical regions. 1.95 + // If we find that we failed to perform a GC because the GC_locker 1.96 + // was active, arrange for one as soon as possible by allowing 1.97 + // all threads in critical regions to complete, but not allowing 1.98 + // other critical regions to be entered. The reasons for that are: 1.99 + // 1) a GC request won't be starved by overlapping JNI critical 1.100 + // region activities, which can cause unnecessary OutOfMemory errors. 1.101 + // 2) even if allocation requests can still be satisfied before GC locker 1.102 + // becomes inactive, for example, in tenured generation possibly with 1.103 + // heap expansion, those allocations can trigger lots of safepointing 1.104 + // attempts (ineffective GC attempts) and require Heap_lock which 1.105 + // slow down allocations tremendously. 1.106 + // 1.107 + // Note that critical regions can be nested in a single thread, so 1.108 + // we must allow threads already in critical regions to continue. 1.109 + // 1.110 + // JNI critical regions are the only participants in this scheme 1.111 + // because they are, by spec, well bounded while in a critical region. 1.112 + // 1.113 + // Each of the following two method is split into a fast path and a slow 1.114 + // path. JNICritical_lock is only grabbed in the slow path. 1.115 + // _needs_gc is initially false and every java thread will go 1.116 + // through the fast path (which does the same thing as the slow path 1.117 + // when _needs_gc is false). When GC happens at a safepoint, 1.118 + // GC_locker::is_active() is checked. Since there is no safepoint in the 1.119 + // fast path of lock_critical() and unlock_critical(), there is no race 1.120 + // condition between the fast path and GC. After _needs_gc is set at a 1.121 + // safepoint, every thread will go through the slow path after the safepoint. 1.122 + // Since after a safepoint, each of the following two methods is either 1.123 + // entered from the method entry and falls into the slow path, or is 1.124 + // resumed from the safepoints in the method, which only exist in the slow 1.125 + // path. So when _needs_gc is set, the slow path is always taken, till 1.126 + // _needs_gc is cleared. 1.127 + static void lock_critical(JavaThread* thread); 1.128 + static void unlock_critical(JavaThread* thread); 1.129 +}; 1.130 + 1.131 + 1.132 +// A No_GC_Verifier object can be placed in methods where one assumes that 1.133 +// no garbage collection will occur. The destructor will verify this property 1.134 +// unless the constructor is called with argument false (not verifygc). 1.135 +// 1.136 +// The check will only be done in debug mode and if verifygc true. 1.137 + 1.138 +class No_GC_Verifier: public StackObj { 1.139 + friend class Pause_No_GC_Verifier; 1.140 + 1.141 + protected: 1.142 + bool _verifygc; 1.143 + unsigned int _old_invocations; 1.144 + 1.145 + public: 1.146 +#ifdef ASSERT 1.147 + No_GC_Verifier(bool verifygc = true); 1.148 + ~No_GC_Verifier(); 1.149 +#else 1.150 + No_GC_Verifier(bool verifygc = true) {} 1.151 + ~No_GC_Verifier() {} 1.152 +#endif 1.153 +}; 1.154 + 1.155 +// A Pause_No_GC_Verifier is used to temporarily pause the behavior 1.156 +// of a No_GC_Verifier object. If we are not in debug mode or if the 1.157 +// No_GC_Verifier object has a _verifygc value of false, then there 1.158 +// is nothing to do. 1.159 + 1.160 +class Pause_No_GC_Verifier: public StackObj { 1.161 + private: 1.162 + No_GC_Verifier * _ngcv; 1.163 + 1.164 + public: 1.165 +#ifdef ASSERT 1.166 + Pause_No_GC_Verifier(No_GC_Verifier * ngcv); 1.167 + ~Pause_No_GC_Verifier(); 1.168 +#else 1.169 + Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {} 1.170 + ~Pause_No_GC_Verifier() {} 1.171 +#endif 1.172 +}; 1.173 + 1.174 + 1.175 +// A No_Safepoint_Verifier object will throw an assertion failure if 1.176 +// the current thread passes a possible safepoint while this object is 1.177 +// instantiated. A safepoint, will either be: an oop allocation, blocking 1.178 +// on a Mutex or JavaLock, or executing a VM operation. 1.179 +// 1.180 +// If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier 1.181 +// 1.182 +class No_Safepoint_Verifier : public No_GC_Verifier { 1.183 + friend class Pause_No_Safepoint_Verifier; 1.184 + 1.185 + private: 1.186 + bool _activated; 1.187 + Thread *_thread; 1.188 + public: 1.189 +#ifdef ASSERT 1.190 + No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) : No_GC_Verifier(verifygc) { 1.191 + _thread = Thread::current(); 1.192 + if (_activated) { 1.193 + _thread->_allow_allocation_count++; 1.194 + _thread->_allow_safepoint_count++; 1.195 + } 1.196 + } 1.197 + 1.198 + ~No_Safepoint_Verifier() { 1.199 + if (_activated) { 1.200 + _thread->_allow_allocation_count--; 1.201 + _thread->_allow_safepoint_count--; 1.202 + } 1.203 + } 1.204 +#else 1.205 + No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){} 1.206 + ~No_Safepoint_Verifier() {} 1.207 +#endif 1.208 +}; 1.209 + 1.210 +// A Pause_No_Safepoint_Verifier is used to temporarily pause the 1.211 +// behavior of a No_Safepoint_Verifier object. If we are not in debug 1.212 +// mode then there is nothing to do. If the No_Safepoint_Verifier 1.213 +// object has an _activated value of false, then there is nothing to 1.214 +// do for safepoint and allocation checking, but there may still be 1.215 +// something to do for the underlying No_GC_Verifier object. 1.216 + 1.217 +class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier { 1.218 + private: 1.219 + No_Safepoint_Verifier * _nsv; 1.220 + 1.221 + public: 1.222 +#ifdef ASSERT 1.223 + Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) 1.224 + : Pause_No_GC_Verifier(nsv) { 1.225 + 1.226 + _nsv = nsv; 1.227 + if (_nsv->_activated) { 1.228 + _nsv->_thread->_allow_allocation_count--; 1.229 + _nsv->_thread->_allow_safepoint_count--; 1.230 + } 1.231 + } 1.232 + 1.233 + ~Pause_No_Safepoint_Verifier() { 1.234 + if (_nsv->_activated) { 1.235 + _nsv->_thread->_allow_allocation_count++; 1.236 + _nsv->_thread->_allow_safepoint_count++; 1.237 + } 1.238 + } 1.239 +#else 1.240 + Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) 1.241 + : Pause_No_GC_Verifier(nsv) {} 1.242 + ~Pause_No_Safepoint_Verifier() {} 1.243 +#endif 1.244 +}; 1.245 + 1.246 +// JRT_LEAF currently can be called from either _thread_in_Java or 1.247 +// _thread_in_native mode. In _thread_in_native, it is ok 1.248 +// for another thread to trigger GC. The rest of the JRT_LEAF 1.249 +// rules apply. 1.250 +class JRT_Leaf_Verifier : public No_Safepoint_Verifier { 1.251 + static bool should_verify_GC(); 1.252 + public: 1.253 +#ifdef ASSERT 1.254 + JRT_Leaf_Verifier(); 1.255 + ~JRT_Leaf_Verifier(); 1.256 +#else 1.257 + JRT_Leaf_Verifier() {} 1.258 + ~JRT_Leaf_Verifier() {} 1.259 +#endif 1.260 +}; 1.261 + 1.262 +// A No_Alloc_Verifier object can be placed in methods where one assumes that 1.263 +// no allocation will occur. The destructor will verify this property 1.264 +// unless the constructor is called with argument false (not activated). 1.265 +// 1.266 +// The check will only be done in debug mode and if activated. 1.267 +// Note: this only makes sense at safepoints (otherwise, other threads may 1.268 +// allocate concurrently.) 1.269 + 1.270 +class No_Alloc_Verifier : public StackObj { 1.271 + private: 1.272 + bool _activated; 1.273 + 1.274 + public: 1.275 +#ifdef ASSERT 1.276 + No_Alloc_Verifier(bool activated = true) { 1.277 + _activated = activated; 1.278 + if (_activated) Thread::current()->_allow_allocation_count++; 1.279 + } 1.280 + 1.281 + ~No_Alloc_Verifier() { 1.282 + if (_activated) Thread::current()->_allow_allocation_count--; 1.283 + } 1.284 +#else 1.285 + No_Alloc_Verifier(bool activated = true) {} 1.286 + ~No_Alloc_Verifier() {} 1.287 +#endif 1.288 +};