forked from datacom-teracom/l2tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl2t_timer.h
65 lines (52 loc) · 1.72 KB
/
l2t_timer.h
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
/*************************************************************************************************/
/**
* \file
* \brief Define a helper classes to manipulate timers.
*/
/*************************************************************************************************/
#ifndef L2T_TIMER_H
#define L2T_TIMER_H
extern "C" {
#include <time.h>
#include <stdint.h>
#include <pthread.h>
}
namespace L2T {
/*************************************************************************************************/
/**
* \brief Class to verify elapsed time and sleep with milliseconds precision.
*/
class Timer {
public:
/**
* \brief Construct new Timer and update internal clock.
*/
Timer();
/**
* \brief Sleep until current time is increased by an interval in milliseconds.
* \param _interval_ms Interval in milliseconds.
*/
void sleep_ms(uint64_t _interval_ms);
/**
* \brief Sleep until current time is increased by an interval in nanoseconds.
* \param _interval_ns Interval in nanoseconds.
*/
void sleep_ns(uint64_t _interval_ns);
/**
* \brief Update internal clock with current time.
*/
void update();
/**
* \brief Elapsed time in milliseconds.
* \param _creation If true, return elapsed time since object creation.
* \return Elapsed time.
*/
uint64_t elapsed_ms(bool _creation = false);
private:
struct timespec created; /**< Creation time. */
struct timespec current; /**< Last updated time. */
pthread_mutex_t mutex; /**< Used to lock access to current clock. */
};
/*************************************************************************************************/
} /* namespace L2T */
#endif /* L2T_TIMER_H */