< prev index next >

src/hotspot/share/runtime/handshake.cpp

Print this page
rev 50557 : [mq]: 8204166

*** 26,35 **** --- 26,36 ---- #include "logging/log.hpp" #include "logging/logStream.hpp" #include "memory/resourceArea.hpp" #include "runtime/handshake.hpp" #include "runtime/interfaceSupport.inline.hpp" + #include "runtime/orderAccess.hpp" #include "runtime/osThread.hpp" #include "runtime/semaphore.inline.hpp" #include "runtime/task.hpp" #include "runtime/timerTrace.hpp" #include "runtime/thread.hpp"
*** 42,71 **** virtual void do_handshake(JavaThread* thread) = 0; virtual void cancel_handshake(JavaThread* thread) = 0; }; class HandshakeThreadsOperation: public HandshakeOperation { ! Semaphore _done; ThreadClosure* _thread_cl; public: ! HandshakeThreadsOperation(ThreadClosure* cl) : _done(0), _thread_cl(cl) {} void do_handshake(JavaThread* thread); void cancel_handshake(JavaThread* thread) { _done.signal(); }; bool thread_has_completed() { return _done.trywait(); } }; class VM_Handshake: public VM_Operation { - HandshakeThreadsOperation* const _op; const jlong _handshake_timeout; public: bool evaluate_at_safepoint() const { return false; } bool evaluate_concurrently() const { return false; } protected: VM_Handshake(HandshakeThreadsOperation* op) : _op(op), _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)) {} --- 43,80 ---- virtual void do_handshake(JavaThread* thread) = 0; virtual void cancel_handshake(JavaThread* thread) = 0; }; class HandshakeThreadsOperation: public HandshakeOperation { ! static Semaphore _done; ThreadClosure* _thread_cl; public: ! HandshakeThreadsOperation(ThreadClosure* cl) : _thread_cl(cl) {} void do_handshake(JavaThread* thread); void cancel_handshake(JavaThread* thread) { _done.signal(); }; bool thread_has_completed() { return _done.trywait(); } + + #ifdef ASSERT + void check_state() { + assert(!_done.trywait(), "Must be zero"); + } + #endif }; + Semaphore HandshakeThreadsOperation::_done(0); + class VM_Handshake: public VM_Operation { const jlong _handshake_timeout; public: bool evaluate_at_safepoint() const { return false; } bool evaluate_concurrently() const { return false; } protected: + HandshakeThreadsOperation* const _op; VM_Handshake(HandshakeThreadsOperation* op) : _op(op), _handshake_timeout(TimeHelper::millis_to_counter(HandshakeTimeout)) {}
*** 100,118 **** } log_stream.flush(); fatal("Handshake operation timed out"); } - class VM_HandshakeOneThread: public VM_Handshake { JavaThread* _target; bool _thread_alive; public: VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) : VM_Handshake(op), _target(target), _thread_alive(false) {} void doit() { TraceTime timer("Performing single-target operation (vmoperation doit)", TRACETIME_LOG(Info, handshake)); { ThreadsListHandle tlh; if (tlh.includes(_target)) { --- 109,127 ---- } log_stream.flush(); fatal("Handshake operation timed out"); } class VM_HandshakeOneThread: public VM_Handshake { JavaThread* _target; bool _thread_alive; public: VM_HandshakeOneThread(HandshakeThreadsOperation* op, JavaThread* target) : VM_Handshake(op), _target(target), _thread_alive(false) {} void doit() { + DEBUG_ONLY(_op->check_state();) TraceTime timer("Performing single-target operation (vmoperation doit)", TRACETIME_LOG(Info, handshake)); { ThreadsListHandle tlh; if (tlh.includes(_target)) {
*** 153,162 **** --- 162,172 ---- // it has been removed from the ThreadsList. So we should just keep // looping here until while below returns false. If we have a bug, // then we hang here, which is good for debugging. } } while (!poll_for_completed_thread()); + DEBUG_ONLY(_op->check_state();) } VMOp_Type type() const { return VMOp_HandshakeOneThread; } bool thread_alive() const { return _thread_alive; }
*** 165,174 **** --- 175,185 ---- class VM_HandshakeAllThreads: public VM_Handshake { public: VM_HandshakeAllThreads(HandshakeThreadsOperation* op) : VM_Handshake(op) {} void doit() { + DEBUG_ONLY(_op->check_state();) TraceTime timer("Performing operation (vmoperation doit)", TRACETIME_LOG(Info, handshake)); int number_of_threads_issued = 0; for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { set_handshake(thr);
*** 211,221 **** while (poll_for_completed_thread()) { // Includes canceled operations by exiting threads. number_of_threads_completed++; } ! } while (number_of_threads_issued != number_of_threads_completed); } VMOp_Type type() const { return VMOp_HandshakeAllThreads; } }; --- 222,234 ---- while (poll_for_completed_thread()) { // Includes canceled operations by exiting threads. number_of_threads_completed++; } ! } while (number_of_threads_issued > number_of_threads_completed); ! assert(number_of_threads_issued == number_of_threads_completed, "Must be the same"); ! DEBUG_ONLY(_op->check_state();) } VMOp_Type type() const { return VMOp_HandshakeAllThreads; } };
*** 243,254 **** VMOp_Type type() const { return VMOp_HandshakeFallback; } bool thread_alive() const { return _thread_alive; } }; - #undef ALL_JAVA_THREADS - void HandshakeThreadsOperation::do_handshake(JavaThread* thread) { ResourceMark rm; FormatBufferResource message("Operation for thread " PTR_FORMAT ", is_vm_thread: %s", p2i(thread), BOOL_TO_STR(Thread::current()->is_VM_thread())); TraceTime timer(message, TRACETIME_LOG(Debug, handshake, task)); --- 256,265 ----
*** 280,290 **** VMThread::execute(&op); return op.thread_alive(); } } ! HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _vmthread_holds_semaphore(false), _thread_in_process_handshake(false) {} void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) { _operation = op; SafepointMechanism::arm_local_poll(target); } --- 291,301 ---- VMThread::execute(&op); return op.thread_alive(); } } ! HandshakeState::HandshakeState() : _operation(NULL), _semaphore(1), _thread_in_process_handshake(false) {} void HandshakeState::set_operation(JavaThread* target, HandshakeOperation* op) { _operation = op; SafepointMechanism::arm_local_poll(target); }
*** 294,327 **** SafepointMechanism::disarm_local_poll(target); } void HandshakeState::process_self_inner(JavaThread* thread) { assert(Thread::current() == thread, "should call from thread"); CautiouslyPreserveExceptionMark pem(thread); ThreadInVMForHandshake tivm(thread); if (!_semaphore.trywait()) { _semaphore.wait_with_safepoint_check(thread); } ! if (has_operation()) { ! HandshakeOperation* op = _operation; ! clear_handshake(thread); if (op != NULL) { op->do_handshake(thread); } - } _semaphore.signal(); } void HandshakeState::cancel_inner(JavaThread* thread) { assert(Thread::current() == thread, "should call from thread"); assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); - #ifdef DEBUG - { - ThreadsListHandle tlh; - assert(!tlh.includes(_target), "java thread must not be on threads list"); - } - #endif HandshakeOperation* op = _operation; clear_handshake(thread); if (op != NULL) { op->cancel_handshake(thread); } --- 305,339 ---- SafepointMechanism::disarm_local_poll(target); } void HandshakeState::process_self_inner(JavaThread* thread) { assert(Thread::current() == thread, "should call from thread"); + + // If thread is not on threads list but armed, we cancel. + if (thread->is_terminated() && thread->has_handshake()) { + assert(false, "Should not happen"); + thread->cancel_handshake(); + return; + } + CautiouslyPreserveExceptionMark pem(thread); ThreadInVMForHandshake tivm(thread); if (!_semaphore.trywait()) { _semaphore.wait_with_safepoint_check(thread); } ! HandshakeOperation* op = OrderAccess::load_acquire(&_operation); if (op != NULL) { + // Disarm before execute the operation + clear_handshake(thread); op->do_handshake(thread); } _semaphore.signal(); } void HandshakeState::cancel_inner(JavaThread* thread) { assert(Thread::current() == thread, "should call from thread"); assert(thread->thread_state() == _thread_in_vm, "must be in vm state"); HandshakeOperation* op = _operation; clear_handshake(thread); if (op != NULL) { op->cancel_handshake(thread); }
*** 330,347 **** bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) { return SafepointSynchronize::safepoint_safe(target, target->thread_state()); } bool HandshakeState::claim_handshake_for_vmthread() { ! if (_semaphore.trywait()) { ! if (has_operation()) { ! _vmthread_holds_semaphore = true; ! } else { ! _semaphore.signal(); } } ! return _vmthread_holds_semaphore; } void HandshakeState::process_by_vmthread(JavaThread* target) { assert(Thread::current()->is_VM_thread(), "should call from vm thread"); --- 342,359 ---- bool HandshakeState::vmthread_can_process_handshake(JavaThread* target) { return SafepointSynchronize::safepoint_safe(target, target->thread_state()); } bool HandshakeState::claim_handshake_for_vmthread() { ! if (!_semaphore.trywait()) { ! return false; } + if (has_operation()) { + return true; } ! _semaphore.signal(); ! return false; } void HandshakeState::process_by_vmthread(JavaThread* target) { assert(Thread::current()->is_VM_thread(), "should call from vm thread");
*** 356,370 **** } // If we own the semaphore at this point and while owning the semaphore // can observe a safe state the thread cannot possibly continue without // getting caught by the semaphore. ! if (claim_handshake_for_vmthread() && vmthread_can_process_handshake(target)) { guarantee(!_semaphore.trywait(), "we should already own the semaphore"); _operation->do_handshake(target); clear_handshake(target); - _vmthread_holds_semaphore = false; // Release the thread _semaphore.signal(); } } --- 368,386 ---- } // If we own the semaphore at this point and while owning the semaphore // can observe a safe state the thread cannot possibly continue without // getting caught by the semaphore. ! if (claim_handshake_for_vmthread()) { ! if(!vmthread_can_process_handshake(target)) { ! _semaphore.signal(); ! return; ! } guarantee(!_semaphore.trywait(), "we should already own the semaphore"); _operation->do_handshake(target); + // Disarm after VM thread have executed the operation. clear_handshake(target); // Release the thread _semaphore.signal(); } }
< prev index next >