0
|
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 # include "incls/_precompiled.incl"
|
|
26 # include "incls/_gcLocker.cpp.incl"
|
|
27
|
|
28 volatile jint GC_locker::_jni_lock_count = 0;
|
|
29 volatile jint GC_locker::_lock_count = 0;
|
|
30 volatile bool GC_locker::_needs_gc = false;
|
|
31 volatile bool GC_locker::_doing_gc = false;
|
|
32
|
|
33 void GC_locker::stall_until_clear() {
|
|
34 assert(!JavaThread::current()->in_critical(), "Would deadlock");
|
|
35 MutexLocker ml(JNICritical_lock);
|
|
36 // Wait for _needs_gc to be cleared
|
|
37 while (GC_locker::needs_gc()) {
|
|
38 JNICritical_lock->wait();
|
|
39 }
|
|
40 }
|
|
41
|
|
42 void GC_locker::jni_lock_slow() {
|
|
43 MutexLocker mu(JNICritical_lock);
|
|
44 // Block entering threads if we know at least one thread is in a
|
|
45 // JNI critical region and we need a GC.
|
|
46 // We check that at least one thread is in a critical region before
|
|
47 // blocking because blocked threads are woken up by a thread exiting
|
|
48 // a JNI critical region.
|
|
49 while ((is_jni_active() && needs_gc()) || _doing_gc) {
|
|
50 JNICritical_lock->wait();
|
|
51 }
|
|
52 jni_lock();
|
|
53 }
|
|
54
|
|
55 void GC_locker::jni_unlock_slow() {
|
|
56 MutexLocker mu(JNICritical_lock);
|
|
57 jni_unlock();
|
|
58 if (needs_gc() && !is_jni_active()) {
|
|
59 // We're the last thread out. Cause a GC to occur.
|
|
60 // GC will also check is_active, so this check is not
|
|
61 // strictly needed. It's added here to make it clear that
|
|
62 // the GC will NOT be performed if any other caller
|
|
63 // of GC_locker::lock() still needs GC locked.
|
|
64 if (!is_active()) {
|
|
65 _doing_gc = true;
|
|
66 {
|
|
67 // Must give up the lock while at a safepoint
|
|
68 MutexUnlocker munlock(JNICritical_lock);
|
|
69 Universe::heap()->collect(GCCause::_gc_locker);
|
|
70 }
|
|
71 _doing_gc = false;
|
|
72 }
|
|
73 clear_needs_gc();
|
|
74 JNICritical_lock->notify_all();
|
|
75 }
|
|
76 }
|
|
77
|
|
78 // Implementation of No_GC_Verifier
|
|
79
|
|
80 #ifdef ASSERT
|
|
81
|
|
82 No_GC_Verifier::No_GC_Verifier(bool verifygc) {
|
|
83 _verifygc = verifygc;
|
|
84 if (_verifygc) {
|
|
85 CollectedHeap* h = Universe::heap();
|
|
86 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
|
|
87 _old_invocations = h->total_collections();
|
|
88 }
|
|
89 }
|
|
90
|
|
91
|
|
92 No_GC_Verifier::~No_GC_Verifier() {
|
|
93 if (_verifygc) {
|
|
94 CollectedHeap* h = Universe::heap();
|
|
95 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
|
|
96 if (_old_invocations != h->total_collections()) {
|
|
97 fatal("collection in a No_GC_Verifier secured function");
|
|
98 }
|
|
99 }
|
|
100 }
|
|
101
|
|
102 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {
|
|
103 _ngcv = ngcv;
|
|
104 if (_ngcv->_verifygc) {
|
|
105 // if we were verifying, then make sure that nothing is
|
|
106 // wrong before we "pause" verification
|
|
107 CollectedHeap* h = Universe::heap();
|
|
108 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
|
|
109 if (_ngcv->_old_invocations != h->total_collections()) {
|
|
110 fatal("collection in a No_GC_Verifier secured function");
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114
|
|
115
|
|
116 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() {
|
|
117 if (_ngcv->_verifygc) {
|
|
118 // if we were verifying before, then reenable verification
|
|
119 CollectedHeap* h = Universe::heap();
|
|
120 assert(!h->is_gc_active(), "GC active during No_GC_Verifier");
|
|
121 _ngcv->_old_invocations = h->total_collections();
|
|
122 }
|
|
123 }
|
|
124
|
|
125
|
|
126 // JRT_LEAF rules:
|
|
127 // A JRT_LEAF method may not interfere with safepointing by
|
|
128 // 1) acquiring or blocking on a Mutex or JavaLock - checked
|
|
129 // 2) allocating heap memory - checked
|
|
130 // 3) executing a VM operation - checked
|
|
131 // 4) executing a system call (including malloc) that could block or grab a lock
|
|
132 // 5) invoking GC
|
|
133 // 6) reaching a safepoint
|
|
134 // 7) running too long
|
|
135 // Nor may any method it calls.
|
|
136 JRT_Leaf_Verifier::JRT_Leaf_Verifier()
|
|
137 : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC())
|
|
138 {
|
|
139 }
|
|
140
|
|
141 JRT_Leaf_Verifier::~JRT_Leaf_Verifier()
|
|
142 {
|
|
143 }
|
|
144
|
|
145 bool JRT_Leaf_Verifier::should_verify_GC() {
|
|
146 switch (JavaThread::current()->thread_state()) {
|
|
147 case _thread_in_Java:
|
|
148 // is in a leaf routine, there must be no safepoint.
|
|
149 return true;
|
|
150 case _thread_in_native:
|
|
151 // A native thread is not subject to safepoints.
|
|
152 // Even while it is in a leaf routine, GC is ok
|
|
153 return false;
|
|
154 default:
|
|
155 // Leaf routines cannot be called from other contexts.
|
|
156 ShouldNotReachHere();
|
|
157 return false;
|
|
158 }
|
|
159 }
|
|
160 #endif
|