rev 57079 : imported patch 8234796

   1 /*
   2  * Copyright (c) 2017, 2019, 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 "runtime/flags/flagSetting.hpp"
  30 #include "runtime/semaphore.hpp"
  31 

  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 HandshakeOperation: public StackObj {
  40   static Semaphore _done;
  41   const char* _name;
  42   bool _executed;
  43 public:
  44   HandshakeOperation(const char* name) : _name(name), _executed(false) {}
  45 
  46   void do_handshake(JavaThread* thread);
  47   bool thread_has_completed() { return _done.trywait(); }
  48   bool executed() const { return _executed; }
  49   const char* name() { return _name; };
  50 
  51 #ifdef ASSERT
  52   void check_state() {
  53     assert(!_done.trywait(), "Must be zero");
  54   }
  55 #endif
  56 
  57   // Impl
  58   virtual void do_thread(JavaThread* thread) = 0;
  59 };
  60 
  61 class Handshake : public AllStatic {
  62  public:
  63   // Execution of handshake operation
  64   static void execute(HandshakeOperation* hs_op);
  65   static bool execute(HandshakeOperation* hs_op, JavaThread* target);
  66 };


  67 
  68 // The HandshakeState keep tracks of an ongoing handshake for one JavaThread.
  69 // VM thread and JavaThread are serialized with the semaphore making sure
  70 // the operation is only done by either VM thread on behalf of the JavaThread
  71 // or the JavaThread itself.
  72 class HandshakeState {
  73   HandshakeOperation* volatile _operation;
  74 
  75   Semaphore _semaphore;
  76   bool _thread_in_process_handshake;
  77 
  78   bool claim_handshake_for_vmthread();
  79   bool vmthread_can_process_handshake(JavaThread* target);
  80 
  81   void clear_handshake(JavaThread* thread);
  82 
  83   void process_self_inner(JavaThread* thread);
  84 public:
  85   HandshakeState();
  86 
  87   void set_operation(JavaThread* thread, HandshakeOperation* op);
  88 
  89   bool has_operation() const {
  90     return _operation != NULL;
  91   }
  92 
  93   void process_by_self(JavaThread* thread) {
  94     if (!_thread_in_process_handshake) {
  95       FlagSetting fs(_thread_in_process_handshake, true);
  96       process_self_inner(thread);
  97     }
  98   }
  99 
 100   void process_by_vmthread(JavaThread* target);
 101 };
 102 
 103 #endif // SHARE_RUNTIME_HANDSHAKE_HPP
--- EOF ---