annotate src/share/vm/memory/gcLocker.hpp @ 1879:f95d63e2154a

6989984: Use standard include model for Hospot Summary: Replaced MakeDeps and the includeDB files with more standardized solutions. Reviewed-by: coleenp, kvn, kamg
author stefank
date Tue, 23 Nov 2010 13:22:55 -0800
parents c18cbe5936b8
children f08d439fab8c
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
stefank@1879 44
duke@0 45 // The direct lock/unlock calls do not force a collection if an unlock
duke@0 46 // decrements the count to zero. Avoid calling these if at all possible.
duke@0 47
duke@0 48 class GC_locker: public AllStatic {
duke@0 49 private:
duke@0 50 static volatile jint _jni_lock_count; // number of jni active instances
duke@0 51 static volatile jint _lock_count; // number of other active instances
duke@0 52 static volatile bool _needs_gc; // heap is filling, we need a GC
duke@0 53 // note: bool is typedef'd as jint
duke@0 54 static volatile bool _doing_gc; // unlock_critical() is doing a GC
duke@0 55
duke@0 56 // Accessors
duke@0 57 static bool is_jni_active() {
duke@0 58 return _jni_lock_count > 0;
duke@0 59 }
duke@0 60
duke@0 61 static void set_needs_gc() {
duke@0 62 assert(SafepointSynchronize::is_at_safepoint(),
duke@0 63 "needs_gc is only set at a safepoint");
duke@0 64 _needs_gc = true;
duke@0 65 }
duke@0 66
duke@0 67 static void clear_needs_gc() {
duke@0 68 assert_lock_strong(JNICritical_lock);
duke@0 69 _needs_gc = false;
duke@0 70 }
duke@0 71
duke@0 72 static void jni_lock() {
duke@0 73 Atomic::inc(&_jni_lock_count);
duke@0 74 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 75 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; })
duke@0 76 assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(),
duke@0 77 "locking failed");
duke@0 78 }
duke@0 79
duke@0 80 static void jni_unlock() {
duke@0 81 Atomic::dec(&_jni_lock_count);
duke@0 82 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 83 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; })
duke@0 84 }
duke@0 85
duke@0 86 static void jni_lock_slow();
duke@0 87 static void jni_unlock_slow();
duke@0 88
duke@0 89 public:
duke@0 90 // Accessors
duke@0 91 static bool is_active();
duke@0 92 static bool needs_gc() { return _needs_gc; }
duke@0 93 // Shorthand
duke@0 94 static bool is_active_and_needs_gc() { return is_active() && needs_gc();}
duke@0 95
duke@0 96 // Calls set_needs_gc() if is_active() is true. Returns is_active().
duke@0 97 static bool check_active_before_gc();
duke@0 98
duke@0 99 // Stalls the caller (who should not be in a jni critical section)
duke@0 100 // until needs_gc() clears. Note however that needs_gc() may be
duke@0 101 // set at a subsequent safepoint and/or cleared under the
duke@0 102 // JNICritical_lock, so the caller may not safely assert upon
duke@0 103 // return from this method that "!needs_gc()" since that is
duke@0 104 // not a stable predicate.
duke@0 105 static void stall_until_clear();
duke@0 106
duke@0 107 // Non-structured GC locking: currently needed for JNI. Use with care!
duke@0 108 static void lock();
duke@0 109 static void unlock();
duke@0 110
duke@0 111 // The following two methods are used for JNI critical regions.
duke@0 112 // If we find that we failed to perform a GC because the GC_locker
duke@0 113 // was active, arrange for one as soon as possible by allowing
duke@0 114 // all threads in critical regions to complete, but not allowing
duke@0 115 // other critical regions to be entered. The reasons for that are:
duke@0 116 // 1) a GC request won't be starved by overlapping JNI critical
duke@0 117 // region activities, which can cause unnecessary OutOfMemory errors.
duke@0 118 // 2) even if allocation requests can still be satisfied before GC locker
duke@0 119 // becomes inactive, for example, in tenured generation possibly with
duke@0 120 // heap expansion, those allocations can trigger lots of safepointing
duke@0 121 // attempts (ineffective GC attempts) and require Heap_lock which
duke@0 122 // slow down allocations tremendously.
duke@0 123 //
duke@0 124 // Note that critical regions can be nested in a single thread, so
duke@0 125 // we must allow threads already in critical regions to continue.
duke@0 126 //
duke@0 127 // JNI critical regions are the only participants in this scheme
duke@0 128 // because they are, by spec, well bounded while in a critical region.
duke@0 129 //
duke@0 130 // Each of the following two method is split into a fast path and a slow
duke@0 131 // path. JNICritical_lock is only grabbed in the slow path.
duke@0 132 // _needs_gc is initially false and every java thread will go
duke@0 133 // through the fast path (which does the same thing as the slow path
duke@0 134 // when _needs_gc is false). When GC happens at a safepoint,
duke@0 135 // GC_locker::is_active() is checked. Since there is no safepoint in the
duke@0 136 // fast path of lock_critical() and unlock_critical(), there is no race
duke@0 137 // condition between the fast path and GC. After _needs_gc is set at a
duke@0 138 // safepoint, every thread will go through the slow path after the safepoint.
duke@0 139 // Since after a safepoint, each of the following two methods is either
duke@0 140 // entered from the method entry and falls into the slow path, or is
duke@0 141 // resumed from the safepoints in the method, which only exist in the slow
duke@0 142 // path. So when _needs_gc is set, the slow path is always taken, till
duke@0 143 // _needs_gc is cleared.
duke@0 144 static void lock_critical(JavaThread* thread);
duke@0 145 static void unlock_critical(JavaThread* thread);
duke@0 146 };
duke@0 147
duke@0 148
duke@0 149 // A No_GC_Verifier object can be placed in methods where one assumes that
duke@0 150 // no garbage collection will occur. The destructor will verify this property
duke@0 151 // unless the constructor is called with argument false (not verifygc).
duke@0 152 //
duke@0 153 // The check will only be done in debug mode and if verifygc true.
duke@0 154
duke@0 155 class No_GC_Verifier: public StackObj {
duke@0 156 friend class Pause_No_GC_Verifier;
duke@0 157
duke@0 158 protected:
duke@0 159 bool _verifygc;
duke@0 160 unsigned int _old_invocations;
duke@0 161
duke@0 162 public:
duke@0 163 #ifdef ASSERT
duke@0 164 No_GC_Verifier(bool verifygc = true);
duke@0 165 ~No_GC_Verifier();
duke@0 166 #else
duke@0 167 No_GC_Verifier(bool verifygc = true) {}
duke@0 168 ~No_GC_Verifier() {}
duke@0 169 #endif
duke@0 170 };
duke@0 171
duke@0 172 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
duke@0 173 // of a No_GC_Verifier object. If we are not in debug mode or if the
duke@0 174 // No_GC_Verifier object has a _verifygc value of false, then there
duke@0 175 // is nothing to do.
duke@0 176
duke@0 177 class Pause_No_GC_Verifier: public StackObj {
duke@0 178 private:
duke@0 179 No_GC_Verifier * _ngcv;
duke@0 180
duke@0 181 public:
duke@0 182 #ifdef ASSERT
duke@0 183 Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
duke@0 184 ~Pause_No_GC_Verifier();
duke@0 185 #else
duke@0 186 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
duke@0 187 ~Pause_No_GC_Verifier() {}
duke@0 188 #endif
duke@0 189 };
duke@0 190
duke@0 191
duke@0 192 // A No_Safepoint_Verifier object will throw an assertion failure if
duke@0 193 // the current thread passes a possible safepoint while this object is
duke@0 194 // instantiated. A safepoint, will either be: an oop allocation, blocking
duke@0 195 // on a Mutex or JavaLock, or executing a VM operation.
duke@0 196 //
duke@0 197 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
duke@0 198 //
duke@0 199 class No_Safepoint_Verifier : public No_GC_Verifier {
duke@0 200 friend class Pause_No_Safepoint_Verifier;
duke@0 201
duke@0 202 private:
duke@0 203 bool _activated;
duke@0 204 Thread *_thread;
duke@0 205 public:
duke@0 206 #ifdef ASSERT
never@98 207 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
never@98 208 No_GC_Verifier(verifygc),
never@98 209 _activated(activated) {
duke@0 210 _thread = Thread::current();
duke@0 211 if (_activated) {
duke@0 212 _thread->_allow_allocation_count++;
duke@0 213 _thread->_allow_safepoint_count++;
duke@0 214 }
duke@0 215 }
duke@0 216
duke@0 217 ~No_Safepoint_Verifier() {
duke@0 218 if (_activated) {
duke@0 219 _thread->_allow_allocation_count--;
duke@0 220 _thread->_allow_safepoint_count--;
duke@0 221 }
duke@0 222 }
duke@0 223 #else
duke@0 224 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
duke@0 225 ~No_Safepoint_Verifier() {}
duke@0 226 #endif
duke@0 227 };
duke@0 228
duke@0 229 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
duke@0 230 // behavior of a No_Safepoint_Verifier object. If we are not in debug
duke@0 231 // mode then there is nothing to do. If the No_Safepoint_Verifier
duke@0 232 // object has an _activated value of false, then there is nothing to
duke@0 233 // do for safepoint and allocation checking, but there may still be
duke@0 234 // something to do for the underlying No_GC_Verifier object.
duke@0 235
duke@0 236 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
duke@0 237 private:
duke@0 238 No_Safepoint_Verifier * _nsv;
duke@0 239
duke@0 240 public:
duke@0 241 #ifdef ASSERT
duke@0 242 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 243 : Pause_No_GC_Verifier(nsv) {
duke@0 244
duke@0 245 _nsv = nsv;
duke@0 246 if (_nsv->_activated) {
duke@0 247 _nsv->_thread->_allow_allocation_count--;
duke@0 248 _nsv->_thread->_allow_safepoint_count--;
duke@0 249 }
duke@0 250 }
duke@0 251
duke@0 252 ~Pause_No_Safepoint_Verifier() {
duke@0 253 if (_nsv->_activated) {
duke@0 254 _nsv->_thread->_allow_allocation_count++;
duke@0 255 _nsv->_thread->_allow_safepoint_count++;
duke@0 256 }
duke@0 257 }
duke@0 258 #else
duke@0 259 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 260 : Pause_No_GC_Verifier(nsv) {}
duke@0 261 ~Pause_No_Safepoint_Verifier() {}
duke@0 262 #endif
duke@0 263 };
duke@0 264
ysr@806 265 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
ysr@806 266 // over a section of execution by a thread. Currently, it's used only to
ysr@806 267 // prevent re-entrant calls to GC.
ysr@806 268 class SkipGCALot : public StackObj {
ysr@806 269 private:
ysr@806 270 bool _saved;
ysr@806 271 Thread* _t;
ysr@806 272
ysr@806 273 public:
ysr@806 274 #ifdef ASSERT
ysr@806 275 SkipGCALot(Thread* t) : _t(t) {
ysr@806 276 _saved = _t->skip_gcalot();
ysr@806 277 _t->set_skip_gcalot(true);
ysr@806 278 }
ysr@806 279
ysr@806 280 ~SkipGCALot() {
ysr@806 281 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
ysr@806 282 _t->set_skip_gcalot(_saved);
ysr@806 283 }
ysr@806 284 #else
ysr@806 285 SkipGCALot(Thread* t) { }
ysr@806 286 ~SkipGCALot() { }
ysr@806 287 #endif
ysr@806 288 };
ysr@806 289
duke@0 290 // JRT_LEAF currently can be called from either _thread_in_Java or
duke@0 291 // _thread_in_native mode. In _thread_in_native, it is ok
duke@0 292 // for another thread to trigger GC. The rest of the JRT_LEAF
duke@0 293 // rules apply.
duke@0 294 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
duke@0 295 static bool should_verify_GC();
duke@0 296 public:
duke@0 297 #ifdef ASSERT
duke@0 298 JRT_Leaf_Verifier();
duke@0 299 ~JRT_Leaf_Verifier();
duke@0 300 #else
duke@0 301 JRT_Leaf_Verifier() {}
duke@0 302 ~JRT_Leaf_Verifier() {}
duke@0 303 #endif
duke@0 304 };
duke@0 305
duke@0 306 // A No_Alloc_Verifier object can be placed in methods where one assumes that
duke@0 307 // no allocation will occur. The destructor will verify this property
duke@0 308 // unless the constructor is called with argument false (not activated).
duke@0 309 //
duke@0 310 // The check will only be done in debug mode and if activated.
duke@0 311 // Note: this only makes sense at safepoints (otherwise, other threads may
duke@0 312 // allocate concurrently.)
duke@0 313
duke@0 314 class No_Alloc_Verifier : public StackObj {
duke@0 315 private:
duke@0 316 bool _activated;
duke@0 317
duke@0 318 public:
duke@0 319 #ifdef ASSERT
duke@0 320 No_Alloc_Verifier(bool activated = true) {
duke@0 321 _activated = activated;
duke@0 322 if (_activated) Thread::current()->_allow_allocation_count++;
duke@0 323 }
duke@0 324
duke@0 325 ~No_Alloc_Verifier() {
duke@0 326 if (_activated) Thread::current()->_allow_allocation_count--;
duke@0 327 }
duke@0 328 #else
duke@0 329 No_Alloc_Verifier(bool activated = true) {}
duke@0 330 ~No_Alloc_Verifier() {}
duke@0 331 #endif
duke@0 332 };
stefank@1879 333
stefank@1879 334 #endif // SHARE_VM_MEMORY_GCLOCKER_HPP