2729 } 2730 2731 // Handler function invoked when a thread's execution is suspended or 2732 // resumed. We have to be careful that only async-safe functions are 2733 // called here (Note: most pthread functions are not async safe and 2734 // should be avoided.) 2735 // 2736 // Note: sigwait() is a more natural fit than sigsuspend() from an 2737 // interface point of view, but sigwait() prevents the signal hander 2738 // from being run. libpthread would get very confused by not having 2739 // its signal handlers run and prevents sigwait()'s use with the 2740 // mutex granting granting signal. 2741 // 2742 // Currently only ever called on the VMThread or JavaThread 2743 // 2744 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) { 2745 // Save and restore errno to avoid confusing native code with EINTR 2746 // after sigsuspend. 2747 int old_errno = errno; 2748 2749 Thread* thread = Thread::current(); 2750 OSThread* osthread = thread->osthread(); 2751 assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread"); 2752 2753 os::SuspendResume::State current = osthread->sr.state(); 2754 if (current == os::SuspendResume::SR_SUSPEND_REQUEST) { 2755 suspend_save_context(osthread, siginfo, context); 2756 2757 // attempt to switch the state, we assume we had a SUSPEND_REQUEST 2758 os::SuspendResume::State state = osthread->sr.suspended(); 2759 if (state == os::SuspendResume::SR_SUSPENDED) { 2760 sigset_t suspend_set; // signals for sigsuspend() 2761 2762 // get current set of blocked signals and unblock resume signal 2763 pthread_sigmask(SIG_BLOCK, NULL, &suspend_set); 2764 sigdelset(&suspend_set, SR_signum); 2765 2766 sr_semaphore.signal(); 2767 // wait here until we are resumed 2768 while (1) { 2769 sigsuspend(&suspend_set); 2770 2771 os::SuspendResume::State result = osthread->sr.running(); | 2729 } 2730 2731 // Handler function invoked when a thread's execution is suspended or 2732 // resumed. We have to be careful that only async-safe functions are 2733 // called here (Note: most pthread functions are not async safe and 2734 // should be avoided.) 2735 // 2736 // Note: sigwait() is a more natural fit than sigsuspend() from an 2737 // interface point of view, but sigwait() prevents the signal hander 2738 // from being run. libpthread would get very confused by not having 2739 // its signal handlers run and prevents sigwait()'s use with the 2740 // mutex granting granting signal. 2741 // 2742 // Currently only ever called on the VMThread or JavaThread 2743 // 2744 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) { 2745 // Save and restore errno to avoid confusing native code with EINTR 2746 // after sigsuspend. 2747 int old_errno = errno; 2748 2749 Thread* thread = Thread::current_or_null_safe(); 2750 assert(thread != NULL, "Missing current thread in SR_handler"); 2751 2752 // On some systems we have seen signal delivery get "stuck" until the signal 2753 // mask is changed as part of thread termination. Check the current thread 2754 // has not already terminated (via SR_lock()) - else the following assertion 2755 // will fail because the thread is no longer a JavaThread as the ~JavaThread 2756 // destructor has completed. 2757 2758 if (thread->SR_lock() == NULL) 2759 return; 2760 2761 assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread"); 2762 2763 OSThread* osthread = thread->osthread(); 2764 2765 os::SuspendResume::State current = osthread->sr.state(); 2766 if (current == os::SuspendResume::SR_SUSPEND_REQUEST) { 2767 suspend_save_context(osthread, siginfo, context); 2768 2769 // attempt to switch the state, we assume we had a SUSPEND_REQUEST 2770 os::SuspendResume::State state = osthread->sr.suspended(); 2771 if (state == os::SuspendResume::SR_SUSPENDED) { 2772 sigset_t suspend_set; // signals for sigsuspend() 2773 2774 // get current set of blocked signals and unblock resume signal 2775 pthread_sigmask(SIG_BLOCK, NULL, &suspend_set); 2776 sigdelset(&suspend_set, SR_signum); 2777 2778 sr_semaphore.signal(); 2779 // wait here until we are resumed 2780 while (1) { 2781 sigsuspend(&suspend_set); 2782 2783 os::SuspendResume::State result = osthread->sr.running(); |