Mercurial > hg > truffle
annotate src/share/vm/memory/gcLocker.hpp @ 614:3db67f76d308
Merge
author | acorn |
---|---|
date | Thu, 05 Mar 2009 22:07:29 -0500 |
parents | d1605aabd0a1 |
children | 821269eca479 |
rev | line source |
---|---|
0 | 1 /* |
196 | 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
0 | 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 | |
98
deb97b8ef02b
6679708: No_Safepoint_Verifier and BacktraceBuilder have uninitialized fields
never
parents:
0
diff
changeset
|
187 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) : |
deb97b8ef02b
6679708: No_Safepoint_Verifier and BacktraceBuilder have uninitialized fields
never
parents:
0
diff
changeset
|
188 No_GC_Verifier(verifygc), |
deb97b8ef02b
6679708: No_Safepoint_Verifier and BacktraceBuilder have uninitialized fields
never
parents:
0
diff
changeset
|
189 _activated(activated) { |
0 | 190 _thread = Thread::current(); |
191 if (_activated) { | |
192 _thread->_allow_allocation_count++; | |
193 _thread->_allow_safepoint_count++; | |
194 } | |
195 } | |
196 | |
197 ~No_Safepoint_Verifier() { | |
198 if (_activated) { | |
199 _thread->_allow_allocation_count--; | |
200 _thread->_allow_safepoint_count--; | |
201 } | |
202 } | |
203 #else | |
204 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){} | |
205 ~No_Safepoint_Verifier() {} | |
206 #endif | |
207 }; | |
208 | |
209 // A Pause_No_Safepoint_Verifier is used to temporarily pause the | |
210 // behavior of a No_Safepoint_Verifier object. If we are not in debug | |
211 // mode then there is nothing to do. If the No_Safepoint_Verifier | |
212 // object has an _activated value of false, then there is nothing to | |
213 // do for safepoint and allocation checking, but there may still be | |
214 // something to do for the underlying No_GC_Verifier object. | |
215 | |
216 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier { | |
217 private: | |
218 No_Safepoint_Verifier * _nsv; | |
219 | |
220 public: | |
221 #ifdef ASSERT | |
222 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) | |
223 : Pause_No_GC_Verifier(nsv) { | |
224 | |
225 _nsv = nsv; | |
226 if (_nsv->_activated) { | |
227 _nsv->_thread->_allow_allocation_count--; | |
228 _nsv->_thread->_allow_safepoint_count--; | |
229 } | |
230 } | |
231 | |
232 ~Pause_No_Safepoint_Verifier() { | |
233 if (_nsv->_activated) { | |
234 _nsv->_thread->_allow_allocation_count++; | |
235 _nsv->_thread->_allow_safepoint_count++; | |
236 } | |
237 } | |
238 #else | |
239 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) | |
240 : Pause_No_GC_Verifier(nsv) {} | |
241 ~Pause_No_Safepoint_Verifier() {} | |
242 #endif | |
243 }; | |
244 | |
245 // JRT_LEAF currently can be called from either _thread_in_Java or | |
246 // _thread_in_native mode. In _thread_in_native, it is ok | |
247 // for another thread to trigger GC. The rest of the JRT_LEAF | |
248 // rules apply. | |
249 class JRT_Leaf_Verifier : public No_Safepoint_Verifier { | |
250 static bool should_verify_GC(); | |
251 public: | |
252 #ifdef ASSERT | |
253 JRT_Leaf_Verifier(); | |
254 ~JRT_Leaf_Verifier(); | |
255 #else | |
256 JRT_Leaf_Verifier() {} | |
257 ~JRT_Leaf_Verifier() {} | |
258 #endif | |
259 }; | |
260 | |
261 // A No_Alloc_Verifier object can be placed in methods where one assumes that | |
262 // no allocation will occur. The destructor will verify this property | |
263 // unless the constructor is called with argument false (not activated). | |
264 // | |
265 // The check will only be done in debug mode and if activated. | |
266 // Note: this only makes sense at safepoints (otherwise, other threads may | |
267 // allocate concurrently.) | |
268 | |
269 class No_Alloc_Verifier : public StackObj { | |
270 private: | |
271 bool _activated; | |
272 | |
273 public: | |
274 #ifdef ASSERT | |
275 No_Alloc_Verifier(bool activated = true) { | |
276 _activated = activated; | |
277 if (_activated) Thread::current()->_allow_allocation_count++; | |
278 } | |
279 | |
280 ~No_Alloc_Verifier() { | |
281 if (_activated) Thread::current()->_allow_allocation_count--; | |
282 } | |
283 #else | |
284 No_Alloc_Verifier(bool activated = true) {} | |
285 ~No_Alloc_Verifier() {} | |
286 #endif | |
287 }; |