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:
56
nwatch.c
56
nwatch.c
@@ -64,7 +64,8 @@ typedef struct __netwatchtimer {
|
|||||||
struct timeval tv; /* time when event is due */
|
struct timeval tv; /* time when event is due */
|
||||||
pNWCallback func; /* function to call */
|
pNWCallback func; /* function to call */
|
||||||
void* cntx; /* abstract context to pass to callback */
|
void* cntx; /* abstract context to pass to callback */
|
||||||
long vrfy; /* integrity check */
|
long int tick; /* millisecond repeat rate */
|
||||||
|
long int vrfy; /* integrity check */
|
||||||
} NWTimer;
|
} NWTimer;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -159,6 +160,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
|||||||
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
|
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
|
||||||
pNew->tv.tv_usec %= 1000000;
|
pNew->tv.tv_usec %= 1000000;
|
||||||
}
|
}
|
||||||
|
pNew->tick = 0;
|
||||||
pNew->func = callback;
|
pNew->func = callback;
|
||||||
pNew->cntx = context;
|
pNew->cntx = context;
|
||||||
pNew->vrfy = NWMAGIC;
|
pNew->vrfy = NWMAGIC;
|
||||||
@@ -167,6 +169,49 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
|||||||
return 1;
|
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)
|
int NetWatchRemoveTimer(pNWTimer handle)
|
||||||
{
|
{
|
||||||
pNetWatch self = instance;
|
pNetWatch self = instance;
|
||||||
@@ -358,6 +403,15 @@ int NetWatchTask (void* pData)
|
|||||||
}
|
}
|
||||||
NetWatchTimerRemQue(self, pNew);
|
NetWatchTimerRemQue(self, pNew);
|
||||||
iStatus = pNew->func(pNew->cntx, 0);
|
iStatus = pNew->func(pNew->cntx, 0);
|
||||||
|
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);
|
free(pNew);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
nwatch.h
15
nwatch.h
@@ -39,6 +39,21 @@ typedef struct __netwatchtimer *pNWTimer;
|
|||||||
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
||||||
pNWCallback callback, void* context);
|
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
|
* \brief remove a registered timer event
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user