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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "memory/gcLocker.inline.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "memory/sharedHeap.hpp"
29
30 volatile jint GC_locker::_jni_lock_count = 0;
31 volatile jint GC_locker::_lock_count = 0;
32 volatile bool GC_locker::_needs_gc = false;
33 volatile bool GC_locker::_doing_gc = false;
34
35 #ifdef ASSERT
36 volatile jint GC_locker::_debug_jni_lock_count = 0;
37 #endif
38
39
40 #ifdef ASSERT
41 void GC_locker::verify_critical_count() {
42 if (SafepointSynchronize::is_at_safepoint()) {
43 assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
44 int count = 0;
45 // Count the number of threads with critical operations in progress
46 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
47 if (thr->in_critical()) {
48 count++;
49 }
50 }
51 if (_jni_lock_count != count) {
85 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
86 gclog_or_tty->print_cr("%.3f: Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.",
87 gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
88 }
89 }
90
91 // Wait for _needs_gc to be cleared
92 while (needs_gc()) {
93 JNICritical_lock->wait();
94 }
95 }
96
97 void GC_locker::jni_lock(JavaThread* thread) {
98 assert(!thread->in_critical(), "shouldn't currently be in a critical region");
99 MutexLocker mu(JNICritical_lock);
100 // Block entering threads if we know at least one thread is in a
101 // JNI critical region and we need a GC.
102 // We check that at least one thread is in a critical region before
103 // blocking because blocked threads are woken up by a thread exiting
104 // a JNI critical region.
105 while ((needs_gc() && is_jni_active()) || _doing_gc) {
106 JNICritical_lock->wait();
107 }
108 thread->enter_critical();
109 _jni_lock_count++;
110 increment_debug_jni_lock_count();
111 }
112
113 void GC_locker::jni_unlock(JavaThread* thread) {
114 assert(thread->in_last_critical(), "should be exiting critical region");
115 MutexLocker mu(JNICritical_lock);
116 _jni_lock_count--;
117 decrement_debug_jni_lock_count();
118 thread->exit_critical();
119 if (needs_gc() && !is_jni_active()) {
120 // We're the last thread out. Cause a GC to occur.
121 // GC will also check is_active, so this check is not
122 // strictly needed. It's added here to make it clear that
123 // the GC will NOT be performed if any other caller
124 // of GC_locker::lock() still needs GC locked.
125 if (!is_active_internal()) {
126 _doing_gc = true;
127 {
128 // Must give up the lock while at a safepoint
129 MutexUnlocker munlock(JNICritical_lock);
130 if (PrintJNIGCStalls && PrintGCDetails) {
131 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
132 gclog_or_tty->print_cr("%.3f: Thread \"%s\" is performing GC after exiting critical section, %d locked",
133 gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
134 }
135 Universe::heap()->collect(GCCause::_gc_locker);
136 }
137 _doing_gc = false;
138 }
139
140 _needs_gc = false;
141 JNICritical_lock->notify_all();
142 }
143 }
144
145 // Implementation of No_GC_Verifier
146
147 #ifdef ASSERT
148
149 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
150 _verifygc = verifygc;
151 if (_verifygc) {
152 CollectedHeap* h = Universe::heap();
153 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
154 _old_invocations = h->total_collections();
155 }
156 }
157
158
159 No_GC_Verifier::~No_GC_Verifier() {
|
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "memory/gcLocker.inline.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "memory/sharedHeap.hpp"
29
30 volatile jint GC_locker::_jni_lock_count = 0;
31 volatile bool GC_locker::_needs_gc = false;
32 volatile bool GC_locker::_doing_gc = false;
33
34 #ifdef ASSERT
35 volatile jint GC_locker::_debug_jni_lock_count = 0;
36 #endif
37
38
39 #ifdef ASSERT
40 void GC_locker::verify_critical_count() {
41 if (SafepointSynchronize::is_at_safepoint()) {
42 assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree");
43 int count = 0;
44 // Count the number of threads with critical operations in progress
45 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) {
46 if (thr->in_critical()) {
47 count++;
48 }
49 }
50 if (_jni_lock_count != count) {
84 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
85 gclog_or_tty->print_cr("%.3f: Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.",
86 gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
87 }
88 }
89
90 // Wait for _needs_gc to be cleared
91 while (needs_gc()) {
92 JNICritical_lock->wait();
93 }
94 }
95
96 void GC_locker::jni_lock(JavaThread* thread) {
97 assert(!thread->in_critical(), "shouldn't currently be in a critical region");
98 MutexLocker mu(JNICritical_lock);
99 // Block entering threads if we know at least one thread is in a
100 // JNI critical region and we need a GC.
101 // We check that at least one thread is in a critical region before
102 // blocking because blocked threads are woken up by a thread exiting
103 // a JNI critical region.
104 while (is_active_and_needs_gc() || _doing_gc) {
105 JNICritical_lock->wait();
106 }
107 thread->enter_critical();
108 _jni_lock_count++;
109 increment_debug_jni_lock_count();
110 }
111
112 void GC_locker::jni_unlock(JavaThread* thread) {
113 assert(thread->in_last_critical(), "should be exiting critical region");
114 MutexLocker mu(JNICritical_lock);
115 _jni_lock_count--;
116 decrement_debug_jni_lock_count();
117 thread->exit_critical();
118 if (needs_gc() && !is_active_internal()) {
119 // We're the last thread out. Cause a GC to occur.
120 _doing_gc = true;
121 {
122 // Must give up the lock while at a safepoint
123 MutexUnlocker munlock(JNICritical_lock);
124 if (PrintJNIGCStalls && PrintGCDetails) {
125 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8
126 gclog_or_tty->print_cr("%.3f: Thread \"%s\" is performing GC after exiting critical section, %d locked",
127 gclog_or_tty->time_stamp().seconds(), Thread::current()->name(), _jni_lock_count);
128 }
129 Universe::heap()->collect(GCCause::_gc_locker);
130 }
131 _doing_gc = false;
132 _needs_gc = false;
133 JNICritical_lock->notify_all();
134 }
135 }
136
137 // Implementation of No_GC_Verifier
138
139 #ifdef ASSERT
140
141 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
142 _verifygc = verifygc;
143 if (_verifygc) {
144 CollectedHeap* h = Universe::heap();
145 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
146 _old_invocations = h->total_collections();
147 }
148 }
149
150
151 No_GC_Verifier::~No_GC_Verifier() {
|