7c53d9dfb959f95a4516dcd3c36a8019eac2f40c
[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 /*#*
15  *#* $Log$
16  *#* Revision 1.5  2005/11/04 16:20:02  bernie
17  *#* Fix reference to README.devlib in header.
18  *#*
19  *#* Revision 1.4  2004/08/25 14:12:09  rasky
20  *#* Aggiornato il comment block dei log RCS
21  *#*
22  *#* Revision 1.3  2004/08/10 05:45:04  bernie
23  *#* Fix spacing in header.
24  *#*
25  *#* Revision 1.2  2004/06/03 11:27:09  bernie
26  *#* Add dual-license information.
27  *#*
28  *#* Revision 1.1  2004/06/03 09:01:06  bernie
29  *#* Import into DevLib.
30  *#*
31  *#*/
32
33 #include "hptime.h"
34
35 #if defined(_WIN32)
36
37 #include <windows.h>
38
39 hptime_t hptime_get(void)
40 {
41         FILETIME ft;
42
43         /*
44          * La precisione dei FileTime sarebbe 100ns, ma il
45          * valore viene ottenuto convertendo una struttura
46          * SYSTEMTIME, che ha precisione di 1ms. Il numero
47          * che otteniamo e' quindi sempre un multiplo di
48          * 100000.
49          */
50         GetSystemTimeAsFileTime(&ft);
51
52         /* Copy the upper/lower into a quadword. */
53         return (((hptime_t)ft.dwHighDateTime) << 32) + (hptime_t)ft.dwLowDateTime;
54 }
55
56 #elif defined(__unix__)
57
58 #include <sys/time.h>
59
60 hptime_t hptime_get(void)
61 {
62         struct timeval tv;
63
64         gettimeofday(&tv, 0);
65         return (hptime_t)tv.tv_sec * HPTIME_TICKS_PER_SECOND
66                 + (hptime_t)tv.tv_usec;
67 }
68
69 #else /* !__unix__ */
70         #error OS dependent support code missing for this OS
71 #endif /* !__unix__ */
72