src/os/bsd/vm/os_bsd.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


1837 
1838 void os::signal_raise(int signal_number) {
1839   ::raise(signal_number);
1840 }
1841 
1842 /*
1843  * The following code is moved from os.cpp for making this
1844  * code platform specific, which it is by its very nature.
1845  */
1846 
1847 // Will be modified when max signal is changed to be dynamic
1848 int os::sigexitnum_pd() {
1849   return NSIG;
1850 }
1851 
1852 // a counter for each possible signal value
1853 static volatile jint pending_signals[NSIG+1] = { 0 };
1854 
1855 // Bsd(POSIX) specific hand shaking semaphore.
1856 #ifdef __APPLE__
1857 static semaphore_t sig_sem;
1858 #define SEM_INIT(sem, value)    semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, value)
1859 #define SEM_WAIT(sem)           semaphore_wait(sem);
1860 #define SEM_POST(sem)           semaphore_signal(sem);

1861 #else
1862 static sem_t sig_sem;
1863 #define SEM_INIT(sem, value)    sem_init(&sem, 0, value)
1864 #define SEM_WAIT(sem)           sem_wait(&sem);
1865 #define SEM_POST(sem)           sem_post(&sem);
1866 #endif




































































































1867 
1868 void os::signal_init_pd() {
1869   // Initialize signal structures
1870   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
1871 
1872   // Initialize signal semaphore
1873   ::SEM_INIT(sig_sem, 0);
1874 }
1875 
1876 void os::signal_notify(int sig) {
1877   Atomic::inc(&pending_signals[sig]);
1878   ::SEM_POST(sig_sem);
1879 }
1880 
1881 static int check_pending_signals(bool wait) {
1882   Atomic::store(0, &sigint_count);
1883   for (;;) {
1884     for (int i = 0; i < NSIG + 1; i++) {
1885       jint n = pending_signals[i];
1886       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {


2642 //      that runs in the watcher thread.
2643 //  The remaining code is greatly simplified from the more general suspension
2644 //  code that used to be used.
2645 //
2646 //  The protocol is quite simple:
2647 //  - suspend:
2648 //      - sends a signal to the target thread
2649 //      - polls the suspend state of the osthread using a yield loop
2650 //      - target thread signal handler (SR_handler) sets suspend state
2651 //        and blocks in sigsuspend until continued
2652 //  - resume:
2653 //      - sets target osthread state to continue
2654 //      - sends signal to end the sigsuspend loop in the SR_handler
2655 //
2656 //  Note that the SR_lock plays no role in this suspend/resume protocol.
2657 //
2658 
2659 static void resume_clear_context(OSThread *osthread) {
2660   osthread->set_ucontext(NULL);
2661   osthread->set_siginfo(NULL);
2662 
2663   // notify the suspend action is completed, we have now resumed
2664   osthread->sr.clear_suspended();
2665 }
2666 
2667 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
2668   osthread->set_ucontext(context);
2669   osthread->set_siginfo(siginfo);
2670 }
2671 
2672 //
2673 // Handler function invoked when a thread's execution is suspended or
2674 // resumed. We have to be careful that only async-safe functions are
2675 // called here (Note: most pthread functions are not async safe and
2676 // should be avoided.)
2677 //
2678 // Note: sigwait() is a more natural fit than sigsuspend() from an
2679 // interface point of view, but sigwait() prevents the signal hander
2680 // from being run. libpthread would get very confused by not having
2681 // its signal handlers run and prevents sigwait()'s use with the
2682 // mutex granting granting signal.
2683 //
2684 // Currently only ever called on the VMThread
2685 //
2686 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
2687   // Save and restore errno to avoid confusing native code with EINTR
2688   // after sigsuspend.
2689   int old_errno = errno;
2690 
2691   Thread* thread = Thread::current();
2692   OSThread* osthread = thread->osthread();
2693   assert(thread->is_VM_thread(), "Must be VMThread");
2694   // read current suspend action
2695   int action = osthread->sr.suspend_action();
2696   if (action == os::Bsd::SuspendResume::SR_SUSPEND) {
2697     suspend_save_context(osthread, siginfo, context);
2698 
2699     // Notify the suspend action is about to be completed. do_suspend()
2700     // waits until SR_SUSPENDED is set and then returns. We will wait
2701     // here for a resume signal and that completes the suspend-other
2702     // action. do_suspend/do_resume is always called as a pair from
2703     // the same thread - so there are no races
2704 
2705     // notify the caller
2706     osthread->sr.set_suspended();

2707 



2708     sigset_t suspend_set;  // signals for sigsuspend()
2709 
2710     // get current set of blocked signals and unblock resume signal
2711     pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
2712     sigdelset(&suspend_set, SR_signum);
2713 

2714     // wait here until we are resumed
2715     do {
2716       sigsuspend(&suspend_set);
2717       // ignore all returns until we get a resume signal
2718     } while (osthread->sr.suspend_action() != os::Bsd::SuspendResume::SR_CONTINUE);
2719 
2720     resume_clear_context(osthread);







2721 


2722   } else {
2723     assert(action == os::Bsd::SuspendResume::SR_CONTINUE, "unexpected sr action");
2724     // nothing special to do - just leave the handler








2725   }
2726 
2727   errno = old_errno;
2728 }
2729 
2730 
2731 static int SR_initialize() {
2732   struct sigaction act;
2733   char *s;
2734   /* Get signal number to use for suspend/resume */
2735   if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
2736     int sig = ::strtol(s, 0, 10);
2737     if (sig > 0 || sig < NSIG) {
2738         SR_signum = sig;
2739     }
2740   }
2741 
2742   assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
2743         "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
2744 


2748   /* Set up signal handler for suspend/resume */
2749   act.sa_flags = SA_RESTART|SA_SIGINFO;
2750   act.sa_handler = (void (*)(int)) SR_handler;
2751 
2752   // SR_signum is blocked by default.
2753   // 4528190 - We also need to block pthread restart signal (32 on all
2754   // supported Bsd platforms). Note that BsdThreads need to block
2755   // this signal for all threads to work properly. So we don't have
2756   // to use hard-coded signal number when setting up the mask.
2757   pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
2758 
2759   if (sigaction(SR_signum, &act, 0) == -1) {
2760     return -1;
2761   }
2762 
2763   // Save signal flag
2764   os::Bsd::set_our_sigflags(SR_signum, act.sa_flags);
2765   return 0;
2766 }
2767 











2768 
2769 // returns true on success and false on error - really an error is fatal
2770 // but this seems the normal response to library errors
2771 static bool do_suspend(OSThread* osthread) {
2772   // mark as suspended and send signal
2773   osthread->sr.set_suspend_action(os::Bsd::SuspendResume::SR_SUSPEND);
2774   int status = pthread_kill(osthread->pthread_id(), SR_signum);
2775   assert_status(status == 0, status, "pthread_kill");
2776 
2777   // check status and wait until notified of suspension
2778   if (status == 0) {
2779     for (int i = 0; !osthread->sr.is_suspended(); i++) {
2780       os::yield_all(i);

2781     }
2782     osthread->sr.set_suspend_action(os::Bsd::SuspendResume::SR_NONE);
2783     return true;

2784   }
2785   else {
2786     osthread->sr.set_suspend_action(os::Bsd::SuspendResume::SR_NONE);







2787     return false;









2788   }



2789 }
2790 
2791 static void do_resume(OSThread* osthread) {
2792   assert(osthread->sr.is_suspended(), "thread should be suspended");
2793   osthread->sr.set_suspend_action(os::Bsd::SuspendResume::SR_CONTINUE);
2794 
2795   int status = pthread_kill(osthread->pthread_id(), SR_signum);
2796   assert_status(status == 0, status, "pthread_kill");
2797   // check status and wait unit notified of resumption
2798   if (status == 0) {
2799     for (int i = 0; osthread->sr.is_suspended(); i++) {
2800       os::yield_all(i);









2801     }
2802   }
2803   osthread->sr.set_suspend_action(os::Bsd::SuspendResume::SR_NONE);

2804 }
2805 
2806 ////////////////////////////////////////////////////////////////////////////////
2807 // interrupt support
2808 
2809 void os::interrupt(Thread* thread) {
2810   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
2811     "possibility of dangling Thread pointer");
2812 
2813   OSThread* osthread = thread->osthread();
2814 
2815   if (!osthread->interrupted()) {
2816     osthread->set_interrupted(true);
2817     // More than one thread can get here with the same value of osthread,
2818     // resulting in multiple notifications.  We do, however, want the store
2819     // to interrupted() to be visible to other threads before we execute unpark().
2820     OrderAccess::fence();
2821     ParkEvent * const slp = thread->_SleepEvent ;
2822     if (slp != NULL) slp->unpark() ;
2823   }


3521   // This is only supported in Snow Leopard and beyond
3522   if (name != NULL) {
3523     // Add a "Java: " prefix to the name
3524     char buf[MAXTHREADNAMESIZE];
3525     snprintf(buf, sizeof(buf), "Java: %s", name);
3526     pthread_setname_np(buf);
3527   }
3528 #endif
3529 }
3530 
3531 bool os::distribute_processes(uint length, uint* distribution) {
3532   // Not yet implemented.
3533   return false;
3534 }
3535 
3536 bool os::bind_to_processor(uint processor_id) {
3537   // Not yet implemented.
3538   return false;
3539 }
3540 
3541 ///






