Merge pull request #2250 from esdrubal/master
[mono.git] / mono / utils / mono-time.h
1 #ifndef __UTILS_MONO_TIME_H__
2 #define __UTILS_MONO_TIME_H__
3
4 #include <mono/utils/mono-compiler.h>
5 #include <glib.h>
6
7 #ifdef HAVE_SYS_TIME_H
8 #include <sys/time.h>
9 #endif
10
11 /* Returns the number of milliseconds from boot time: this should be monotonic
12  *
13  * Prefer to use mono_msec_ticks for elapsed time calculation. */
14 gint64 mono_msec_boottime (void);
15
16 /* Returns the number of milliseconds ticks from unspecified time: this should be monotonic */
17 gint64 mono_msec_ticks (void);
18
19 /* Returns the number of 100ns ticks from unspecified time: this should be monotonic */
20 gint64 mono_100ns_ticks (void);
21
22 /* Returns the number of 100ns ticks since 1/1/1601, UTC timezone */
23 gint64 mono_100ns_datetime (void);
24
25 #ifndef HOST_WIN32
26 gint64 mono_100ns_datetime_from_timeval (struct timeval tv);
27 #endif
28
29 /* Stopwatch class for internal runtime use */
30 typedef struct {
31         gint64 start, stop;
32 } MonoStopwatch;
33
34 static inline void
35 mono_stopwatch_start (MonoStopwatch *w)
36 {
37         w->start = mono_100ns_ticks ();
38         w->stop = 0;
39 }
40
41 static inline void
42 mono_stopwatch_stop (MonoStopwatch *w)
43 {
44         w->stop = mono_100ns_ticks ();
45 }
46
47 static inline guint64
48 mono_stopwatch_elapsed (MonoStopwatch *w)
49 {
50         return (w->stop - w->start) / 10;
51 }
52
53 static inline guint64
54 mono_stopwatch_elapsed_ms (MonoStopwatch *w)
55 {
56         return (mono_stopwatch_elapsed (w) + 500) / 1000;
57 }
58
59 #endif /* __UTILS_MONO_TIME_H__ */
60