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