diff --git a/nserver.c b/nserver.c index 4e0ecb84..9b32a07a 100644 --- a/nserver.c +++ b/nserver.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "sics.h" #include "network.h" #include "ifile.h" @@ -377,13 +378,12 @@ void RunServer(pServer self) /*------------------------------------------------------------------------*/ typedef struct { - time_t tFinish; + double dFinish; int iEnd; } WaitStruct, *pWaitStruct; /*-------------------------------------------------------------------------*/ static int WaitTask(void *pData) { - time_t tNow; pWaitStruct self = NULL; self = (pWaitStruct) pData; @@ -391,8 +391,7 @@ static int WaitTask(void *pData) return 0; } - tNow = time(NULL); - if (tNow >= self->tFinish) { + if (DoubleTime() >= self->dFinish) { return 0; /* done */ } else { return 1; @@ -428,7 +427,6 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData, char pBueffel[80]; float fVal; Status eOld; - time_t tNow; WaitStruct sWait; pTaskMan pTask; long lID; @@ -460,8 +458,7 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData, eOld = GetStatus(); SetStatus(eUserWait); - tNow = time(NULL); - sWait.tFinish = tNow + (time_t) fVal; + sWait.dFinish = DoubleTime() + (double)fVal; sWait.iEnd = 0; lID = TaskRegister(pTask, WaitTask, WaitSignal, NULL, &sWait, 1); TaskWait(pTask, lID); @@ -478,7 +475,6 @@ int SicsWaitOld(long lTime) { WaitStruct sWait; pTaskMan pTasker = NULL; - time_t tNow; long lID; if (pServ->simMode) { @@ -486,8 +482,7 @@ int SicsWaitOld(long lTime) } pTasker = GetTasker(); - tNow = time(NULL); - sWait.tFinish = tNow + lTime; + sWait.dFinish = DoubleTime() + lTime; sWait.iEnd = 0; lID = TaskRegister(pTasker, WaitTask, WaitSignal, NULL, &sWait, 1); TaskWait(pTasker, lID); @@ -530,3 +525,15 @@ int ServerIsStarting(pServer self) { return self->pReader == NULL; } +/*-------------------------------------------------------------------------*/ +double DoubleTime(void) +{ + struct timeval now; + /* the resolution of this function is usec, if the machine supports this + and the mantissa of a double is 51 bits or more (31 for sec and 20 for mic$ + */ + gettimeofday(&now, NULL); + return now.tv_sec + now.tv_usec / 1e6; +} + + diff --git a/nserver.h b/nserver.h index 8a9619b1..5baee31f 100644 --- a/nserver.h +++ b/nserver.h @@ -51,4 +51,8 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData, int SicsWait(long lTime); int ServerIsStarting(pServer self); + +double DoubleTime(void); +/* returns time since epoch in seconds + the resultion is machine dependent, but mostly better than msec */ #endif