1 /*
  2  * Copyright (c) 2017, 2021, 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_HANDSHAKE_HPP
 26 #define SHARE_RUNTIME_HANDSHAKE_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/iterator.hpp"
 30 #include "runtime/flags/flagSetting.hpp"
 31 #include "runtime/mutex.hpp"
 32 #include "runtime/orderAccess.hpp"
 33 #include "utilities/filterQueue.hpp"
 34 
 35 class HandshakeOperation;
 36 class JavaThread;
 37 class SuspendThreadHandshake;
 38 class ThreadSelfSuspensionHandshake;
 39 
 40 // A handshake closure is a callback that is executed for a JavaThread
 41 // while it is in a safepoint/handshake-safe state. Depending on the
 42 // nature of the closure, the callback may be executed by the initiating
 43 // thread, the target thread, or the VMThread. If the callback is not executed
 44 // by the target thread it will remain in a blocked state until the callback completes.
 45 class HandshakeClosure : public ThreadClosure, public CHeapObj<mtThread> {
 46   const char* const _name;
 47  public:
 48   HandshakeClosure(const char* name) : _name(name) {}
 49   virtual ~HandshakeClosure()                      {}
 50   const char* name() const                         { return _name; }
 51   virtual bool is_async()                          { return false; }
 52   virtual bool is_suspend()                        { return false; }
 53   virtual void do_thread(Thread* thread) = 0;
 54   virtual bool can_be_processed_by(Thread* thread) { return true; }
 55 };
 56 
 57 class AsyncHandshakeClosure : public HandshakeClosure {
 58  public:
 59    AsyncHandshakeClosure(const char* name) : HandshakeClosure(name) {}
 60    virtual ~AsyncHandshakeClosure() {}
 61    virtual bool is_async()          { return true; }
 62 };
 63 
 64 class Handshake : public AllStatic {
 65  public:
 66   // Execution of handshake operation
 67   static void execute(HandshakeClosure*       hs_cl);
 68   static void execute(HandshakeClosure*       hs_cl, JavaThread* target);
 69   static void execute(AsyncHandshakeClosure*  hs_cl, JavaThread* target);
 70 };
 71 
 72 class JvmtiRawMonitor;
 73 
 74 // The HandshakeState keeps track of an ongoing handshake for this JavaThread.
 75 // VMThread/Handshaker and JavaThread are serialized with _lock making sure the
 76 // operation is only done by either VMThread/Handshaker on behalf of the
 77 // JavaThread or by the target JavaThread itself.
 78 class HandshakeState {
 79   friend ThreadSelfSuspensionHandshake;
 80   friend SuspendThreadHandshake;
 81   friend JavaThread;
 82 
 83   // This a back reference to the JavaThread,
 84   // the target for all operation in the queue.
 85   JavaThread* _handshakee;
 86   // The queue containing handshake operations to be performed on _handshakee.
 87   FilterQueue<HandshakeOperation*> _queue;
 88   // Provides mutual exclusion to this state and queue. Also used for
 89   // JavaThread suspend/resume operations.
 90   Monitor _lock;
 91   // Set to the thread executing the handshake operation.
 92   Thread* volatile _active_handshaker;
 93 
 94   // Caller of suspension.  Only the caller can resume the thread.
 95   JavaThread* _caller;
 96 
 97   bool claim_handshake();
 98   bool possibly_can_process_handshake();
 99   bool can_process_handshake();
100 
101   bool have_non_self_executable_operation();
102   static bool non_self_queue_filter(HandshakeOperation* op);
103   HandshakeOperation* get_op_for_self(bool allow_suspend);
104   HandshakeOperation* get_op();
105   void remove_op(HandshakeOperation* op);
106 
107   void set_active_handshaker(Thread* thread) { Atomic::store(&_active_handshaker, thread); }
108 
109   class MatchOp {
110     HandshakeOperation* _op;
111    public:
112     MatchOp(HandshakeOperation* op) : _op(op) {}
113     bool operator()(HandshakeOperation* op) {
114       return op == _op;
115     }
116   };
117 
118  public:
119   HandshakeState(JavaThread* thread);
120 
121   void add_operation(HandshakeOperation* op);
122 
123   bool has_operation() {
124     return !_queue.is_empty();
125   }
126   bool has_a_non_suspend_operation();
127 
128   bool operation_pending(HandshakeOperation* op);
129 
130   // If the method returns true we need to check for a possible safepoint.
131   // This is due to a suspension handshake which put the JavaThread in blocked
132   // state so a safepoint may be in-progress.
133   bool process_by_self(bool allow_suspend);
134 
135   enum ProcessResult {
136     _no_operation = 0,
137     _not_safe,
138     _claim_failed,
139     _processed,
140     _succeeded,
141     _number_states
142   };
143   ProcessResult try_process(HandshakeOperation* match_op);
144 
145   Thread* active_handshaker() const { return Atomic::load(&_active_handshaker); }
146 
147   // Suspend/resume support
148  private:
149   // This flag is true when the thread owning this
150   // HandshakeState (the _handshakee) is suspended.
151   volatile bool _suspended;
152   // This flag is true while there is async handshake (trap)
153   // on queue. Since we do only need one, we can reuse it if
154   // thread gets suspended again (after a resume)
155   // and we have not yet processed it.
156   bool _async_suspend_handshake;
157 
158   // Called from the suspend handshake.
159   bool suspend_with_handshake(JavaThread* caller);
160 
161   // Called from the async handshake (the trap)
162   // to stop a thread from continuing execution when suspended.
163   void do_self_suspend();
164 
165   bool is_suspended() const                 { return Atomic::load(&_suspended); }
166   void set_suspended(bool to)               { return Atomic::store(&_suspended, to); }
167   bool has_async_suspend_handshake()        { return _async_suspend_handshake; }
168   void set_async_suspend_handshake(bool to) { _async_suspend_handshake = to; }
169 
170   void set_caller_thread(JavaThread* caller){ return Atomic::store(&_caller, caller); }
171   JavaThread* caller_thread() const         { return Atomic::load(&_caller); }
172 
173   // "blocked" is short for saying "suspended by caller"
174   bool is_blocked() const                   { return caller_thread() != nullptr; }
175   bool is_suspended_or_blocked() const      { return is_suspended() || is_blocked(); }
176 
177   bool suspend();
178   bool resume();
179 
180   bool block_suspend(JavaThread* caller);
181   bool continue_resume(JavaThread* caller);
182 };
183 
184 #endif // SHARE_RUNTIME_HANDSHAKE_HPP