643 //////////////////////////////////////////////////////////////////////////////// |
643 //////////////////////////////////////////////////////////////////////////////// |
644 // interrupt support |
644 // interrupt support |
645 |
645 |
646 void os::interrupt(Thread* thread) { |
646 void os::interrupt(Thread* thread) { |
647 debug_only(Thread::check_for_dangling_thread_pointer(thread);) |
647 debug_only(Thread::check_for_dangling_thread_pointer(thread);) |
648 |
648 assert(thread->is_Java_thread(), "invariant"); |
|
649 JavaThread* jt = (JavaThread*) thread; |
649 OSThread* osthread = thread->osthread(); |
650 OSThread* osthread = thread->osthread(); |
650 |
651 |
651 if (!osthread->interrupted()) { |
652 if (!osthread->interrupted()) { |
652 osthread->set_interrupted(true); |
653 osthread->set_interrupted(true); |
653 // More than one thread can get here with the same value of osthread, |
654 // More than one thread can get here with the same value of osthread, |
654 // resulting in multiple notifications. We do, however, want the store |
655 // resulting in multiple notifications. We do, however, want the store |
655 // to interrupted() to be visible to other threads before we execute unpark(). |
656 // to interrupted() to be visible to other threads before we execute unpark(). |
656 OrderAccess::fence(); |
657 OrderAccess::fence(); |
657 ParkEvent * const slp = thread->_SleepEvent ; |
658 ParkEvent * const slp = jt->_SleepEvent ; |
658 if (slp != NULL) slp->unpark() ; |
659 if (slp != NULL) slp->unpark() ; |
659 } |
660 } |
660 |
661 |
661 // For JSR166. Unpark even if interrupt status already was set |
662 // For JSR166. Unpark even if interrupt status already was set |
662 if (thread->is_Java_thread()) |
663 jt->parker()->unpark(); |
663 ((JavaThread*)thread)->parker()->unpark(); |
|
664 |
664 |
665 ParkEvent * ev = thread->_ParkEvent ; |
665 ParkEvent * ev = thread->_ParkEvent ; |
666 if (ev != NULL) ev->unpark() ; |
666 if (ev != NULL) ev->unpark() ; |
667 } |
667 } |
668 |
668 |
680 // intentional. The effect of this is that Object.wait() and |
680 // intentional. The effect of this is that Object.wait() and |
681 // LockSupport.park() will appear to have a spurious wakeup, which |
681 // LockSupport.park() will appear to have a spurious wakeup, which |
682 // is allowed and not harmful, and the possibility is so rare that |
682 // is allowed and not harmful, and the possibility is so rare that |
683 // it is not worth the added complexity to add yet another lock. |
683 // it is not worth the added complexity to add yet another lock. |
684 // For the sleep event an explicit reset is performed on entry |
684 // For the sleep event an explicit reset is performed on entry |
685 // to os::sleep, so there is no early return. It has also been |
685 // to JavaThread::sleep, so there is no early return. It has also been |
686 // recommended not to put the interrupted flag into the "event" |
686 // recommended not to put the interrupted flag into the "event" |
687 // structure because it hides the issue. |
687 // structure because it hides the issue. |
688 if (interrupted && clear_interrupted) { |
688 if (interrupted && clear_interrupted) { |
689 osthread->set_interrupted(false); |
689 osthread->set_interrupted(false); |
690 // consider thread->_SleepEvent->reset() ... optional optimization |
690 // consider thread->_SleepEvent->reset() ... optional optimization |
2047 // thread to block. This has the benefit of forcing a spurious return |
2047 // thread to block. This has the benefit of forcing a spurious return |
2048 // from the first park() call after an unpark() call which will help |
2048 // from the first park() call after an unpark() call which will help |
2049 // shake out uses of park() and unpark() without checking state conditions |
2049 // shake out uses of park() and unpark() without checking state conditions |
2050 // properly. This spurious return doesn't manifest itself in any user code |
2050 // properly. This spurious return doesn't manifest itself in any user code |
2051 // but only in the correctly written condition checking loops of ObjectMonitor, |
2051 // but only in the correctly written condition checking loops of ObjectMonitor, |
2052 // Mutex/Monitor, Thread::muxAcquire and os::sleep |
2052 // Mutex/Monitor, Thread::muxAcquire and JavaThread::sleep |
2053 |
2053 |
2054 if (Atomic::xchg(1, &_event) >= 0) return; |
2054 if (Atomic::xchg(1, &_event) >= 0) return; |
2055 |
2055 |
2056 int status = pthread_mutex_lock(_mutex); |
2056 int status = pthread_mutex_lock(_mutex); |
2057 assert_status(status == 0, status, "mutex_lock"); |
2057 assert_status(status == 0, status, "mutex_lock"); |