Print this page
rev 2755 : 7099824: G1: we should take the pending list lock before doing the remark pause
Summary: Acquire the pending list lock in the prologue method of G1's
concurrent VM_Operation and release the lock in the epilogue() method.
The locking/unlocking order of the pending list lock and the Heap_lock
should match that in the prologue and epilogue methods of VM_GC_Operation.
Reviewed-by:
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/shared/concurrentGCThread.cpp
+++ new/src/share/vm/gc_implementation/shared/concurrentGCThread.cpp
1 1 /*
2 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #include "precompiled.hpp"
26 26 #include "classfile/systemDictionary.hpp"
27 27 #include "gc_implementation/shared/concurrentGCThread.hpp"
28 28 #include "oops/instanceRefKlass.hpp"
29 29 #include "oops/oop.inline.hpp"
30 30 #include "runtime/init.hpp"
31 31 #include "runtime/interfaceSupport.hpp"
32 32 #include "runtime/java.hpp"
33 33 #include "runtime/javaCalls.hpp"
34 34
35 35 // CopyrightVersion 1.2
36 36
37 37 int ConcurrentGCThread::_CGC_flag = CGC_nil;
38 38
39 39 SuspendibleThreadSet ConcurrentGCThread::_sts;
40 40
41 41 ConcurrentGCThread::ConcurrentGCThread() :
42 42 _should_terminate(false), _has_terminated(false) {
43 43 _sts.initialize();
44 44 };
45 45
46 46 void ConcurrentGCThread::safepoint_synchronize() {
47 47 _sts.suspend_all();
48 48 }
49 49
50 50 void ConcurrentGCThread::safepoint_desynchronize() {
51 51 _sts.resume_all();
52 52 }
53 53
54 54 void ConcurrentGCThread::create_and_start() {
55 55 if (os::create_thread(this, os::cgc_thread)) {
56 56 // XXX: need to set this to low priority
57 57 // unless "agressive mode" set; priority
58 58 // should be just less than that of VMThread.
59 59 os::set_priority(this, NearMaxPriority);
60 60 if (!_should_terminate && !DisableStartThread) {
61 61 os::start_thread(this);
62 62 }
63 63 }
64 64 }
65 65
66 66 void ConcurrentGCThread::initialize_in_thread() {
67 67 this->record_stack_base_and_size();
68 68 this->initialize_thread_local_storage();
69 69 this->set_active_handles(JNIHandleBlock::allocate_block());
70 70 // From this time Thread::current() should be working.
71 71 assert(this == Thread::current(), "just checking");
72 72 }
73 73
74 74 void ConcurrentGCThread::wait_for_universe_init() {
75 75 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
76 76 while (!is_init_completed() && !_should_terminate) {
77 77 CGC_lock->wait(Mutex::_no_safepoint_check_flag, 200);
78 78 }
79 79 }
80 80
81 81 void ConcurrentGCThread::terminate() {
82 82 // Signal that it is terminated
83 83 {
84 84 MutexLockerEx mu(Terminator_lock,
85 85 Mutex::_no_safepoint_check_flag);
86 86 _has_terminated = true;
87 87 Terminator_lock->notify();
88 88 }
89 89
90 90 // Thread destructor usually does this..
91 91 ThreadLocalStorage::set_thread(NULL);
92 92 }
93 93
94 94
95 95 void SuspendibleThreadSet::initialize_work() {
96 96 MutexLocker x(STS_init_lock);
97 97 if (!_initialized) {
98 98 _m = new Monitor(Mutex::leaf,
99 99 "SuspendibleThreadSetLock", true);
100 100 _async = 0;
101 101 _async_stop = false;
102 102 _async_stopped = 0;
103 103 _initialized = true;
104 104 }
105 105 }
106 106
107 107 void SuspendibleThreadSet::join() {
108 108 initialize();
109 109 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
110 110 while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
111 111 _async++;
112 112 assert(_async > 0, "Huh.");
113 113 }
114 114
115 115 void SuspendibleThreadSet::leave() {
116 116 assert(_initialized, "Must be initialized.");
117 117 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
118 118 _async--;
119 119 assert(_async >= 0, "Huh.");
120 120 if (_async_stop) _m->notify_all();
121 121 }
122 122
123 123 void SuspendibleThreadSet::yield(const char* id) {
124 124 assert(_initialized, "Must be initialized.");
125 125 if (_async_stop) {
126 126 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
127 127 if (_async_stop) {
128 128 _async_stopped++;
129 129 assert(_async_stopped > 0, "Huh.");
130 130 if (_async_stopped == _async) {
131 131 if (ConcGCYieldTimeout > 0) {
132 132 double now = os::elapsedTime();
133 133 guarantee((now - _suspend_all_start) * 1000.0 <
134 134 (double)ConcGCYieldTimeout,
135 135 "Long delay; whodunit?");
136 136 }
137 137 }
138 138 _m->notify_all();
139 139 while (_async_stop) _m->wait(Mutex::_no_safepoint_check_flag);
140 140 _async_stopped--;
141 141 assert(_async >= 0, "Huh");
142 142 _m->notify_all();
143 143 }
144 144 }
145 145 }
146 146
147 147 void SuspendibleThreadSet::suspend_all() {
148 148 initialize(); // If necessary.
149 149 if (ConcGCYieldTimeout > 0) {
150 150 _suspend_all_start = os::elapsedTime();
151 151 }
152 152 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
153 153 assert(!_async_stop, "Only one at a time.");
154 154 _async_stop = true;
155 155 while (_async_stopped < _async) _m->wait(Mutex::_no_safepoint_check_flag);
156 156 }
157 157
158 158 void SuspendibleThreadSet::resume_all() {
159 159 assert(_initialized, "Must be initialized.");
160 160 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
161 161 assert(_async_stopped == _async, "Huh.");
162 162 _async_stop = false;
163 163 _m->notify_all();
164 164 }
165 165
166 166 static void _sltLoop(JavaThread* thread, TRAPS) {
167 167 SurrogateLockerThread* slt = (SurrogateLockerThread*)thread;
168 168 slt->loop();
169 169 }
170 170
171 171 SurrogateLockerThread::SurrogateLockerThread() :
172 172 JavaThread(&_sltLoop),
173 173 _monitor(Mutex::nonleaf, "SLTMonitor"),
174 174 _buffer(empty)
175 175 {}
176 176
177 177 SurrogateLockerThread* SurrogateLockerThread::make(TRAPS) {
178 178 klassOop k =
179 179 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(),
180 180 true, CHECK_NULL);
181 181 instanceKlassHandle klass (THREAD, k);
182 182 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK_NULL);
183 183
184 184 const char thread_name[] = "Surrogate Locker Thread (Concurrent GC)";
185 185 Handle string = java_lang_String::create_from_str(thread_name, CHECK_NULL);
186 186
187 187 // Initialize thread_oop to put it into the system threadGroup
188 188 Handle thread_group (THREAD, Universe::system_thread_group());
189 189 JavaValue result(T_VOID);
190 190 JavaCalls::call_special(&result, thread_oop,
191 191 klass,
192 192 vmSymbols::object_initializer_name(),
193 193 vmSymbols::threadgroup_string_void_signature(),
194 194 thread_group,
195 195 string,
196 196 CHECK_NULL);
197 197
198 198 SurrogateLockerThread* res;
199 199 {
200 200 MutexLocker mu(Threads_lock);
201 201 res = new SurrogateLockerThread();
202 202
203 203 // At this point it may be possible that no osthread was created for the
204 204 // JavaThread due to lack of memory. We would have to throw an exception
205 205 // in that case. However, since this must work and we do not allow
206 206 // exceptions anyway, check and abort if this fails.
207 207 if (res == NULL || res->osthread() == NULL) {
208 208 vm_exit_during_initialization("java.lang.OutOfMemoryError",
209 209 "unable to create new native thread");
210 210 }
211 211 java_lang_Thread::set_thread(thread_oop(), res);
212 212 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
213 213 java_lang_Thread::set_daemon(thread_oop());
214 214
215 215 res->set_threadObj(thread_oop());
216 216 Threads::add(res);
↓ open down ↓ |
216 lines elided |
↑ open up ↑ |
217 217 Thread::start(res);
218 218 }
219 219 os::yield(); // This seems to help with initial start-up of SLT
220 220 return res;
221 221 }
222 222
223 223 void SurrogateLockerThread::manipulatePLL(SLT_msg_type msg) {
224 224 MutexLockerEx x(&_monitor, Mutex::_no_safepoint_check_flag);
225 225 assert(_buffer == empty, "Should be empty");
226 226 assert(msg != empty, "empty message");
227 + assert(!Heap_lock->owned_by_self(), "Heap_lock owned by requesting thread");
228 +
227 229 _buffer = msg;
228 230 while (_buffer != empty) {
229 231 _monitor.notify();
230 232 _monitor.wait(Mutex::_no_safepoint_check_flag);
231 233 }
232 234 }
233 235
234 236 // ======= Surrogate Locker Thread =============
235 237
236 238 void SurrogateLockerThread::loop() {
237 239 BasicLock pll_basic_lock;
238 240 SLT_msg_type msg;
239 241 debug_only(unsigned int owned = 0;)
240 242
241 243 while (/* !isTerminated() */ 1) {
242 244 {
243 245 MutexLocker x(&_monitor);
244 246 // Since we are a JavaThread, we can't be here at a safepoint.
245 247 assert(!SafepointSynchronize::is_at_safepoint(),
246 248 "SLT is a JavaThread");
247 249 // wait for msg buffer to become non-empty
248 250 while (_buffer == empty) {
249 251 _monitor.notify();
250 252 _monitor.wait();
251 253 }
252 254 msg = _buffer;
253 255 }
254 256 switch(msg) {
255 257 case acquirePLL: {
256 258 instanceRefKlass::acquire_pending_list_lock(&pll_basic_lock);
257 259 debug_only(owned++;)
258 260 break;
259 261 }
260 262 case releaseAndNotifyPLL: {
261 263 assert(owned > 0, "Don't have PLL");
262 264 instanceRefKlass::release_and_notify_pending_list_lock(&pll_basic_lock);
263 265 debug_only(owned--;)
264 266 break;
265 267 }
266 268 case empty:
267 269 default: {
268 270 guarantee(false,"Unexpected message in _buffer");
269 271 break;
270 272 }
271 273 }
272 274 {
273 275 MutexLocker x(&_monitor);
274 276 // Since we are a JavaThread, we can't be here at a safepoint.
275 277 assert(!SafepointSynchronize::is_at_safepoint(),
276 278 "SLT is a JavaThread");
277 279 _buffer = empty;
278 280 _monitor.notify();
279 281 }
280 282 }
281 283 assert(!_monitor.owned_by_self(), "Should unlock before exit.");
282 284 }
283 285
284 286
285 287 // ===== STS Access From Outside CGCT =====
286 288
287 289 void ConcurrentGCThread::stsYield(const char* id) {
288 290 assert( Thread::current()->is_ConcurrentGC_thread(),
289 291 "only a conc GC thread can call this" );
290 292 _sts.yield(id);
291 293 }
292 294
293 295 bool ConcurrentGCThread::stsShouldYield() {
294 296 assert( Thread::current()->is_ConcurrentGC_thread(),
295 297 "only a conc GC thread can call this" );
296 298 return _sts.should_yield();
297 299 }
298 300
299 301 void ConcurrentGCThread::stsJoin() {
300 302 assert( Thread::current()->is_ConcurrentGC_thread(),
301 303 "only a conc GC thread can call this" );
302 304 _sts.join();
303 305 }
304 306
305 307 void ConcurrentGCThread::stsLeave() {
306 308 assert( Thread::current()->is_ConcurrentGC_thread(),
307 309 "only a conc GC thread can call this" );
308 310 _sts.leave();
309 311 }
↓ open down ↓ |
73 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX