diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-11-19 12:21:05 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-11-20 10:16:58 +0000 |
commit | 496e5cf57060fb068a9dc73ac7ff44333a60774e (patch) | |
tree | d8482043790e6c2fc673d66d8f1c1bd340d1d421 /fastos | |
parent | 9225d36f8a4f5a6e12b8163c47d3cad272bd833c (diff) |
Remove FastOS_Time usage
Diffstat (limited to 'fastos')
-rw-r--r-- | fastos/src/vespa/fastos/timestamp.cpp | 22 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/timestamp.h | 3 |
2 files changed, 25 insertions, 0 deletions
diff --git a/fastos/src/vespa/fastos/timestamp.cpp b/fastos/src/vespa/fastos/timestamp.cpp index a1da4322f8c..5daad63f698 100644 --- a/fastos/src/vespa/fastos/timestamp.cpp +++ b/fastos/src/vespa/fastos/timestamp.cpp @@ -2,6 +2,7 @@ #include "timestamp.h" #include <chrono> #include <cmath> +#include <thread> #include <sys/time.h> using namespace std::chrono; @@ -90,10 +91,31 @@ StopWatch::StopWatch() _stopTime(_startTime) { } +void +StopWatch::restart() { + _startTime = steady_now(); + _stopTime = _startTime; +} + StopWatch & StopWatch::stop() { _stopTime = steady_now(); return *this; } +void +StopWatch::waitAtLeast(std::chrono::microseconds us, bool busyWait) { + steady_clock::time_point startTime = steady_clock::now(); + steady_clock::time_point deadline = startTime + us; + while (steady_clock::now() < deadline) { + if (busyWait) { + for (int i = 0; i < 1000; i++) + ; + } else { + microseconds rem = (us - duration_cast<microseconds>(steady_clock::now() - startTime)); + std::this_thread::sleep_for(rem); + } + } +} + } diff --git a/fastos/src/vespa/fastos/timestamp.h b/fastos/src/vespa/fastos/timestamp.h index c670e9f9b49..36f296b891f 100644 --- a/fastos/src/vespa/fastos/timestamp.h +++ b/fastos/src/vespa/fastos/timestamp.h @@ -4,6 +4,7 @@ #include <cstdint> #include <limits> #include <string> +#include <chrono> namespace fastos { @@ -152,11 +153,13 @@ public: StopWatch(); StopWatch & stop(); + void restart(); TimeStamp elapsed() const { TimeStamp diff(_stopTime - _startTime); return (diff > 0) ? diff : TimeStamp(0); } + static void waitAtLeast(std::chrono::microseconds us, bool busyWait); private: SteadyTimeStamp _startTime; SteadyTimeStamp _stopTime; |