annotate src/share/vm/memory/gcLocker.cpp @ 8:762905818571

6665445: Backout change to CardTableModRefBS::resize_covered_region() Summary: Backed out part of cahnge for 6624765 because of nightly testing regressions. Reviewers below were for 6624765. Reviewed-by: ysr, apetrusenko
author jmasa
date Wed, 20 Feb 2008 08:40:31 -0800
parents a61af66fc99e
children c0492d52d55b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_gcLocker.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 volatile jint GC_locker::_jni_lock_count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 volatile jint GC_locker::_lock_count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 volatile bool GC_locker::_needs_gc = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 volatile bool GC_locker::_doing_gc = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 void GC_locker::stall_until_clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
34 assert(!JavaThread::current()->in_critical(), "Would deadlock");
a61af66fc99e Initial load
duke
parents:
diff changeset
35 MutexLocker ml(JNICritical_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // Wait for _needs_gc to be cleared
a61af66fc99e Initial load
duke
parents:
diff changeset
37 while (GC_locker::needs_gc()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
38 JNICritical_lock->wait();
a61af66fc99e Initial load
duke
parents:
diff changeset
39 }
a61af66fc99e Initial load
duke
parents:
diff changeset
40 }
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 void GC_locker::jni_lock_slow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 MutexLocker mu(JNICritical_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
44 // Block entering threads if we know at least one thread is in a
a61af66fc99e Initial load
duke
parents:
diff changeset
45 // JNI critical region and we need a GC.
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // We check that at least one thread is in a critical region before
a61af66fc99e Initial load
duke
parents:
diff changeset
47 // blocking because blocked threads are woken up by a thread exiting
a61af66fc99e Initial load
duke
parents:
diff changeset
48 // a JNI critical region.
a61af66fc99e Initial load
duke
parents:
diff changeset
49 while ((is_jni_active() && needs_gc()) || _doing_gc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 JNICritical_lock->wait();
a61af66fc99e Initial load
duke
parents:
diff changeset
51 }
a61af66fc99e Initial load
duke
parents:
diff changeset
52 jni_lock();
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 void GC_locker::jni_unlock_slow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 MutexLocker mu(JNICritical_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
57 jni_unlock();
a61af66fc99e Initial load
duke
parents:
diff changeset
58 if (needs_gc() && !is_jni_active()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // We're the last thread out. Cause a GC to occur.
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // GC will also check is_active, so this check is not
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // strictly needed. It's added here to make it clear that
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // the GC will NOT be performed if any other caller
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // of GC_locker::lock() still needs GC locked.
a61af66fc99e Initial load
duke
parents:
diff changeset
64 if (!is_active()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
65 _doing_gc = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // Must give up the lock while at a safepoint
a61af66fc99e Initial load
duke
parents:
diff changeset
68 MutexUnlocker munlock(JNICritical_lock);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 Universe::heap()->collect(GCCause::_gc_locker);
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71 _doing_gc = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 }
a61af66fc99e Initial load
duke
parents:
diff changeset
73 clear_needs_gc();
a61af66fc99e Initial load
duke
parents:
diff changeset
74 JNICritical_lock->notify_all();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76 }
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // Implementation of No_GC_Verifier
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 _verifygc = verifygc;
a61af66fc99e Initial load
duke
parents:
diff changeset
84 if (_verifygc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 CollectedHeap* h = Universe::heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
86 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
a61af66fc99e Initial load
duke
parents:
diff changeset
87 _old_invocations = h->total_collections();
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 No_GC_Verifier::~No_GC_Verifier() {
a61af66fc99e Initial load
duke
parents:
diff changeset
93 if (_verifygc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 CollectedHeap* h = Universe::heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
95 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
a61af66fc99e Initial load
duke
parents:
diff changeset
96 if (_old_invocations != h->total_collections()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 fatal("collection in a No_GC_Verifier secured function");
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 _ngcv = ngcv;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 if (_ngcv->_verifygc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // if we were verifying, then make sure that nothing is
a61af66fc99e Initial load
duke
parents:
diff changeset
106 // wrong before we "pause" verification
a61af66fc99e Initial load
duke
parents:
diff changeset
107 CollectedHeap* h = Universe::heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
108 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
a61af66fc99e Initial load
duke
parents:
diff changeset
109 if (_ngcv->_old_invocations != h->total_collections()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 fatal("collection in a No_GC_Verifier secured function");
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 if (_ngcv->_verifygc) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // if we were verifying before, then reenable verification
a61af66fc99e Initial load
duke
parents:
diff changeset
119 CollectedHeap* h = Universe::heap();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
a61af66fc99e Initial load
duke
parents:
diff changeset
121 _ngcv->_old_invocations = h->total_collections();
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 }
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // JRT_LEAF rules:
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // A JRT_LEAF method may not interfere with safepointing by
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // 1) acquiring or blocking on a Mutex or JavaLock - checked
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // 2) allocating heap memory - checked
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // 3) executing a VM operation - checked
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // 4) executing a system call (including malloc) that could block or grab a lock
a61af66fc99e Initial load
duke
parents:
diff changeset
132 // 5) invoking GC
a61af66fc99e Initial load
duke
parents:
diff changeset
133 // 6) reaching a safepoint
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // 7) running too long
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // Nor may any method it calls.
a61af66fc99e Initial load
duke
parents:
diff changeset
136 JRT_Leaf_Verifier::JRT_Leaf_Verifier()
a61af66fc99e Initial load
duke
parents:
diff changeset
137 : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC())
a61af66fc99e Initial load
duke
parents:
diff changeset
138 {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 JRT_Leaf_Verifier::~JRT_Leaf_Verifier()
a61af66fc99e Initial load
duke
parents:
diff changeset
142 {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 bool JRT_Leaf_Verifier::should_verify_GC() {
a61af66fc99e Initial load
duke
parents:
diff changeset
146 switch (JavaThread::current()->thread_state()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 case _thread_in_Java:
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // is in a leaf routine, there must be no safepoint.
a61af66fc99e Initial load
duke
parents:
diff changeset
149 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 case _thread_in_native:
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // A native thread is not subject to safepoints.
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // Even while it is in a leaf routine, GC is ok
a61af66fc99e Initial load
duke
parents:
diff changeset
153 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
154 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // Leaf routines cannot be called from other contexts.
a61af66fc99e Initial load
duke
parents:
diff changeset
156 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
157 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160 #endif