366 } 367 hsy.process(); 368 } 369 370 // This pairs up with the release store in do_handshake(). It prevents future 371 // loads from floating above the load of _pending_threads in is_completed() 372 // and thus prevents reading stale data modified in the handshake closure 373 // by the Handshakee. 374 OrderAccess::acquire(); 375 376 log_handshake_info(start_time_ns, op.name(), 1, (pr == HandshakeState::_success) ? 1 : 0); 377 378 return op.executed(); 379 } 380 381 HandshakeState::HandshakeState() : 382 _operation(NULL), 383 _operation_direct(NULL), 384 _handshake_turn_sem(1), 385 _processing_sem(1), 386 _thread_in_process_handshake(false) 387 { 388 DEBUG_ONLY(_active_handshaker = NULL;) 389 } 390 391 void HandshakeState::set_operation(HandshakeOperation* op) { 392 if (!op->is_direct()) { 393 assert(Thread::current()->is_VM_thread(), "should be the VMThread"); 394 _operation = op; 395 } else { 396 assert(Thread::current()->is_Java_thread(), "should be a JavaThread"); 397 // Serialize direct handshakes so that only one proceeds at a time for a given target 398 _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current()); 399 _operation_direct = op; 400 } 401 SafepointMechanism::arm_local_poll_release(_handshakee); 402 } 403 404 void HandshakeState::clear_handshake(bool is_direct) { 405 if (!is_direct) { 406 _operation = NULL; 407 } else { 408 _operation_direct = NULL; 496 // Claim the semaphore if there still an operation to be executed. 497 if (!claim_handshake(is_direct)) { 498 return _state_busy; 499 } 500 501 // Check if the handshake operation is the same as the one we meant to execute. The 502 // handshake could have been already processed by the handshakee and a new handshake 503 // by another JavaThread might be in progress. 504 if (is_direct && op != _operation_direct) { 505 _processing_sem.signal(); 506 return _no_operation; 507 } 508 509 // If we own the semaphore at this point and while owning the semaphore 510 // can observe a safe state the thread cannot possibly continue without 511 // getting caught by the semaphore. 512 ProcessResult pr = _not_safe; 513 if (can_process_handshake()) { 514 guarantee(!_processing_sem.trywait(), "we should already own the semaphore"); 515 log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker"); 516 DEBUG_ONLY(_active_handshaker = Thread::current();) 517 op->do_handshake(_handshakee); 518 DEBUG_ONLY(_active_handshaker = NULL;) 519 // Disarm after we have executed the operation. 520 clear_handshake(is_direct); 521 pr = _success; 522 } 523 524 // Release the thread 525 _processing_sem.signal(); 526 527 return pr; 528 } | 366 } 367 hsy.process(); 368 } 369 370 // This pairs up with the release store in do_handshake(). It prevents future 371 // loads from floating above the load of _pending_threads in is_completed() 372 // and thus prevents reading stale data modified in the handshake closure 373 // by the Handshakee. 374 OrderAccess::acquire(); 375 376 log_handshake_info(start_time_ns, op.name(), 1, (pr == HandshakeState::_success) ? 1 : 0); 377 378 return op.executed(); 379 } 380 381 HandshakeState::HandshakeState() : 382 _operation(NULL), 383 _operation_direct(NULL), 384 _handshake_turn_sem(1), 385 _processing_sem(1), 386 _thread_in_process_handshake(false), 387 _active_handshaker(NULL) 388 { 389 } 390 391 void HandshakeState::set_operation(HandshakeOperation* op) { 392 if (!op->is_direct()) { 393 assert(Thread::current()->is_VM_thread(), "should be the VMThread"); 394 _operation = op; 395 } else { 396 assert(Thread::current()->is_Java_thread(), "should be a JavaThread"); 397 // Serialize direct handshakes so that only one proceeds at a time for a given target 398 _handshake_turn_sem.wait_with_safepoint_check(JavaThread::current()); 399 _operation_direct = op; 400 } 401 SafepointMechanism::arm_local_poll_release(_handshakee); 402 } 403 404 void HandshakeState::clear_handshake(bool is_direct) { 405 if (!is_direct) { 406 _operation = NULL; 407 } else { 408 _operation_direct = NULL; 496 // Claim the semaphore if there still an operation to be executed. 497 if (!claim_handshake(is_direct)) { 498 return _state_busy; 499 } 500 501 // Check if the handshake operation is the same as the one we meant to execute. The 502 // handshake could have been already processed by the handshakee and a new handshake 503 // by another JavaThread might be in progress. 504 if (is_direct && op != _operation_direct) { 505 _processing_sem.signal(); 506 return _no_operation; 507 } 508 509 // If we own the semaphore at this point and while owning the semaphore 510 // can observe a safe state the thread cannot possibly continue without 511 // getting caught by the semaphore. 512 ProcessResult pr = _not_safe; 513 if (can_process_handshake()) { 514 guarantee(!_processing_sem.trywait(), "we should already own the semaphore"); 515 log_trace(handshake)("Processing handshake by %s", Thread::current()->is_VM_thread() ? "VMThread" : "Handshaker"); 516 _active_handshaker = Thread::current(); 517 op->do_handshake(_handshakee); 518 _active_handshaker = NULL; 519 // Disarm after we have executed the operation. 520 clear_handshake(is_direct); 521 pr = _success; 522 } 523 524 // Release the thread 525 _processing_sem.signal(); 526 527 return pr; 528 } |