summaryrefslogtreecommitdiff
path: root/include/arch/posix/driver/counter.h
blob: e37bfd78ab588575f00369c743e2d476605dfd7d (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
/*
 * Copyright 2020 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#ifndef COUNTER_H
#define COUNTER_H

#include <stdint.h>
#include <time.h>

typedef uint64_t counter_value_t;
typedef uint8_t counter_overflow_t;

class Counter {
	private:
		Counter(const Counter &copy);
		uint64_t start_sec, start_nsec;

	public:
		uint64_t value;
		volatile uint8_t overflow;

		Counter() : overflow(0) {}

		inline void start() {
			struct timespec ts;
			clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
			start_sec = ts.tv_sec;
			start_nsec = ts.tv_nsec;
		}

		inline void stop() {
			struct timespec ts;
			clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);

			value = (ts.tv_sec - start_sec) * 1000000000UL;
			value += ts.tv_nsec - start_nsec;
		}
};

extern Counter counter;

#endif