< prev index next >

src/hotspot/share/runtime/handshake.hpp

Print this page




  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. In that case the callback is executed either by the
  41 // thread itself or, depending on whether the operation is a direct handshake
  42 // or not, by the JavaThread that requested the handshake or the VMThread
  43 // respectively.
  44 class HandshakeClosure : public ThreadClosure {
  45   const char* const _name;
  46  public:
  47   HandshakeClosure(const char* name) : _name(name) {}
  48   const char* name() const {
  49     return _name;
  50   }
  51   virtual void do_thread(Thread* thread) = 0;
  52 };
  53 
  54 class Handshake : public AllStatic {
  55  public:
  56   // Execution of handshake operation
  57   static void execute(HandshakeClosure* hs_cl);
  58   static bool execute(HandshakeClosure* hs_cl, JavaThread* target);
  59   static bool execute_direct(HandshakeClosure* hs_cl, JavaThread* target);
  60 };
  61 
  62 // The HandshakeState keep tracks of an ongoing handshake for one JavaThread.
  63 // VMThread/Handshaker and JavaThread are serialized with the semaphore making
  64 // sure the operation is only done by either VMThread/Handshaker on behalf of
  65 // the JavaThread or the JavaThread itself.
  66 class HandshakeState {
  67   JavaThread *_thread;
  68   HandshakeOperation* volatile _operation;
  69   HandshakeOperation* volatile _operation_direct;
  70 
  71   Semaphore _handshake_turn_sem;
  72   Semaphore _processing_sem;
  73   bool _thread_in_process_handshake;
  74 
  75   bool claim_handshake(bool is_direct);
  76   bool possibly_can_process_handshake();
  77   bool can_process_handshake();
  78   void clear_handshake(bool is_direct);
  79   
  80   void process_self_inner();
  81 
  82 public:
  83   HandshakeState();
  84 
  85   void set_thread(JavaThread *thread) { _thread = thread; }
  86 
  87   void set_operation(HandshakeOperation* op);
  88   bool has_operation() const { return _operation != NULL || _operation_direct != NULL; }
  89   bool has_specific_operation(bool is_direct) const {
  90     return is_direct ? _operation_direct != NULL : _operation != NULL;
  91   }
  92 
  93   void process_by_self() {
  94     if (!_thread_in_process_handshake) {
  95       FlagSetting fs(_thread_in_process_handshake, true);
  96       process_self_inner();
  97     }
  98   }
  99   bool try_process(HandshakeOperation* op);
 100 
 101 #ifdef ASSERT
 102   Thread* _active_handshaker;
 103   Thread* get_active_handshaker() const { return _active_handshaker; }
 104 #endif
 105 


  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 HandshakeOperation;
  34 class JavaThread;
  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 target JavaThread itself or by the VMThread while keeping
  39 // the target thread in a blocked state. A handshake can be performed with a
  40 // single JavaThread as well. In that case, the callback is executed either
  41 // by the target JavaThread itself or, depending on whether the operation is
  42 // a direct handshake or not, by the JavaThread that requested the handshake
  43 // or the VMThread respectively.
  44 class HandshakeClosure : public ThreadClosure {
  45   const char* const _name;
  46  public:
  47   HandshakeClosure(const char* name) : _name(name) {}
  48   const char* name() const {
  49     return _name;
  50   }
  51   virtual void do_thread(Thread* thread) = 0;
  52 };
  53 
  54 class Handshake : public AllStatic {
  55  public:
  56   // Execution of handshake operation
  57   static void execute(HandshakeClosure* hs_cl);
  58   static bool execute(HandshakeClosure* hs_cl, JavaThread* target);
  59   static bool execute_direct(HandshakeClosure* hs_cl, JavaThread* target);
  60 };
  61 
  62 // The HandshakeState keeps track of an ongoing handshake for this JavaThread.
  63 // VMThread/Handshaker and JavaThread are serialized with semaphore _processing_sem
  64 // making sure the operation is only done by either VMThread/Handshaker on behalf
  65 // of the JavaThread or by the target JavaThread itself.
  66 class HandshakeState {
  67   JavaThread* _handshakee;
  68   HandshakeOperation* volatile _operation;
  69   HandshakeOperation* volatile _operation_direct;
  70 
  71   Semaphore _handshake_turn_sem;  // Used to serialize direct handshakes for this JavaThread.
  72   Semaphore _processing_sem;
  73   bool _thread_in_process_handshake;
  74 
  75   bool claim_handshake(bool is_direct);
  76   bool possibly_can_process_handshake();
  77   bool can_process_handshake();
  78   void clear_handshake(bool is_direct);
  79   
  80   void process_self_inner();
  81 
  82 public:
  83   HandshakeState();
  84 
  85   void set_thread(JavaThread* thread) { _handshakee = thread; }
  86 
  87   void set_operation(HandshakeOperation* op);
  88   bool has_operation() const { return _operation != NULL || _operation_direct != NULL; }
  89   bool has_specific_operation(bool is_direct) const {
  90     return is_direct ? _operation_direct != NULL : _operation != NULL;
  91   }
  92 
  93   void process_by_self() {
  94     if (!_thread_in_process_handshake) {
  95       FlagSetting fs(_thread_in_process_handshake, true);
  96       process_self_inner();
  97     }
  98   }
  99   bool try_process(HandshakeOperation* op);
 100 
 101 #ifdef ASSERT
 102   Thread* _active_handshaker;
 103   Thread* get_active_handshaker() const { return _active_handshaker; }
 104 #endif
 105 
< prev index next >