annotate src/share/vm/memory/gcLocker.hpp @ 2796:f08d439fab8c

7089790: integrate bsd-port changes Reviewed-by: kvn, twisti, jrose Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>
author never
date Sun, 25 Sep 2011 16:03:29 -0700
parents f95d63e2154a
children 1a2723f7ad8e
rev   line source
duke@0 1 /*
stefank@1879 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_MEMORY_GCLOCKER_HPP
stefank@1879 26 #define SHARE_VM_MEMORY_GCLOCKER_HPP
stefank@1879 27
stefank@1879 28 #include "gc_interface/collectedHeap.hpp"
stefank@1879 29 #include "memory/genCollectedHeap.hpp"
stefank@1879 30 #include "memory/universe.hpp"
stefank@1879 31 #include "oops/oop.hpp"
stefank@1879 32 #ifdef TARGET_OS_FAMILY_linux
stefank@1879 33 # include "os_linux.inline.hpp"
stefank@1879 34 # include "thread_linux.inline.hpp"
stefank@1879 35 #endif
stefank@1879 36 #ifdef TARGET_OS_FAMILY_solaris
stefank@1879 37 # include "os_solaris.inline.hpp"
stefank@1879 38 # include "thread_solaris.inline.hpp"
stefank@1879 39 #endif
stefank@1879 40 #ifdef TARGET_OS_FAMILY_windows
stefank@1879 41 # include "os_windows.inline.hpp"
stefank@1879 42 # include "thread_windows.inline.hpp"
stefank@1879 43 #endif
never@2796 44 #ifdef TARGET_OS_FAMILY_bsd
never@2796 45 # include "os_bsd.inline.hpp"
never@2796 46 # include "thread_bsd.inline.hpp"
never@2796 47 #endif
stefank@1879 48
duke@0 49 // The direct lock/unlock calls do not force a collection if an unlock
duke@0 50 // decrements the count to zero. Avoid calling these if at all possible.
duke@0 51
duke@0 52 class GC_locker: public AllStatic {
duke@0 53 private:
duke@0 54 static volatile jint _jni_lock_count; // number of jni active instances
duke@0 55 static volatile jint _lock_count; // number of other active instances
duke@0 56 static volatile bool _needs_gc; // heap is filling, we need a GC
duke@0 57 // note: bool is typedef'd as jint
duke@0 58 static volatile bool _doing_gc; // unlock_critical() is doing a GC
duke@0 59
duke@0 60 // Accessors
duke@0 61 static bool is_jni_active() {
duke@0 62 return _jni_lock_count > 0;
duke@0 63 }
duke@0 64
duke@0 65 static void set_needs_gc() {
duke@0 66 assert(SafepointSynchronize::is_at_safepoint(),
duke@0 67 "needs_gc is only set at a safepoint");
duke@0 68 _needs_gc = true;
duke@0 69 }
duke@0 70
duke@0 71 static void clear_needs_gc() {
duke@0 72 assert_lock_strong(JNICritical_lock);
duke@0 73 _needs_gc = false;
duke@0 74 }
duke@0 75
duke@0 76 static void jni_lock() {
duke@0 77 Atomic::inc(&_jni_lock_count);
duke@0 78 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 79 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; })
duke@0 80 assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(),
duke@0 81 "locking failed");
duke@0 82 }
duke@0 83
duke@0 84 static void jni_unlock() {
duke@0 85 Atomic::dec(&_jni_lock_count);
duke@0 86 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 87 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; })
duke@0 88 }
duke@0 89
duke@0 90 static void jni_lock_slow();
duke@0 91 static void jni_unlock_slow();
duke@0 92
duke@0 93 public:
duke@0 94 // Accessors
duke@0 95 static bool is_active();
duke@0 96 static bool needs_gc() { return _needs_gc; }
duke@0 97 // Shorthand
duke@0 98 static bool is_active_and_needs_gc() { return is_active() && needs_gc();}
duke@0 99
duke@0 100 // Calls set_needs_gc() if is_active() is true. Returns is_active().
duke@0 101 static bool check_active_before_gc();
duke@0 102
duke@0 103 // Stalls the caller (who should not be in a jni critical section)
duke@0 104 // until needs_gc() clears. Note however that needs_gc() may be
duke@0 105 // set at a subsequent safepoint and/or cleared under the
duke@0 106 // JNICritical_lock, so the caller may not safely assert upon
duke@0 107 // return from this method that "!needs_gc()" since that is
duke@0 108 // not a stable predicate.
duke@0 109 static void stall_until_clear();
duke@0 110
duke@0 111 // Non-structured GC locking: currently needed for JNI. Use with care!
duke@0 112 static void lock();
duke@0 113 static void unlock();
duke@0 114
duke@0 115 // The following two methods are used for JNI critical regions.
duke@0 116 // If we find that we failed to perform a GC because the GC_locker
duke@0 117 // was active, arrange for one as soon as possible by allowing
duke@0 118 // all threads in critical regions to complete, but not allowing
duke@0 119 // other critical regions to be entered. The reasons for that are:
duke@0 120 // 1) a GC request won't be starved by overlapping JNI critical
duke@0 121 // region activities, which can cause unnecessary OutOfMemory errors.
duke@0 122 // 2) even if allocation requests can still be satisfied before GC locker
duke@0 123 // becomes inactive, for example, in tenured generation possibly with
duke@0 124 // heap expansion, those allocations can trigger lots of safepointing
duke@0 125 // attempts (ineffective GC attempts) and require Heap_lock which
duke@0 126 // slow down allocations tremendously.
duke@0 127 //
duke@0 128 // Note that critical regions can be nested in a single thread, so
duke@0 129 // we must allow threads already in critical regions to continue.
duke@0 130 //
duke@0 131 // JNI critical regions are the only participants in this scheme
duke@0 132 // because they are, by spec, well bounded while in a critical region.
duke@0 133 //
duke@0 134 // Each of the following two method is split into a fast path and a slow
duke@0 135 // path. JNICritical_lock is only grabbed in the slow path.
duke@0 136 // _needs_gc is initially false and every java thread will go
duke@0 137 // through the fast path (which does the same thing as the slow path
duke@0 138 // when _needs_gc is false). When GC happens at a safepoint,
duke@0 139 // GC_locker::is_active() is checked. Since there is no safepoint in the
duke@0 140 // fast path of lock_critical() and unlock_critical(), there is no race
duke@0 141 // condition between the fast path and GC. After _needs_gc is set at a
duke@0 142 // safepoint, every thread will go through the slow path after the safepoint.
duke@0 143 // Since after a safepoint, each of the following two methods is either
duke@0 144 // entered from the method entry and falls into the slow path, or is
duke@0 145 // resumed from the safepoints in the method, which only exist in the slow
duke@0 146 // path. So when _needs_gc is set, the slow path is always taken, till
duke@0 147 // _needs_gc is cleared.
duke@0 148 static void lock_critical(JavaThread* thread);
duke@0 149 static void unlock_critical(JavaThread* thread);
duke@0 150 };
duke@0 151
duke@0 152
duke@0 153 // A No_GC_Verifier object can be placed in methods where one assumes that
duke@0 154 // no garbage collection will occur. The destructor will verify this property
duke@0 155 // unless the constructor is called with argument false (not verifygc).
duke@0 156 //
duke@0 157 // The check will only be done in debug mode and if verifygc true.
duke@0 158
duke@0 159 class No_GC_Verifier: public StackObj {
duke@0 160 friend class Pause_No_GC_Verifier;
duke@0 161
duke@0 162 protected:
duke@0 163 bool _verifygc;
duke@0 164 unsigned int _old_invocations;
duke@0 165
duke@0 166 public:
duke@0 167 #ifdef ASSERT
duke@0 168 No_GC_Verifier(bool verifygc = true);
duke@0 169 ~No_GC_Verifier();
duke@0 170 #else
duke@0 171 No_GC_Verifier(bool verifygc = true) {}
duke@0 172 ~No_GC_Verifier() {}
duke@0 173 #endif
duke@0 174 };
duke@0 175
duke@0 176 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
duke@0 177 // of a No_GC_Verifier object. If we are not in debug mode or if the
duke@0 178 // No_GC_Verifier object has a _verifygc value of false, then there
duke@0 179 // is nothing to do.
duke@0 180
duke@0 181 class Pause_No_GC_Verifier: public StackObj {
duke@0 182 private:
duke@0 183 No_GC_Verifier * _ngcv;
duke@0 184
duke@0 185 public:
duke@0 186 #ifdef ASSERT
duke@0 187 Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
duke@0 188 ~Pause_No_GC_Verifier();
duke@0 189 #else
duke@0 190 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
duke@0 191 ~Pause_No_GC_Verifier() {}
duke@0 192 #endif
duke@0 193 };
duke@0 194
duke@0 195
duke@0 196 // A No_Safepoint_Verifier object will throw an assertion failure if
duke@0 197 // the current thread passes a possible safepoint while this object is
duke@0 198 // instantiated. A safepoint, will either be: an oop allocation, blocking
duke@0 199 // on a Mutex or JavaLock, or executing a VM operation.
duke@0 200 //
duke@0 201 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
duke@0 202 //
duke@0 203 class No_Safepoint_Verifier : public No_GC_Verifier {
duke@0 204 friend class Pause_No_Safepoint_Verifier;
duke@0 205
duke@0 206 private:
duke@0 207 bool _activated;
duke@0 208 Thread *_thread;
duke@0 209 public:
duke@0 210 #ifdef ASSERT
never@98 211 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
never@98 212 No_GC_Verifier(verifygc),
never@98 213 _activated(activated) {
duke@0 214 _thread = Thread::current();
duke@0 215 if (_activated) {
duke@0 216 _thread->_allow_allocation_count++;
duke@0 217 _thread->_allow_safepoint_count++;
duke@0 218 }
duke@0 219 }
duke@0 220
duke@0 221 ~No_Safepoint_Verifier() {
duke@0 222 if (_activated) {
duke@0 223 _thread->_allow_allocation_count--;
duke@0 224 _thread->_allow_safepoint_count--;
duke@0 225 }
duke@0 226 }
duke@0 227 #else
duke@0 228 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
duke@0 229 ~No_Safepoint_Verifier() {}
duke@0 230 #endif
duke@0 231 };
duke@0 232
duke@0 233 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
duke@0 234 // behavior of a No_Safepoint_Verifier object. If we are not in debug
duke@0 235 // mode then there is nothing to do. If the No_Safepoint_Verifier
duke@0 236 // object has an _activated value of false, then there is nothing to
duke@0 237 // do for safepoint and allocation checking, but there may still be
duke@0 238 // something to do for the underlying No_GC_Verifier object.
duke@0 239
duke@0 240 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
duke@0 241 private:
duke@0 242 No_Safepoint_Verifier * _nsv;
duke@0 243
duke@0 244 public:
duke@0 245 #ifdef ASSERT
duke@0 246 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 247 : Pause_No_GC_Verifier(nsv) {
duke@0 248
duke@0 249 _nsv = nsv;
duke@0 250 if (_nsv->_activated) {
duke@0 251 _nsv->_thread->_allow_allocation_count--;
duke@0 252 _nsv->_thread->_allow_safepoint_count--;
duke@0 253 }
duke@0 254 }
duke@0 255
duke@0 256 ~Pause_No_Safepoint_Verifier() {
duke@0 257 if (_nsv->_activated) {
duke@0 258 _nsv->_thread->_allow_allocation_count++;
duke@0 259 _nsv->_thread->_allow_safepoint_count++;
duke@0 260 }
duke@0 261 }
duke@0 262 #else
duke@0 263 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 264 : Pause_No_GC_Verifier(nsv) {}
duke@0 265 ~Pause_No_Safepoint_Verifier() {}
duke@0 266 #endif
duke@0 267 };
duke@0 268
ysr@806 269 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
ysr@806 270 // over a section of execution by a thread. Currently, it's used only to
ysr@806 271 // prevent re-entrant calls to GC.
ysr@806 272 class SkipGCALot : public StackObj {
ysr@806 273 private:
ysr@806 274 bool _saved;
ysr@806 275 Thread* _t;
ysr@806 276
ysr@806 277 public:
ysr@806 278 #ifdef ASSERT
ysr@806 279 SkipGCALot(Thread* t) : _t(t) {
ysr@806 280 _saved = _t->skip_gcalot();
ysr@806 281 _t->set_skip_gcalot(true);
ysr@806 282 }
ysr@806 283
ysr@806 284 ~SkipGCALot() {
ysr@806 285 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
ysr@806 286 _t->set_skip_gcalot(_saved);
ysr@806 287 }
ysr@806 288 #else
ysr@806 289 SkipGCALot(Thread* t) { }
ysr@806 290 ~SkipGCALot() { }
ysr@806 291 #endif
ysr@806 292 };
ysr@806 293
duke@0 294 // JRT_LEAF currently can be called from either _thread_in_Java or
duke@0 295 // _thread_in_native mode. In _thread_in_native, it is ok
duke@0 296 // for another thread to trigger GC. The rest of the JRT_LEAF
duke@0 297 // rules apply.
duke@0 298 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
duke@0 299 static bool should_verify_GC();
duke@0 300 public:
duke@0 301 #ifdef ASSERT
duke@0 302 JRT_Leaf_Verifier();
duke@0 303 ~JRT_Leaf_Verifier();
duke@0 304 #else
duke@0 305 JRT_Leaf_Verifier() {}
duke@0 306 ~JRT_Leaf_Verifier() {}
duke@0 307 #endif
duke@0 308 };
duke@0 309
duke@0 310 // A No_Alloc_Verifier object can be placed in methods where one assumes that
duke@0 311 // no allocation will occur. The destructor will verify this property
duke@0 312 // unless the constructor is called with argument false (not activated).
duke@0 313 //
duke@0 314 // The check will only be done in debug mode and if activated.
duke@0 315 // Note: this only makes sense at safepoints (otherwise, other threads may
duke@0 316 // allocate concurrently.)
duke@0 317
duke@0 318 class No_Alloc_Verifier : public StackObj {
duke@0 319 private:
duke@0 320 bool _activated;
duke@0 321
duke@0 322 public:
duke@0 323 #ifdef ASSERT
duke@0 324 No_Alloc_Verifier(bool activated = true) {
duke@0 325 _activated = activated;
duke@0 326 if (_activated) Thread::current()->_allow_allocation_count++;
duke@0 327 }
duke@0 328
duke@0 329 ~No_Alloc_Verifier() {
duke@0 330 if (_activated) Thread::current()->_allow_allocation_count--;
duke@0 331 }
duke@0 332 #else
duke@0 333 No_Alloc_Verifier(bool activated = true) {}
duke@0 334 ~No_Alloc_Verifier() {}
duke@0 335 #endif
duke@0 336 };
stefank@1879 337
stefank@1879 338 #endif // SHARE_VM_MEMORY_GCLOCKER_HPP