src/share/vm/gc_implementation/shared/concurrentGCThread.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial