X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=common%2Futility%2Ftimer.h;h=ce399327572e9190ef87d6dfbe27e56523bb2adc;hb=f9c7bf3eee1eefc10e11d8d760d627c70a8d111f;hp=6d5a15dcaa55acbf6a4248ae52ad8ba8d356835e;hpb=d7d6f3c631e5e7b4066761161a30ba778878477e;p=casparcg diff --git a/common/utility/timer.h b/common/utility/timer.h index 6d5a15dca..ce3993275 100644 --- a/common/utility/timer.h +++ b/common/utility/timer.h @@ -1,50 +1,55 @@ +/* +* copyright (c) 2010 Sveriges Television AB +* +* This file is part of CasparCG. +* +* CasparCG is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* CasparCG is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License +* along with CasparCG. If not, see . +* +*/ #pragma once +#define NOMINMAX + #include +#include namespace caspar { -class timer +class high_prec_timer { public: - timer() + high_prec_timer() + : time_(0) { - QueryPerformanceFrequency(&freq_); - QueryPerformanceCounter(&time_); - } - - double elapsed() - { - LARGE_INTEGER t; - QueryPerformanceCounter(&t); - int ticks_passed = static_cast(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart)); - return static_cast(ticks_passed)/static_cast(freq_.QuadPart); - } - - void reset() - { - QueryPerformanceCounter(&time_); } // Author: Ryan M. Geiss // http://www.geisswerks.com/ryan/FAQS/timing.html void tick(double interval) { - LARGE_INTEGER t; - QueryPerformanceCounter(&t); + auto t = ::timeGetTime(); - if (time_.QuadPart != 0) + if (time_ != 0) { - int ticks_to_wait = static_cast(static_cast(freq_.QuadPart) * interval); - int done = 0; + auto ticks_to_wait = static_cast(interval*1000.0); + bool done = 0; do - { - QueryPerformanceCounter(&t); - - int ticks_passed = static_cast(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart)); - int ticks_left = ticks_to_wait - ticks_passed; + { + auto ticks_passed = t - time_; + auto ticks_left = ticks_to_wait - ticks_passed; - if (t.QuadPart < time_.QuadPart) // time wrap + if (t < time_) // time wrap done = 1; if (ticks_passed >= ticks_to_wait) done = 1; @@ -57,12 +62,14 @@ public: // otherwise, do a few Sleep(0)'s, which just give up the timeslice, // but don't really save cpu or battery, but do pass a tiny // amount of time. - if (ticks_left > static_cast((freq_.QuadPart*2)/1000)) + if (ticks_left > 2) Sleep(1); else for (int i = 0; i < 10; ++i) Sleep(0); // causes thread to give up its timeslice } + + t = ::timeGetTime(); } while (!done); } @@ -70,8 +77,8 @@ public: time_ = t; } private: - LARGE_INTEGER freq_; - LARGE_INTEGER time_; + DWORD time_; }; + } \ No newline at end of file