duke@435: /* xdono@1279: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // The direct lock/unlock calls do not force a collection if an unlock duke@435: // decrements the count to zero. Avoid calling these if at all possible. duke@435: duke@435: class GC_locker: public AllStatic { duke@435: private: duke@435: static volatile jint _jni_lock_count; // number of jni active instances duke@435: static volatile jint _lock_count; // number of other active instances duke@435: static volatile bool _needs_gc; // heap is filling, we need a GC duke@435: // note: bool is typedef'd as jint duke@435: static volatile bool _doing_gc; // unlock_critical() is doing a GC duke@435: duke@435: // Accessors duke@435: static bool is_jni_active() { duke@435: return _jni_lock_count > 0; duke@435: } duke@435: duke@435: static void set_needs_gc() { duke@435: assert(SafepointSynchronize::is_at_safepoint(), duke@435: "needs_gc is only set at a safepoint"); duke@435: _needs_gc = true; duke@435: } duke@435: duke@435: static void clear_needs_gc() { duke@435: assert_lock_strong(JNICritical_lock); duke@435: _needs_gc = false; duke@435: } duke@435: duke@435: static void jni_lock() { duke@435: Atomic::inc(&_jni_lock_count); duke@435: CHECK_UNHANDLED_OOPS_ONLY( duke@435: if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; }) duke@435: assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(), duke@435: "locking failed"); duke@435: } duke@435: duke@435: static void jni_unlock() { duke@435: Atomic::dec(&_jni_lock_count); duke@435: CHECK_UNHANDLED_OOPS_ONLY( duke@435: if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; }) duke@435: } duke@435: duke@435: static void jni_lock_slow(); duke@435: static void jni_unlock_slow(); duke@435: duke@435: public: duke@435: // Accessors duke@435: static bool is_active(); duke@435: static bool needs_gc() { return _needs_gc; } duke@435: // Shorthand duke@435: static bool is_active_and_needs_gc() { return is_active() && needs_gc();} duke@435: duke@435: // Calls set_needs_gc() if is_active() is true. Returns is_active(). duke@435: static bool check_active_before_gc(); duke@435: duke@435: // Stalls the caller (who should not be in a jni critical section) duke@435: // until needs_gc() clears. Note however that needs_gc() may be duke@435: // set at a subsequent safepoint and/or cleared under the duke@435: // JNICritical_lock, so the caller may not safely assert upon duke@435: // return from this method that "!needs_gc()" since that is duke@435: // not a stable predicate. duke@435: static void stall_until_clear(); duke@435: duke@435: // Non-structured GC locking: currently needed for JNI. Use with care! duke@435: static void lock(); duke@435: static void unlock(); duke@435: duke@435: // The following two methods are used for JNI critical regions. duke@435: // If we find that we failed to perform a GC because the GC_locker duke@435: // was active, arrange for one as soon as possible by allowing duke@435: // all threads in critical regions to complete, but not allowing duke@435: // other critical regions to be entered. The reasons for that are: duke@435: // 1) a GC request won't be starved by overlapping JNI critical duke@435: // region activities, which can cause unnecessary OutOfMemory errors. duke@435: // 2) even if allocation requests can still be satisfied before GC locker duke@435: // becomes inactive, for example, in tenured generation possibly with duke@435: // heap expansion, those allocations can trigger lots of safepointing duke@435: // attempts (ineffective GC attempts) and require Heap_lock which duke@435: // slow down allocations tremendously. duke@435: // duke@435: // Note that critical regions can be nested in a single thread, so duke@435: // we must allow threads already in critical regions to continue. duke@435: // duke@435: // JNI critical regions are the only participants in this scheme duke@435: // because they are, by spec, well bounded while in a critical region. duke@435: // duke@435: // Each of the following two method is split into a fast path and a slow duke@435: // path. JNICritical_lock is only grabbed in the slow path. duke@435: // _needs_gc is initially false and every java thread will go duke@435: // through the fast path (which does the same thing as the slow path duke@435: // when _needs_gc is false). When GC happens at a safepoint, duke@435: // GC_locker::is_active() is checked. Since there is no safepoint in the duke@435: // fast path of lock_critical() and unlock_critical(), there is no race duke@435: // condition between the fast path and GC. After _needs_gc is set at a duke@435: // safepoint, every thread will go through the slow path after the safepoint. duke@435: // Since after a safepoint, each of the following two methods is either duke@435: // entered from the method entry and falls into the slow path, or is duke@435: // resumed from the safepoints in the method, which only exist in the slow duke@435: // path. So when _needs_gc is set, the slow path is always taken, till duke@435: // _needs_gc is cleared. duke@435: static void lock_critical(JavaThread* thread); duke@435: static void unlock_critical(JavaThread* thread); duke@435: }; duke@435: duke@435: duke@435: // A No_GC_Verifier object can be placed in methods where one assumes that duke@435: // no garbage collection will occur. The destructor will verify this property duke@435: // unless the constructor is called with argument false (not verifygc). duke@435: // duke@435: // The check will only be done in debug mode and if verifygc true. duke@435: duke@435: class No_GC_Verifier: public StackObj { duke@435: friend class Pause_No_GC_Verifier; duke@435: duke@435: protected: duke@435: bool _verifygc; duke@435: unsigned int _old_invocations; duke@435: duke@435: public: duke@435: #ifdef ASSERT duke@435: No_GC_Verifier(bool verifygc = true); duke@435: ~No_GC_Verifier(); duke@435: #else duke@435: No_GC_Verifier(bool verifygc = true) {} duke@435: ~No_GC_Verifier() {} duke@435: #endif duke@435: }; duke@435: duke@435: // A Pause_No_GC_Verifier is used to temporarily pause the behavior duke@435: // of a No_GC_Verifier object. If we are not in debug mode or if the duke@435: // No_GC_Verifier object has a _verifygc value of false, then there duke@435: // is nothing to do. duke@435: duke@435: class Pause_No_GC_Verifier: public StackObj { duke@435: private: duke@435: No_GC_Verifier * _ngcv; duke@435: duke@435: public: duke@435: #ifdef ASSERT duke@435: Pause_No_GC_Verifier(No_GC_Verifier * ngcv); duke@435: ~Pause_No_GC_Verifier(); duke@435: #else duke@435: Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {} duke@435: ~Pause_No_GC_Verifier() {} duke@435: #endif duke@435: }; duke@435: duke@435: duke@435: // A No_Safepoint_Verifier object will throw an assertion failure if duke@435: // the current thread passes a possible safepoint while this object is duke@435: // instantiated. A safepoint, will either be: an oop allocation, blocking duke@435: // on a Mutex or JavaLock, or executing a VM operation. duke@435: // duke@435: // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier duke@435: // duke@435: class No_Safepoint_Verifier : public No_GC_Verifier { duke@435: friend class Pause_No_Safepoint_Verifier; duke@435: duke@435: private: duke@435: bool _activated; duke@435: Thread *_thread; duke@435: public: duke@435: #ifdef ASSERT never@533: No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) : never@533: No_GC_Verifier(verifygc), never@533: _activated(activated) { duke@435: _thread = Thread::current(); duke@435: if (_activated) { duke@435: _thread->_allow_allocation_count++; duke@435: _thread->_allow_safepoint_count++; duke@435: } duke@435: } duke@435: duke@435: ~No_Safepoint_Verifier() { duke@435: if (_activated) { duke@435: _thread->_allow_allocation_count--; duke@435: _thread->_allow_safepoint_count--; duke@435: } duke@435: } duke@435: #else duke@435: No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){} duke@435: ~No_Safepoint_Verifier() {} duke@435: #endif duke@435: }; duke@435: duke@435: // A Pause_No_Safepoint_Verifier is used to temporarily pause the duke@435: // behavior of a No_Safepoint_Verifier object. If we are not in debug duke@435: // mode then there is nothing to do. If the No_Safepoint_Verifier duke@435: // object has an _activated value of false, then there is nothing to duke@435: // do for safepoint and allocation checking, but there may still be duke@435: // something to do for the underlying No_GC_Verifier object. duke@435: duke@435: class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier { duke@435: private: duke@435: No_Safepoint_Verifier * _nsv; duke@435: duke@435: public: duke@435: #ifdef ASSERT duke@435: Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) duke@435: : Pause_No_GC_Verifier(nsv) { duke@435: duke@435: _nsv = nsv; duke@435: if (_nsv->_activated) { duke@435: _nsv->_thread->_allow_allocation_count--; duke@435: _nsv->_thread->_allow_safepoint_count--; duke@435: } duke@435: } duke@435: duke@435: ~Pause_No_Safepoint_Verifier() { duke@435: if (_nsv->_activated) { duke@435: _nsv->_thread->_allow_allocation_count++; duke@435: _nsv->_thread->_allow_safepoint_count++; duke@435: } duke@435: } duke@435: #else duke@435: Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) duke@435: : Pause_No_GC_Verifier(nsv) {} duke@435: ~Pause_No_Safepoint_Verifier() {} duke@435: #endif duke@435: }; duke@435: ysr@1241: // A SkipGCALot object is used to elide the usual effect of gc-a-lot ysr@1241: // over a section of execution by a thread. Currently, it's used only to ysr@1241: // prevent re-entrant calls to GC. ysr@1241: class SkipGCALot : public StackObj { ysr@1241: private: ysr@1241: bool _saved; ysr@1241: Thread* _t; ysr@1241: ysr@1241: public: ysr@1241: #ifdef ASSERT ysr@1241: SkipGCALot(Thread* t) : _t(t) { ysr@1241: _saved = _t->skip_gcalot(); ysr@1241: _t->set_skip_gcalot(true); ysr@1241: } ysr@1241: ysr@1241: ~SkipGCALot() { ysr@1241: assert(_t->skip_gcalot(), "Save-restore protocol invariant"); ysr@1241: _t->set_skip_gcalot(_saved); ysr@1241: } ysr@1241: #else ysr@1241: SkipGCALot(Thread* t) { } ysr@1241: ~SkipGCALot() { } ysr@1241: #endif ysr@1241: }; ysr@1241: duke@435: // JRT_LEAF currently can be called from either _thread_in_Java or duke@435: // _thread_in_native mode. In _thread_in_native, it is ok duke@435: // for another thread to trigger GC. The rest of the JRT_LEAF duke@435: // rules apply. duke@435: class JRT_Leaf_Verifier : public No_Safepoint_Verifier { duke@435: static bool should_verify_GC(); duke@435: public: duke@435: #ifdef ASSERT duke@435: JRT_Leaf_Verifier(); duke@435: ~JRT_Leaf_Verifier(); duke@435: #else duke@435: JRT_Leaf_Verifier() {} duke@435: ~JRT_Leaf_Verifier() {} duke@435: #endif duke@435: }; duke@435: duke@435: // A No_Alloc_Verifier object can be placed in methods where one assumes that duke@435: // no allocation will occur. The destructor will verify this property duke@435: // unless the constructor is called with argument false (not activated). duke@435: // duke@435: // The check will only be done in debug mode and if activated. duke@435: // Note: this only makes sense at safepoints (otherwise, other threads may duke@435: // allocate concurrently.) duke@435: duke@435: class No_Alloc_Verifier : public StackObj { duke@435: private: duke@435: bool _activated; duke@435: duke@435: public: duke@435: #ifdef ASSERT duke@435: No_Alloc_Verifier(bool activated = true) { duke@435: _activated = activated; duke@435: if (_activated) Thread::current()->_allow_allocation_count++; duke@435: } duke@435: duke@435: ~No_Alloc_Verifier() { duke@435: if (_activated) Thread::current()->_allow_allocation_count--; duke@435: } duke@435: #else duke@435: No_Alloc_Verifier(bool activated = true) {} duke@435: ~No_Alloc_Verifier() {} duke@435: #endif duke@435: };