epicsAutoMutex has a new name, fixed free lists, and allow timer to be deleted during expire

This commit is contained in:
Jeff Hill
2002-03-21 22:45:32 +00:00
parent e66da4714b
commit fec6ba2dbf
6 changed files with 220 additions and 196 deletions

View File

@@ -30,18 +30,10 @@
*/
#define epicsExportSharedSymbols
#include "epicsGuard.h"
#include "timerPrivate.h"
#if defined ( _MSC_VER )
# pragma warning ( push )
# pragma warning ( disable: 4660 )
#endif
template class tsFreeList < timer, 32, 0 >;
#if defined ( _MSC_VER )
# pragma warning ( pop )
#endif
epicsSingleton < tsFreeList < timer, 0x20 > > timer::pFreeList;
timer::timer ( timerQueue &queueIn ) :
curState ( stateLimbo ), pNotify ( 0 ), queue ( queueIn )
@@ -55,9 +47,7 @@ timer::~timer ()
void timer::destroy ()
{
this->~timer ();
epicsAutoMutex autoLock ( this->queue.mutex );
this->queue.timerFreeList.release ( this, sizeof( *this ) );
delete this;
}
void timer::start ( epicsTimerNotify & notify, double delaySeconds )
@@ -67,7 +57,7 @@ void timer::start ( epicsTimerNotify & notify, double delaySeconds )
void timer::start ( epicsTimerNotify & notify, const epicsTime & expire )
{
epicsAutoMutex locker ( this->queue.mutex );
epicsGuard < epicsMutex > locker ( this->queue.mutex );
this->privateCancel ( locker );
this->privateStart ( notify, expire );
}
@@ -126,46 +116,44 @@ void timer::privateStart ( epicsTimerNotify & notify, const epicsTime & expire )
void timer::cancel ()
{
epicsAutoMutex locker ( this->queue.mutex );
this->privateCancel ( locker );
if ( this->curState == statePending || this->curState == stateActive ) {
epicsGuard < epicsMutex > locker ( this->queue.mutex );
this->privateCancel ( locker );
}
}
void timer::privateCancel ( epicsAutoMutex & locker )
void timer::privateCancel ( epicsGuard < epicsMutex > & locker )
{
while ( true ) {
if ( this->curState == statePending ) {
if ( this->queue.timerList.first() == this ) {
this->queue.notify.reschedule ();
}
this->queue.timerList.remove ( *this );
this->curState = stateLimbo;
this->pNotify = 0;
if ( this->curState == statePending ) {
if ( this->queue.timerList.first() == this ) {
this->queue.notify.reschedule ();
}
if ( this->queue.pExpireTmr == this &&
this->queue.processThread != epicsThreadGetIdSelf() ) {
this->queue.cancelPending = true;
this->queue.timerList.remove ( *this );
this->curState = stateLimbo;
this->pNotify = 0;
}
else if ( this->curState == stateActive ) {
this->queue.cancelPending = true;
this->curState = timer::stateLimbo;
if ( this->queue.processThread != epicsThreadGetIdSelf() ) {
// make certain timer expire() does not run after cancel () returns,
// but dont require that lock is applied while calling expire()
{
epicsAutoMutexRelease autoRelease ( locker );
while ( this->queue.cancelPending ) {
epicsGuardRelease < epicsMutex > autoRelease ( locker );
while ( this->queue.cancelPending &&
this->queue.pExpireTmr == this ) {
this->queue.cancelBlockingEvent.wait ();
}
// in case other threads are waiting
this->queue.cancelBlockingEvent.signal ();
}
}
else {
// dont wait if this was called indirectly by expire ()
return;
}
}
}
epicsTimer::expireInfo timer::getExpireInfo () const
{
epicsAutoMutex locker ( this->queue.mutex );
if ( this->curState == statePending || this->queue.pExpireTmr == this ) {
if ( this->curState == statePending || this->curState == stateActive ) {
return expireInfo ( true, this->exp );
}
return expireInfo ( false, epicsTime() );
@@ -173,21 +161,34 @@ epicsTimer::expireInfo timer::getExpireInfo () const
void timer::show ( unsigned int level ) const
{
epicsAutoMutex locker ( this->queue.mutex );
epicsGuard < epicsMutex > locker ( this->queue.mutex );
const char * pName = "<no notify attached>";
const char *pStateName;
if ( this->pNotify ) {
pName = typeid ( *this->pNotify ).name ();
}
double delay;
if ( this->curState == statePending ) {
if ( this->curState == statePending || this->curState == stateActive ) {
delay = this->exp - epicsTime::getCurrent();
}
else {
delay = -DBL_MAX;
}
if ( this->curState == statePending ) {
pStateName = "pending";
}
else if ( this->curState == stateActive ) {
pStateName = "active";
}
else if ( this->curState == stateLimbo ) {
pStateName = "limbo";
}
else {
pStateName = "corrupt";
}
printf ( "%s, state = %s, delay = %f\n",
pName, this->curState == statePending ? "pending" : "limbo",
delay );
pName, pStateName, delay );
if ( level >= 1u && this->pNotify ) {
this->pNotify->show ( level - 1u );
}

View File

@@ -32,6 +32,7 @@
#define epicsTimerPrivate_h
#include "tsFreeList.h"
#include "epicsSingleton.h"
#include "tsDLList.h"
#include "epicsTimer.h"
@@ -41,6 +42,8 @@
# define debugPrintf(ARGSINPAREN)
#endif
template < class T > class epicsGuard;
class timer : public epicsTimer, public tsDLNode < timer > {
public:
void destroy ();
@@ -50,17 +53,21 @@ public:
expireInfo getExpireInfo () const;
void show ( unsigned int level ) const;
class timerQueue & getPrivTimerQueue ();
void * operator new ( size_t size );
void operator delete ( void *pCadaver, size_t size );
protected:
timer ( class timerQueue & );
~timer ();
private:
enum state { statePending = 45, stateLimbo = 78 };
enum state { statePending = 45, stateActive = 56, stateLimbo = 78 };
epicsTime exp; // experation time
state curState; // current state
epicsTimerNotify *pNotify; // callback
timerQueue &queue;
epicsTimerNotify * pNotify; // callback
timerQueue & queue;
static epicsSingleton < tsFreeList < timer, 0x20 > > pFreeList;
void privateStart ( epicsTimerNotify & notify, const epicsTime & );
void privateCancel ( epicsAutoMutex & );
void privateCancel ( epicsGuard < epicsMutex > & );
timer & operator = ( const timer & );
friend class timerQueue;
};
@@ -71,9 +78,12 @@ public:
protected:
epicsTimerForC ( timerQueue &, epicsTimerCallback, void *pPrivateIn );
~epicsTimerForC ();
void * operator new ( size_t size );
void operator delete ( void *pCadaver, size_t size );
private:
epicsTimerCallback pCallBack;
void * pPrivate;
static epicsSingleton < tsFreeList < epicsTimerForC, 0x20 > > pFreeList;
expireStatus expire ( const epicsTime & currentTime );
epicsTimerForC & operator = ( const epicsTimerForC & );
friend class timerQueue;
@@ -83,15 +93,12 @@ class timerQueue : public epicsTimerQueue {
public:
timerQueue ( epicsTimerQueueNotify &notify );
virtual ~timerQueue ();
double process ( const epicsTime & currentTime );
epicsTimer & createTimer ();
epicsTimerForC & createTimerForC ( epicsTimerCallback pCallback, void *pArg );
double process ( const epicsTime & currentTime );
void show ( unsigned int level ) const;
epicsTimerForC & createTimerForC ( epicsTimerCallback, void *pPrivateIn );
void destroyTimerForC ( epicsTimerForC & );
private:
mutable epicsMutex mutex;
tsFreeList < class timer, 0x20 > timerFreeList;
tsFreeList < epicsTimerForC, 0x20 > cTimerfreeList;
epicsEvent cancelBlockingEvent;
tsDLList < timer > timerList;
epicsTimerQueueNotify &notify;
@@ -120,11 +127,10 @@ public:
timerQueueActive ( bool okToShare, unsigned priority );
virtual ~timerQueueActive () = 0;
epicsTimer & createTimer ();
epicsTimerForC & createTimerForC ( epicsTimerCallback pCallback, void *pArg );
void show ( unsigned int level ) const;
bool sharingOK () const;
unsigned threadPriority () const;
epicsTimerForC & createTimerForC ( epicsTimerCallback, void *pPrivateIn );
void destroyTimerForC ( epicsTimerForC & );
private:
timerQueue queue;
epicsEvent rescheduleEvent;
@@ -140,22 +146,6 @@ private:
timerQueueActive & operator = ( const timerQueueActive & );
};
struct epicsTimerQueueActiveForC : public timerQueueActive,
public tsDLNode < epicsTimerQueueActiveForC > {
public:
epicsTimerQueueActiveForC ( bool okToShare, unsigned priority );
void release ();
void * operator new ( size_t size );
void operator delete ( void *pCadaver, size_t size );
protected:
virtual ~epicsTimerQueueActiveForC ();
private:
static tsFreeList < epicsTimerQueueActiveForC > freeList;
static epicsMutex freeListMutex;
epicsTimerQueueActiveForC ( const epicsTimerQueueActiveForC & );
epicsTimerQueueActiveForC & operator = ( const epicsTimerQueueActiveForC & );
};
class timerQueueActiveMgr {
public:
timerQueueActiveMgr ();
@@ -176,10 +166,9 @@ class timerQueuePassive : public epicsTimerQueuePassive {
public:
timerQueuePassive ( epicsTimerQueueNotify & );
epicsTimer & createTimer ();
epicsTimerForC & createTimerForC ( epicsTimerCallback pCallback, void *pArg );
void show ( unsigned int level ) const;
double process ( const epicsTime & currentTime );
epicsTimerForC & createTimerForC ( epicsTimerCallback, void *pPrivateIn );
void destroyTimerForC ( epicsTimerForC & );
protected:
timerQueue queue;
~timerQueuePassive ();
@@ -188,22 +177,35 @@ protected:
timerQueuePassive & operator = ( const timerQueuePassive & );
};
inline epicsTimerForC & timerQueue::createTimerForC
( epicsTimerCallback pCB, void *pPriv )
{
epicsAutoMutex autoLock ( this->mutex );
void *pBuf = this->cTimerfreeList.allocate ( sizeof (epicsTimerForC) );
if ( ! pBuf ) {
throw std::bad_alloc();
}
return * new ( pBuf ) epicsTimerForC ( *this, pCB, pPriv );
}
struct epicsTimerQueuePassiveForC : public epicsTimerQueueNotify, public timerQueuePassive {
public:
epicsTimerQueuePassiveForC ( epicsTimerQueueRescheduleCallback pCallback, void *pPrivate );
void destroy ();
void * operator new ( size_t size );
void operator delete ( void *pCadaver, size_t size );
protected:
virtual ~epicsTimerQueuePassiveForC ();
private:
epicsTimerQueueRescheduleCallback pCallback;
void *pPrivate;
static epicsSingleton < tsFreeList < epicsTimerQueuePassiveForC, 0x10 > > pFreeList;
void reschedule ();
};
inline void timerQueue::destroyTimerForC ( epicsTimerForC & tmr )
{
tmr.~epicsTimerForC ();
this->cTimerfreeList.release ( &tmr, sizeof ( tmr ) );
}
struct epicsTimerQueueActiveForC : public timerQueueActive,
public tsDLNode < epicsTimerQueueActiveForC > {
public:
epicsTimerQueueActiveForC ( bool okToShare, unsigned priority );
void release ();
void * operator new ( size_t size );
void operator delete ( void *pCadaver, size_t size );
protected:
virtual ~epicsTimerQueueActiveForC ();
private:
static epicsSingleton < tsFreeList < epicsTimerQueueActiveForC, 0x10 > > pFreeList;
epicsTimerQueueActiveForC ( const epicsTimerQueueActiveForC & );
epicsTimerQueueActiveForC & operator = ( const epicsTimerQueueActiveForC & );
};
inline bool timerQueueActive::sharingOK () const
{
@@ -215,16 +217,44 @@ inline unsigned timerQueueActive::threadPriority () const
return thread.getPriority ();
}
inline void * epicsTimerQueueActiveForC::operator new ( size_t size )
inline void * timer::operator new ( size_t size )
{
return timer::pFreeList->allocate ( size );
}
inline void timer::operator delete ( void *pCadaver, size_t size )
{
timer::pFreeList->release ( pCadaver, size );
}
inline void * epicsTimerForC::operator new ( size_t size )
{
return epicsTimerForC::pFreeList->allocate ( size );
}
inline void epicsTimerForC::operator delete ( void *pCadaver, size_t size )
{
epicsTimerForC::pFreeList->release ( pCadaver, size );
}
inline void * epicsTimerQueuePassiveForC::operator new ( size_t size )
{
epicsAutoMutex locker ( epicsTimerQueueActiveForC::freeListMutex );
return epicsTimerQueueActiveForC::freeList.allocate ( size );
return epicsTimerQueuePassiveForC::pFreeList->allocate ( size );
}
inline void epicsTimerQueuePassiveForC::operator delete ( void *pCadaver, size_t size )
{
epicsTimerQueuePassiveForC::pFreeList->release ( pCadaver, size );
}
inline void * epicsTimerQueueActiveForC::operator new ( size_t size )
{
return epicsTimerQueueActiveForC::pFreeList->allocate ( size );
}
inline void epicsTimerQueueActiveForC::operator delete ( void *pCadaver, size_t size )
{
epicsAutoMutex locker ( epicsTimerQueueActiveForC::freeListMutex );
epicsTimerQueueActiveForC::freeList.release ( pCadaver, size );
{
epicsTimerQueueActiveForC::pFreeList->release ( pCadaver, size );
}
#endif // epicsTimerPrivate_h

View File

@@ -29,6 +29,7 @@
*/
#define epicsExportSharedSymbols
#include "epicsGuard.h"
#include "timerPrivate.h"
epicsTimerQueue::~epicsTimerQueue () {}
@@ -50,98 +51,104 @@ timerQueue::~timerQueue ()
double timerQueue::process ( const epicsTime & currentTime )
{
{
epicsAutoMutex locker ( this->mutex );
epicsGuard < epicsMutex > guard ( this->mutex );
if ( this->pExpireTmr ) {
// if some other thread is processing the queue
// (or if this is a recursive call)
timer *pTmr = this->timerList.first ();
if ( pTmr ) {
double delay = pTmr->exp - currentTime;
if ( delay < 0.0 ) {
delay = 0.0;
}
return delay;
}
else {
return DBL_MAX;
}
}
//
// Tag current epired tmr so that we can detect if call back
// is in progress when canceling the timer.
//
this->pExpireTmr = this->timerList.first ();
if ( this->pExpireTmr ) {
if ( currentTime >= this->pExpireTmr->exp ) {
this->timerList.remove ( *this->pExpireTmr );
this->pExpireTmr->curState = timer::stateLimbo;
this->processThread = epicsThreadGetIdSelf ();
# ifdef DEBUG
this->pExpireTmr->show ( 0u );
# endif
}
else {
double delay = this->pExpireTmr->exp - currentTime;
this->pExpireTmr = 0;
debugPrintf ( ( "no activity process %f to next\n", delay ) );
return delay;
if ( this->pExpireTmr ) {
// if some other thread is processing the queue
// (or if this is a recursive call)
timer *pTmr = this->timerList.first ();
if ( pTmr ) {
double delay = pTmr->exp - currentTime;
if ( delay < 0.0 ) {
delay = 0.0;
}
return delay;
}
else {
return DBL_MAX;
}
}
//
// Tag current epired tmr so that we can detect if call back
// is in progress when canceling the timer.
//
if ( this->timerList.first () ) {
if ( currentTime >= this->timerList.first ()->exp ) {
this->pExpireTmr = this->timerList.first ();
this->timerList.remove ( *this->pExpireTmr );
this->pExpireTmr->curState = timer::stateActive;
this->processThread = epicsThreadGetIdSelf ();
# ifdef DEBUG
this->pExpireTmr->show ( 0u );
# endif
}
else {
double delay = this->timerList.first ()->exp - currentTime;
debugPrintf ( ( "no activity process %f to next\n", delay ) );
return delay;
}
}
else {
return DBL_MAX;
}
# ifdef DEBUG
unsigned N = 0u;
# endif
while ( true ) {
debugPrintf ( ( "%5u expired \"%s\" with error %f sec\n",
N++, typeid ( this->pExpireTmr->notify ).name (),
currentTime - this->pExpireTmr->exp ) );
epicsTimerNotify *pTmpNotify = this->pExpireTmr->pNotify;
this->pExpireTmr->pNotify = 0;
epicsTimerNotify::expireStatus expStat ( epicsTimerNotify::noRestart );
epicsTimerNotify::expireStatus expStat =
this->pExpireTmr->pNotify->expire ( currentTime );
{
epicsGuardRelease < epicsMutex > unguard ( guard );
epicsAutoMutex locker ( this->mutex );
debugPrintf ( ( "%5u expired \"%s\" with error %f sec\n",
N++, typeid ( this->pExpireTmr->notify ).name (),
currentTime - this->pExpireTmr->exp ) );
//
// only restart if they didnt cancel() the timer
// while the call back was running
//
if ( this->cancelPending ) {
// cancel() waits for this
this->cancelPending = false;
this->cancelBlockingEvent.signal ();
this->pExpireTmr->pNotify = 0;
}
// restart as nec
else if ( expStat.restart() ) {
this->pExpireTmr->privateStart (
*this->pExpireTmr->pNotify,
currentTime + expStat.expirationDelay() );
}
else {
this->pExpireTmr->pNotify = 0;
expStat = pTmpNotify->expire ( currentTime );
}
this->pExpireTmr = this->timerList.first ();
if ( this->pExpireTmr ) {
if ( currentTime >= this->pExpireTmr->exp ) {
this->timerList.remove ( *this->pExpireTmr );
{
//
// only restart if they didnt cancel() the timer
// while the call back was running
//
if ( this->cancelPending ) {
// 1) if another thread is canceling cancel() waits for this
// 2) if this thread is canceling in the timer callback then
// dont touch timer or notify here because the cancel might
// have occurred because they destroyed the time rin the
// callback
this->cancelPending = false;
this->cancelBlockingEvent.signal ();
}
// restart as nec
else {
this->pExpireTmr->curState = timer::stateLimbo;
if ( expStat.restart() ) {
this->pExpireTmr->privateStart (
*pTmpNotify, currentTime + expStat.expirationDelay() );
}
}
this->pExpireTmr = 0;
}
if ( this->timerList.first () ) {
if ( currentTime >= this->timerList.first ()->exp ) {
this->pExpireTmr = this->timerList.first ();
this->timerList.remove ( *this->pExpireTmr );
this->pExpireTmr->curState = timer::stateActive;
# ifdef DEBUG
this->pExpireTmr->show ( 0u );
# endif
}
else {
double delay = this->pExpireTmr->exp - currentTime;
this->pExpireTmr = 0;
double delay = this->timerList.first ()->exp - currentTime;
this->processThread = 0;
return delay;
}
@@ -156,17 +163,18 @@ double timerQueue::process ( const epicsTime & currentTime )
epicsTimer & timerQueue::createTimer ()
{
epicsAutoMutex autoLock ( this->mutex );
void *pBuf = this->timerFreeList.allocate ( sizeof (timer) );
if ( ! pBuf ) {
throw std::bad_alloc();
}
return * new ( pBuf ) timer ( *this );
return * new timer ( * this );
}
epicsTimerForC & timerQueue::createTimerForC ( epicsTimerCallback pCallback, void *pArg )
{
return * new epicsTimerForC ( *this, pCallback, pArg );
}
void timerQueue::show ( unsigned level ) const
{
epicsAutoMutex locker ( this->mutex );
epicsGuard < epicsMutex > locker ( this->mutex );
printf ( "epicsTimerQueue with %u items pending\n", this->timerList.count () );
if ( level >= 1u ) {
tsDLIterConstBD < timer > iter = this->timerList.firstIter ();

View File

@@ -73,17 +73,12 @@ void timerQueueActive::run ()
epicsTimer & timerQueueActive::createTimer ()
{
return this->queue.createTimer ();
return this->queue.createTimer();
}
epicsTimerForC & timerQueueActive::createTimerForC ( epicsTimerCallback pCB, void *pPrivateIn )
epicsTimerForC & timerQueueActive::createTimerForC ( epicsTimerCallback pCallback, void * pArg )
{
return this->queue.createTimerForC ( pCB, pPrivateIn );
}
void timerQueueActive::destroyTimerForC ( epicsTimerForC &tmr )
{
this->queue.destroyTimerForC ( tmr );
return this->queue.createTimerForC ( pCallback, pArg );
}
void timerQueueActive::reschedule ()

View File

@@ -31,6 +31,7 @@
#include <limits.h>
#define epicsExportSharedSymbols
#include "epicsGuard.h"
#include "timerPrivate.h"
timerQueueActiveMgr::timerQueueActiveMgr ()
@@ -39,13 +40,13 @@ timerQueueActiveMgr::timerQueueActiveMgr ()
timerQueueActiveMgr::~timerQueueActiveMgr ()
{
epicsAutoMutex locker ( this->mutex );
epicsGuard < epicsMutex > locker ( this->mutex );
}
epicsTimerQueueActiveForC & timerQueueActiveMgr::allocate (
bool okToShare, unsigned threadPriority )
{
epicsAutoMutex locker ( this->mutex );
epicsGuard < epicsMutex > locker ( this->mutex );
if ( okToShare ) {
tsDLIterBD < epicsTimerQueueActiveForC > iter = this->sharedQueueList.firstIter ();
while ( iter.valid () ) {
@@ -56,20 +57,18 @@ epicsTimerQueueActiveForC & timerQueueActiveMgr::allocate (
}
}
}
epicsTimerQueueActiveForC *pQueue = new epicsTimerQueueActiveForC ( okToShare, threadPriority );
if ( ! pQueue ) {
throw std::bad_alloc ();
}
pQueue->timerQueueActiveMgrPrivate::referenceCount = 1u;
epicsTimerQueueActiveForC & queue = * new epicsTimerQueueActiveForC ( okToShare, threadPriority );
queue.timerQueueActiveMgrPrivate::referenceCount = 1u;
if ( okToShare ) {
this->sharedQueueList.add ( *pQueue );
this->sharedQueueList.add ( queue );
}
return *pQueue;
return queue;
}
void timerQueueActiveMgr::release ( epicsTimerQueueActiveForC &queue )
{
epicsAutoMutex locker ( this->mutex );
epicsGuard < epicsMutex > locker ( this->mutex );
assert ( queue.timerQueueActiveMgrPrivate::referenceCount > 0u );
queue.timerQueueActiveMgrPrivate::referenceCount--;
if ( queue.timerQueueActiveMgrPrivate::referenceCount == 0u ) {

View File

@@ -41,13 +41,9 @@
epicsTimerQueuePassive::~epicsTimerQueuePassive () {}
epicsTimerQueuePassive &epicsTimerQueuePassive::create ( epicsTimerQueueNotify &notify )
epicsTimerQueuePassive & epicsTimerQueuePassive::create ( epicsTimerQueueNotify &notify )
{
timerQueuePassive *pQueue = new timerQueuePassive ( notify );
if ( ! pQueue ) {
throw std::bad_alloc ();
}
return *pQueue;
return * new timerQueuePassive ( notify );
}
timerQueuePassive::timerQueuePassive ( epicsTimerQueueNotify &notifyIn ) :
@@ -60,14 +56,9 @@ epicsTimer & timerQueuePassive::createTimer ()
return this->queue.createTimer ();
}
epicsTimerForC & timerQueuePassive::createTimerForC ( epicsTimerCallback pCB, void *pPrivateIn )
epicsTimerForC & timerQueuePassive::createTimerForC ( epicsTimerCallback pCallback, void * pArg )
{
return this->queue.createTimerForC ( pCB, pPrivateIn );
}
void timerQueuePassive::destroyTimerForC ( epicsTimerForC &tmr )
{
this->queue.destroyTimerForC ( tmr );
return this->queue.createTimerForC ( pCallback, pArg );
}
double timerQueuePassive::process ( const epicsTime & currentTime )