mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2025-09-05 22:57:51 -04:00
Use more a reliable time source in Windows for the Timer class
This commit is contained in:
parent
a541e55ff6
commit
c1fef72103
@ -5,13 +5,18 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <mmsystem.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Timer::Timer(void) : time_elapsed(0), system_milliseconds(0), start_time(0), end_time(0), last_update(0), num_updates(0), paused_time(0), offset(0), paused_state(false), lock_state(false), lock_rate(0)
|
Timer::Timer(void) : time_elapsed(0), system_milliseconds(0), start_time(0), end_time(0), last_update(0), num_updates(0), paused_time(0), offset(0), paused_state(false), lock_state(false), lock_rate(0)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
// According to Microsoft, QueryPerformanceXXX API is perfectly
|
||||||
|
//fine for Windows 7+ systems, and use the highest appropriate counter.
|
||||||
|
//this only need to be done once.
|
||||||
|
::QueryPerformanceFrequency(&win_frequency);
|
||||||
|
#endif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -82,7 +87,12 @@ void Timer::update(void)
|
|||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
system_milliseconds = timeGetTime ();
|
//Use QuaryPerformanceCounter, imune to problems sometimes
|
||||||
|
//multimedia timers have.
|
||||||
|
LARGE_INTEGER win_current_count;
|
||||||
|
::QueryPerformanceCounter(&win_current_count);
|
||||||
|
|
||||||
|
system_milliseconds = (unsigned long)(win_current_count.QuadPart * 1000.0 / win_frequency.QuadPart);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
gettimeofday(&time_val,&time_zone);
|
gettimeofday(&time_val,&time_zone);
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
class Timer {
|
class Timer {
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
//units are microsecs:
|
||||||
unsigned long time_elapsed;
|
unsigned long time_elapsed;
|
||||||
unsigned long system_milliseconds;
|
unsigned long system_milliseconds;
|
||||||
unsigned long start_time;
|
unsigned long start_time;
|
||||||
@ -30,7 +31,9 @@ private:
|
|||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
struct timeval time_val;
|
struct timeval time_val;
|
||||||
struct timezone time_zone;
|
struct timezone time_zone;
|
||||||
#endif
|
#else
|
||||||
|
LARGE_INTEGER win_frequency;
|
||||||
|
#endif;
|
||||||
|
|
||||||
bool paused_state;
|
bool paused_state;
|
||||||
bool lock_state;
|
bool lock_state;
|
||||||
@ -91,6 +94,7 @@ public:
|
|||||||
* \return Total time elapsed since the timer start() to the last update() excluding time paused() in milliseconds
|
* \return Total time elapsed since the timer start() to the last update() excluding time paused() in milliseconds
|
||||||
*/
|
*/
|
||||||
unsigned long getMilliseconds(void);
|
unsigned long getMilliseconds(void);
|
||||||
|
|
||||||
/// Alias of getMilliseconds() which returns time in seconds
|
/// Alias of getMilliseconds() which returns time in seconds
|
||||||
/**
|
/**
|
||||||
* \return Total time elapsed since the timer start() to the last update() excluding time paused() in seconds
|
* \return Total time elapsed since the timer start() to the last update() excluding time paused() in seconds
|
||||||
@ -159,6 +163,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool paused();
|
bool paused();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void timerTestFunc();
|
void timerTestFunc();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user