Add PBD::QPC::initialize to initialize timer and call it from PBD::init
[ardour.git] / libs / pbd / pbd / windows_timer_utils.h
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifndef PBD_WINDOWS_TIMER_UTILS_H
20 #define PBD_WINDOWS_TIMER_UTILS_H
21
22 #include <stdint.h>
23
24 namespace PBD {
25
26 namespace MMTIMERS {
27
28 /**
29  * Get the minimum Multimedia Timer resolution as supported by the system
30  * @return true if getting min timer resolution was not successful
31  */
32 bool get_min_resolution (uint32_t& timer_resolution_ms);
33
34 /**
35  * Set the minimum Multimedia Timer resolution as supported by the system
36  * @return true if setting min timer resolution was successful
37  */
38 bool set_min_resolution ();
39
40 /**
41  * Set current Multimedia Timer resolution. If the timer resolution has already
42  * been set then reset_resolution() must be called before set_resolution will
43  * succeed.
44  * @return true if setting the timer value was successful, false if setting the
45  * timer resolution failed or the resolution has already been set.
46  */
47 bool set_resolution(uint32_t timer_resolution_ms);
48
49 /**
50  * Reset Multimedia Timer resolution. In my testing, if the timer resolution is
51  * set below the default, then resetting the resolution will not reset the
52  * timer resolution back to 15ms. At least it does not reset immediately
53  * even after calling Sleep.
54  * @return true if setting the timer value was successful
55  */
56 bool reset_resolution();
57
58 } // namespace MMTIMERS
59
60 namespace QPC {
61
62 /**
63  * Initialize the QPC timer, must be called before QPC::get_microseconds will
64  * return a valid value.
65  * @return true if QPC timer is usable, use check_timer_valid to try to check
66  * if it is monotonic.
67  */
68 bool initialize ();
69
70 /**
71  * @return true if QueryPerformanceCounter is usable as a timer source
72  * This should always return true for systems > XP as those versions of windows
73  * have there own tests to check timer validity and will select an appropriate
74  * timer source. This check is not conclusive and there are probably conditions
75  * under which this check will return true but the timer is not monotonic.
76  */
77 bool check_timer_valid ();
78
79 /**
80  * @return the value of the performance counter converted to microseconds
81  *
82  * If initialize returns true then get_microseconds will always return a
83  * positive value. If QPC is not supported(OS < XP) then -1 is returned but the
84  * MS docs say that this won't occur for systems >= XP.
85  */
86 int64_t get_microseconds ();
87
88 } // namespace QPC
89
90 /**
91  * The highest resolution timer source provided by the system. On Vista and
92  * above this is the value returned by QueryPerformanceCounter(QPC). On XP,
93  * this will QPC if supported or otherwise g_get_monotonic_time will be used.
94  *
95  * @return A timer value in microseconds or -1 in the event that the reading
96  * the timer source fails.
97  */
98 int64_t get_microseconds ();
99
100 } // namespace PBD
101
102 #endif // PBD_WINDOWS_TIMER_UTILS_H