3542 
3543 // Suspends the target using the signal mechanism and then grabs the PC before
3544 // resuming the target. Used by the flat-profiler only
3545 ExtendedPC os::get_thread_pc(Thread* thread) {
3546   // Make sure that it is called by the watcher for the VMThread
3547   assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
3548   assert(thread->is_VM_thread(), "Can only be called for VMThread");




3549 
3550   ExtendedPC epc;



3551 


3552   OSThread* osthread = thread->osthread();
3553   if (do_suspend(osthread)) {
3554     if (osthread->ucontext() != NULL) {
3555       epc = os::Bsd::ucontext_get_pc(osthread->ucontext());
3556     } else {
3557       // NULL context is unexpected, double-check this is the VMThread
3558       guarantee(thread->is_VM_thread(), "can only be called for VMThread");
3559     }
3560     do_resume(osthread);
3561   }
3562   // failure means pthread_kill failed for some reason - arguably this is
3563   // a fatal problem, but such problems are ignored elsewhere




3564 
3565   return epc;


3566 }
3567 
3568 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
3569 {
3570   return pthread_cond_timedwait(_cond, _mutex, _abstime);
3571 }
3572 
3573 ////////////////////////////////////////////////////////////////////////////////
3574 // debug support
3575 
3576 bool os::find(address addr, outputStream* st) {
3577   Dl_info dlinfo;
3578   memset(&dlinfo, 0, sizeof(dlinfo));
3579   if (dladdr(addr, &dlinfo)) {
3580     st->print(PTR_FORMAT ": ", addr);
3581     if (dlinfo.dli_sname != NULL) {
3582       st->print("%s+%#x", dlinfo.dli_sname,
3583                  addr - (intptr_t)dlinfo.dli_saddr);
3584     } else if (dlinfo.dli_fname) {
3585       st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);


4530     if (::stat(libmawtpath, &statbuf) == 0) return false;
4531 
4532     // check libawt_xawt.so
4533     strcpy(libmawtpath, buf);
4534     strcat(libmawtpath, new_xawtstr);
4535     if (::stat(libmawtpath, &statbuf) == 0) return false;
4536 
4537     return true;
4538 }
4539 
4540 // Get the default path to the core file
4541 // Returns the length of the string
4542 int os::get_core_path(char* buffer, size_t bufferSize) {
4543   int n = jio_snprintf(buffer, bufferSize, "/cores");
4544 
4545   // Truncate if theoretical string was longer than bufferSize
4546   n = MIN2(n, (int)bufferSize);
4547 
4548   return n;
4549 }



1837 
1838 void os::signal_raise(int signal_number) {
1839   ::raise(signal_number);
1840 }
1841 
1842 /*
1843  * The following code is moved from os.cpp for making this
1844  * code platform specific, which it is by its very nature.
1845  */
1846 
1847 // Will be modified when max signal is changed to be dynamic
1848 int os::sigexitnum_pd() {
1849   return NSIG;
1850 }
1851 
1852 // a counter for each possible signal value
1853 static volatile jint pending_signals[NSIG+1] = { 0 };
1854 
1855 // Bsd(POSIX) specific hand shaking semaphore.
1856 #ifdef __APPLE__
1857 typedef semaphore_t os_semaphore_t;
1858 #define SEM_INIT(sem, value)    semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, value)
1859 #define SEM_WAIT(sem)           semaphore_wait(sem)
1860 #define SEM_POST(sem)           semaphore_signal(sem)
1861 #define SEM_DESTROY(sem)        semaphore_destroy(mach_task_self(), sem)
1862 #else
1863 typedef sem_t os_semaphore_t;
1864 #define SEM_INIT(sem, value)    sem_init(&sem, 0, value)
1865 #define SEM_WAIT(sem)           sem_wait(&sem)
1866 #define SEM_POST(sem)           sem_post(&sem)
1867 #define SEM_DESTROY(sem)        sem_destroy(&sem)
1868 #endif
1869 
1870 class Semaphore : public StackObj {
1871   public:
1872     Semaphore();
1873     ~Semaphore();
1874     void signal();
1875     void wait();
1876     bool trywait();
1877     bool timedwait(unsigned int sec, int nsec);
1878   private:
1879     jlong currenttime() const;
1880     semaphore_t _semaphore;
1881 };
1882 
1883 Semaphore::Semaphore() : _semaphore(0) {
1884   SEM_INIT(_semaphore, 0);
1885 }
1886 
1887 Semaphore::~Semaphore() {
1888   SEM_DESTROY(_semaphore);
1889 }
1890 
1891 void Semaphore::signal() {
1892   SEM_POST(_semaphore);
1893 }
1894 
1895 void Semaphore::wait() {
1896   SEM_WAIT(_semaphore);
1897 }
1898 
1899 jlong Semaphore::currenttime() const {
1900     struct timeval tv;
1901     gettimeofday(&tv, NULL);
1902     return (tv.tv_sec * NANOSECS_PER_SEC) + (tv.tv_usec * 1000);
1903 }
1904 
1905 #ifdef __APPLE__
1906 bool Semaphore::trywait() {
1907   return timedwait(0, 0);
1908 }
1909 
1910 bool Semaphore::timedwait(unsigned int sec, int nsec) {
1911   kern_return_t kr = KERN_ABORTED;
1912   mach_timespec_t waitspec;
1913   waitspec.tv_sec = sec;
1914   waitspec.tv_nsec = nsec;
1915 
1916   jlong starttime = currenttime();
1917 
1918   kr = semaphore_timedwait(_semaphore, waitspec);
1919   while (kr == KERN_ABORTED) {
1920     jlong totalwait = (sec * NANOSECS_PER_SEC) + nsec;
1921 
1922     jlong current = currenttime();
1923     jlong passedtime = current - starttime;
1924 
1925     if (passedtime >= totalwait) {
1926       waitspec.tv_sec = 0;
1927       waitspec.tv_nsec = 0;
1928     } else {
1929       jlong waittime = totalwait - (current - starttime);
1930       waitspec.tv_sec = waittime / NANOSECS_PER_SEC;
1931       waitspec.tv_nsec = waittime % NANOSECS_PER_SEC;
1932     }
1933 
1934     kr = semaphore_timedwait(_semaphore, waitspec);
1935   }
1936 
1937   return kr == KERN_SUCCESS;
1938 }
1939 
1940 #else
1941 
1942 bool Semaphore::trywait() {
1943   return sem_trywait(&_semaphore) == 0;
1944 }
1945 
1946 bool Semaphore::timedwait(unsigned int sec, int nsec) {
1947   struct timespec ts;
1948   jlong endtime = unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
1949 
1950   while (1) {
1951     int result = sem_timedwait(&_semaphore, &ts);
1952     if (result == 0) {
1953       return true;
1954     } else if (errno == EINTR) {
1955       continue;
1956     } else if (errno == ETIMEDOUT) {
1957       return false;
1958     } else {
1959       return false;
1960     }
1961   }
1962 }
1963 
1964 #endif // __APPLE__
1965 
1966 static os_semaphore_t sig_sem;
1967 static Semaphore sr_semaphore;
1968 
1969 void os::signal_init_pd() {
1970   // Initialize signal structures
1971   ::memset((void*)pending_signals, 0, sizeof(pending_signals));
1972 
1973   // Initialize signal semaphore
1974   ::SEM_INIT(sig_sem, 0);
1975 }
1976 
1977 void os::signal_notify(int sig) {
1978   Atomic::inc(&pending_signals[sig]);
1979   ::SEM_POST(sig_sem);
1980 }
1981 
1982 static int check_pending_signals(bool wait) {
1983   Atomic::store(0, &sigint_count);
1984   for (;;) {
1985     for (int i = 0; i < NSIG + 1; i++) {
1986       jint n = pending_signals[i];
1987       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {


2743 //      that runs in the watcher thread.
2744 //  The remaining code is greatly simplified from the more general suspension
2745 //  code that used to be used.
2746 //
2747 //  The protocol is quite simple:
2748 //  - suspend:
2749 //      - sends a signal to the target thread
2750 //      - polls the suspend state of the osthread using a yield loop
2751 //      - target thread signal handler (SR_handler) sets suspend state
2752 //        and blocks in sigsuspend until continued
2753 //  - resume:
2754 //      - sets target osthread state to continue
2755 //      - sends signal to end the sigsuspend loop in the SR_handler
2756 //
2757 //  Note that the SR_lock plays no role in this suspend/resume protocol.
2758 //
2759 
2760 static void resume_clear_context(OSThread *osthread) {
2761   osthread->set_ucontext(NULL);
2762   osthread->set_siginfo(NULL);



2763 }
2764 
2765 static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
2766   osthread->set_ucontext(context);
2767   osthread->set_siginfo(siginfo);
2768 }
2769 
2770 //
2771 // Handler function invoked when a thread's execution is suspended or
2772 // resumed. We have to be careful that only async-safe functions are
2773 // called here (Note: most pthread functions are not async safe and
2774 // should be avoided.)
2775 //
2776 // Note: sigwait() is a more natural fit than sigsuspend() from an
2777 // interface point of view, but sigwait() prevents the signal hander
2778 // from being run. libpthread would get very confused by not having
2779 // its signal handlers run and prevents sigwait()'s use with the
2780 // mutex granting granting signal.
2781 //
2782 // Currently only ever called on the VMThread or JavaThread
2783 //
2784 static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
2785   // Save and restore errno to avoid confusing native code with EINTR
2786   // after sigsuspend.
2787   int old_errno = errno;
2788 
2789   Thread* thread = Thread::current();
2790   OSThread* osthread = thread->osthread();
2791   assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");










2792 
2793   os::SuspendResume::State current = osthread->sr.state();
2794   if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {
2795     suspend_save_context(osthread, siginfo, context);
2796 
2797     // attempt to switch the state, we assume we had a SUSPEND_REQUEST
2798     os::SuspendResume::State state = osthread->sr.suspended();
2799     if (state == os::SuspendResume::SR_SUSPENDED) {
2800       sigset_t suspend_set;  // signals for sigsuspend()
2801 
2802       // get current set of blocked signals and unblock resume signal
2803       pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
2804       sigdelset(&suspend_set, SR_signum);
2805 
2806       sr_semaphore.signal();
2807       // wait here until we are resumed
2808       while (1) {
2809         sigsuspend(&suspend_set);


2810 
2811         os::SuspendResume::State result = osthread->sr.running();
2812         if (result == os::SuspendResume::SR_RUNNING) {
2813           sr_semaphore.signal();
2814           break;
2815         } else if (result != os::SuspendResume::SR_SUSPENDED) {
2816           ShouldNotReachHere();
2817         }
2818       }
2819 
2820     } else if (state == os::SuspendResume::SR_RUNNING) {
2821       // request was cancelled, continue
2822     } else {
2823       ShouldNotReachHere();
2824     }
2825 
2826     resume_clear_context(osthread);
2827   } else if (current == os::SuspendResume::SR_RUNNING) {
2828     // request was cancelled, continue
2829   } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {
2830     // ignore
2831   } else {
2832     // ignore
2833   }
2834 
2835   errno = old_errno;
2836 }
2837 
2838 
2839 static int SR_initialize() {
2840   struct sigaction act;
2841   char *s;
2842   /* Get signal number to use for suspend/resume */
2843   if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
2844     int sig = ::strtol(s, 0, 10);
2845     if (sig > 0 || sig < NSIG) {
2846         SR_signum = sig;
2847     }
2848   }
2849 
2850   assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
2851         "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
2852 


2856   /* Set up signal handler for suspend/resume */
2857   act.sa_flags = SA_RESTART|SA_SIGINFO;
2858   act.sa_handler = (void (*)(int)) SR_handler;
2859 
2860   // SR_signum is blocked by default.
2861   // 4528190 - We also need to block pthread restart signal (32 on all
2862   // supported Bsd platforms). Note that BsdThreads need to block
2863   // this signal for all threads to work properly. So we don't have
2864   // to use hard-coded signal number when setting up the mask.
2865   pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
2866 
2867   if (sigaction(SR_signum, &act, 0) == -1) {
2868     return -1;
2869   }
2870 
2871   // Save signal flag
2872   os::Bsd::set_our_sigflags(SR_signum, act.sa_flags);
2873   return 0;
2874 }
2875 
2876 static int sr_notify(OSThread* osthread) {
2877   int status = pthread_kill(osthread->pthread_id(), SR_signum);
2878   assert_status(status == 0, status, "pthread_kill");
2879   return status;
2880 }
2881 
2882 // "Randomly" selected value for how long we want to spin
2883 // before bailing out on suspending a thread, also how often
2884 // we send a signal to a thread we want to resume
2885 static const int RANDOMLY_LARGE_INTEGER = 1000000;
2886 static const int RANDOMLY_LARGE_INTEGER2 = 100;
2887 
2888 // returns true on success and false on error - really an error is fatal
2889 // but this seems the normal response to library errors
2890 static bool do_suspend(OSThread* osthread) {
2891   assert(osthread->sr.is_running(), "thread should be running");
2892   assert(!sr_semaphore.trywait(), "semaphore has invalid state");


2893 
2894   // mark as suspended and send signal
2895   if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {
2896     // failed to switch, state wasn't running?
2897     ShouldNotReachHere();
2898     return false;
2899   }
2900 
2901   if (sr_notify(osthread) != 0) {
2902     ShouldNotReachHere();
2903   }
2904 
2905   // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
2906   while (true) {
2907     if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
2908       break;
2909     } else {
2910       // timeout
2911       os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
2912       if (cancelled == os::SuspendResume::SR_RUNNING) {
2913         return false;
2914       } else if (cancelled == os::SuspendResume::SR_SUSPENDED) {
2915         // make sure that we consume the signal on the semaphore as well
2916         sr_semaphore.wait();
2917         break;
2918       } else {
2919         ShouldNotReachHere();
2920         return false;
2921       }
2922     }
2923   }
2924 
2925   guarantee(osthread->sr.is_suspended(), "Must be suspended");
2926   return true;
2927 }
2928 
2929 static void do_resume(OSThread* osthread) {
2930   assert(osthread->sr.is_suspended(), "thread should be suspended");
2931   assert(!sr_semaphore.trywait(), "invalid semaphore state");
2932 
2933   if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {
2934     // failed to switch to WAKEUP_REQUEST
2935     ShouldNotReachHere();
2936     return;
2937   }
2938 
2939   while (true) {
2940     if (sr_notify(osthread) == 0) {
2941       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
2942         if (osthread->sr.is_running()) {
2943           return;
2944         }
2945       }
2946     } else {
2947       ShouldNotReachHere();
2948     }
2949   }
2950 
2951   guarantee(osthread->sr.is_running(), "Must be running!");
2952 }
2953 
2954 ////////////////////////////////////////////////////////////////////////////////
2955 // interrupt support
2956 
2957 void os::interrupt(Thread* thread) {
2958   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
2959     "possibility of dangling Thread pointer");
2960 
2961   OSThread* osthread = thread->osthread();
2962 
2963   if (!osthread->interrupted()) {
2964     osthread->set_interrupted(true);
2965     // More than one thread can get here with the same value of osthread,
2966     // resulting in multiple notifications.  We do, however, want the store
2967     // to interrupted() to be visible to other threads before we execute unpark().
2968     OrderAccess::fence();
2969     ParkEvent * const slp = thread->_SleepEvent ;
2970     if (slp != NULL) slp->unpark() ;
2971   }


