< prev index next >

src/hotspot/share/runtime/handshake.hpp

Print this page

 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 };
 55 
 56 class AsyncHandshakeClosure : public HandshakeClosure {
 57  public:
 58    AsyncHandshakeClosure(const char* name) : HandshakeClosure(name) {}
 59    virtual ~AsyncHandshakeClosure() {}
 60    virtual bool is_async()          { return true; }
 61 };
 62 
 63 class Handshake : public AllStatic {
 64  public:
 65   // Execution of handshake operation
 66   static void execute(HandshakeClosure*       hs_cl);
 67   static void execute(HandshakeClosure*       hs_cl, JavaThread* target);
 68   static void execute(AsyncHandshakeClosure*  hs_cl, JavaThread* target);
 69 };
 70 
 71 class JvmtiRawMonitor;
 72 
 73 // The HandshakeState keeps track of an ongoing handshake for this JavaThread.
 74 // VMThread/Handshaker and JavaThread are serialized with _lock making sure the
 75 // operation is only done by either VMThread/Handshaker on behalf of the
 76 // JavaThread or by the target JavaThread itself.
 77 class HandshakeState {
 78   friend ThreadSelfSuspensionHandshake;
 79   friend SuspendThreadHandshake;
 80   friend JavaThread;

 81   // This a back reference to the JavaThread,
 82   // the target for all operation in the queue.
 83   JavaThread* _handshakee;
 84   // The queue containing handshake operations to be performed on _handshakee.
 85   FilterQueue<HandshakeOperation*> _queue;
 86   // Provides mutual exclusion to this state and queue. Also used for
 87   // JavaThread suspend/resume operations.
 88   Monitor _lock;
 89   // Set to the thread executing the handshake operation.
 90   Thread* volatile _active_handshaker;
 91 



 92   bool claim_handshake();
 93   bool possibly_can_process_handshake();
 94   bool can_process_handshake();
 95 
 96   bool have_non_self_executable_operation();

 97   HandshakeOperation* get_op_for_self(bool allow_suspend);
 98   HandshakeOperation* get_op();
 99   void remove_op(HandshakeOperation* op);
100 
101   void set_active_handshaker(Thread* thread) { Atomic::store(&_active_handshaker, thread); }
102 
103   class MatchOp {
104     HandshakeOperation* _op;
105    public:
106     MatchOp(HandshakeOperation* op) : _op(op) {}
107     bool operator()(HandshakeOperation* op) {
108       return op == _op;
109     }
110   };
111 
112  public:
113   HandshakeState(JavaThread* thread);
114 
115   void add_operation(HandshakeOperation* op);
116 

133     _processed,
134     _succeeded,
135     _number_states
136   };
137   ProcessResult try_process(HandshakeOperation* match_op);
138 
139   Thread* active_handshaker() const { return Atomic::load(&_active_handshaker); }
140 
141   // Suspend/resume support
142  private:
143   // This flag is true when the thread owning this
144   // HandshakeState (the _handshakee) is suspended.
145   volatile bool _suspended;
146   // This flag is true while there is async handshake (trap)
147   // on queue. Since we do only need one, we can reuse it if
148   // thread gets suspended again (after a resume)
149   // and we have not yet processed it.
150   bool _async_suspend_handshake;
151 
152   // Called from the suspend handshake.
153   bool suspend_with_handshake();

154   // Called from the async handshake (the trap)
155   // to stop a thread from continuing execution when suspended.
156   void do_self_suspend();
157 
158   bool is_suspended()                       { return Atomic::load(&_suspended); }
159   void set_suspended(bool to)               { return Atomic::store(&_suspended, to); }
160   bool has_async_suspend_handshake()        { return _async_suspend_handshake; }
161   void set_async_suspend_handshake(bool to) { _async_suspend_handshake = to; }
162 







163   bool suspend();
164   bool resume();



165 };
166 
167 #endif // SHARE_RUNTIME_HANDSHAKE_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 

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
< prev index next >