- move DoubleTime to nserver.c

- extend wait command to fraction of seconds
This commit is contained in:
zolliker
2010-01-27 13:37:06 +00:00
parent cbcdd26fbc
commit 7976b52212
2 changed files with 21 additions and 10 deletions

View File

@ -20,6 +20,7 @@
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <sys/time.h>
#include "sics.h" #include "sics.h"
#include "network.h" #include "network.h"
#include "ifile.h" #include "ifile.h"
@ -377,13 +378,12 @@ void RunServer(pServer self)
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
typedef struct { typedef struct {
time_t tFinish; double dFinish;
int iEnd; int iEnd;
} WaitStruct, *pWaitStruct; } WaitStruct, *pWaitStruct;
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
static int WaitTask(void *pData) static int WaitTask(void *pData)
{ {
time_t tNow;
pWaitStruct self = NULL; pWaitStruct self = NULL;
self = (pWaitStruct) pData; self = (pWaitStruct) pData;
@ -391,8 +391,7 @@ static int WaitTask(void *pData)
return 0; return 0;
} }
tNow = time(NULL); if (DoubleTime() >= self->dFinish) {
if (tNow >= self->tFinish) {
return 0; /* done */ return 0; /* done */
} else { } else {
return 1; return 1;
@ -428,7 +427,6 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData,
char pBueffel[80]; char pBueffel[80];
float fVal; float fVal;
Status eOld; Status eOld;
time_t tNow;
WaitStruct sWait; WaitStruct sWait;
pTaskMan pTask; pTaskMan pTask;
long lID; long lID;
@ -460,8 +458,7 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData,
eOld = GetStatus(); eOld = GetStatus();
SetStatus(eUserWait); SetStatus(eUserWait);
tNow = time(NULL); sWait.dFinish = DoubleTime() + (double)fVal;
sWait.tFinish = tNow + (time_t) fVal;
sWait.iEnd = 0; sWait.iEnd = 0;
lID = TaskRegister(pTask, WaitTask, WaitSignal, NULL, &sWait, 1); lID = TaskRegister(pTask, WaitTask, WaitSignal, NULL, &sWait, 1);
TaskWait(pTask, lID); TaskWait(pTask, lID);
@ -478,7 +475,6 @@ int SicsWaitOld(long lTime)
{ {
WaitStruct sWait; WaitStruct sWait;
pTaskMan pTasker = NULL; pTaskMan pTasker = NULL;
time_t tNow;
long lID; long lID;
if (pServ->simMode) { if (pServ->simMode) {
@ -486,8 +482,7 @@ int SicsWaitOld(long lTime)
} }
pTasker = GetTasker(); pTasker = GetTasker();
tNow = time(NULL); sWait.dFinish = DoubleTime() + lTime;
sWait.tFinish = tNow + lTime;
sWait.iEnd = 0; sWait.iEnd = 0;
lID = TaskRegister(pTasker, WaitTask, WaitSignal, NULL, &sWait, 1); lID = TaskRegister(pTasker, WaitTask, WaitSignal, NULL, &sWait, 1);
TaskWait(pTasker, lID); TaskWait(pTasker, lID);
@ -530,3 +525,15 @@ int ServerIsStarting(pServer self)
{ {
return self->pReader == NULL; 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;
}

View File

@ -51,4 +51,8 @@ int UserWait(SConnection * pCon, SicsInterp * pSics, void *pData,
int SicsWait(long lTime); int SicsWait(long lTime);
int ServerIsStarting(pServer self); int ServerIsStarting(pServer self);
double DoubleTime(void);
/* returns time since epoch in seconds
the resultion is machine dependent, but mostly better than msec */
#endif #endif