< prev index next >

src/hotspot/share/runtime/handshake.cpp

Print this page
rev 51997 : [mq]: 8210303


 330 void HandshakeState::cancel_inner(JavaThread* thread) {
 331   assert(Thread::current() == thread, "should call from thread");
 332   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
 333   HandshakeOperation* op = _operation;
 334   clear_handshake(thread);
 335   if (op != NULL) {
 336     op->cancel_handshake(thread);
 337   }
 338 }
 339 
 340 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 341   // SafepointSynchronize::safepoint_safe() does not consider an externally
 342   // suspended thread to be safe. However, this function must be called with
 343   // the Threads_lock held so an externally suspended thread cannot be
 344   // resumed thus it is safe.
 345   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 346   return SafepointSynchronize::safepoint_safe(target, target->thread_state()) ||
 347          target->is_ext_suspended();
 348 }
 349 














 350 bool HandshakeState::claim_handshake_for_vmthread() {
 351   if (!_semaphore.trywait()) {
 352     return false;
 353   }
 354   if (has_operation()) {
 355     return true;
 356   }
 357   _semaphore.signal();
 358   return false;
 359 }
 360 
 361 void HandshakeState::process_by_vmthread(JavaThread* target) {
 362   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 363 
 364   if (!has_operation()) {
 365     // JT has already cleared its handshake
 366     return;
 367   }
 368 
 369   if (!vmthread_can_process_handshake(target)) {
 370     // JT is observed in an unsafe state, it must notice the handshake itself
 371     return;
 372   }
 373 
 374   // Claim the semaphore if there still an operation to be executed.
 375   if (!claim_handshake_for_vmthread()) {
 376     return;
 377   }
 378 
 379   // If we own the semaphore at this point and while owning the semaphore
 380   // can observe a safe state the thread cannot possibly continue without
 381   // getting caught by the semaphore.
 382   if (vmthread_can_process_handshake(target)) {
 383     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 384 
 385     _operation->do_handshake(target);
 386     // Disarm after VM thread have executed the operation.
 387     clear_handshake(target);
 388     // Release the thread
 389   }


 330 void HandshakeState::cancel_inner(JavaThread* thread) {
 331   assert(Thread::current() == thread, "should call from thread");
 332   assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
 333   HandshakeOperation* op = _operation;
 334   clear_handshake(thread);
 335   if (op != NULL) {
 336     op->cancel_handshake(thread);
 337   }
 338 }
 339 
 340 bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) {
 341   // SafepointSynchronize::safepoint_safe() does not consider an externally
 342   // suspended thread to be safe. However, this function must be called with
 343   // the Threads_lock held so an externally suspended thread cannot be
 344   // resumed thus it is safe.
 345   assert(Threads_lock->owned_by_self(), "Not holding Threads_lock.");
 346   return SafepointSynchronize::safepoint_safe(target, target->thread_state()) ||
 347          target->is_ext_suspended();
 348 }
 349 
 350 static bool possibly_vmthread_can_process_handshake(JavaThread* target) {
 351   switch(target->thread_state()) {
 352   case _thread_in_native:
 353     // native threads are safe if they have no java stack or have walkable stack
 354     return !target->has_last_Java_frame() || target->frame_anchor()->walkable();
 355 
 356   case _thread_blocked:
 357     return true;
 358 
 359   default:
 360     return false;
 361   }
 362 }
 363 
 364 bool HandshakeState::claim_handshake_for_vmthread() {
 365   if (!_semaphore.trywait()) {
 366     return false;
 367   }
 368   if (has_operation()) {
 369     return true;
 370   }
 371   _semaphore.signal();
 372   return false;
 373 }
 374 
 375 void HandshakeState::process_by_vmthread(JavaThread* target) {
 376   assert(Thread::current()->is_VM_thread(), "should call from vm thread");
 377 
 378   if (!has_operation()) {
 379     // JT has already cleared its handshake
 380     return;
 381   }
 382 
 383   if (!possibly_vmthread_can_process_handshake(target)) {
 384     // JT is observed in an unsafe state, it must notice the handshake itself
 385     return;
 386   }
 387 
 388   // Claim the semaphore if there still an operation to be executed.
 389   if (!claim_handshake_for_vmthread()) {
 390     return;
 391   }
 392 
 393   // If we own the semaphore at this point and while owning the semaphore
 394   // can observe a safe state the thread cannot possibly continue without
 395   // getting caught by the semaphore.
 396   if (vmthread_can_process_handshake(target)) {
 397     guarantee(!_semaphore.trywait(), "we should already own the semaphore");
 398 
 399     _operation->do_handshake(target);
 400     // Disarm after VM thread have executed the operation.
 401     clear_handshake(target);
 402     // Release the thread
 403   }
< prev index next >