summaryrefslogtreecommitdiffstats
path: root/src/uscxml/concurrency/Timer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/uscxml/concurrency/Timer.h')
-rw-r--r--src/uscxml/concurrency/Timer.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/uscxml/concurrency/Timer.h b/src/uscxml/concurrency/Timer.h
new file mode 100644
index 0000000..60a20a3
--- /dev/null
+++ b/src/uscxml/concurrency/Timer.h
@@ -0,0 +1,68 @@
+// Copyright 2013 Alex Reece.
+//
+// A cross platform monotonic timer.
+
+// see https://github.com/awreece/monotonic_timer
+
+#ifndef MONOTONIC_TIMER_H_
+#define MONOTONIC_TIMER_H_
+
+// Returns seconds since some unspecified start time (guaranteed to be
+// monotonicly increasing).
+
+// Copyright 2015 Stefan Radomski.
+
+namespace uscxml {
+
+class USCXML_API Timer {
+public:
+
+ static double monotonic_seconds();
+
+ Timer() {
+ invocations = 0;
+ elapsed = 0;
+ }
+
+ void start() {
+ if (invocations == 0) {
+ started = monotonic_seconds();
+ }
+ invocations++;
+ }
+
+ void stop() {
+ if (invocations == 0)
+ return;
+
+ invocations--;
+ if (invocations == 0) {
+ elapsed += monotonic_seconds() - started;
+ }
+ }
+
+ ~Timer() {
+ }
+ double elapsed;
+
+protected:
+ size_t invocations;
+ double started;
+};
+
+class USCXML_API Measurement {
+public:
+ Measurement(Timer* timer) : timer(timer) {
+ timer->start();
+ }
+
+ ~Measurement() {
+ timer->stop();
+ }
+
+protected:
+ Timer* timer;
+};
+
+}
+#endif // MONOTONIC_TIMER_H_