|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. 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 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 "classfile/javaClasses.hpp" |
| 27 | +#include "runtime/interfaceSupport.inline.hpp" |
| 28 | +#include "runtime/java.hpp" |
| 29 | +#include "runtime/javaCalls.hpp" |
| 30 | +#include "runtime/monitorDeflationThread.hpp" |
| 31 | +#include "runtime/mutexLocker.hpp" |
| 32 | + |
| 33 | +MonitorDeflationThread* MonitorDeflationThread::_instance = NULL; |
| 34 | + |
| 35 | +void MonitorDeflationThread::initialize() { |
| 36 | + EXCEPTION_MARK; |
| 37 | + |
| 38 | + const char* name = "Monitor Deflation Thread"; |
| 39 | + Handle string = java_lang_String::create_from_str(name, CHECK); |
| 40 | + |
| 41 | + // Initialize thread_oop to put it into the system threadGroup |
| 42 | + Handle thread_group (THREAD, Universe::system_thread_group()); |
| 43 | + Handle thread_oop = JavaCalls::construct_new_instance( |
| 44 | + SystemDictionary::Thread_klass(), |
| 45 | + vmSymbols::threadgroup_string_void_signature(), |
| 46 | + thread_group, |
| 47 | + string, |
| 48 | + CHECK); |
| 49 | + |
| 50 | + { |
| 51 | + MutexLocker mu(THREAD, Threads_lock); |
| 52 | + MonitorDeflationThread* thread = new MonitorDeflationThread(&monitor_deflation_thread_entry); |
| 53 | + |
| 54 | + // At this point it may be possible that no osthread was created for the |
| 55 | + // JavaThread due to lack of memory. We would have to throw an exception |
| 56 | + // in that case. However, since this must work and we do not allow |
| 57 | + // exceptions anyway, check and abort if this fails. |
| 58 | + if (thread == NULL || thread->osthread() == NULL) { |
| 59 | + vm_exit_during_initialization("java.lang.OutOfMemoryError", |
| 60 | + os::native_thread_creation_failed_msg()); |
| 61 | + } |
| 62 | + |
| 63 | + java_lang_Thread::set_thread(thread_oop(), thread); |
| 64 | + java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); |
| 65 | + java_lang_Thread::set_daemon(thread_oop()); |
| 66 | + thread->set_threadObj(thread_oop()); |
| 67 | + _instance = thread; |
| 68 | + |
| 69 | + Threads::add(thread); |
| 70 | + Thread::start(thread); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void MonitorDeflationThread::monitor_deflation_thread_entry(JavaThread* jt, TRAPS) { |
| 75 | + while (true) { |
| 76 | + { |
| 77 | + // Need state transition ThreadBlockInVM so that this thread |
| 78 | + // will be handled by safepoint correctly when this thread is |
| 79 | + // notified at a safepoint. |
| 80 | + |
| 81 | + // This ThreadBlockInVM object is not also considered to be |
| 82 | + // suspend-equivalent because MonitorDeflationThread is not |
| 83 | + // visible to external suspension. |
| 84 | + |
| 85 | + ThreadBlockInVM tbivm(jt); |
| 86 | + |
| 87 | + MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag); |
| 88 | + while (!ObjectSynchronizer::is_async_deflation_needed()) { |
| 89 | + // Wait until notified that there is some work to do. |
| 90 | + // We wait for GuaranteedSafepointInterval so that |
| 91 | + // is_async_deflation_needed() is checked at the same interval. |
| 92 | + ml.wait(GuaranteedSafepointInterval); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + (void)ObjectSynchronizer::deflate_idle_monitors(); |
| 97 | + } |
| 98 | +} |
0 commit comments