771832307efe1493b58a327ef4541ccf1132070d
[bertos.git] / os / hptime.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Portable abstraction for high-resolution time handling (implementation)
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 #include "hptime.h"
15
16 #if defined(_WIN32)
17
18 #include <windows.h>
19
20 hptime_t hptime_get(void)
21 {
22         FILETIME ft;
23
24         /*
25          * La precisione dei FileTime sarebbe 100ns, ma il
26          * valore viene ottenuto convertendo una struttura
27          * SYSTEMTIME, che ha precisione di 1ms. Il numero
28          * che otteniamo e' quindi sempre un multiplo di
29          * 100000.
30          */
31         GetSystemTimeAsFileTime(&ft);
32
33         /* Copy the upper/lower into a quadword. */
34         return (((hptime_t)ft.dwHighDateTime) << 32) + (hptime_t)ft.dwLowDateTime;
35 }
36
37 #elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
38
39 #include <sys/time.h> /* for gettimeofday() */
40 #include <stddef.h> /* for NULL */
41
42 hptime_t hptime_get(void)
43 {
44         struct timeval tv;
45
46         gettimeofday(&tv, NULL);
47         return (hptime_t)tv.tv_sec * HPTIME_TICKS_PER_SECOND
48                 + (hptime_t)tv.tv_usec;
49 }
50
51 #else /* !__unix__ */
52         #error OS dependent support code missing for this OS
53 #endif /* !__unix__ */
54