< prev index next >

src/hotspot/share/runtime/handshake.cpp

Print this page




  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   int64_t _pending_threads;
  44   bool _executed;
  45   bool _is_direct;
  46 public:
  47   HandshakeOperation(HandshakeClosure* cl) : _handshake_cl(cl), _pending_threads(1), _executed(false), _is_direct(false) {}
  48   HandshakeOperation(HandshakeClosure* cl, bool is_direct) : _handshake_cl(cl), _pending_threads(1), _executed(false), _is_direct(is_direct) {}
  49   void do_handshake(JavaThread* thread);
  50   bool is_completed() {
  51     assert(_pending_threads >= 0, "_pending_threads cannot be negative");
  52     return _pending_threads == 0;

  53   }
  54   void add_target_count(int count) { Atomic::add(&_pending_threads, count); }
  55   bool executed() const { return _executed; }
  56   const char* name() { return _handshake_cl->name(); }
  57 
  58   bool is_direct() { return _is_direct; }
  59 
  60 #ifdef ASSERT
  61   void check_state() {
  62     assert(_pending_threads == 0, "Must be zero");
  63   }
  64 #endif
  65 };
  66 
  67 class VM_Handshake: public VM_Operation {
  68   const jlong _handshake_timeout;
  69  public:
  70   bool evaluate_at_safepoint() const { return false; }
  71 
  72  protected:


 154   VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {}
 155 
 156   void doit() {
 157     jlong start_time_ns = 0;
 158     if (log_is_enabled(Info, handshake)) {
 159       start_time_ns = os::javaTimeNanos();
 160     }
 161     int handshake_executed_by_vm_thread = 0;
 162 
 163     JavaThreadIteratorWithHandle jtiwh;
 164     int number_of_threads_issued = 0;
 165     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 166       thr->set_handshake_operation(_op);
 167       number_of_threads_issued++;
 168     }
 169 
 170     if (number_of_threads_issued < 1) {
 171       log_handshake_info(start_time_ns, _op->name(), 0, 0);
 172       return;
 173     }

 174     _op->add_target_count(number_of_threads_issued - 1);
 175 
 176     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 177     const jlong start_time = os::elapsed_counter();
 178     do {
 179       // Check if handshake operation has timed out
 180       if (handshake_has_timed_out(start_time)) {
 181         handle_timeout();
 182       }
 183 
 184       // Have VM thread perform the handshake operation for blocked threads.
 185       // Observing a blocked state may of course be transient but the processing is guarded
 186       // by semaphores and we optimistically begin by working on the blocked threads
 187       jtiwh.rewind();
 188       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 189         // A new thread on the ThreadsList will not have an operation,
 190         // hence it is skipped in handshake_try_process.
 191         if (thr->handshake_try_process(_op)) {
 192           handshake_executed_by_vm_thread++;
 193         }


 265 
 266 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 267   if (SafepointMechanism::uses_thread_local_poll()) {
 268     HandshakeOperation ho(thread_cl);
 269     VM_HandshakeOneThread handshake(&ho, target);
 270     VMThread::execute(&handshake);
 271     return handshake.executed();
 272   } else {
 273     VM_HandshakeFallbackOperation op(thread_cl, target);
 274     VMThread::execute(&op);
 275     return op.executed();
 276   }
 277 }
 278 
 279 bool Handshake::execute_direct(HandshakeClosure* thread_cl, JavaThread* target) {
 280   if (!SafepointMechanism::uses_thread_local_poll()) {
 281     VM_HandshakeFallbackOperation op(thread_cl, target);
 282     VMThread::execute(&op);
 283     return op.executed();
 284   }
 285   JavaThread *self = (JavaThread*)Thread::current();
 286   HandshakeOperation op(thread_cl, true);
 287 
 288   jlong start_time_ns = 0;
 289   if (log_is_enabled(Info, handshake)) {
 290     start_time_ns = os::javaTimeNanos();
 291   }
 292 
 293   ThreadsListHandle tlh;
 294   if (tlh.includes(target)) {
 295     target->set_handshake_operation(&op);
 296   } else {
 297     log_handshake_info(start_time_ns, op.name(), 0, 0, "(thread dead)");
 298     return false;
 299   }
 300 
 301   bool by_handshaker = false;
 302   while (!op.is_completed()) {
 303     by_handshaker = target->handshake_try_process(&op);
 304     // Check for pending handshakes to avoid possible deadlocks where our
 305     // target is trying to handshake us.
 306     if (SafepointMechanism::should_block(self)) {
 307       ThreadBlockInVM tbivm(self);
 308     }
 309   }
 310   DEBUG_ONLY(op.check_state();)
 311   log_handshake_info(start_time_ns, op.name(), 1, by_handshaker ? 1 : 0);
 312 
 313   return op.executed();
 314 }
 315 
 316 HandshakeState::HandshakeState() : _operation(NULL), _operation_direct(NULL), _handshake_turn_sem(1), _processing_sem(1), _thread_in_process_handshake(false) {






 317   DEBUG_ONLY(_active_handshaker = NULL;)
 318 }
 319 
 320 void HandshakeState::set_operation(HandshakeOperation* op) {
 321   if (!op->is_direct()) {

 322     _operation = op;
 323   } else {

 324     // Serialize direct handshakes so that only one proceeds at a time for a given target
 325     _handshake_turn_sem.wait_with_safepoint_check((JavaThread*)Thread::current());
 326     _operation_direct = op;
 327   }
 328   SafepointMechanism::arm_local_poll_release(_thread);
 329 }
 330 
 331 void HandshakeState::clear_handshake(bool is_direct) {
 332   if (!is_direct) {
 333     _operation = NULL;
 334   } else {
 335     _operation_direct = NULL;
 336     _handshake_turn_sem.signal();
 337   }
 338 }
 339 
 340 void HandshakeState::process_self_inner() {
 341   assert(Thread::current() == _thread, "should call from thread");
 342   assert(!_thread->is_terminated(), "should not be a terminated thread");
 343   assert(_thread->thread_state() != _thread_blocked, "should not be in a blocked state");
 344   assert(_thread->thread_state() != _thread_in_native, "should not be in native");

 345 
 346   do {
 347     ThreadInVMForHandshake tivm(_thread);
 348     if (!_processing_sem.trywait()) {
 349       _processing_sem.wait_with_safepoint_check(_thread);
 350     }
 351     if (has_operation()) {
 352       HandleMark hm(_thread);
 353       CautiouslyPreserveExceptionMark pem(_thread);
 354       HandshakeOperation * op = _operation;
 355       if (op != NULL) {
 356         // Disarm before execute the operation
 357         clear_handshake(false);
 358         op->do_handshake(_thread);
 359       }
 360       op = _operation_direct;
 361       if (op != NULL) {
 362         // Disarm before execute the operation
 363         clear_handshake(true);
 364         op->do_handshake(_thread);
 365       }
 366     }
 367     _processing_sem.signal();
 368   } while (has_operation());
 369 }
 370 
 371 bool HandshakeState::can_process_handshake() {
 372   // handshake_safe may only be called with polls armed.
 373   // Handshaker controls this by first claiming the handshake via claim_handshake().
 374   return SafepointSynchronize::handshake_safe(_thread);
 375 }
 376 
 377 bool HandshakeState::possibly_can_process_handshake() {
 378   // Note that this method is allowed to produce false positives.
 379   if (_thread->is_ext_suspended()) {
 380     return true;
 381   }
 382   if (_thread->is_terminated()) {
 383     return true;
 384   }
 385   switch (_thread->thread_state()) {
 386   case _thread_in_native:
 387     // native threads are safe if they have no java stack or have walkable stack
 388     return !_thread->has_last_Java_frame() || _thread->frame_anchor()->walkable();
 389 
 390   case _thread_blocked:
 391     return true;
 392 
 393   default:
 394     return false;
 395   }
 396 }
 397 
 398 bool HandshakeState::claim_handshake(bool is_direct) {
 399   if (!_processing_sem.trywait()) {
 400     return false;
 401   }
 402   if (has_specific_operation(is_direct)){
 403     return true;
 404   }
 405   _processing_sem.signal();
 406   return false;
 407 }
 408 


 423   if (!claim_handshake(is_direct)) {
 424     return false;
 425   }
 426 
 427   // Check if the handshake operation is the same as the one we meant to execute. The
 428   // handshake could have been already processed by the handshakee and a new handshake
 429   // by another JavaThread might be in progress.
 430   if ( (is_direct && op != _operation_direct)) {
 431     _processing_sem.signal();
 432     return false;
 433   }
 434 
 435   // If we own the semaphore at this point and while owning the semaphore
 436   // can observe a safe state the thread cannot possibly continue without
 437   // getting caught by the semaphore.
 438   bool executed = false;
 439   if (can_process_handshake()) {
 440     guarantee(!_processing_sem.trywait(), "we should already own the semaphore");
 441     log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker");
 442     DEBUG_ONLY(_active_handshaker = Thread::current();)
 443     op->do_handshake(_thread);
 444     DEBUG_ONLY(_active_handshaker = NULL;)
 445     // Disarm after we have executed the operation.
 446     clear_handshake(is_direct);
 447     executed = true;
 448   }
 449 
 450   // Release the thread
 451   _processing_sem.signal();
 452 
 453   return executed;
 454 }


  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   int64_t _pending_threads;
  44   bool _executed;
  45   bool _is_direct;
  46 public:
  47   HandshakeOperation(HandshakeClosure* cl, bool is_direct = false) : _handshake_cl(cl), _pending_threads(1), _executed(false), _is_direct(is_direct) {}

  48   void do_handshake(JavaThread* thread);
  49   bool is_completed() {
  50     int64_t val = Atomic::load(&_pending_threads);
  51     assert(val >= 0, "_pending_threads cannot be negative");
  52     return val == 0;
  53   }
  54   void add_target_count(int count) { Atomic::add(&_pending_threads, count); }
  55   bool executed() const { return _executed; }
  56   const char* name() { return _handshake_cl->name(); }
  57 
  58   bool is_direct() { return _is_direct; }
  59 
  60 #ifdef ASSERT
  61   void check_state() {
  62     assert(_pending_threads == 0, "Must be zero");
  63   }
  64 #endif
  65 };
  66 
  67 class VM_Handshake: public VM_Operation {
  68   const jlong _handshake_timeout;
  69  public:
  70   bool evaluate_at_safepoint() const { return false; }
  71 
  72  protected:


 154   VM_HandshakeAllThreads(HandshakeOperation* op) : VM_Handshake(op) {}
 155 
 156   void doit() {
 157     jlong start_time_ns = 0;
 158     if (log_is_enabled(Info, handshake)) {
 159       start_time_ns = os::javaTimeNanos();
 160     }
 161     int handshake_executed_by_vm_thread = 0;
 162 
 163     JavaThreadIteratorWithHandle jtiwh;
 164     int number_of_threads_issued = 0;
 165     for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 166       thr->set_handshake_operation(_op);
 167       number_of_threads_issued++;
 168     }
 169 
 170     if (number_of_threads_issued < 1) {
 171       log_handshake_info(start_time_ns, _op->name(), 0, 0);
 172       return;
 173     }
 174     // _op was created with a count == 1 so don't double count.
 175     _op->add_target_count(number_of_threads_issued - 1);
 176 
 177     log_trace(handshake)("Threads signaled, begin processing blocked threads by VMThread");
 178     const jlong start_time = os::elapsed_counter();
 179     do {
 180       // Check if handshake operation has timed out
 181       if (handshake_has_timed_out(start_time)) {
 182         handle_timeout();
 183       }
 184 
 185       // Have VM thread perform the handshake operation for blocked threads.
 186       // Observing a blocked state may of course be transient but the processing is guarded
 187       // by semaphores and we optimistically begin by working on the blocked threads
 188       jtiwh.rewind();
 189       for (JavaThread *thr = jtiwh.next(); thr != NULL; thr = jtiwh.next()) {
 190         // A new thread on the ThreadsList will not have an operation,
 191         // hence it is skipped in handshake_try_process.
 192         if (thr->handshake_try_process(_op)) {
 193           handshake_executed_by_vm_thread++;
 194         }


 266 
 267 bool Handshake::execute(HandshakeClosure* thread_cl, JavaThread* target) {
 268   if (SafepointMechanism::uses_thread_local_poll()) {
 269     HandshakeOperation ho(thread_cl);
 270     VM_HandshakeOneThread handshake(&ho, target);
 271     VMThread::execute(&handshake);
 272     return handshake.executed();
 273   } else {
 274     VM_HandshakeFallbackOperation op(thread_cl, target);
 275     VMThread::execute(&op);
 276     return op.executed();
 277   }
 278 }
 279 
 280 bool Handshake::execute_direct(HandshakeClosure* thread_cl, JavaThread* target) {
 281   if (!SafepointMechanism::uses_thread_local_poll()) {
 282     VM_HandshakeFallbackOperation op(thread_cl, target);
 283     VMThread::execute(&op);
 284     return op.executed();
 285   }
 286   JavaThread *self = JavaThread::current();
 287   HandshakeOperation op(thread_cl, /*is_direct*/ true);
 288 
 289   jlong start_time_ns = 0;
 290   if (log_is_enabled(Info, handshake)) {
 291     start_time_ns = os::javaTimeNanos();
 292   }
 293 
 294   ThreadsListHandle tlh;
 295   if (tlh.includes(target)) {
 296     target->set_handshake_operation(&op);
 297   } else {
 298     log_handshake_info(start_time_ns, op.name(), 0, 0, "(thread dead)");
 299     return false;
 300   }
 301 
 302   bool by_handshaker = false;
 303   while (!op.is_completed()) {
 304     by_handshaker = target->handshake_try_process(&op);
 305     // Check for pending handshakes to avoid possible deadlocks where our
 306     // target is trying to handshake us.
 307     if (SafepointMechanism::should_block(self)) {
 308       ThreadBlockInVM tbivm(self);
 309     }
 310   }
 311   DEBUG_ONLY(op.check_state();)
 312   log_handshake_info(start_time_ns, op.name(), 1, by_handshaker ? 1 : 0);
 313 
 314   return op.executed();
 315 }
 316 
 317 HandshakeState::HandshakeState() :
 318   _operation(NULL),
 319   _operation_direct(NULL),
 320   _handshake_turn_sem(1),
 321   _processing_sem(1),
 322   _thread_in_process_handshake(false)
 323 {
 324   DEBUG_ONLY(_active_handshaker = NULL;)
 325 }
 326 
 327 void HandshakeState::set_operation(HandshakeOperation* op) {
 328   if (!op->is_direct()) {
 329     assert(Thread::current()->is_VM_thread(), "should be the VMThread");
 330     _operation = op;
 331   } else {
 332     assert(Thread::current()->is_Java_thread(), "should be a JavaThread");
 333     // Serialize direct handshakes so that only one proceeds at a time for a given target
 334     _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current());
 335     _operation_direct = op;
 336   }
 337   SafepointMechanism::arm_local_poll_release(_handshakee);
 338 }
 339 
 340 void HandshakeState::clear_handshake(bool is_direct) {
 341   if (!is_direct) {
 342     _operation = NULL;
 343   } else {
 344     _operation_direct = NULL;
 345     _handshake_turn_sem.signal();
 346   }
 347 }
 348 
 349 void HandshakeState::process_self_inner() {
 350   assert(Thread::current() == _handshakee, "should call from _handshakee");
 351   assert(!_handshakee->is_terminated(), "should not be a terminated thread");
 352   assert(_handshakee->thread_state() != _thread_blocked, "should not be in a blocked state");
 353   assert(_handshakee->thread_state() != _thread_in_native, "should not be in native");
 354   JavaThread* self = _handshakee;
 355 
 356   do {
 357     ThreadInVMForHandshake tivm(self);
 358     if (!_processing_sem.trywait()) {
 359       _processing_sem.wait_with_safepoint_check(self);
 360     }
 361     if (has_operation()) {
 362       HandleMark hm(self);
 363       CautiouslyPreserveExceptionMark pem(self);
 364       HandshakeOperation * op = _operation;
 365       if (op != NULL) {
 366         // Disarm before executing the operation
 367         clear_handshake( /*is_direct*/ false);
 368         op->do_handshake(self);
 369       }
 370       op = _operation_direct;
 371       if (op != NULL) {
 372         // Disarm before executing the operation
 373         clear_handshake( /*is_direct*/ true);
 374         op->do_handshake(self);
 375       }
 376     }
 377     _processing_sem.signal();
 378   } while (has_operation());
 379 }
 380 
 381 bool HandshakeState::can_process_handshake() {
 382   // handshake_safe may only be called with polls armed.
 383   // Handshaker controls this by first claiming the handshake via claim_handshake().
 384   return SafepointSynchronize::handshake_safe(_handshakee);
 385 }
 386 
 387 bool HandshakeState::possibly_can_process_handshake() {
 388   // Note that this method is allowed to produce false positives.
 389   if (_handshakee->is_ext_suspended()) {
 390     return true;
 391   }
 392   if (_handshakee->is_terminated()) {
 393     return true;
 394   }
 395   switch (_handshakee->thread_state()) {
 396   case _thread_in_native:
 397     // native threads are safe if they have no java stack or have walkable stack
 398     return !_handshakee->has_last_Java_frame() || _handshakee->frame_anchor()->walkable();
 399 
 400   case _thread_blocked:
 401     return true;
 402 
 403   default:
 404     return false;
 405   }
 406 }
 407 
 408 bool HandshakeState::claim_handshake(bool is_direct) {
 409   if (!_processing_sem.trywait()) {
 410     return false;
 411   }
 412   if (has_specific_operation(is_direct)){
 413     return true;
 414   }
 415   _processing_sem.signal();
 416   return false;
 417 }
 418 


 433   if (!claim_handshake(is_direct)) {
 434     return false;
 435   }
 436 
 437   // Check if the handshake operation is the same as the one we meant to execute. The
 438   // handshake could have been already processed by the handshakee and a new handshake
 439   // by another JavaThread might be in progress.
 440   if ( (is_direct && op != _operation_direct)) {
 441     _processing_sem.signal();
 442     return false;
 443   }
 444 
 445   // If we own the semaphore at this point and while owning the semaphore
 446   // can observe a safe state the thread cannot possibly continue without
 447   // getting caught by the semaphore.
 448   bool executed = false;
 449   if (can_process_handshake()) {
 450     guarantee(!_processing_sem.trywait(), "we should already own the semaphore");
 451     log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker");
 452     DEBUG_ONLY(_active_handshaker = Thread::current();)
 453     op->do_handshake(_handshakee);
 454     DEBUG_ONLY(_active_handshaker = NULL;)
 455     // Disarm after we have executed the operation.
 456     clear_handshake(is_direct);
 457     executed = true;
 458   }
 459 
 460   // Release the thread
 461   _processing_sem.signal();
 462 
 463   return executed;
 464 }
< prev index next >