src/share/vm/memory/gcLocker.hpp

Wed, 02 Jul 2008 12:55:16 -0700

author
xdono
date
Wed, 02 Jul 2008 12:55:16 -0700
changeset 631
d1605aabd0a1
parent 533
deb97b8ef02b
child 1241
821269eca479
permissions
-rw-r--r--

6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell

duke@435 1 /*
xdono@631 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // The direct lock/unlock calls do not force a collection if an unlock
duke@435 26 // decrements the count to zero. Avoid calling these if at all possible.
duke@435 27
duke@435 28 class GC_locker: public AllStatic {
duke@435 29 private:
duke@435 30 static volatile jint _jni_lock_count; // number of jni active instances
duke@435 31 static volatile jint _lock_count; // number of other active instances
duke@435 32 static volatile bool _needs_gc; // heap is filling, we need a GC
duke@435 33 // note: bool is typedef'd as jint
duke@435 34 static volatile bool _doing_gc; // unlock_critical() is doing a GC
duke@435 35
duke@435 36 // Accessors
duke@435 37 static bool is_jni_active() {
duke@435 38 return _jni_lock_count > 0;
duke@435 39 }
duke@435 40
duke@435 41 static void set_needs_gc() {
duke@435 42 assert(SafepointSynchronize::is_at_safepoint(),
duke@435 43 "needs_gc is only set at a safepoint");
duke@435 44 _needs_gc = true;
duke@435 45 }
duke@435 46
duke@435 47 static void clear_needs_gc() {
duke@435 48 assert_lock_strong(JNICritical_lock);
duke@435 49 _needs_gc = false;
duke@435 50 }
duke@435 51
duke@435 52 static void jni_lock() {
duke@435 53 Atomic::inc(&_jni_lock_count);
duke@435 54 CHECK_UNHANDLED_OOPS_ONLY(
duke@435 55 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; })
duke@435 56 assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(),
duke@435 57 "locking failed");
duke@435 58 }
duke@435 59
duke@435 60 static void jni_unlock() {
duke@435 61 Atomic::dec(&_jni_lock_count);
duke@435 62 CHECK_UNHANDLED_OOPS_ONLY(
duke@435 63 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; })
duke@435 64 }
duke@435 65
duke@435 66 static void jni_lock_slow();
duke@435 67 static void jni_unlock_slow();
duke@435 68
duke@435 69 public:
duke@435 70 // Accessors
duke@435 71 static bool is_active();
duke@435 72 static bool needs_gc() { return _needs_gc; }
duke@435 73 // Shorthand
duke@435 74 static bool is_active_and_needs_gc() { return is_active() && needs_gc();}
duke@435 75
duke@435 76 // Calls set_needs_gc() if is_active() is true. Returns is_active().
duke@435 77 static bool check_active_before_gc();
duke@435 78
duke@435 79 // Stalls the caller (who should not be in a jni critical section)
duke@435 80 // until needs_gc() clears. Note however that needs_gc() may be
duke@435 81 // set at a subsequent safepoint and/or cleared under the
duke@435 82 // JNICritical_lock, so the caller may not safely assert upon
duke@435 83 // return from this method that "!needs_gc()" since that is
duke@435 84 // not a stable predicate.
duke@435 85 static void stall_until_clear();
duke@435 86
duke@435 87 // Non-structured GC locking: currently needed for JNI. Use with care!
duke@435 88 static void lock();
duke@435 89 static void unlock();
duke@435 90
duke@435 91 // The following two methods are used for JNI critical regions.
duke@435 92 // If we find that we failed to perform a GC because the GC_locker
duke@435 93 // was active, arrange for one as soon as possible by allowing
duke@435 94 // all threads in critical regions to complete, but not allowing
duke@435 95 // other critical regions to be entered. The reasons for that are:
duke@435 96 // 1) a GC request won't be starved by overlapping JNI critical
duke@435 97 // region activities, which can cause unnecessary OutOfMemory errors.
duke@435 98 // 2) even if allocation requests can still be satisfied before GC locker
duke@435 99 // becomes inactive, for example, in tenured generation possibly with
duke@435 100 // heap expansion, those allocations can trigger lots of safepointing
duke@435 101 // attempts (ineffective GC attempts) and require Heap_lock which
duke@435 102 // slow down allocations tremendously.
duke@435 103 //
duke@435 104 // Note that critical regions can be nested in a single thread, so
duke@435 105 // we must allow threads already in critical regions to continue.
duke@435 106 //
duke@435 107 // JNI critical regions are the only participants in this scheme
duke@435 108 // because they are, by spec, well bounded while in a critical region.
duke@435 109 //
duke@435 110 // Each of the following two method is split into a fast path and a slow
duke@435 111 // path. JNICritical_lock is only grabbed in the slow path.
duke@435 112 // _needs_gc is initially false and every java thread will go
duke@435 113 // through the fast path (which does the same thing as the slow path
duke@435 114 // when _needs_gc is false). When GC happens at a safepoint,
duke@435 115 // GC_locker::is_active() is checked. Since there is no safepoint in the
duke@435 116 // fast path of lock_critical() and unlock_critical(), there is no race
duke@435 117 // condition between the fast path and GC. After _needs_gc is set at a
duke@435 118 // safepoint, every thread will go through the slow path after the safepoint.
duke@435 119 // Since after a safepoint, each of the following two methods is either
duke@435 120 // entered from the method entry and falls into the slow path, or is
duke@435 121 // resumed from the safepoints in the method, which only exist in the slow
duke@435 122 // path. So when _needs_gc is set, the slow path is always taken, till
duke@435 123 // _needs_gc is cleared.
duke@435 124 static void lock_critical(JavaThread* thread);
duke@435 125 static void unlock_critical(JavaThread* thread);
duke@435 126 };
duke@435 127
duke@435 128
duke@435 129 // A No_GC_Verifier object can be placed in methods where one assumes that
duke@435 130 // no garbage collection will occur. The destructor will verify this property
duke@435 131 // unless the constructor is called with argument false (not verifygc).
duke@435 132 //
duke@435 133 // The check will only be done in debug mode and if verifygc true.
duke@435 134
duke@435 135 class No_GC_Verifier: public StackObj {
duke@435 136 friend class Pause_No_GC_Verifier;
duke@435 137
duke@435 138 protected:
duke@435 139 bool _verifygc;
duke@435 140 unsigned int _old_invocations;
duke@435 141
duke@435 142 public:
duke@435 143 #ifdef ASSERT
duke@435 144 No_GC_Verifier(bool verifygc = true);
duke@435 145 ~No_GC_Verifier();
duke@435 146 #else
duke@435 147 No_GC_Verifier(bool verifygc = true) {}
duke@435 148 ~No_GC_Verifier() {}
duke@435 149 #endif
duke@435 150 };
duke@435 151
duke@435 152 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
duke@435 153 // of a No_GC_Verifier object. If we are not in debug mode or if the
duke@435 154 // No_GC_Verifier object has a _verifygc value of false, then there
duke@435 155 // is nothing to do.
duke@435 156
duke@435 157 class Pause_No_GC_Verifier: public StackObj {
duke@435 158 private:
duke@435 159 No_GC_Verifier * _ngcv;
duke@435 160
duke@435 161 public:
duke@435 162 #ifdef ASSERT
duke@435 163 Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
duke@435 164 ~Pause_No_GC_Verifier();
duke@435 165 #else
duke@435 166 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
duke@435 167 ~Pause_No_GC_Verifier() {}
duke@435 168 #endif
duke@435 169 };
duke@435 170
duke@435 171
duke@435 172 // A No_Safepoint_Verifier object will throw an assertion failure if
duke@435 173 // the current thread passes a possible safepoint while this object is
duke@435 174 // instantiated. A safepoint, will either be: an oop allocation, blocking
duke@435 175 // on a Mutex or JavaLock, or executing a VM operation.
duke@435 176 //
duke@435 177 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
duke@435 178 //
duke@435 179 class No_Safepoint_Verifier : public No_GC_Verifier {
duke@435 180 friend class Pause_No_Safepoint_Verifier;
duke@435 181
duke@435 182 private:
duke@435 183 bool _activated;
duke@435 184 Thread *_thread;
duke@435 185 public:
duke@435 186 #ifdef ASSERT
never@533 187 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
never@533 188 No_GC_Verifier(verifygc),
never@533 189 _activated(activated) {
duke@435 190 _thread = Thread::current();
duke@435 191 if (_activated) {
duke@435 192 _thread->_allow_allocation_count++;
duke@435 193 _thread->_allow_safepoint_count++;
duke@435 194 }
duke@435 195 }
duke@435 196
duke@435 197 ~No_Safepoint_Verifier() {
duke@435 198 if (_activated) {
duke@435 199 _thread->_allow_allocation_count--;
duke@435 200 _thread->_allow_safepoint_count--;
duke@435 201 }
duke@435 202 }
duke@435 203 #else
duke@435 204 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
duke@435 205 ~No_Safepoint_Verifier() {}
duke@435 206 #endif
duke@435 207 };
duke@435 208
duke@435 209 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
duke@435 210 // behavior of a No_Safepoint_Verifier object. If we are not in debug
duke@435 211 // mode then there is nothing to do. If the No_Safepoint_Verifier
duke@435 212 // object has an _activated value of false, then there is nothing to
duke@435 213 // do for safepoint and allocation checking, but there may still be
duke@435 214 // something to do for the underlying No_GC_Verifier object.
duke@435 215
duke@435 216 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
duke@435 217 private:
duke@435 218 No_Safepoint_Verifier * _nsv;
duke@435 219
duke@435 220 public:
duke@435 221 #ifdef ASSERT
duke@435 222 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@435 223 : Pause_No_GC_Verifier(nsv) {
duke@435 224
duke@435 225 _nsv = nsv;
duke@435 226 if (_nsv->_activated) {
duke@435 227 _nsv->_thread->_allow_allocation_count--;
duke@435 228 _nsv->_thread->_allow_safepoint_count--;
duke@435 229 }
duke@435 230 }
duke@435 231
duke@435 232 ~Pause_No_Safepoint_Verifier() {
duke@435 233 if (_nsv->_activated) {
duke@435 234 _nsv->_thread->_allow_allocation_count++;
duke@435 235 _nsv->_thread->_allow_safepoint_count++;
duke@435 236 }
duke@435 237 }
duke@435 238 #else
duke@435 239 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@435 240 : Pause_No_GC_Verifier(nsv) {}
duke@435 241 ~Pause_No_Safepoint_Verifier() {}
duke@435 242 #endif
duke@435 243 };
duke@435 244
duke@435 245 // JRT_LEAF currently can be called from either _thread_in_Java or
duke@435 246 // _thread_in_native mode. In _thread_in_native, it is ok
duke@435 247 // for another thread to trigger GC. The rest of the JRT_LEAF
duke@435 248 // rules apply.
duke@435 249 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
duke@435 250 static bool should_verify_GC();
duke@435 251 public:
duke@435 252 #ifdef ASSERT
duke@435 253 JRT_Leaf_Verifier();
duke@435 254 ~JRT_Leaf_Verifier();
duke@435 255 #else
duke@435 256 JRT_Leaf_Verifier() {}
duke@435 257 ~JRT_Leaf_Verifier() {}
duke@435 258 #endif
duke@435 259 };
duke@435 260
duke@435 261 // A No_Alloc_Verifier object can be placed in methods where one assumes that
duke@435 262 // no allocation will occur. The destructor will verify this property
duke@435 263 // unless the constructor is called with argument false (not activated).
duke@435 264 //
duke@435 265 // The check will only be done in debug mode and if activated.
duke@435 266 // Note: this only makes sense at safepoints (otherwise, other threads may
duke@435 267 // allocate concurrently.)
duke@435 268
duke@435 269 class No_Alloc_Verifier : public StackObj {
duke@435 270 private:
duke@435 271 bool _activated;
duke@435 272
duke@435 273 public:
duke@435 274 #ifdef ASSERT
duke@435 275 No_Alloc_Verifier(bool activated = true) {
duke@435 276 _activated = activated;
duke@435 277 if (_activated) Thread::current()->_allow_allocation_count++;
duke@435 278 }
duke@435 279
duke@435 280 ~No_Alloc_Verifier() {
duke@435 281 if (_activated) Thread::current()->_allow_allocation_count--;
duke@435 282 }
duke@435 283 #else
duke@435 284 No_Alloc_Verifier(bool activated = true) {}
duke@435 285 ~No_Alloc_Verifier() {}
duke@435 286 #endif
duke@435 287 };

mercurial