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);
}
}

View File

@@ -39,6 +39,21 @@ typedef struct __netwatchtimer *pNWTimer;
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
pNWCallback callback, void* context);
/**
* \brief register a periodic timer
*
* \param handle pointer to location to receive the timer object handle
* \param mSec milliseconds after which the timer should expire
* \param mSecPeriod milliseconds after which the timer should repeat
* \param callback function when timer expires
* \param context abstract context passed to callback function
* \return success=1, failure=0
*/
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
pNWCallback callback, void* context);
int NetWatchGetTimerPeriod(pNWTimer handle);
int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod);
/**
* \brief remove a registered timer event
*