55#include < limits>
66#include < ctime>
77
8+ // Returns a timestamp in nanoseconds, monotonic
9+ static inline int64_t nowts () {
10+ timespec ts;
11+ int res = clock_gettime (CLOCK_MONOTONIC , &ts);
12+ if (res == 0 ) {
13+ return int64_t (ts.tv_sec ) * 1000000000L + int64_t (ts.tv_nsec );
14+ }
15+ return 0 ;
16+ }
17+
818// ! Returns the number of nanoseconds in UTC
9- int64_t utcnow () {
19+ static inline int64_t utcnow () {
1020 struct timespec ts;
1121 int res = ::clock_gettime (CLOCK_REALTIME , &ts);
1222 if (res == 0 ) {
@@ -19,10 +29,6 @@ int64_t utcnow() {
1929
2030#include < x86intrin.h>
2131
22- std::uint64_t tic () {
23- return __builtin_ia32_rdtsc ();
24- }
25-
2632template <typename IntType,
2733 typename RangeType = typename std::make_unsigned<IntType>::type>
2834static inline RangeType forward_difference (const IntType& a, const IntType& b) {
@@ -38,6 +44,10 @@ static inline void disable_reorder() {
3844 asm volatile (" " ::: " memory" );
3945}
4046
47+ static inline std::uint64_t tic () {
48+ return __builtin_ia32_rdtsc ();
49+ }
50+
4151#else
4252
4353#include < chrono>
@@ -91,3 +101,14 @@ double timeit(HistT& hist, Fn&& fn) {
91101 hist.add (elapsed / count);
92102 return elapsed;
93103}
104+
105+ template <typename Fn>
106+ void busyWait (uint64_t ticks, Fn fn) {
107+ uint64_t start = tic ();
108+ uint64_t finish = start + ticks;
109+ uint64_t now;
110+ while ((now = tic ()) < finish) {
111+ mfence ();
112+ fn (now);
113+ }
114+ }
0 commit comments