7be9ea206c0c9428871846444b88b8e5f0ababe0
[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>
40
41 hptime_t hptime_get(void)
42 {
43         struct timeval tv;
44
45         gettimeofday(&tv, 0);
46         return (hptime_t)tv.tv_sec * HPTIME_TICKS_PER_SECOND
47                 + (hptime_t)tv.tv_usec;
48 }
49
50 #else /* !__unix__ */
51         #error OS dependent support code missing for this OS
52 #endif /* !__unix__ */
53