1723 // interrupt support |
1723 // interrupt support |
1724 |
1724 |
1725 void JavaThread::interrupt() { |
1725 void JavaThread::interrupt() { |
1726 debug_only(check_for_dangling_thread_pointer(this);) |
1726 debug_only(check_for_dangling_thread_pointer(this);) |
1727 |
1727 |
1728 if (!osthread()->interrupted()) { |
1728 // For Windows _interrupt_event |
1729 osthread()->set_interrupted(true); |
1729 osthread()->set_interrupted(true); |
1730 // More than one thread can get here with the same value of osthread, |
1730 |
1731 // resulting in multiple notifications. We do, however, want the store |
1731 // For Thread.sleep |
1732 // to interrupted() to be visible to other threads before we execute unpark(). |
1732 _SleepEvent->unpark(); |
1733 OrderAccess::fence(); |
1733 |
1734 |
1734 // For JSR166 LockSupport.park |
1735 // For JavaThread::sleep. Historically we only unpark if changing to the interrupted |
|
1736 // state, in contrast to the other events below. Not clear exactly why. |
|
1737 _SleepEvent->unpark(); |
|
1738 } |
|
1739 |
|
1740 // For JSR166. Unpark even if interrupt status already was set. |
|
1741 parker()->unpark(); |
1735 parker()->unpark(); |
1742 |
1736 |
1743 // For ObjectMonitor and JvmtiRawMonitor |
1737 // For ObjectMonitor and JvmtiRawMonitor |
1744 _ParkEvent->unpark(); |
1738 _ParkEvent->unpark(); |
1745 } |
1739 } |
1746 |
1740 |
1747 |
1741 |
1748 bool JavaThread::is_interrupted(bool clear_interrupted) { |
1742 bool JavaThread::is_interrupted(bool clear_interrupted) { |
1749 debug_only(check_for_dangling_thread_pointer(this);) |
1743 debug_only(check_for_dangling_thread_pointer(this);) |
1750 bool interrupted = osthread()->interrupted(); |
1744 bool interrupted = java_lang_Thread::interrupted(threadObj()); |
1751 |
1745 |
1752 // NOTE that since there is no "lock" around the interrupt and |
1746 // NOTE that since there is no "lock" around the interrupt and |
1753 // is_interrupted operations, there is the possibility that the |
1747 // is_interrupted operations, there is the possibility that the |
1754 // interrupted flag (in osThread) will be "false" but that the |
1748 // interrupted flag will be "false" but that the |
1755 // low-level events will be in the signaled state. This is |
1749 // low-level events will be in the signaled state. This is |
1756 // intentional. The effect of this is that Object.wait() and |
1750 // intentional. The effect of this is that Object.wait() and |
1757 // LockSupport.park() will appear to have a spurious wakeup, which |
1751 // LockSupport.park() will appear to have a spurious wakeup, which |
1758 // is allowed and not harmful, and the possibility is so rare that |
1752 // is allowed and not harmful, and the possibility is so rare that |
1759 // it is not worth the added complexity to add yet another lock. |
1753 // it is not worth the added complexity to add yet another lock. |
1760 // For the sleep event an explicit reset is performed on entry |
1754 // For the sleep event an explicit reset is performed on entry |
1761 // to JavaThread::sleep, so there is no early return. It has also been |
1755 // to JavaThread::sleep, so there is no early return. It has also been |
1762 // recommended not to put the interrupted flag into the "event" |
1756 // recommended not to put the interrupted flag into the "event" |
1763 // structure because it hides the issue. |
1757 // structure because it hides the issue. |
|
1758 // Also, because there is no lock, we must only clear the interrupt |
|
1759 // state if we are going to report that we were interrupted; otherwise |
|
1760 // an interrupt that happens just after we read the field would be lost. |
1764 if (interrupted && clear_interrupted) { |
1761 if (interrupted && clear_interrupted) { |
|
1762 java_lang_Thread::set_interrupted(threadObj(), false); |
1765 osthread()->set_interrupted(false); |
1763 osthread()->set_interrupted(false); |
1766 // consider thread->_SleepEvent->reset() ... optional optimization |
|
1767 } |
1764 } |
1768 |
1765 |
1769 return interrupted; |
1766 return interrupted; |
1770 } |
1767 } |
1771 |
1768 |