mirror of
https://github.com/vdr-projects/vdr.git
synced 2025-03-01 10:50:46 +00:00
Replaced time_ms() with a threadsafe and non-overflowing cTimeMs
This commit is contained in:
44
tools.c
44
tools.c
@@ -4,7 +4,7 @@
|
||||
* See the main source file 'vdr.c' for copyright information and
|
||||
* how to reach the author.
|
||||
*
|
||||
* $Id: tools.c 1.84 2004/12/19 17:19:46 kls Exp $
|
||||
* $Id: tools.c 1.85 2004/12/19 18:06:16 kls Exp $
|
||||
*/
|
||||
|
||||
#include "tools.h"
|
||||
@@ -187,18 +187,6 @@ int numdigits(int n)
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
int time_ms(void)
|
||||
{
|
||||
static time_t t0 = 0;
|
||||
struct timeval t;
|
||||
if (gettimeofday(&t, NULL) == 0) {
|
||||
if (t0 == 0)
|
||||
t0 = t.tv_sec; // this avoids an overflow (we only work with deltas)
|
||||
return (t.tv_sec - t0) * 1000 + t.tv_usec / 1000;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool isnumber(const char *s)
|
||||
{
|
||||
if (!*s)
|
||||
@@ -432,6 +420,36 @@ time_t LastModifiedTime(const char *FileName)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- cTimeMs ---------------------------------------------------------------
|
||||
|
||||
cTimeMs::cTimeMs(void)
|
||||
{
|
||||
Set();
|
||||
}
|
||||
|
||||
void cTimeMs::Set(int Ms)
|
||||
{
|
||||
begin = Now() + Ms;
|
||||
}
|
||||
|
||||
bool cTimeMs::TimedOut(void)
|
||||
{
|
||||
return Now() >= begin;
|
||||
}
|
||||
|
||||
uint64 cTimeMs::Now(void)
|
||||
{
|
||||
struct timeval t;
|
||||
if (gettimeofday(&t, NULL) == 0)
|
||||
return (uint64(t.tv_sec)) * 1000 + t.tv_usec / 1000;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64 cTimeMs::Elapsed(void)
|
||||
{
|
||||
return Now() - begin;
|
||||
}
|
||||
|
||||
// --- cBufferedStringFunction -----------------------------------------------
|
||||
|
||||
cBufferedStringFunction::cBufferedStringFunction(void)
|
||||
|
Reference in New Issue
Block a user