623 } 624 return agent_entry_name; 625 } 626 627 628 void os::naked_short_nanosleep(jlong ns) { 629 struct timespec req; 630 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only"); 631 req.tv_sec = 0; 632 req.tv_nsec = ns; 633 ::nanosleep(&req, NULL); 634 return; 635 } 636 637 void os::naked_short_sleep(jlong ms) { 638 assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only"); 639 os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS)); 640 return; 641 } 642 643 //////////////////////////////////////////////////////////////////////////////// 644 // interrupt support 645 646 void os::interrupt(Thread* thread) { 647 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 648 assert(thread->is_Java_thread(), "invariant"); 649 JavaThread* jt = (JavaThread*) thread; 650 OSThread* osthread = thread->osthread(); 651 652 if (!osthread->interrupted()) { 653 osthread->set_interrupted(true); 654 // More than one thread can get here with the same value of osthread, 655 // resulting in multiple notifications. We do, however, want the store 656 // to interrupted() to be visible to other threads before we execute unpark(). 657 OrderAccess::fence(); 658 ParkEvent * const slp = jt->_SleepEvent ; 659 if (slp != NULL) slp->unpark() ; 660 } 661 662 // For JSR166. Unpark even if interrupt status already was set 663 jt->parker()->unpark(); 664 665 ParkEvent * ev = thread->_ParkEvent ; 666 if (ev != NULL) ev->unpark() ; 667 } 668 669 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 670 debug_only(Thread::check_for_dangling_thread_pointer(thread);) 671 672 OSThread* osthread = thread->osthread(); 673 674 bool interrupted = osthread->interrupted(); 675 676 // NOTE that since there is no "lock" around the interrupt and 677 // is_interrupted operations, there is the possibility that the 678 // interrupted flag (in osThread) will be "false" but that the 679 // low-level events will be in the signaled state. This is 680 // intentional. The effect of this is that Object.wait() and 681 // LockSupport.park() will appear to have a spurious wakeup, which 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. 684 // For the sleep event an explicit reset is performed on entry 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" 687 // structure because it hides the issue. 688 if (interrupted && clear_interrupted) { 689 osthread->set_interrupted(false); 690 // consider thread->_SleepEvent->reset() ... optional optimization 691 } 692 693 return interrupted; 694 } 695 696 697 698 static const struct { 699 int sig; const char* name; 700 } 701 g_signal_info[] = 702 { 703 { SIGABRT, "SIGABRT" }, 704 #ifdef SIGAIO 705 { SIGAIO, "SIGAIO" }, 706 #endif 707 { SIGALRM, "SIGALRM" }, 708 #ifdef SIGALRM1 709 { SIGALRM1, "SIGALRM1" }, 710 #endif 711 { SIGBUS, "SIGBUS" }, 712 #ifdef SIGCANCEL 713 { SIGCANCEL, "SIGCANCEL" }, 714 #endif 715 { SIGCHLD, "SIGCHLD" }, 716 #ifdef SIGCLD 717 { SIGCLD, "SIGCLD" }, 2090 // Parker::park decrements count if > 0, else does a condvar wait. Unpark 2091 // sets count to 1 and signals condvar. Only one thread ever waits 2092 // on the condvar. Contention seen when trying to park implies that someone 2093 // is unparking you, so don't wait. And spurious returns are fine, so there 2094 // is no need to track notifications. 2095 2096 void Parker::park(bool isAbsolute, jlong time) { 2097 2098 // Optional fast-path check: 2099 // Return immediately if a permit is available. 2100 // We depend on Atomic::xchg() having full barrier semantics 2101 // since we are doing a lock-free update to _counter. 2102 if (Atomic::xchg(0, &_counter) > 0) return; 2103 2104 Thread* thread = Thread::current(); 2105 assert(thread->is_Java_thread(), "Must be JavaThread"); 2106 JavaThread *jt = (JavaThread *)thread; 2107 2108 // Optional optimization -- avoid state transitions if there's 2109 // an interrupt pending. 2110 if (Thread::is_interrupted(thread, false)) { 2111 return; 2112 } 2113 2114 // Next, demultiplex/decode time arguments 2115 struct timespec absTime; 2116 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 2117 return; 2118 } 2119 if (time > 0) { 2120 to_abstime(&absTime, time, isAbsolute, false); 2121 } 2122 2123 // Enter safepoint region 2124 // Beware of deadlocks such as 6317397. 2125 // The per-thread Parker:: mutex is a classic leaf-lock. 2126 // In particular a thread must never block on the Threads_lock while 2127 // holding the Parker:: mutex. If safepoints are pending both the 2128 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 2129 ThreadBlockInVM tbivm(jt); 2130 2131 // Don't wait if cannot get lock since interference arises from 2132 // unparking. Also re-check interrupt before trying wait. 2133 if (Thread::is_interrupted(thread, false) || 2134 pthread_mutex_trylock(_mutex) != 0) { 2135 return; 2136 } 2137 2138 int status; 2139 if (_counter > 0) { // no wait needed 2140 _counter = 0; 2141 status = pthread_mutex_unlock(_mutex); 2142 assert_status(status == 0, status, "invariant"); 2143 // Paranoia to ensure our locked and lock-free paths interact 2144 // correctly with each other and Java-level accesses. 2145 OrderAccess::fence(); 2146 return; 2147 } 2148 2149 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 2150 jt->set_suspend_equivalent(); 2151 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 2152 2153 assert(_cur_index == -1, "invariant"); | 623 } 624 return agent_entry_name; 625 } 626 627 628 void os::naked_short_nanosleep(jlong ns) { 629 struct timespec req; 630 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only"); 631 req.tv_sec = 0; 632 req.tv_nsec = ns; 633 ::nanosleep(&req, NULL); 634 return; 635 } 636 637 void os::naked_short_sleep(jlong ms) { 638 assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only"); 639 os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS)); 640 return; 641 } 642 643 static const struct { 644 int sig; const char* name; 645 } 646 g_signal_info[] = 647 { 648 { SIGABRT, "SIGABRT" }, 649 #ifdef SIGAIO 650 { SIGAIO, "SIGAIO" }, 651 #endif 652 { SIGALRM, "SIGALRM" }, 653 #ifdef SIGALRM1 654 { SIGALRM1, "SIGALRM1" }, 655 #endif 656 { SIGBUS, "SIGBUS" }, 657 #ifdef SIGCANCEL 658 { SIGCANCEL, "SIGCANCEL" }, 659 #endif 660 { SIGCHLD, "SIGCHLD" }, 661 #ifdef SIGCLD 662 { SIGCLD, "SIGCLD" }, 2035 // Parker::park decrements count if > 0, else does a condvar wait. Unpark 2036 // sets count to 1 and signals condvar. Only one thread ever waits 2037 // on the condvar. Contention seen when trying to park implies that someone 2038 // is unparking you, so don't wait. And spurious returns are fine, so there 2039 // is no need to track notifications. 2040 2041 void Parker::park(bool isAbsolute, jlong time) { 2042 2043 // Optional fast-path check: 2044 // Return immediately if a permit is available. 2045 // We depend on Atomic::xchg() having full barrier semantics 2046 // since we are doing a lock-free update to _counter. 2047 if (Atomic::xchg(0, &_counter) > 0) return; 2048 2049 Thread* thread = Thread::current(); 2050 assert(thread->is_Java_thread(), "Must be JavaThread"); 2051 JavaThread *jt = (JavaThread *)thread; 2052 2053 // Optional optimization -- avoid state transitions if there's 2054 // an interrupt pending. 2055 if (jt->is_interrupted(false)) { 2056 return; 2057 } 2058 2059 // Next, demultiplex/decode time arguments 2060 struct timespec absTime; 2061 if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all 2062 return; 2063 } 2064 if (time > 0) { 2065 to_abstime(&absTime, time, isAbsolute, false); 2066 } 2067 2068 // Enter safepoint region 2069 // Beware of deadlocks such as 6317397. 2070 // The per-thread Parker:: mutex is a classic leaf-lock. 2071 // In particular a thread must never block on the Threads_lock while 2072 // holding the Parker:: mutex. If safepoints are pending both the 2073 // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 2074 ThreadBlockInVM tbivm(jt); 2075 2076 // Don't wait if cannot get lock since interference arises from 2077 // unparking. Also re-check interrupt before trying wait. 2078 if (jt->is_interrupted(false) || 2079 pthread_mutex_trylock(_mutex) != 0) { 2080 return; 2081 } 2082 2083 int status; 2084 if (_counter > 0) { // no wait needed 2085 _counter = 0; 2086 status = pthread_mutex_unlock(_mutex); 2087 assert_status(status == 0, status, "invariant"); 2088 // Paranoia to ensure our locked and lock-free paths interact 2089 // correctly with each other and Java-level accesses. 2090 OrderAccess::fence(); 2091 return; 2092 } 2093 2094 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 2095 jt->set_suspend_equivalent(); 2096 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 2097 2098 assert(_cur_index == -1, "invariant"); |