diff options
author | Arne H Juul <arnej@yahoo-inc.com> | 2016-09-08 12:08:55 +0200 |
---|---|---|
committer | Arne H Juul <arnej@yahoo-inc.com> | 2016-09-08 14:04:53 +0200 |
commit | 7363ab9ab054f654d2ea64916ccd40a43566d7be (patch) | |
tree | 6a44aa405777eac647662072e01f14ddcaa3ef5c /vespalib/src | |
parent | ec5d6bc110c72a32d4d3ad8c2e7d7b3cf7ef247d (diff) |
add TimeBoxer class
Diffstat (limited to 'vespalib/src')
-rw-r--r-- | vespalib/src/vespa/vespalib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/time/CMakeLists.txt | 6 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/time/time_boxer.cpp | 20 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/time/time_boxer.h | 71 |
4 files changed, 98 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/CMakeLists.txt b/vespalib/src/vespa/vespalib/CMakeLists.txt index 22a654b02b7..7372288a64c 100644 --- a/vespalib/src/vespa/vespalib/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/CMakeLists.txt @@ -14,6 +14,7 @@ vespa_add_library(vespalib $<TARGET_OBJECTS:vespalib_vespalib_stllike> $<TARGET_OBJECTS:vespalib_vespalib_testkit> $<TARGET_OBJECTS:vespalib_vespalib_text> + $<TARGET_OBJECTS:vespalib_vespalib_time> $<TARGET_OBJECTS:vespalib_vespalib_trace> $<TARGET_OBJECTS:vespalib_vespalib_util> $<TARGET_OBJECTS:vespalib_vespalib_websocket> diff --git a/vespalib/src/vespa/vespalib/time/CMakeLists.txt b/vespalib/src/vespa/vespalib/time/CMakeLists.txt new file mode 100644 index 00000000000..8c24e7b9e5e --- /dev/null +++ b/vespalib/src/vespa/vespalib/time/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_library(vespalib_vespalib_time OBJECT + SOURCES + time_boxer.cpp + DEPENDS +) diff --git a/vespalib/src/vespa/vespalib/time/time_boxer.cpp b/vespalib/src/vespa/vespalib/time/time_boxer.cpp new file mode 100644 index 00000000000..df057fc4ccf --- /dev/null +++ b/vespalib/src/vespa/vespalib/time/time_boxer.cpp @@ -0,0 +1,20 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "time_boxer.h" + +#if 0 + +#include <time.h> +#include <unistd.h> +#include <stdio.h> + +int main(int, char **) +{ + vespalib::TimeBoxer tb(3.0); + while (tb.hasTimeLeft()) { + double tl = tb.timeLeft(); + printf("time left: %g seconds\n", tl); + sleep(1); + } +} + +#endif diff --git a/vespalib/src/vespa/vespalib/time/time_boxer.h b/vespalib/src/vespa/vespalib/time/time_boxer.h new file mode 100644 index 00000000000..b5ac6a8ed82 --- /dev/null +++ b/vespalib/src/vespa/vespalib/time/time_boxer.h @@ -0,0 +1,71 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <chrono> + +namespace vespalib { + +/** + * Simple utility for time boxing your activity + * + * <pre> + * Example: + * + * TimeBoxer timebox(5.0); + * while (timebox.hasTimeLeft()) { + * ... do stuff + * ... do stuff with timeout(timebox.timeLeft()) + * } + * </pre> + **/ +class TimeBoxer +{ +private: + using clock = std::conditional<std::chrono::high_resolution_clock::is_steady, + std::chrono::high_resolution_clock, + std::chrono::steady_clock>::type; + + using seconds = std::chrono::duration<double, std::ratio<1,1>>; + + template<typename DURATION> + static inline clock::duration to_internal(DURATION x) { + return std::chrono::duration_cast<clock::duration>(x); + } + + template<typename DURATION> + static inline double to_external(DURATION x) { + return std::chrono::duration_cast<seconds>(x).count(); + } + + clock::time_point _end_time; + +public: + /** + * Construct a TimeBoxer with a given budget from now + * @param budget amount of time in seconds + **/ + explicit TimeBoxer(double budget) + : _end_time(clock::now() + to_internal(seconds(budget))) + { + } + + /** + * @return true if there is time left in the budget + **/ + bool hasTimeLeft() { + return clock::now() < _end_time; + } + + /** + * @return the seconds left before the budget elapses + **/ + double timeLeft() { + clock::time_point now = clock::now(); + if (now < _end_time) { + return to_external(_end_time - now); + } + return 0.0; + } +}; + +} // namespace vespalib |