< prev index next >

src/hotspot/share/runtime/handshake.cpp

Print this page




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/log.hpp"
  27 #include "logging/logStream.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "runtime/atomic.hpp"
  30 #include "runtime/handshake.hpp"
  31 #include "runtime/interfaceSupport.inline.hpp"
  32 #include "runtime/osThread.hpp"
  33 #include "runtime/semaphore.inline.hpp"
  34 #include "runtime/task.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 #include "utilities/preserveException.hpp"
  39 
  40 class HandshakeOperation: public StackObj {
  41 public:
  42   virtual void do_handshake(JavaThread* thread) = 0;
  43 };
  44 
  45 class HandshakeThreadsOperation: public HandshakeOperation {
  46   static Semaphore _done;
  47   HandshakeClosure* _handshake_cl;

  48   bool _executed;

  49 public:
  50   HandshakeThreadsOperation(HandshakeClosure* cl) : _handshake_cl(cl), _executed(false) {}





  51   void do_handshake(JavaThread* thread);
  52   bool thread_has_completed() { return _done.trywait(); }





  53   bool executed() const { return _executed; }
  54   const char* name() { return _handshake_cl->name(); }
  55 


  56 #ifdef ASSERT
  57   void check_state() {
  58     assert(!_done.trywait(), "Must be zero");
  59   }
  60 #endif
  61 };
  62 
  63 Semaphore HandshakeThreadsOperation::_done(0);
  64 
  65 class VM_Handshake: public VM_Operation {
  66   const jlong _handshake_timeout;
  67  public:
  68   bool evaluate_at_safepoint() const { return false; }
  69 
  70  protected:
  71   HandshakeThreadsOperation* const _op;
  72 
  73   VM_Handshake(HandshakeThreadsOperation* op) :
  74       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
  75 
  76   void set_handshake(JavaThread* target) {
  77     target->set_handshake_operation(_op);
  78   }
  79 
  80   // This method returns true for threads completed their operation
  81   // and true for threads canceled their operation.
  82   // A cancellation can happen if the thread is exiting.
  83   bool poll_for_completed_thread() { return _op->thread_has_completed(); }
  84 
  85   bool handshake_has_timed_out(jlong start_time);
  86   static void handle_timeout();
  87 };
  88 
  89 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
  90   // Check if handshake operation has timed out
  91   if (_handshake_timeout > 0) {
  92     return os::elapsed_counter() >= (start_time + _handshake_timeout);
  93   }
  94   return false;
  95 }
  96 
  97 void VM_Handshake::handle_timeout() {
  98   LogStreamHandle(Warning, handshake) log_stream;
  99   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
 100     if (thr->has_handshake()) {
 101       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
 102       thr->print_thread_state_on(&log_stream);
 103     }
 104   }
 105   log_stream.flush();
 106   fatal("Handshake operation timed out");
 107 }
 108 
 109 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) {
 110   if (start_time_ns != 0) {
 111     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 112     log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
 113                         name, targets,
 114                         targets - vmt_executed,
 115                         completion_time,
 116                         extra != NULL ? ", " : "",
 117                         extra != NULL ? extra : "");
 118   }
 119 }
 120 
 121 class VM_HandshakeOneThread: public VM_Handshake {
 122   JavaThread* _target;
 123  public:
 124   VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) :
 125     VM_Handshake(op), _target(target) {}
 126 
 127   void doit() {
 128     DEBUG_ONLY(_op->check_state();)
 129 
 130     jlong start_time_ns = 0;
 131     if (log_is_enabled(Info, handshake)) {
 132       start_time_ns = os::javaTimeNanos();
 133     }
 134 
 135     ThreadsListHandle tlh;
 136     if (tlh.includes(_target)) {
 137       set_handshake(_target);
 138     } else {
 139       log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)");
 140       return;
 141     }
 142 
 143     log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
 144     jlong timeout_start_time = os::elapsed_counter();
 145     bool by_vm_thread = false;
 146     do {
 147       if (handshake_has_timed_out(timeout_start_time)) {
 148         handle_timeout();
 149       }
 150       by_vm_thread = _target->handshake_try_process_by_vmThread();
 151     } while (!poll_for_completed_thread());
 152     DEBUG_ONLY(_op->check_state();)
 153     log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0);
 154   }
 155 
 156   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 157 
 158   bool executed() const { return _op->executed(); }
 159 };
 160 
 161 class VM_HandshakeAllThreads: public VM_Handshake {
 162  public:
 163   VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {}
 164 
 165   void doit() {
 166     DEBUG_ONLY(_op->check_state();)
 167 
 168     jlong start_time_ns = 0;
 169     if (log_is_enabled(Info, handshake)) {
 170       start_time_ns = os::javaTimeNanos();
 171     }
 172     int handshake_executed_by_vm_thread = 0;
 173 
 174     JavaThreadIteratorWithHandle jtiwh;
 175     int number_of_threads_issued = 0;
 176     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 177       set_handshake(thr);
 178       number_of_threads_issued++;
 179     }
 180 
 181     if (number_of_threads_issued < 1) {
 182       log_handshake_info(start_time_ns, _op->name(), 0, 0);
 183       return;
 184     }


 185 
 186     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 187     const jlong start_time = os::elapsed_counter();
 188     int number_of_threads_completed = 0;
 189     do {
 190       // Check if handshake operation has timed out
 191       if (handshake_has_timed_out(start_time)) {
 192         handle_timeout();
 193       }
 194 
 195       // Have VM thread perform the handshake operation for blocked threads.
 196       // Observing a blocked state may of course be transient but the processing is guarded
 197       // by semaphores and we optimistically begin by working on the blocked threads
 198       jtiwh.rewind();
 199       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 200         // A new thread on the ThreadsList will not have an operation,
 201         // hence it is skipped in handshake_process_by_vmthread.
 202         if (thr->handshake_try_process_by_vmThread()) {
 203           handshake_executed_by_vm_thread++;
 204         }
 205       }
 206       while (poll_for_completed_thread()) {
 207         // Includes canceled operations by exiting threads.
 208         number_of_threads_completed++;
 209       }
 210 
 211     } while (number_of_threads_issued > number_of_threads_completed);
 212     assert(number_of_threads_issued == number_of_threads_completed, "Must be the same");
 213     DEBUG_ONLY(_op->check_state();)
 214 
 215     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
 216   }
 217 
 218   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 219 };
 220 
 221 void HandshakeThreadsOperation::do_handshake(JavaThread* thread) {
 222   jlong start_time_ns = 0;
 223   if (log_is_enabled(Debug, handshake, task)) {
 224     start_time_ns = os::javaTimeNanos();
 225   }
 226 
 227   // Only actually execute the operation for non terminated threads.
 228   if (!thread->is_terminated()) {
 229     _handshake_cl->do_thread(thread);
 230     _executed = true;
 231   }
 232 
 233   if (start_time_ns != 0) {
 234     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 235     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
 236                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
 237   }
 238 
 239   // Use the semaphore to inform the VM thread that we have completed the operation
 240   _done.signal();
 241 
 242   // It is no longer safe to refer to 'this' as the VMThread may have destroyed this operation
 243 }
 244 
 245 void Handshake::execute(HandshakeClosure* thread_cl) {
 246   HandshakeThreadsOperation cto(thread_cl);
 247   VM_HandshakeAllThreads handshake(&cto);
 248   VMThread::execute(&handshake);
 249 }
 250 
 251 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 252   HandshakeThreadsOperation cto(thread_cl);
 253   VM_HandshakeOneThread handshake(&cto, target);
 254   VMThread::execute(&handshake);
 255   return handshake.executed();
 256 }
 257 
 258 HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {
 259   DEBUG_ONLY(_vmthread_processing_handshake = false;)
 260 }













 261 
 262 void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) {



























 263   _operation = op;
 264   SafepointMechanism::arm_local_poll_release(target);






 265 }
 266 
 267 void HandshakeState::clear_handshake(JavaThread* target) {

 268   _operation = NULL;
 269   SafepointMechanism::disarm_if_needed(target, true /* release */);



 270 }
 271 
 272 void HandshakeState::process_self_inner(JavaThread* thread) {
 273   assert(Thread::current() == thread, "should call from thread");
 274   assert(!thread->is_terminated(), "should not be a terminated thread");
 275   assert(thread->thread_state() != _thread_blocked, "should not be in a blocked state");
 276   assert(thread->thread_state() != _thread_in_native, "should not be in native");

 277 
 278   do {
 279     ThreadInVMForHandshake tivm(thread);
 280     if (!_semaphore.trywait()) {
 281       _semaphore.wait_with_safepoint_check(thread);









 282     }
 283     HandshakeOperation* op = Atomic::load_acquire(&_operation);
 284     if (op != NULL) {
 285       HandleMark hm(thread);
 286       CautiouslyPreserveExceptionMark pem(thread);
 287       // Disarm before execute the operation
 288       clear_handshake(thread);
 289       op->do_handshake(thread);
 290     }
 291     _semaphore.signal();

 292   } while (has_operation());
 293 }
 294 
 295 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 296   // handshake_safe may only be called with polls armed.
 297   // VM thread controls this by first claiming the handshake via claim_handshake_for_vmthread.
 298   return SafepointSynchronize::handshake_safe(target);
 299 }
 300 
 301 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
 302   // Note that this method is allowed to produce false positives.
 303   if (target->is_ext_suspended()) {
 304     return true;
 305   }
 306   if (target->is_terminated()) {
 307     return true;
 308   }
 309   switch (target->thread_state()) {
 310   case _thread_in_native:
 311     // native threads are safe if they have no java stack or have walkable stack
 312     return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
 313 
 314   case _thread_blocked:
 315     return true;
 316 
 317   default:
 318     return false;
 319   }
 320 }
 321 
 322 bool HandshakeState::claim_handshake_for_vmthread() {
 323   if (!_semaphore.trywait()) {
 324     return false;
 325   }
 326   if (has_operation()) {
 327     return true;
 328   }
 329   _semaphore.signal();
 330   return false;
 331 }
 332 
 333 bool HandshakeState::try_process_by_vmThread(JavaThread* target) {
 334   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 335 
 336   if (!has_operation()) {
 337     // JT has already cleared its handshake
 338     return false;
 339   }
 340 
 341   if (!possibly_vmthread_can_process_handshake(target)) {
 342     // JT is observed in an unsafe state, it must notice the handshake itself
 343     return false;
 344   }
 345 
 346   // Claim the semaphore if there still an operation to be executed.
 347   if (!claim_handshake_for_vmthread()) {








 348     return false;
 349   }
 350 
 351   // If we own the semaphore at this point and while owning the semaphore
 352   // can observe a safe state the thread cannot possibly continue without
 353   // getting caught by the semaphore.
 354   bool executed = false;
 355   if (vmthread_can_process_handshake(target)) {
 356     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 357     log_trace(handshake)("Processing handshake by VMThtread");
 358     DEBUG_ONLY(_vmthread_processing_handshake = true;)
 359     _operation->do_handshake(target);
 360     DEBUG_ONLY(_vmthread_processing_handshake = false;)
 361     // Disarm after VM thread have executed the operation.
 362     clear_handshake(target);
 363     executed = true;
 364   }
 365 
 366   // Release the thread
 367   _semaphore.signal();
 368 
 369   return executed;
 370 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/log.hpp"
  27 #include "logging/logStream.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "runtime/atomic.hpp"
  30 #include "runtime/handshake.hpp"
  31 #include "runtime/interfaceSupport.inline.hpp"
  32 #include "runtime/osThread.hpp"
  33 #include "runtime/semaphore.inline.hpp"
  34 #include "runtime/task.hpp"
  35 #include "runtime/thread.hpp"
  36 #include "runtime/vmThread.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 #include "utilities/preserveException.hpp"
  39 




  40 
  41 class HandshakeOperation: public StackObj {

  42   HandshakeClosure* _handshake_cl;
  43   int32_t _pending_threads;
  44   bool _executed;
  45   bool _is_direct;
  46 public:
  47   HandshakeOperation(HandshakeClosure* cl, bool is_direct = false) :
  48     _handshake_cl(cl),
  49     _pending_threads(1),
  50     _executed(false),
  51     _is_direct(is_direct) {}
  52 
  53   void do_handshake(JavaThread* thread);
  54   bool is_completed() {
  55     int32_t val = Atomic::load(&_pending_threads);
  56     assert(val >= 0, "_pending_threads cannot be negative");
  57     return val == 0;
  58   }
  59   void add_target_count(int count) { Atomic::add(&_pending_threads, count); }
  60   bool executed() const { return _executed; }
  61   const char* name() { return _handshake_cl->name(); }
  62 
  63   bool is_direct() { return _is_direct; }
  64 
  65 #ifdef ASSERT
  66   void check_state() {
  67     assert(_pending_threads == 0, "Must be zero");
  68   }
  69 #endif
  70 };
  71 


  72 class VM_Handshake: public VM_Operation {
  73   const jlong _handshake_timeout;
  74  public:
  75   bool evaluate_at_safepoint() const { return false; }
  76 
  77  protected:
  78   HandshakeOperation* const _op;
  79 
  80   VM_Handshake(HandshakeOperation* op) :
  81       _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)), _op(op) {}
  82 









  83   bool handshake_has_timed_out(jlong start_time);
  84   static void handle_timeout();
  85 };
  86 
  87 bool VM_Handshake::handshake_has_timed_out(jlong start_time) {
  88   // Check if handshake operation has timed out
  89   if (_handshake_timeout > 0) {
  90     return os::elapsed_counter() >= (start_time + _handshake_timeout);
  91   }
  92   return false;
  93 }
  94 
  95 void VM_Handshake::handle_timeout() {
  96   LogStreamHandle(Warning, handshake) log_stream;
  97   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
  98     if (thr->has_handshake()) {
  99       log_stream.print("Thread " PTR_FORMAT " has not cleared its handshake op", p2i(thr));
 100       thr->print_thread_state_on(&log_stream);
 101     }
 102   }
 103   log_stream.flush();
 104   fatal("Handshake operation timed out");
 105 }
 106 
 107 static void log_handshake_info(jlong start_time_ns, const char* name, int targets, int vmt_executed, const char* extra = NULL) {
 108   if (start_time_ns != 0) {
 109     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 110     log_info(handshake)("Handshake \"%s\", Targeted threads: %d, Executed by targeted threads: %d, Total completion time: " JLONG_FORMAT " ns%s%s",
 111                         name, targets,
 112                         targets - vmt_executed,
 113                         completion_time,
 114                         extra != NULL ? ", " : "",
 115                         extra != NULL ? extra : "");
 116   }
 117 }
 118 
 119 class VM_HandshakeOneThread: public VM_Handshake {
 120   JavaThread* _target;
 121  public:
 122   VM_HandshakeOneThread(HandshakeOperation* op, JavaThread* target) :
 123     VM_Handshake(op), _target(target) {}
 124 
 125   void doit() {


 126     jlong start_time_ns = 0;
 127     if (log_is_enabled(Info, handshake)) {
 128       start_time_ns = os::javaTimeNanos();
 129     }
 130 
 131     ThreadsListHandle tlh;
 132     if (tlh.includes(_target)) {
 133       _target->set_handshake_operation(_op);
 134     } else {
 135       log_handshake_info(start_time_ns, _op->name(), 0, 0, "(thread dead)");
 136       return;
 137     }
 138 
 139     log_trace(handshake)("JavaThread " INTPTR_FORMAT " signaled, begin attempt to process by VMThtread", p2i(_target));
 140     jlong timeout_start_time = os::elapsed_counter();
 141     bool by_vm_thread = false;
 142     do {
 143       if (handshake_has_timed_out(timeout_start_time)) {
 144         handle_timeout();
 145       }
 146       by_vm_thread = _target->handshake_try_process(_op);
 147     } while (!_op->is_completed());
 148     DEBUG_ONLY(_op->check_state();)
 149     log_handshake_info(start_time_ns, _op->name(), 1, by_vm_thread ? 1 : 0);
 150   }
 151 
 152   VMOp_Type type() const { return VMOp_HandshakeOneThread; }
 153 
 154   bool executed() const { return _op->executed(); }
 155 };
 156 
 157 class VM_HandshakeAllThreads: public VM_Handshake {
 158  public:
 159   VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {}
 160 
 161   void doit() {


 162     jlong start_time_ns = 0;
 163     if (log_is_enabled(Info, handshake)) {
 164       start_time_ns = os::javaTimeNanos();
 165     }
 166     int handshake_executed_by_vm_thread = 0;
 167 
 168     JavaThreadIteratorWithHandle jtiwh;
 169     int number_of_threads_issued = 0;
 170     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 171       thr->set_handshake_operation(_op);
 172       number_of_threads_issued++;
 173     }
 174 
 175     if (number_of_threads_issued < 1) {
 176       log_handshake_info(start_time_ns, _op->name(), 0, 0);
 177       return;
 178     }
 179     // _op was created with a count == 1 so don't double count.
 180     _op->add_target_count(number_of_threads_issued - 1);
 181 
 182     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 183     const jlong start_time = os::elapsed_counter();

 184     do {
 185       // Check if handshake operation has timed out
 186       if (handshake_has_timed_out(start_time)) {
 187         handle_timeout();
 188       }
 189 
 190       // Have VM thread perform the handshake operation for blocked threads.
 191       // Observing a blocked state may of course be transient but the processing is guarded
 192       // by semaphores and we optimistically begin by working on the blocked threads
 193       jtiwh.rewind();
 194       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 195         // A new thread on the ThreadsList will not have an operation,
 196         // hence it is skipped in handshake_try_process.
 197         if (thr->handshake_try_process(_op)) {
 198           handshake_executed_by_vm_thread++;
 199         }
 200       }
 201     } while (!_op->is_completed());






 202     DEBUG_ONLY(_op->check_state();)
 203 
 204     log_handshake_info(start_time_ns, _op->name(), number_of_threads_issued, handshake_executed_by_vm_thread);
 205   }
 206 
 207   VMOp_Type type() const { return VMOp_HandshakeAllThreads; }
 208 };
 209 
 210 void HandshakeOperation::do_handshake(JavaThread* thread) {
 211   jlong start_time_ns = 0;
 212   if (log_is_enabled(Debug, handshake, task)) {
 213     start_time_ns = os::javaTimeNanos();
 214   }
 215 
 216   // Only actually execute the operation for non terminated threads.
 217   if (!thread->is_terminated()) {
 218     _handshake_cl->do_thread(thread);
 219     _executed = true;
 220   }
 221 
 222   if (start_time_ns != 0) {
 223     jlong completion_time = os::javaTimeNanos() - start_time_ns;
 224     log_debug(handshake, task)("Operation: %s for thread " PTR_FORMAT ", is_vm_thread: %s, completed in " JLONG_FORMAT " ns",
 225                                name(), p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread()), completion_time);
 226   }
 227 
 228   // Inform VMThread/Handshaker that we have completed the operation
 229   Atomic::dec(&_pending_threads);
 230 
 231   // It is no longer safe to refer to 'this' as the VMThread/Handshaker may have destroyed this operation
 232 }
 233 
 234 void Handshake::execute(HandshakeClosure* thread_cl) {
 235   HandshakeOperation cto(thread_cl);
 236   VM_HandshakeAllThreads handshake(&cto);
 237   VMThread::execute(&handshake);
 238 }
 239 
 240 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 241   HandshakeOperation cto(thread_cl);
 242   VM_HandshakeOneThread handshake(&cto, target);
 243   VMThread::execute(&handshake);
 244   return handshake.executed();
 245 }
 246 
 247 bool Handshake::execute_direct(HandshakeClosure* thread_cl, JavaThread* target) {
 248   JavaThread* self = JavaThread::current();
 249   HandshakeOperation op(thread_cl, /*is_direct*/ true);
 250 
 251   jlong start_time_ns = 0;
 252   if (log_is_enabled(Info, handshake)) {
 253     start_time_ns = os::javaTimeNanos();
 254   }
 255 
 256   ThreadsListHandle tlh;
 257   if (tlh.includes(target)) {
 258     target->set_handshake_operation(&op);
 259   } else {
 260     log_handshake_info(start_time_ns, op.name(), 0, 0, "(thread dead)");
 261     return false;
 262   }
 263 
 264   bool by_handshaker = false;
 265   while (!op.is_completed()) {
 266     by_handshaker = target->handshake_try_process(&op);
 267     // Check for pending handshakes to avoid possible deadlocks where our
 268     // target is trying to handshake us.
 269     if (SafepointMechanism::should_block(self)) {
 270       ThreadBlockInVM tbivm(self);
 271     }
 272   }
 273   DEBUG_ONLY(op.check_state();)
 274   log_handshake_info(start_time_ns, op.name(), 1, by_handshaker ? 1 : 0);
 275 
 276   return op.executed();
 277 }
 278 
 279 HandshakeState::HandshakeState() :
 280   _operation(NULL),
 281   _operation_direct(NULL),
 282   _handshake_turn_sem(1),
 283   _processing_sem(1),
 284   _thread_in_process_handshake(false)
 285 {
 286   DEBUG_ONLY(_active_handshaker = NULL;)
 287 }
 288 
 289 void HandshakeState::set_operation(HandshakeOperation* op) {
 290   if (!op->is_direct()) {
 291     assert(Thread::current()->is_VM_thread(), "should be the VMThread");
 292     _operation = op;
 293   } else {
 294     assert(Thread::current()->is_Java_thread(), "should be a JavaThread");
 295     // Serialize direct handshakes so that only one proceeds at a time for a given target
 296     _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current());
 297     _operation_direct = op;
 298   }
 299   SafepointMechanism::arm_local_poll_release(_handshakee);
 300 }
 301 
 302 void HandshakeState::clear_handshake(bool is_direct) {
 303   if (!is_direct) {
 304     _operation = NULL;
 305   } else {
 306     _operation_direct = NULL;
 307     _handshake_turn_sem.signal();
 308   }
 309 }
 310 
 311 void HandshakeState::process_self_inner() {
 312   assert(Thread::current() == _handshakee, "should call from _handshakee");
 313   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
 314   assert(_handshakee->thread_state() != _thread_blocked, "should not be in a blocked state");
 315   assert(_handshakee->thread_state() != _thread_in_native, "should not be in native");
 316   JavaThread* self = _handshakee;
 317 
 318   do {
 319     ThreadInVMForHandshake tivm(self);
 320     if (!_processing_sem.trywait()) {
 321       _processing_sem.wait_with_safepoint_check(self);
 322     }
 323     if (has_operation()) {
 324       HandleMark hm(self);
 325       CautiouslyPreserveExceptionMark pem(self);
 326       HandshakeOperation * op = _operation;
 327       if (op != NULL) {
 328         // Disarm before executing the operation
 329         clear_handshake(/*is_direct*/ false);
 330         op->do_handshake(self);
 331       }
 332       op = _operation_direct;
 333       if (op != NULL) {
 334         // Disarm before executing the operation
 335         clear_handshake(/*is_direct*/ true);
 336         op->do_handshake(self);


 337       }
 338     }
 339     _processing_sem.signal();
 340   } while (has_operation());
 341 }
 342 
 343 bool HandshakeState::can_process_handshake() {
 344   // handshake_safe may only be called with polls armed.
 345   // Handshaker controls this by first claiming the handshake via claim_handshake().
 346   return SafepointSynchronize::handshake_safe(_handshakee);
 347 }
 348 
 349 bool HandshakeState::possibly_can_process_handshake() {
 350   // Note that this method is allowed to produce false positives.
 351   if (_handshakee->is_ext_suspended()) {
 352     return true;
 353   }
 354   if (_handshakee->is_terminated()) {
 355     return true;
 356   }
 357   switch (_handshakee->thread_state()) {
 358   case _thread_in_native:
 359     // native threads are safe if they have no java stack or have walkable stack
 360     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
 361 
 362   case _thread_blocked:
 363     return true;
 364 
 365   default:
 366     return false;
 367   }
 368 }
 369 
 370 bool HandshakeState::claim_handshake(bool is_direct) {
 371   if (!_processing_sem.trywait()) {
 372     return false;
 373   }
 374   if (has_specific_operation(is_direct)){
 375     return true;
 376   }
 377   _processing_sem.signal();
 378   return false;
 379 }
 380 
 381 bool HandshakeState::try_process(HandshakeOperation* op) {
 382   bool is_direct = op->is_direct();
 383 
 384   if (!has_specific_operation(is_direct)){
 385     // JT has already cleared its handshake
 386     return false;
 387   }
 388 
 389   if (!possibly_can_process_handshake()) {
 390     // JT is observed in an unsafe state, it must notice the handshake itself
 391     return false;
 392   }
 393 
 394   // Claim the semaphore if there still an operation to be executed.
 395   if (!claim_handshake(is_direct)) {
 396     return false;
 397   }
 398 
 399   // Check if the handshake operation is the same as the one we meant to execute. The
 400   // handshake could have been already processed by the handshakee and a new handshake
 401   // by another JavaThread might be in progress.
 402   if ( (is_direct && op != _operation_direct)) {
 403     _processing_sem.signal();
 404     return false;
 405   }
 406 
 407   // If we own the semaphore at this point and while owning the semaphore
 408   // can observe a safe state the thread cannot possibly continue without
 409   // getting caught by the semaphore.
 410   bool executed = false;
 411   if (can_process_handshake()) {
 412     guarantee(!_processing_sem.trywait(), "we should already own the semaphore");
 413     log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker");
 414     DEBUG_ONLY(_active_handshaker = Thread::current();)
 415     op->do_handshake(_handshakee);
 416     DEBUG_ONLY(_active_handshaker = NULL;)
 417     // Disarm after we have executed the operation.
 418     clear_handshake(is_direct);
 419     executed = true;
 420   }
 421 
 422   // Release the thread
 423   _processing_sem.signal();
 424 
 425   return executed;
 426 }
< prev index next >