1 /*
   2  * Copyright (c) 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 #ifndef SHARE_VM_RUNTIME_HANDSHAKE_HPP
  26 #define SHARE_VM_RUNTIME_HANDSHAKE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/semaphore.hpp"
  30 
  31 class ThreadClosure;
  32 class JavaThread;
  33 
  34 // A handshake operation is a callback that is executed for each JavaThread
  35 // while that thread is in a safepoint safe state. The callback is executed
  36 // either by the thread itself or by the VM thread while keeping the thread
  37 // in a blocked state. A handshake can be performed with a single
  38 // JavaThread as well.
  39 class Handshake : public AllStatic {
  40  public:
  41   // Execution of handshake operation
  42   static void execute(ThreadClosure* thread_cl);
  43   static bool execute(ThreadClosure* thread_cl, JavaThread* target);
  44 };
  45 
  46 class HandshakeOperation;
  47 
  48 // The HandshakeState keep tracks of an ongoing handshake for one JavaThread.
  49 // VM thread and JavaThread are serialized with the semaphore making sure
  50 // the operation is only done by either VM thread on behalf of the JavaThread
  51 // or the JavaThread it self.
  52 class HandshakeState VALUE_OBJ_CLASS_SPEC {
  53   HandshakeOperation* volatile _operation;
  54 
  55   Semaphore _semaphore;
  56   bool _vmthread_holds_semaphore;
  57   bool _thread_in_process_handshake;
  58 
  59   bool claim_handshake_for_vmthread();
  60   bool vmthread_can_process_handshake(JavaThread* target);
  61 
  62   void clear_handshake(JavaThread* thread);
  63   void cancel_inner(JavaThread* thread);
  64 
  65   void process_self_inner(JavaThread* thread);
  66 public:
  67   HandshakeState();
  68 
  69   void set_operation(JavaThread* thread, HandshakeOperation* op);
  70 
  71   bool has_operation() const {
  72     return _operation != NULL;
  73   }
  74 
  75   void cancel(JavaThread* thread) {
  76     if (!_thread_in_process_handshake) {
  77       FlagSetting fs(_thread_in_process_handshake, true);
  78       cancel_inner(thread);
  79     }
  80   }
  81 
  82   void process_by_self(JavaThread* thread) {
  83     if (!_thread_in_process_handshake) {
  84       FlagSetting fs(_thread_in_process_handshake, true);
  85       process_self_inner(thread);
  86     }
  87   }
  88   void process_by_vmthread(JavaThread* target);
  89 };
  90 
  91 #endif // SHARE_VM_RUNTIME_HANDSHAKE_HPP