|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 2017, 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 "mmtk.h" |
| 27 | +#include "mmtkVMCompanionThread.hpp" |
| 28 | +#include "runtime/mutex.hpp" |
| 29 | + |
| 30 | +MMTkVMCompanionThread::MMTkVMCompanionThread(): |
| 31 | + NamedThread(), |
| 32 | + _desired_state(_threads_resumed), |
| 33 | + _reached_state(_threads_resumed) { |
| 34 | + set_name("MMTK VM Companion Thread"); |
| 35 | + _lock = new Monitor(Monitor::nonleaf, |
| 36 | + "MMTkVMCompanionThread::_lock", |
| 37 | + true, |
| 38 | + Monitor::_safepoint_check_never); |
| 39 | +} |
| 40 | + |
| 41 | +MMTkVMCompanionThread::~MMTkVMCompanionThread() { |
| 42 | + guarantee(false, "MMTkVMCompanionThread deletion must fix the race with VM termination"); |
| 43 | +} |
| 44 | + |
| 45 | +void MMTkVMCompanionThread::run() { |
| 46 | + this->initialize_named_thread(); |
| 47 | + |
| 48 | + for (;;) { |
| 49 | + // Wait for suspend request |
| 50 | + log_trace(gc)("MMTkVMCompanionThread: Waiting for suspend request..."); |
| 51 | + { |
| 52 | + MutexLockerEx locker(_lock, Mutex::_no_safepoint_check_flag); |
| 53 | + assert(_reached_state == _threads_resumed, "Threads should be running at this moment."); |
| 54 | + while (_desired_state != _threads_suspended) { |
| 55 | + _lock->wait(true); |
| 56 | + } |
| 57 | + assert(_reached_state == _threads_resumed, "Threads should still be running at this moment."); |
| 58 | + } |
| 59 | + |
| 60 | + // Let the VM thread stop the world. |
| 61 | + log_trace(gc)("MMTkVMCompanionThread: Letting VMThread execute VM op..."); |
| 62 | + VM_MMTkSTWOperation op(this); |
| 63 | + // VMThread::execute() is blocking. The companion thread will be blocked |
| 64 | + // here waiting for the VM thread to execute op, and the VM thread will |
| 65 | + // be blocked in reach_suspended_and_wait_for_resume() until a GC thread |
| 66 | + // calls request(_threads_resumed). |
| 67 | + VMThread::execute(&op); |
| 68 | + |
| 69 | + // Tell the waiter thread that the world has resumed. |
| 70 | + log_trace(gc)("MMTkVMCompanionThread: Notifying threads resumption..."); |
| 71 | + { |
| 72 | + MutexLockerEx locker(_lock, Mutex::_no_safepoint_check_flag); |
| 73 | + assert(_desired_state == _threads_resumed, "start-the-world should be requested."); |
| 74 | + assert(_reached_state == _threads_suspended, "Threads should still be suspended at this moment."); |
| 75 | + _reached_state = _threads_resumed; |
| 76 | + _lock->notify_all(); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Request stop-the-world or start-the-world. This method is supposed to be |
| 82 | +// called by a GC thread. |
| 83 | +// |
| 84 | +// If wait_until_reached is true, the caller will block until all Java threads |
| 85 | +// have stopped, or until they have been waken up. |
| 86 | +// |
| 87 | +// If wait_until_reached is false, the caller will return immediately, while |
| 88 | +// the companion thread will ask the VM thread to perform the state transition |
| 89 | +// in the background. The caller may call the wait_for_reached method to block |
| 90 | +// until the desired state is reached. |
| 91 | +void MMTkVMCompanionThread::request(stw_state desired_state, bool wait_until_reached) { |
| 92 | + assert(!Thread::current()->is_VM_thread(), "Requests can only be made by GC threads. Found VM thread."); |
| 93 | + assert(Thread::current() != this, "Requests can only be made by GC threads. Found companion thread."); |
| 94 | + assert(!Thread::current()->is_Java_thread(), "Requests can only be made by GC threads. Found Java thread."); |
| 95 | + |
| 96 | + MutexLockerEx locker(_lock, Mutex::_no_safepoint_check_flag); |
| 97 | + assert(_desired_state != desired_state, "State %d already requested.", desired_state); |
| 98 | + _desired_state = desired_state; |
| 99 | + _lock->notify_all(); |
| 100 | + |
| 101 | + if (wait_until_reached) { |
| 102 | + while (_reached_state != desired_state) { |
| 103 | + _lock->wait(true); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Wait until the desired state is reached. Usually called after calling the |
| 109 | +// request method. Supposed to be called by a GC thread. |
| 110 | +void MMTkVMCompanionThread::wait_for_reached(stw_state desired_state) { |
| 111 | + assert(!Thread::current()->is_VM_thread(), "Supposed to be called by GC threads. Found VM thread."); |
| 112 | + assert(Thread::current() != this, "Supposed to be called by GC threads. Found companion thread."); |
| 113 | + assert(!Thread::current()->is_Java_thread(), "Supposed to be called by GC threads. Found Java thread."); |
| 114 | + |
| 115 | + MutexLockerEx locker(_lock, Mutex::_no_safepoint_check_flag); |
| 116 | + assert(_desired_state == desired_state, "State %d not requested.", desired_state); |
| 117 | + |
| 118 | + while (_reached_state != desired_state) { |
| 119 | + _lock->wait(true); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Called by the VM thread to indicate that all Java threads have stopped. |
| 124 | +// This method will block until the GC requests start-the-world. |
| 125 | +void MMTkVMCompanionThread::reach_suspended_and_wait_for_resume() { |
| 126 | + assert(Thread::current()->is_VM_thread(), "reach_suspended_and_wait_for_resume can only be executed by the VM thread"); |
| 127 | + |
| 128 | + MutexLockerEx locker(_lock, Mutex::_no_safepoint_check_flag); |
| 129 | + |
| 130 | + // Tell the waiter thread that the world has stopped. |
| 131 | + _reached_state = _threads_suspended; |
| 132 | + _lock->notify_all(); |
| 133 | + |
| 134 | + // Wait until resume-the-world is requested |
| 135 | + while (_desired_state != _threads_resumed) { |
| 136 | + _lock->wait(true); |
| 137 | + } |
| 138 | +} |
0 commit comments