1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/serviceThread.cpp Wed Feb 02 14:38:01 2011 -0500 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "runtime/interfaceSupport.hpp" 1.30 +#include "runtime/javaCalls.hpp" 1.31 +#include "runtime/serviceThread.hpp" 1.32 +#include "runtime/mutexLocker.hpp" 1.33 +#include "prims/jvmtiImpl.hpp" 1.34 + 1.35 +ServiceThread* ServiceThread::_instance = NULL; 1.36 + 1.37 +void ServiceThread::initialize() { 1.38 + EXCEPTION_MARK; 1.39 + 1.40 + instanceKlassHandle klass (THREAD, SystemDictionary::Thread_klass()); 1.41 + instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); 1.42 + 1.43 + const char* name = JDK_Version::is_gte_jdk17x_version() ? 1.44 + "Service Thread" : "Low Memory Detector"; 1.45 + 1.46 + Handle string = java_lang_String::create_from_str(name, CHECK); 1.47 + 1.48 + // Initialize thread_oop to put it into the system threadGroup 1.49 + Handle thread_group (THREAD, Universe::system_thread_group()); 1.50 + JavaValue result(T_VOID); 1.51 + JavaCalls::call_special(&result, thread_oop, 1.52 + klass, 1.53 + vmSymbols::object_initializer_name(), 1.54 + vmSymbols::threadgroup_string_void_signature(), 1.55 + thread_group, 1.56 + string, 1.57 + CHECK); 1.58 + 1.59 + { 1.60 + MutexLocker mu(Threads_lock); 1.61 + ServiceThread* thread = new ServiceThread(&service_thread_entry); 1.62 + 1.63 + // At this point it may be possible that no osthread was created for the 1.64 + // JavaThread due to lack of memory. We would have to throw an exception 1.65 + // in that case. However, since this must work and we do not allow 1.66 + // exceptions anyway, check and abort if this fails. 1.67 + if (thread == NULL || thread->osthread() == NULL) { 1.68 + vm_exit_during_initialization("java.lang.OutOfMemoryError", 1.69 + "unable to create new native thread"); 1.70 + } 1.71 + 1.72 + java_lang_Thread::set_thread(thread_oop(), thread); 1.73 + java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 1.74 + java_lang_Thread::set_daemon(thread_oop()); 1.75 + thread->set_threadObj(thread_oop()); 1.76 + 1.77 + Threads::add(thread); 1.78 + Thread::start(thread); 1.79 + 1.80 + _instance = thread; 1.81 + } 1.82 +} 1.83 + 1.84 +void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { 1.85 + while (true) { 1.86 + bool sensors_changed = false; 1.87 + bool has_jvmti_events = false; 1.88 + JvmtiDeferredEvent jvmti_event; 1.89 + { 1.90 + // Need state transition ThreadBlockInVM so that this thread 1.91 + // will be handled by safepoint correctly when this thread is 1.92 + // notified at a safepoint. 1.93 + 1.94 + // This ThreadBlockInVM object is not also considered to be 1.95 + // suspend-equivalent because ServiceThread is not visible to 1.96 + // external suspension. 1.97 + 1.98 + ThreadBlockInVM tbivm(jt); 1.99 + 1.100 + MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); 1.101 + while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) && 1.102 + !(has_jvmti_events = JvmtiDeferredEventQueue::has_events())) { 1.103 + // wait until one of the sensors has pending requests, or there is a 1.104 + // pending JVMTI event to post 1.105 + Service_lock->wait(Mutex::_no_safepoint_check_flag); 1.106 + } 1.107 + 1.108 + if (has_jvmti_events) { 1.109 + jvmti_event = JvmtiDeferredEventQueue::dequeue(); 1.110 + } 1.111 + } 1.112 + 1.113 + if (has_jvmti_events) { 1.114 + jvmti_event.post(); 1.115 + } 1.116 + 1.117 + if (sensors_changed) { 1.118 + LowMemoryDetector::process_sensor_changes(jt); 1.119 + } 1.120 + } 1.121 +} 1.122 + 1.123 +bool ServiceThread::is_service_thread(Thread* thread) { 1.124 + return thread == _instance; 1.125 +}