Access to current timer from callback and to time value

r2585 | dcl | 2008-05-29 14:47:52 +1000 (Thu, 29 May 2008) | 2 lines
This commit is contained in:
Douglas Clowes
2008-05-29 14:47:52 +10:00
parent 63cd8f3ea3
commit c12595c6a4
2 changed files with 33 additions and 4 deletions

View File

@@ -65,10 +65,13 @@ 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 int tick; /* millisecond repeat rate */ int msec; /* millisecond delay time */
int tick; /* millisecond repeat rate */
long int vrfy; /* integrity check */ long int vrfy; /* integrity check */
} NWTimer; } NWTimer;
static pNWTimer activeTimer = NULL;
/* /*
* \brief private function to insert an entry into the sorted timer queue. * \brief private function to insert an entry into the sorted timer queue.
* *
@@ -173,6 +176,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
pNew->tv.tv_sec ++; pNew->tv.tv_sec ++;
pNew->tv.tv_usec -= 1000000; pNew->tv.tv_usec -= 1000000;
} }
pNew->msec = mSec;
pNew->tick = 0; pNew->tick = 0;
pNew->func = callback; pNew->func = callback;
pNew->cntx = context; pNew->cntx = context;
@@ -196,16 +200,33 @@ int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPer
return 0; return 0;
} }
pNWTimer NetWatchGetActiveTimer(void)
{
return activeTimer;
}
int NetWatchGetTimerInitial(pNWTimer handle)
{
if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0;
return handle->msec;
}
int NetWatchGetTimerDelay(pNWTimer handle)
{
return NetWatchGetTimerInitial(handle);
}
int NetWatchGetTimerPeriod(pNWTimer handle) int NetWatchGetTimerPeriod(pNWTimer handle)
{ {
if (handle == NULL || handle->vrfy != NWMAGIC) if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0; return 0;
return handle->tick; return handle->tick;
} }
int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod) int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod)
{ {
if (handle == NULL || handle->vrfy != NWMAGIC) if (handle == NULL || (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0; return 0;
handle->tick = mSecPeriod; handle->tick = mSecPeriod;
return 1; return 1;
@@ -214,7 +235,7 @@ int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod)
int NetWatchRemoveTimer(pNWTimer handle) int NetWatchRemoveTimer(pNWTimer handle)
{ {
pNetWatch self = instance; pNetWatch self = instance;
if (!self || self->lMagic != NWMAGIC)\ if (!self || self->lMagic != NWMAGIC)
return 0; return 0;
if (handle == NULL || handle->vrfy != NWMAGIC) if (handle == NULL || handle->vrfy != NWMAGIC)
return 0; return 0;
@@ -447,7 +468,11 @@ int NetWatchTask (void* pData)
break; break;
} }
NetWatchTimerRemQue(self, pNew); NetWatchTimerRemQue(self, pNew);
activeTimer = pNew;
activeTimer->vrfy = ~NWMAGIC;
iStatus = pNew->func(pNew->cntx, 0); iStatus = pNew->func(pNew->cntx, 0);
activeTimer->vrfy = 0;
activeTimer = NULL;
/* /*
* If this is a recurrent timer and the function * If this is a recurrent timer and the function
* indicates to keep it going, put it back in * indicates to keep it going, put it back in

View File

@@ -52,6 +52,10 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod, int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
pNWCallback callback, void* context); pNWCallback callback, void* context);
pNWTimer NetWatchGetActiveTimer(void);
int NetWatchGetTimerDelay(pNWTimer handle);
int NetWatchGetTimerInitial(pNWTimer handle);
int NetWatchGetTimerPeriod(pNWTimer handle); int NetWatchGetTimerPeriod(pNWTimer handle);
int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod); int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod);
/** /**