summaryrefslogtreecommitdiffstats
path: root/src/uscxml/concurrency/Timer.h
blob: 1ecfeb265994b1e2126db3aacd68b2b72d10e60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2013 Alex Reece.
//
// A cross platform monotonic timer.

// see https://github.com/awreece/monotonic_timer

#ifndef MONOTONIC_TIMER_H_
#define MONOTONIC_TIMER_H_

#include "uscxml/Common.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 reset() {
		elapsed = 0;
	}

	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_