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/*======
6This file is part of TokuDB
7
8
9Copyright (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
32namespace tokudb {
33namespace time {
34
35static const ulonglong MILLISECONDS = 1000;
36static const ulonglong MICROSECONDS = 1000000;
37static const ulonglong NANOSECONDS = 1000000000;
38
39// gets curent time of day in microseconds
40ulonglong microsec(void);
41
42// gets a timespec in the future based on the current time and an offset forward
43timespec offset_timespec(ulonglong offset);
44
45// sleep microseconds
46void sleep_microsec(ulong tm);
47
48
49
50inline ulonglong microsec(void) {
51 timeval t;
52 gettimeofday(&t, NULL);
53 return t.tv_sec * (1UL * 1000 * 1000) + t.tv_usec;
54}
55inline 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}
62inline 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