3669   // This is only supported in Snow Leopard and beyond
3670   if (name != NULL) {
3671     // Add a "Java: " prefix to the name
3672     char buf[MAXTHREADNAMESIZE];
3673     snprintf(buf, sizeof(buf), "Java: %s", name);
3674     pthread_setname_np(buf);
3675   }
3676 #endif
3677 }
3678 
3679 bool os::distribute_processes(uint length, uint* distribution) {
3680   // Not yet implemented.
3681   return false;
3682 }
3683 
3684 bool os::bind_to_processor(uint processor_id) {
3685   // Not yet implemented.
3686   return false;
3687 }
3688 
3689 void os::SuspendedThreadTask::internal_do_task() {
3690   if (do_suspend(_thread->osthread())) {
3691     SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());
3692     do_task(context);
3693     do_resume(_thread->osthread());
3694   }
3695 }
3696 
3697 ///
3698 class PcFetcher : public os::SuspendedThreadTask {
3699 public:
3700   PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}
3701   ExtendedPC result();
3702 protected:
3703   void do_task(const os::SuspendedThreadTaskContext& context);
3704 private:
3705   ExtendedPC _epc;
3706 };
3707 
3708 ExtendedPC PcFetcher::result() {
3709   guarantee(is_done(), "task is not done yet.");
3710   return _epc;
3711 }
3712 
3713 void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {
3714   Thread* thread = context.thread();
3715   OSThread* osthread = thread->osthread();

3716   if (osthread->ucontext() != NULL) {
3717     _epc = os::Bsd::ucontext_get_pc((ucontext_t *) context.ucontext());
3718   } else {
3719     // NULL context is unexpected, double-check this is the VMThread
3720     guarantee(thread->is_VM_thread(), "can only be called for VMThread");
3721   }
3722 }
3723 
3724 // Suspends the target using the signal mechanism and then grabs the PC before
3725 // resuming the target. Used by the flat-profiler only
3726 ExtendedPC os::get_thread_pc(Thread* thread) {
3727   // Make sure that it is called by the watcher for the VMThread
3728   assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
3729   assert(thread->is_VM_thread(), "Can only be called for VMThread");
3730 
3731   PcFetcher fetcher(thread);
3732   fetcher.run();
3733   return fetcher.result();
3734 }
3735 
3736 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
3737 {
3738   return pthread_cond_timedwait(_cond, _mutex, _abstime);
3739 }
3740 
3741 ////////////////////////////////////////////////////////////////////////////////
3742 // debug support
3743 
3744 bool os::find(address addr, outputStream* st) {
3745   Dl_info dlinfo;
3746   memset(&dlinfo, 0, sizeof(dlinfo));
3747   if (dladdr(addr, &dlinfo)) {
3748     st->print(PTR_FORMAT ": ", addr);
3749     if (dlinfo.dli_sname != NULL) {
3750       st->print("%s+%#x", dlinfo.dli_sname,
3751                  addr - (intptr_t)dlinfo.dli_saddr);
3752     } else if (dlinfo.dli_fname) {
3753       st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);


4698     if (::stat(libmawtpath, &statbuf) == 0) return false;
4699 
4700     // check libawt_xawt.so
4701     strcpy(libmawtpath, buf);
4702     strcat(libmawtpath, new_xawtstr);
4703     if (::stat(libmawtpath, &statbuf) == 0) return false;
4704 
4705     return true;
4706 }
4707 
4708 // Get the default path to the core file
4709 // Returns the length of the string
4710 int os::get_core_path(char* buffer, size_t bufferSize) {
4711   int n = jio_snprintf(buffer, bufferSize, "/cores");
4712 
4713   // Truncate if theoretical string was longer than bufferSize
4714   n = MIN2(n, (int)bufferSize);
4715 
4716   return n;
4717 }
4718