+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG.\r
+*\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
#pragma once\r
\r
+#define NOMINMAX\r
+\r
#include <windows.h>\r
\r
namespace caspar {\r
\r
-class timer\r
+class high_prec_timer\r
{\r
public:\r
- timer() : time_(timeGetTime()){}\r
-\r
- double elapsed()\r
- {\r
- return static_cast<double>(timeGetTime() - time_)/1000.0;\r
- }\r
- \r
- void reset()\r
+ high_prec_timer()\r
{\r
- time_ = timeGetTime();\r
+ QueryPerformanceFrequency(&freq_);\r
+ QueryPerformanceCounter(&time_);\r
}\r
- \r
+\r
+ // Author: Ryan M. Geiss\r
+ // http://www.geisswerks.com/ryan/FAQS/timing.html\r
+ void tick(double interval)\r
+ { \r
+ LARGE_INTEGER t;\r
+ QueryPerformanceCounter(&t);\r
+\r
+ if (time_.QuadPart != 0)\r
+ {\r
+ __int64 ticks_to_wait = static_cast<int>(static_cast<double>(freq_.QuadPart) * interval);\r
+ __int64 done = 0;\r
+ do\r
+ {\r
+ QueryPerformanceCounter(&t);\r
+ \r
+ __int64 ticks_passed = static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart);\r
+ __int64 ticks_left = ticks_to_wait - ticks_passed;\r
+\r
+ if (t.QuadPart < time_.QuadPart) // time wrap\r
+ done = 1;\r
+ if (ticks_passed >= ticks_to_wait)\r
+ done = 1;\r
+ \r
+ if (!done)\r
+ {\r
+ // if > 0.002s left, do Sleep(1), which will actually sleep some \r
+ // steady amount, probably 1-2 ms,\r
+ // and do so in a nice way (cpu meter drops; laptop battery spared).\r
+ // otherwise, do a few Sleep(0)'s, which just give up the timeslice,\r
+ // but don't really save cpu or battery, but do pass a tiny\r
+ // amount of time.\r
+ if (ticks_left > static_cast<__int64>((freq_.QuadPart*2)/1000))\r
+ Sleep(1);\r
+ else \r
+ for (int i = 0; i < 10; ++i) \r
+ Sleep(0); // causes thread to give up its timeslice\r
+ }\r
+ }\r
+ while (!done); \r
+ }\r
+\r
+ time_ = t;\r
+ } \r
private: \r
- DWORD time_;\r
+ LARGE_INTEGER freq_;\r
+ LARGE_INTEGER time_;\r
};\r
\r
+\r
}
\ No newline at end of file