Commit 23121b49 authored by massiot's avatar massiot

* util.c: Use a monotonic clock for mdate() and msleep().


git-svn-id: svn://svn.videolan.org/dvblast/trunk@84 55d3f8b6-4a41-4d2d-a900-313d1436a5b8
parent 953a7295
......@@ -4,6 +4,7 @@
CFLAGS += -Wall -O3 -fomit-frame-pointer
CFLAGS += -g
CFLAGS += -I/usr/src/kernel/linux-2.6.29.1/include
LDFLAGS += -lrt
LDFLAGS_DVBLAST += -ldvbpsi -lpthread
OBJ_DVBLAST = dvblast.o util.o dvb.o udp.o demux.o output.o en50221.o comm.o
......@@ -14,7 +15,7 @@ all: dvblast dvblastctl
$(OBJ_DVBLAST) $(OBJ_DVBLASTCTL): Makefile dvblast.h en50221.h comm.h version.h
dvblast: $(OBJ_DVBLAST)
$(CC) -o $@ $(OBJ_DVBLAST) $(LDFLAGS_DVBLAST)
$(CC) -o $@ $(OBJ_DVBLAST) $(LDFLAGS_DVBLAST) $(LDFLAGS)
dvblastctl: $(OBJ_DVBLASTCTL)
......
......@@ -31,9 +31,12 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include "dvblast.h"
#define HAVE_CLOCK_NANOSLEEP
/*****************************************************************************
* Local declarations
*****************************************************************************/
......@@ -121,6 +124,17 @@ void msg_Raw( void *_unused, const char *psz_format, ... )
*****************************************************************************/
mtime_t mdate( void )
{
#if defined (HAVE_CLOCK_NANOSLEEP)
struct timespec ts;
/* Try to use POSIX monotonic clock if available */
if( clock_gettime( CLOCK_MONOTONIC, &ts ) == EINVAL )
/* Run-time fallback to real-time clock (always available) */
(void)clock_gettime( CLOCK_REALTIME, &ts );
return ((mtime_t)ts.tv_sec * (mtime_t)1000000)
+ (mtime_t)(ts.tv_nsec / 1000);
#else
struct timeval tv_date;
/* gettimeofday() could return an error, and should be tested. However, the
......@@ -128,6 +142,7 @@ mtime_t mdate( void )
* here, since tv is a local variable. */
gettimeofday( &tv_date, NULL );
return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
#endif
}
/*****************************************************************************
......@@ -135,12 +150,22 @@ mtime_t mdate( void )
*****************************************************************************/
void msleep( mtime_t delay )
{
struct timespec ts_delay;
ts_delay.tv_sec = delay / 1000000;
ts_delay.tv_nsec = (delay % 1000000) * 1000;
nanosleep( &ts_delay, NULL );
struct timespec ts;
ts.tv_sec = delay / 1000000;
ts.tv_nsec = (delay % 1000000) * 1000;
#if defined( HAVE_CLOCK_NANOSLEEP )
int val;
while ( ( val = clock_nanosleep( CLOCK_MONOTONIC, 0, &ts, &ts ) ) == EINTR );
if( val == EINVAL )
{
ts.tv_sec = delay / 1000000;
ts.tv_nsec = (delay % 1000000) * 1000;
while ( clock_nanosleep( CLOCK_REALTIME, 0, &ts, &ts ) == EINTR );
}
#else
while ( nanosleep( &ts, &ts ) && errno == EINTR );
#endif
}
/*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment