838 // if q overflowed, ignore the overflow and increment q
839 if (lo > m) {
840 lo &= m;
841 ++lo;
842 }
843 lo += hi >> 15;
844
845 // if (p+q) overflowed, ignore the overflow and increment (p+q)
846 if (lo > m) {
847 lo &= m;
848 ++lo;
849 }
850 return lo;
851 }
852
853 int os::random() {
854 // Make updating the random seed thread safe.
855 while (true) {
856 unsigned int seed = _rand_seed;
857 unsigned int rand = random_helper(seed);
858 if (Atomic::cmpxchg(rand, &_rand_seed, seed) == seed) {
859 return static_cast<int>(rand);
860 }
861 }
862 }
863
864 // The INITIALIZED state is distinguished from the SUSPENDED state because the
865 // conditions in which a thread is first started are different from those in which
866 // a suspension is resumed. These differences make it hard for us to apply the
867 // tougher checks when starting threads that we want to do when resuming them.
868 // However, when start_thread is called as a result of Thread.start, on a Java
869 // thread, the operation is synchronized on the Java Thread object. So there
870 // cannot be a race to start the thread and hence for the thread to exit while
871 // we are working on it. Non-Java threads that start Java threads either have
872 // to do so in a context in which races are impossible, or should do appropriate
873 // locking.
874
875 void os::start_thread(Thread* thread) {
876 // guard suspend/resume
877 MutexLocker ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
878 OSThread* osthread = thread->osthread();
1787 result = pd_unmap_memory(addr, bytes);
1788 }
1789 return result;
1790 }
1791
1792 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1793 pd_free_memory(addr, bytes, alignment_hint);
1794 }
1795
1796 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1797 pd_realign_memory(addr, bytes, alignment_hint);
1798 }
1799
1800 #ifndef _WINDOWS
1801 /* try to switch state from state "from" to state "to"
1802 * returns the state set after the method is complete
1803 */
1804 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
1805 os::SuspendResume::State to)
1806 {
1807 os::SuspendResume::State result = Atomic::cmpxchg(to, &_state, from);
1808 if (result == from) {
1809 // success
1810 return to;
1811 }
1812 return result;
1813 }
1814 #endif
1815
1816 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1817 // times. Only for use by non-JavaThreads.
1818 void os::naked_sleep(jlong millis) {
1819 assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1820 const jlong limit = 999;
1821 while (millis > limit) {
1822 naked_short_sleep(limit);
1823 millis -= limit;
1824 }
1825 naked_short_sleep(millis);
1826 }
|
838 // if q overflowed, ignore the overflow and increment q
839 if (lo > m) {
840 lo &= m;
841 ++lo;
842 }
843 lo += hi >> 15;
844
845 // if (p+q) overflowed, ignore the overflow and increment (p+q)
846 if (lo > m) {
847 lo &= m;
848 ++lo;
849 }
850 return lo;
851 }
852
853 int os::random() {
854 // Make updating the random seed thread safe.
855 while (true) {
856 unsigned int seed = _rand_seed;
857 unsigned int rand = random_helper(seed);
858 if (Atomic::cmpxchg(&_rand_seed, seed, rand) == seed) {
859 return static_cast<int>(rand);
860 }
861 }
862 }
863
864 // The INITIALIZED state is distinguished from the SUSPENDED state because the
865 // conditions in which a thread is first started are different from those in which
866 // a suspension is resumed. These differences make it hard for us to apply the
867 // tougher checks when starting threads that we want to do when resuming them.
868 // However, when start_thread is called as a result of Thread.start, on a Java
869 // thread, the operation is synchronized on the Java Thread object. So there
870 // cannot be a race to start the thread and hence for the thread to exit while
871 // we are working on it. Non-Java threads that start Java threads either have
872 // to do so in a context in which races are impossible, or should do appropriate
873 // locking.
874
875 void os::start_thread(Thread* thread) {
876 // guard suspend/resume
877 MutexLocker ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
878 OSThread* osthread = thread->osthread();
1787 result = pd_unmap_memory(addr, bytes);
1788 }
1789 return result;
1790 }
1791
1792 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1793 pd_free_memory(addr, bytes, alignment_hint);
1794 }
1795
1796 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1797 pd_realign_memory(addr, bytes, alignment_hint);
1798 }
1799
1800 #ifndef _WINDOWS
1801 /* try to switch state from state "from" to state "to"
1802 * returns the state set after the method is complete
1803 */
1804 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
1805 os::SuspendResume::State to)
1806 {
1807 os::SuspendResume::State result = Atomic::cmpxchg(&_state, from, to);
1808 if (result == from) {
1809 // success
1810 return to;
1811 }
1812 return result;
1813 }
1814 #endif
1815
1816 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1817 // times. Only for use by non-JavaThreads.
1818 void os::naked_sleep(jlong millis) {
1819 assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1820 const jlong limit = 999;
1821 while (millis > limit) {
1822 naked_short_sleep(limit);
1823 millis -= limit;
1824 }
1825 naked_short_sleep(millis);
1826 }
|