kamg@2511: /* kamg@2511: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. kamg@2511: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. kamg@2511: * kamg@2511: * This code is free software; you can redistribute it and/or modify it kamg@2511: * under the terms of the GNU General Public License version 2 only, as kamg@2511: * published by the Free Software Foundation. kamg@2511: * kamg@2511: * This code is distributed in the hope that it will be useful, but WITHOUT kamg@2511: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or kamg@2511: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License kamg@2511: * version 2 for more details (a copy is included in the LICENSE file that kamg@2511: * accompanied this code). kamg@2511: * kamg@2511: * You should have received a copy of the GNU General Public License version kamg@2511: * 2 along with this work; if not, write to the Free Software Foundation, kamg@2511: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. kamg@2511: * kamg@2511: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA kamg@2511: * or visit www.oracle.com if you need additional information or have any kamg@2511: * questions. kamg@2511: * kamg@2511: */ kamg@2511: kamg@2511: #include "precompiled.hpp" kamg@2511: #include "runtime/interfaceSupport.hpp" kamg@2511: #include "runtime/javaCalls.hpp" kamg@2511: #include "runtime/serviceThread.hpp" kamg@2511: #include "runtime/mutexLocker.hpp" kamg@2511: #include "prims/jvmtiImpl.hpp" kamg@2511: kamg@2511: ServiceThread* ServiceThread::_instance = NULL; kamg@2511: kamg@2511: void ServiceThread::initialize() { kamg@2511: EXCEPTION_MARK; kamg@2511: kamg@2511: instanceKlassHandle klass (THREAD, SystemDictionary::Thread_klass()); kamg@2511: instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); kamg@2511: kamg@2511: const char* name = JDK_Version::is_gte_jdk17x_version() ? kamg@2511: "Service Thread" : "Low Memory Detector"; kamg@2511: kamg@2511: Handle string = java_lang_String::create_from_str(name, CHECK); kamg@2511: kamg@2511: // Initialize thread_oop to put it into the system threadGroup kamg@2511: Handle thread_group (THREAD, Universe::system_thread_group()); kamg@2511: JavaValue result(T_VOID); kamg@2511: JavaCalls::call_special(&result, thread_oop, kamg@2511: klass, kamg@2511: vmSymbols::object_initializer_name(), kamg@2511: vmSymbols::threadgroup_string_void_signature(), kamg@2511: thread_group, kamg@2511: string, kamg@2511: CHECK); kamg@2511: kamg@2511: { kamg@2511: MutexLocker mu(Threads_lock); kamg@2511: ServiceThread* thread = new ServiceThread(&service_thread_entry); kamg@2511: kamg@2511: // At this point it may be possible that no osthread was created for the kamg@2511: // JavaThread due to lack of memory. We would have to throw an exception kamg@2511: // in that case. However, since this must work and we do not allow kamg@2511: // exceptions anyway, check and abort if this fails. kamg@2511: if (thread == NULL || thread->osthread() == NULL) { kamg@2511: vm_exit_during_initialization("java.lang.OutOfMemoryError", kamg@2511: "unable to create new native thread"); kamg@2511: } kamg@2511: kamg@2511: java_lang_Thread::set_thread(thread_oop(), thread); kamg@2511: java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); kamg@2511: java_lang_Thread::set_daemon(thread_oop()); kamg@2511: thread->set_threadObj(thread_oop()); dcubed@2623: _instance = thread; kamg@2511: kamg@2511: Threads::add(thread); kamg@2511: Thread::start(thread); kamg@2511: } kamg@2511: } kamg@2511: kamg@2511: void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { kamg@2511: while (true) { kamg@2511: bool sensors_changed = false; kamg@2511: bool has_jvmti_events = false; kamg@2511: JvmtiDeferredEvent jvmti_event; kamg@2511: { kamg@2511: // Need state transition ThreadBlockInVM so that this thread kamg@2511: // will be handled by safepoint correctly when this thread is kamg@2511: // notified at a safepoint. kamg@2511: kamg@2511: // This ThreadBlockInVM object is not also considered to be kamg@2511: // suspend-equivalent because ServiceThread is not visible to kamg@2511: // external suspension. kamg@2511: kamg@2511: ThreadBlockInVM tbivm(jt); kamg@2511: kamg@2511: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); kamg@2511: while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) && kamg@2511: !(has_jvmti_events = JvmtiDeferredEventQueue::has_events())) { kamg@2511: // wait until one of the sensors has pending requests, or there is a kamg@2511: // pending JVMTI event to post kamg@2511: Service_lock->wait(Mutex::_no_safepoint_check_flag); kamg@2511: } kamg@2511: kamg@2511: if (has_jvmti_events) { kamg@2511: jvmti_event = JvmtiDeferredEventQueue::dequeue(); kamg@2511: } kamg@2511: } kamg@2511: kamg@2511: if (has_jvmti_events) { kamg@2511: jvmti_event.post(); kamg@2511: } kamg@2511: kamg@2511: if (sensors_changed) { kamg@2511: LowMemoryDetector::process_sensor_changes(jt); kamg@2511: } kamg@2511: } kamg@2511: } kamg@2511: kamg@2511: bool ServiceThread::is_service_thread(Thread* thread) { kamg@2511: return thread == _instance; kamg@2511: }