Path: blob/master/src/hotspot/share/prims/jvmtiRawMonitor.hpp
41144 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_PRIMS_JVMTIRAWMONITOR_HPP25#define SHARE_PRIMS_JVMTIRAWMONITOR_HPP2627#include "memory/allocation.hpp"28#include "runtime/park.hpp"29#include "utilities/growableArray.hpp"3031//32// class JvmtiRawMonitor33//34// Used by JVMTI methods: All RawMonitor methods (CreateRawMonitor, EnterRawMonitor, etc.)35//36// A simplified version of the ObjectMonitor code.37//3839// Important note:40// Raw monitors can be used in callbacks which happen during safepoint by the VM41// thread (e.g., heapRootCallback). This means we may not transition/safepoint42// poll in many cases, else the agent JavaThread can deadlock with the VM thread,43// as this old comment says:44// "We can't safepoint block in here because we could deadlock the vmthread. Blech."45// The rules are:46// - We must never safepoint poll if raw monitor is owned.47// - We may safepoint poll before it is owned and after it has been released.48// If this were the only thing we needed to think about we could just stay in49// native for all operations. However we need to honor a suspend request, not50// entering a monitor if suspended, and check for interrupts. Honoring a suspend51// request and reading the interrupt flag must be done from VM state52// (a safepoint unsafe state).5354class JvmtiRawMonitor : public CHeapObj<mtSynchronizer> {5556// Helper class to allow Threads to be linked into queues.57// This is a stripped down version of ObjectWaiter.58class QNode : public StackObj {59friend class JvmtiRawMonitor;60enum TStates { TS_READY, TS_RUN, TS_WAIT, TS_ENTER };61QNode* volatile _next;62QNode* volatile _prev;63ParkEvent* _event;64volatile int _notified;65volatile TStates _t_state;6667QNode(Thread* thread);68};6970Thread* volatile _owner; // pointer to owning thread71volatile int _recursions; // recursion count, 0 for first entry72QNode* volatile _entry_list; // Threads blocked on entry or reentry.73// The list is actually composed of nodes,74// acting as proxies for Threads.75QNode* volatile _wait_set; // Threads wait()ing on the monitor76int _magic;77char* _name;78// JVMTI_RM_MAGIC is set in contructor and unset in destructor.79enum { JVMTI_RM_MAGIC = (int)(('T' << 24) | ('I' << 16) | ('R' << 8) | 'M') };8081// Helpers for queue management isolation82void enqueue_waiter(QNode& node);83void dequeue_waiter(QNode& node);8485// Mostly low-level implementation routines86void simple_enter(Thread* self);87void simple_exit(Thread* self);88int simple_wait(Thread* self, jlong millis);89void simple_notify(Thread* self, bool all);9091class ExitOnSuspend {92protected:93JvmtiRawMonitor* _rm;94bool _rm_exited;95public:96ExitOnSuspend(JvmtiRawMonitor* rm) : _rm(rm), _rm_exited(false) {}97void operator()(JavaThread* current);98bool monitor_exited() { return _rm_exited; }99};100101public:102103// return codes104enum {105M_OK, // no error106M_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException107M_INTERRUPTED // Thread.interrupt()108};109110// Non-aborting operator new111void* operator new(size_t size) throw() {112return CHeapObj::operator new(size, std::nothrow);113}114115JvmtiRawMonitor(const char* name);116~JvmtiRawMonitor();117118Thread* owner() const { return _owner; }119void set_owner(Thread* owner) { _owner = owner; }120int recursions() const { return _recursions; }121void raw_enter(Thread* self);122int raw_exit(Thread* self);123int raw_wait(jlong millis, Thread* self);124int raw_notify(Thread* self);125int raw_notifyAll(Thread* self);126int magic() const { return _magic; }127const char* get_name() const { return _name; }128bool is_valid();129};130131// Onload pending raw monitors132// Class is used to cache onload or onstart monitor enter133// which will transition into real monitor when134// VM is fully initialized.135class JvmtiPendingMonitors : public AllStatic {136137private:138static GrowableArray<JvmtiRawMonitor*>* _monitors; // Cache raw monitor enter139140inline static GrowableArray<JvmtiRawMonitor*>* monitors() { return _monitors; }141142static void dispose() {143delete monitors();144}145146public:147static void enter(JvmtiRawMonitor* monitor) {148monitors()->append(monitor);149}150151static int count() {152return monitors()->length();153}154155static void destroy(JvmtiRawMonitor* monitor) {156while (monitors()->contains(monitor)) {157monitors()->remove(monitor);158}159}160161// Return false if monitor is not found in the list.162static bool exit(JvmtiRawMonitor* monitor) {163return monitors()->remove_if_existing(monitor);164}165166static void transition_raw_monitors();167};168169#endif // SHARE_PRIMS_JVMTIRAWMONITOR_HPP170171172