diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-10 12:45:01 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-10 12:45:01 +0200 |
commit | b67d81563718bd1f773f9e586b04b285e45b1208 (patch) | |
tree | f591d180832dd8ca31e7a76c08c244f6cd16c2c3 /compat | |
parent | e3cf9b75b24ce6b202f33f2efc9de41e7c2aa01e (diff) |
compat/timer-resolution: we want higher timer resolution
Windows scheduler performs badly with 1000 Hz.
Diffstat (limited to 'compat')
-rw-r--r-- | compat/CMakeLists.txt | 4 | ||||
-rw-r--r-- | compat/timer-resolution.cpp | 59 | ||||
-rw-r--r-- | compat/timer-resolution.hpp | 11 |
3 files changed, 37 insertions, 37 deletions
diff --git a/compat/CMakeLists.txt b/compat/CMakeLists.txt index 33cb02bd..7e5dd4cd 100644 --- a/compat/CMakeLists.txt +++ b/compat/CMakeLists.txt @@ -4,6 +4,10 @@ if(NOT WIN32 AND NOT APPLE) target_link_libraries(opentrack-compat rt) endif() +if(WIN32) + target_link_libraries(opentrack-compat winmm) +endif() + if(CMAKE_COMPILER_IS_GNUCXX) otr_prop(SOURCE nan.cpp COMPILE_FLAGS "-fno-lto -fno-fast-math -fno-finite-math-only -O0") endif() diff --git a/compat/timer-resolution.cpp b/compat/timer-resolution.cpp index d59c250d..eece39e9 100644 --- a/compat/timer-resolution.cpp +++ b/compat/timer-resolution.cpp @@ -5,16 +5,21 @@ * copyright notice and this permission notice appear in all copies. */ +#ifdef _WIN32 + #include "timer-resolution.hpp" +#include "time.hpp" +#include "compat/util.hpp" -#if defined _WIN32 -# include <QLibrary> -# include <QDebug> +#include <utility> -using namespace timer_impl; +#include <QLibrary> +#include <QDebug> -static funptr_NtSetTimerResolution init_timer_resolution_funptr(); -static funptr_NtSetTimerResolution get_funptr(); +#include <windows.h> + +using namespace time_units; +using namespace timer_impl; static funptr_NtSetTimerResolution init_timer_resolution_funptr() { @@ -49,37 +54,42 @@ static funptr_NtSetTimerResolution get_funptr() return ret; } -timer_resolution::timer_resolution(int msecs) : old_value(fail_()) +timer_resolution::timer_resolution() { - if (msecs <= 0 || msecs > 100) - { - qDebug() << "can't set timer resolution to" << msecs << "ms"; - return; - } + for (int k = 14; k > 0; k--) + if (unlikely(timeEndPeriod(k) == 0)) + { + qDebug() << "removed mm timer for" << k << "ms"; + k++; + } - funptr_NtSetTimerResolution set_timer_res = get_funptr(); - if (set_timer_res == nullptr) - return; + static funptr_NtSetTimerResolution set_timer_res = get_funptr(); // hundredth of a nanosecond - const ulong_ value = msecs * ulong_(10000); + //const ulong_ value = msecs * ulong_(10000); + + const ulong_ value = 156250; // default win32 timer length ntstatus_ res; + ulong_ old_value = fail_(); res = set_timer_res(value, true_(), &old_value); - if (res < 0) + if (unlikely(res != 0)) { old_value = fail_(); qDebug() << "NtSetTimerResolution erred with" << res; return; } + if (likely(std::abs(long(old_value) - long(value)) < 10000)) + return; + // see if it stuck ulong_ old_value_ = fail_(); res = set_timer_res(value, true_(), &old_value_); - if (res < 0) + if (unlikely(res != 0)) { old_value = fail_(); qDebug() << "NtSetTimerResolution check erred with" << res; @@ -92,21 +102,10 @@ timer_resolution::timer_resolution(int msecs) : old_value(fail_()) qDebug() << "NtSetTimerResolution:" << "old value didn't stick" - << "current resolution" << (t(old_value_) * t(100)) - << "ns"; + << "current resolution" << time_cast<ms>(ns(t(old_value_) * t(100))).count() << "ms"; old_value = fail_(); return; } } -timer_resolution::~timer_resolution() -{ - if (old_value != fail_()) - { - funptr_NtSetTimerResolution f = get_funptr(); - ulong_ fuzz = fail_(); - (void) f(old_value, true_(), &fuzz); - } -} - #endif diff --git a/compat/timer-resolution.hpp b/compat/timer-resolution.hpp index 8c18a600..8f1d4bc7 100644 --- a/compat/timer-resolution.hpp +++ b/compat/timer-resolution.hpp @@ -43,11 +43,11 @@ using funptr_NtSetTimerResolution = ntstatus_ (__stdcall *)(ulong_, boolean_, ul class OTR_COMPAT_EXPORT timer_resolution final { - ulong_ old_value; - public: - timer_resolution(int msecs); + timer_resolution(); +#if 0 ~timer_resolution(); +#endif }; } @@ -55,8 +55,5 @@ public: using timer_impl::timer_resolution; #else -struct timer_resolution final -{ - inline timer_resolution(int) {} -}; +# error "this is win32-only header" #endif |