935 struct timespec tp;
936 if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
937 ::clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
938 // yes, monotonic clock is supported
939 _clock_gettime = ::clock_gettime;
940 }
941 }
942 #endif
943
944
945
946 #ifdef __APPLE__
947
948 jlong os::javaTimeNanos() {
949 const uint64_t tm = mach_absolute_time();
950 const uint64_t now = (tm * Bsd::_timebase_info.numer) / Bsd::_timebase_info.denom;
951 const uint64_t prev = Bsd::_max_abstime;
952 if (now <= prev) {
953 return prev; // same or retrograde time;
954 }
955 const uint64_t obsv = Atomic::cmpxchg(now, (volatile jlong*)&Bsd::_max_abstime, prev);
956 assert(obsv >= prev, "invariant"); // Monotonicity
957 // If the CAS succeeded then we're done and return "now".
958 // If the CAS failed and the observed value "obsv" is >= now then
959 // we should return "obsv". If the CAS failed and now > obsv > prv then
960 // some other thread raced this thread and installed a new value, in which case
961 // we could either (a) retry the entire operation, (b) retry trying to install now
962 // or (c) just return obsv. We use (c). No loop is required although in some cases
963 // we might discard a higher "now" value in deference to a slightly lower but freshly
964 // installed obsv value. That's entirely benign -- it admits no new orderings compared
965 // to (a) or (b) -- and greatly reduces coherence traffic.
966 // We might also condition (c) on the magnitude of the delta between obsv and now.
967 // Avoiding excessive CAS operations to hot RW locations is critical.
968 // See https://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate
969 return (prev == obsv) ? now : obsv;
970 }
971
972 #else // __APPLE__
973
974 jlong os::javaTimeNanos() {
975 if (os::supports_monotonic_clock()) {
|
935 struct timespec tp;
936 if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
937 ::clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
938 // yes, monotonic clock is supported
939 _clock_gettime = ::clock_gettime;
940 }
941 }
942 #endif
943
944
945
946 #ifdef __APPLE__
947
948 jlong os::javaTimeNanos() {
949 const uint64_t tm = mach_absolute_time();
950 const uint64_t now = (tm * Bsd::_timebase_info.numer) / Bsd::_timebase_info.denom;
951 const uint64_t prev = Bsd::_max_abstime;
952 if (now <= prev) {
953 return prev; // same or retrograde time;
954 }
955 const uint64_t obsv = Atomic::cmpxchg(now, &Bsd::_max_abstime, prev);
956 assert(obsv >= prev, "invariant"); // Monotonicity
957 // If the CAS succeeded then we're done and return "now".
958 // If the CAS failed and the observed value "obsv" is >= now then
959 // we should return "obsv". If the CAS failed and now > obsv > prv then
960 // some other thread raced this thread and installed a new value, in which case
961 // we could either (a) retry the entire operation, (b) retry trying to install now
962 // or (c) just return obsv. We use (c). No loop is required although in some cases
963 // we might discard a higher "now" value in deference to a slightly lower but freshly
964 // installed obsv value. That's entirely benign -- it admits no new orderings compared
965 // to (a) or (b) -- and greatly reduces coherence traffic.
966 // We might also condition (c) on the magnitude of the delta between obsv and now.
967 // Avoiding excessive CAS operations to hot RW locations is critical.
968 // See https://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate
969 return (prev == obsv) ? now : obsv;
970 }
971
972 #else // __APPLE__
973
974 jlong os::javaTimeNanos() {
975 if (os::supports_monotonic_clock()) {
|