1 /*
   2  * Copyright (c) 2017, 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_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/semaphore.hpp"
  32 
  33 class JavaThread;
  34 class HandshakeOperation;
  35 
  36 // A handshake closure is a callback that is executed for each JavaThread
  37 // while that thread is in a safepoint safe state. The callback is executed
  38 // either by the thread itself or by the VM thread while keeping the thread
  39 // in a blocked state. A handshake can be performed with a single
  40 // JavaThread as well.
  41 class HandshakeClosure : public ThreadClosure {
  42   const char* const _name;
  43  public:
  44   HandshakeClosure(const char* name) : _name(name) {}
  45   const char* name() const {
  46     return _name;
  47   }
  48   virtual void do_thread(Thread* thread) = 0;
  49 };
  50 
  51 class Handshake : public AllStatic {
  52   static void direct_handshake(JavaThread* target, HandshakeOperation *op);
  53  public:
  54   // Execution of handshake operation
  55   static void execute(HandshakeClosure* hs_cl);
  56   static bool execute(HandshakeClosure* hs_cl, JavaThread* target, bool is_direct_handshake = false);
  57 };
  58 
  59 // The HandshakeState keep tracks of an ongoing handshake for one JavaThread.
  60 // VM thread and JavaThread are serialized with the semaphore making sure
  61 // the operation is only done by either VM thread on behalf of the JavaThread
  62 // or the JavaThread itself.
  63 class HandshakeState {
  64   JavaThread *_thread;
  65   HandshakeOperation* volatile _operation;
  66   HandshakeOperation* volatile _operation_direct;
  67 
  68   Semaphore _handshake_turn_sem;
  69   Semaphore _processing_sem;
  70   bool _thread_in_process_handshake;
  71 
  72   bool claim_handshake(bool is_direct);
  73   bool possibly_can_process_handshake();
  74   bool can_process_handshake();
  75   void clear_handshake(bool is_direct);
  76   
  77   void process_self_inner();
  78 
  79 public:
  80   HandshakeState();
  81 
  82   void set_thread(JavaThread *thread) { _thread = thread; }
  83 
  84   void set_operation(HandshakeOperation* op);
  85   bool has_operation() const { return _operation != NULL || _operation_direct != NULL; }
  86 
  87   void process_by_self() {
  88     if (!_thread_in_process_handshake) {
  89       FlagSetting fs(_thread_in_process_handshake, true);
  90       process_self_inner();
  91     }
  92   }
  93   bool try_process(HandshakeOperation* op);
  94 
  95 #ifdef ASSERT
  96   Thread* _active_handshaker;
  97   Thread* get_active_handshaker() const { return _active_handshaker; }
  98 #endif
  99 
 100 };
 101 
 102 #endif // SHARE_RUNTIME_HANDSHAKE_HPP