1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
2 | // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: |
3 | /* -*- mode: C; c-basic-offset: 4 -*- */ |
4 | #ident "$Id$" |
5 | /*====== |
6 | This file is part of TokuDB |
7 | |
8 | |
9 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
10 | |
11 | TokuDBis is free software: you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License, version 2, |
13 | as published by the Free Software Foundation. |
14 | |
15 | TokuDB is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with TokuDB. If not, see <http://www.gnu.org/licenses/>. |
22 | |
23 | ======= */ |
24 | |
25 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
26 | |
27 | #ifndef _TOKUDB_TIME_H |
28 | #define _TOKUDB_TIME_H |
29 | |
30 | #include "hatoku_defines.h" |
31 | |
32 | namespace tokudb { |
33 | namespace time { |
34 | |
35 | static const ulonglong MILLISECONDS = 1000; |
36 | static const ulonglong MICROSECONDS = 1000000; |
37 | static const ulonglong NANOSECONDS = 1000000000; |
38 | |
39 | // gets curent time of day in microseconds |
40 | ulonglong microsec(void); |
41 | |
42 | // gets a timespec in the future based on the current time and an offset forward |
43 | timespec offset_timespec(ulonglong offset); |
44 | |
45 | // sleep microseconds |
46 | void sleep_microsec(ulong tm); |
47 | |
48 | |
49 | |
50 | inline ulonglong microsec(void) { |
51 | timeval t; |
52 | gettimeofday(&t, NULL); |
53 | return t.tv_sec * (1UL * 1000 * 1000) + t.tv_usec; |
54 | } |
55 | inline timespec offset_timespec(ulonglong offset) { |
56 | timespec ret; |
57 | ulonglong tm = offset + microsec(); |
58 | ret.tv_sec = tm / MICROSECONDS; |
59 | ret.tv_nsec = (tm % MICROSECONDS) * 1000; |
60 | return ret; |
61 | } |
62 | inline void sleep_microsec(ulong tm) { |
63 | timeval t; |
64 | t.tv_sec = tm / MICROSECONDS; |
65 | t.tv_usec = tm % MICROSECONDS; |
66 | |
67 | select(0, NULL, NULL, NULL, &t); |
68 | } |
69 | |
70 | } // namespace time |
71 | } // namespace tokudb |
72 | |
73 | #endif // _TOKUDB_TIME_H |
74 | |