Mir
posix_timestamp.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Daniel van Vugt <daniel.van.vugt@canonical.com>
17  */
18 
19 #ifndef MIR_TIME_POSIX_TIMESTAMP_H_
20 #define MIR_TIME_POSIX_TIMESTAMP_H_
21 
22 #include <chrono>
23 #include <ctime>
24 
25 namespace mir { namespace time {
26 
27 /*
28  * We need absolute precision here so sadly can't use high-level C++ clocks...
29  * - Graphics frame timing needs support for at least the kernel clocks
30  * CLOCK_REALTIME and CLOCK_MONOTONIC, to be selected at runtime, whereas
31  * std::chrono does not support CLOCK_REALTIME or easily switching clocks.
32  * - mir::time::Timestamp is relative to the (wrong) epoch of steady_clock,
33  * so converting to/from mir::time::Timestamp would be dangerously
34  * inaccurate at best.
35  */
36 
38 {
39  clockid_t clock_id;
40  std::chrono::nanoseconds nanoseconds;
41 
43  : clock_id{CLOCK_MONOTONIC}, nanoseconds{0} {}
44  PosixTimestamp(clockid_t clk, std::chrono::nanoseconds ns)
45  : clock_id{clk}, nanoseconds{ns} {}
46  PosixTimestamp(clockid_t clk, struct timespec const& ts)
47  : clock_id{clk}, nanoseconds{ts.tv_sec*1000000000LL + ts.tv_nsec} {}
48 
49  static PosixTimestamp now(clockid_t clock_id)
50  {
51  struct timespec ts;
52  clock_gettime(clock_id, &ts);
53  return PosixTimestamp(clock_id, ts);
54  }
55 };
56 
57 }} // namespace mir::time
58 
59 #endif // MIR_TIME_POSIX_TIMESTAMP_H_
Definition: as_render_target.h:27
static PosixTimestamp now(clockid_t clock_id)
Definition: posix_timestamp.h:49
PosixTimestamp(clockid_t clk, std::chrono::nanoseconds ns)
Definition: posix_timestamp.h:44
PosixTimestamp(clockid_t clk, struct timespec const &ts)
Definition: posix_timestamp.h:46
PosixTimestamp()
Definition: posix_timestamp.h:42
clockid_t clock_id
Definition: posix_timestamp.h:39
std::chrono::nanoseconds nanoseconds
Definition: posix_timestamp.h:40
Definition: posix_timestamp.h:37

Copyright © 2012-2016 Canonical Ltd.
Generated on Sat Dec 3 12:48:59 UTC 2016