diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
commit | 541c8c407b7ba8dd10f85bb48bcb5900270b3f84 (patch) | |
tree | 71a6abc0f2a43bcfd33f80b3b30b878f234cbf05 /src/FbTk/FbTime.cc | |
parent | 60a53113e05db443af4d520883ec3145680642a8 (diff) | |
download | fluxbox-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.zip fluxbox-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.tar.bz2 |
changed timing functions to use a monotonic increasing clock
gettimeofday() is subject to be changed on daylight-saving or to ntp-related
(think leap-seconds). even worse, it is subject to be changed BACK in time. this
is hard to fix correctly (see commit 45726d3016e and bug #3560509). it is
irrelevant for timers to know the nano-seconds since the epoch anyways.
Diffstat (limited to 'src/FbTk/FbTime.cc')
-rw-r--r-- | src/FbTk/FbTime.cc | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/FbTk/FbTime.cc b/src/FbTk/FbTime.cc new file mode 100644 index 0000000..54eda75 --- /dev/null +++ b/src/FbTk/FbTime.cc | |||
@@ -0,0 +1,97 @@ | |||
1 | // FbTime.cc for FbTk - Fluxbox Toolkit | ||
2 | // Copyright (c) 2012 Mathias Gumz (akira at fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "FbTime.hh" | ||
23 | |||
24 | |||
25 | |||
26 | #ifdef HAVE_CLOCK_GETTIME // linux|*bsd|solaris | ||
27 | #include <time.h> | ||
28 | |||
29 | namespace { | ||
30 | |||
31 | uint64_t _now() { | ||
32 | |||
33 | uint64_t n = 0L; | ||
34 | timespec ts; | ||
35 | |||
36 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { | ||
37 | n = (ts.tv_sec * FbTk::FbTime::IN_SECONDS) + (ts.tv_nsec / 1000L); | ||
38 | } | ||
39 | |||
40 | return n; | ||
41 | } | ||
42 | |||
43 | } | ||
44 | |||
45 | #endif // HAVE_CLOCK_GETTIME | ||
46 | |||
47 | |||
48 | |||
49 | |||
50 | |||
51 | #ifdef HAVE_MACH_ABSOLUTE_TIME // macosx | ||
52 | |||
53 | // http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x | ||
54 | // https://github.com/ThomasHabets/monotonic_clock/blob/master/src/monotonic_mach.c | ||
55 | // http://shiftedbits.org/2008/10/01/mach_absolute_time-on-the-iphone/ | ||
56 | |||
57 | |||
58 | #include <mach/mach_time.h> | ||
59 | |||
60 | namespace { | ||
61 | |||
62 | uint64_t _now() { | ||
63 | |||
64 | // mach_absolute_time() * info.numer / info.denom yields | ||
65 | // nanoseconds. | ||
66 | |||
67 | static double micro_scale = 0.001; // 1000ms == 1ns | ||
68 | static bool initial = true; | ||
69 | |||
70 | if (initial) { | ||
71 | initial = false; | ||
72 | mach_timebase_info_data_t info; | ||
73 | if (mach_timebase_info(&info) == 0) { | ||
74 | micro_scale *= static_cast<double>info.numer / static_cast<double>(info.denom); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | return static_cast<uint64_t>(mach_absolute_time() * micro_scale); | ||
79 | } | ||
80 | |||
81 | } | ||
82 | |||
83 | #endif // HAVE_MACH_ABSOLUTE_TIME | ||
84 | |||
85 | |||
86 | |||
87 | |||
88 | |||
89 | uint64_t FbTk::FbTime::now() { | ||
90 | return ::_now(); | ||
91 | } | ||
92 | |||
93 | |||
94 | uint64_t FbTk::FbTime::remainingNext(uint64_t unit) { | ||
95 | return (unit - (::_now() % unit) - 1); | ||
96 | } | ||
97 | |||