1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/vmThread.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,155 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2012, 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 +#ifndef SHARE_VM_RUNTIME_VMTHREAD_HPP 1.29 +#define SHARE_VM_RUNTIME_VMTHREAD_HPP 1.30 + 1.31 +#include "runtime/perfData.hpp" 1.32 +#include "runtime/thread.inline.hpp" 1.33 +#include "runtime/vm_operations.hpp" 1.34 + 1.35 +// 1.36 +// Prioritized queue of VM operations. 1.37 +// 1.38 +// Encapsulates both queue management and 1.39 +// and priority policy 1.40 +// 1.41 +class VMOperationQueue : public CHeapObj<mtInternal> { 1.42 + private: 1.43 + enum Priorities { 1.44 + SafepointPriority, // Highest priority (operation executed at a safepoint) 1.45 + MediumPriority, // Medium priority 1.46 + nof_priorities 1.47 + }; 1.48 + 1.49 + // We maintain a doubled linked list, with explicit count. 1.50 + int _queue_length[nof_priorities]; 1.51 + int _queue_counter; 1.52 + VM_Operation* _queue [nof_priorities]; 1.53 + // we also allow the vmThread to register the ops it has drained so we 1.54 + // can scan them from oops_do 1.55 + VM_Operation* _drain_list; 1.56 + 1.57 + // Double-linked non-empty list insert. 1.58 + void insert(VM_Operation* q,VM_Operation* n); 1.59 + void unlink(VM_Operation* q); 1.60 + 1.61 + // Basic queue manipulation 1.62 + bool queue_empty (int prio); 1.63 + void queue_add_front (int prio, VM_Operation *op); 1.64 + void queue_add_back (int prio, VM_Operation *op); 1.65 + VM_Operation* queue_remove_front(int prio); 1.66 + void queue_oops_do(int queue, OopClosure* f); 1.67 + void drain_list_oops_do(OopClosure* f); 1.68 + VM_Operation* queue_drain(int prio); 1.69 + // lock-free query: may return the wrong answer but must not break 1.70 + bool queue_peek(int prio) { return _queue_length[prio] > 0; } 1.71 + 1.72 + public: 1.73 + VMOperationQueue(); 1.74 + 1.75 + // Highlevel operations. Encapsulates policy 1.76 + bool add(VM_Operation *op); 1.77 + VM_Operation* remove_next(); // Returns next or null 1.78 + VM_Operation* remove_next_at_safepoint_priority() { return queue_remove_front(SafepointPriority); } 1.79 + VM_Operation* drain_at_safepoint_priority() { return queue_drain(SafepointPriority); } 1.80 + void set_drain_list(VM_Operation* list) { _drain_list = list; } 1.81 + bool peek_at_safepoint_priority() { return queue_peek(SafepointPriority); } 1.82 + 1.83 + // GC support 1.84 + void oops_do(OopClosure* f); 1.85 + 1.86 + void verify_queue(int prio) PRODUCT_RETURN; 1.87 +}; 1.88 + 1.89 + 1.90 +// 1.91 +// A single VMThread (the primordial thread) spawns all other threads 1.92 +// and is itself used by other threads to offload heavy vm operations 1.93 +// like scavenge, garbage_collect etc. 1.94 +// 1.95 + 1.96 +class VMThread: public NamedThread { 1.97 + private: 1.98 + static ThreadPriority _current_priority; 1.99 + 1.100 + static bool _should_terminate; 1.101 + static bool _terminated; 1.102 + static Monitor * _terminate_lock; 1.103 + static PerfCounter* _perf_accumulated_vm_operation_time; 1.104 + 1.105 + void evaluate_operation(VM_Operation* op); 1.106 + public: 1.107 + // Constructor 1.108 + VMThread(); 1.109 + 1.110 + // Tester 1.111 + bool is_VM_thread() const { return true; } 1.112 + bool is_GC_thread() const { return true; } 1.113 + 1.114 + // The ever running loop for the VMThread 1.115 + void loop(); 1.116 + 1.117 + // Called to stop the VM thread 1.118 + static void wait_for_vm_thread_exit(); 1.119 + static bool should_terminate() { return _should_terminate; } 1.120 + static bool is_terminated() { return _terminated == true; } 1.121 + 1.122 + // Execution of vm operation 1.123 + static void execute(VM_Operation* op); 1.124 + 1.125 + // Returns the current vm operation if any. 1.126 + static VM_Operation* vm_operation() { return _cur_vm_operation; } 1.127 + 1.128 + // Returns the single instance of VMThread. 1.129 + static VMThread* vm_thread() { return _vm_thread; } 1.130 + 1.131 + // GC support 1.132 + void oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf); 1.133 + 1.134 + // Debugging 1.135 + void print_on(outputStream* st) const; 1.136 + void print() const { print_on(tty); } 1.137 + void verify(); 1.138 + 1.139 + // Performance measurement 1.140 + static PerfCounter* perf_accumulated_vm_operation_time() { return _perf_accumulated_vm_operation_time; } 1.141 + 1.142 + // Entry for starting vm thread 1.143 + virtual void run(); 1.144 + 1.145 + // Creations/Destructions 1.146 + static void create(); 1.147 + static void destroy(); 1.148 + 1.149 + private: 1.150 + // VM_Operation support 1.151 + static VM_Operation* _cur_vm_operation; // Current VM operation 1.152 + static VMOperationQueue* _vm_queue; // Queue (w/ policy) of VM operations 1.153 + 1.154 + // Pointer to single-instance of VM thread 1.155 + static VMThread* _vm_thread; 1.156 +}; 1.157 + 1.158 +#endif // SHARE_VM_RUNTIME_VMTHREAD_HPP