Add periodic, or repeating, timer events

r1559 | dcl | 2007-03-01 09:03:07 +1100 (Thu, 01 Mar 2007) | 2 lines
This commit is contained in:
Douglas Clowes
2007-03-01 09:03:07 +11:00
parent e57006789b
commit 029c9cc982
2 changed files with 71 additions and 2 deletions

View File

@@ -64,7 +64,8 @@ typedef struct __netwatchtimer {
struct timeval tv; /* time when event is due */
pNWCallback func; /* function to call */
void* cntx; /* abstract context to pass to callback */
long vrfy; /* integrity check */
long int tick; /* millisecond repeat rate */
long int vrfy; /* integrity check */
} NWTimer;
/*
@@ -159,6 +160,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
pNew->tv.tv_usec %= 1000000;
}
pNew->tick = 0;
pNew->func = callback;
pNew->cntx = context;
pNew->vrfy = NWMAGIC;
@@ -167,6 +169,49 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
return 1;
}
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
pNWCallback callback, void* context)
{
pNetWatch self = instance;
if(!self || self->lMagic != NWMAGIC)
return 0;
pNWTimer pNew = (pNWTimer) malloc(sizeof(NWTimer));
if (pNew == NULL)
return 0;
memset(pNew, 0, sizeof(NWTimer));
gettimeofday(&pNew->tv, NULL);
pNew->tv.tv_usec += 1000 * mSecInitial;
if (pNew->tv.tv_usec > 1000000) {
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
pNew->tv.tv_usec %= 1000000;
}
if (mSecPeriod > 0)
pNew->tick = mSecPeriod;
else
pNew->tick = 0;
pNew->func = callback;
pNew->cntx = context;
pNew->vrfy = NWMAGIC;
NetWatchTimerInsQue(self, pNew);
*handle = pNew;
return 1;
}
int NetWatchGetTimerPeriod(pNWTimer handle)
{
if (handle == NULL || handle->vrfy != NWMAGIC)
return 0;
return handle->tick;
}
int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod)
{
if (handle == NULL || handle->vrfy != NWMAGIC)
return 0;
handle->tick = mSecPeriod;
return 1;
}
int NetWatchRemoveTimer(pNWTimer handle)
{
pNetWatch self = instance;
@@ -358,7 +403,16 @@ int NetWatchTask (void* pData)
}
NetWatchTimerRemQue(self, pNew);
iStatus = pNew->func(pNew->cntx, 0);
free(pNew);
if (pNew->tick && iStatus == 1) {
pNew->tv.tv_usec += 1000 * pNew->tick;
if (pNew->tv.tv_usec > 1000000) {
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
pNew->tv.tv_usec %= 1000000;
}
NetWatchTimerInsQue(self, pNew);
}
else
free(pNew);
}
}