1 /* 2 * Copyright (c) 1998, 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 #ifndef SHARE_RUNTIME_VMTHREAD_HPP 26 #define SHARE_RUNTIME_VMTHREAD_HPP 27 28 #include "runtime/perfData.hpp" 29 #include "runtime/thread.hpp" 30 #include "runtime/task.hpp" 31 #include "runtime/vmOperations.hpp" 32 33 // VM operation timeout handling: warn or abort the VM when VM operation takes 34 // too long. Periodic tasks do not participate in safepoint protocol, and therefore 35 // can fire when application threads are stopped. 36 37 class VMOperationTimeoutTask : public PeriodicTask { 38 private: 39 volatile int _armed; 40 jlong _arm_time; 41 42 public: VMOperationTimeoutTask(size_t interval_time)43 VMOperationTimeoutTask(size_t interval_time) : 44 PeriodicTask(interval_time), _armed(0), _arm_time(0) {} 45 46 virtual void task(); 47 48 bool is_armed(); 49 void arm(); 50 void disarm(); 51 }; 52 53 // 54 // A single VMThread (the primordial thread) spawns all other threads 55 // and is itself used by other threads to offload heavy vm operations 56 // like scavenge, garbage_collect etc. 57 // 58 59 class VMThread: public NamedThread { 60 private: 61 static ThreadPriority _current_priority; 62 63 static bool _should_terminate; 64 static bool _terminated; 65 static Monitor * _terminate_lock; 66 static PerfCounter* _perf_accumulated_vm_operation_time; 67 68 static VMOperationTimeoutTask* _timeout_task; 69 70 static bool handshake_alot(); 71 static void setup_periodic_safepoint_if_needed(); 72 73 void evaluate_operation(VM_Operation* op); 74 void inner_execute(VM_Operation* op); 75 void wait_for_operation(); 76 77 public: 78 // Constructor 79 VMThread(); 80 81 // No destruction allowed ~VMThread()82 ~VMThread() { 83 guarantee(false, "VMThread deletion must fix the race with VM termination"); 84 } 85 86 87 // Tester is_VM_thread() const88 bool is_VM_thread() const { return true; } 89 90 // The ever running loop for the VMThread 91 void loop(); 92 93 // Called to stop the VM thread 94 static void wait_for_vm_thread_exit(); should_terminate()95 static bool should_terminate() { return _should_terminate; } is_terminated()96 static bool is_terminated() { return _terminated == true; } 97 98 // Execution of vm operation 99 static void execute(VM_Operation* op); 100 101 // Returns the current vm operation if any. vm_operation()102 static VM_Operation* vm_operation() { 103 assert(Thread::current()->is_VM_thread(), "Must be"); 104 return _cur_vm_operation; 105 } 106 vm_op_type()107 static VM_Operation::VMOp_Type vm_op_type() { 108 VM_Operation* op = vm_operation(); 109 assert(op != NULL, "sanity"); 110 return op->type(); 111 } 112 113 // Returns the single instance of VMThread. vm_thread()114 static VMThread* vm_thread() { return _vm_thread; } 115 116 void verify(); 117 118 // Performance measurement perf_accumulated_vm_operation_time()119 static PerfCounter* perf_accumulated_vm_operation_time() { 120 return _perf_accumulated_vm_operation_time; 121 } 122 123 // Entry for starting vm thread 124 virtual void run(); 125 126 // Creations/Destructions 127 static void create(); 128 static void destroy(); 129 130 static void wait_until_executed(VM_Operation* op); 131 132 private: 133 // VM_Operation support 134 static VM_Operation* _cur_vm_operation; // Current VM operation 135 static VM_Operation* _next_vm_operation; // Next VM operation 136 137 bool set_next_operation(VM_Operation *op); // Set the _next_vm_operation if possible. 138 139 // Pointer to single-instance of VM thread 140 static VMThread* _vm_thread; 141 }; 142 143 #endif // SHARE_RUNTIME_VMTHREAD_HPP 144