@ -2,16 +2,21 @@
# include "basicEventHandler.h"
# include <algorithm>
# include <chrono>
# define LOG(X) m_pLog->Log("basicEventHeap", this, X)
# define LOG_ERROR(X) m_pLog->Log("basicEventHeap", this, _LOG_ERROR, X)
basicEventHeap * basicEventHeap : : _instance = 0 ;
std : : mutex basicEventHeap : : _instanceLock ;
/**
* Returns the single instance of the object .
*/
basicEventHeap * basicEventHeap : : getInstance ( )
{
std : : lock_guard < std : : mutex > tLock ( _instanceLock ) ;
if ( 0 = = _instance )
{
_instance = new basicEventHeap ;
@ -19,100 +24,207 @@ basicEventHeap * basicEventHeap::getInstance()
return _instance ;
}
void basicEventHeap : : addHandler ( basicEventHandler * handler , bool atBottom )
basicEventHeap * basicEventHeap : : existingInstance ( )
{
m_pHandlerMutex - > lock ( ) ;
if ( atBottom ) {
m_vHandlers . push_back ( handler ) ;
} else {
m_vHandlers . insert ( m_vHandlers . begin ( ) , handler ) ;
} ;
m_pHandlerMutex - > unlock ( ) ;
std : : lock_guard < std : : mutex > tLock ( _instanceLock ) ;
return _instance ;
}
void basicEventHeap : : removeHandler ( basicEventHandler * handler )
void basicEventHeap : : destroyInstance ( )
{
m_pHandlerMutex - > lock ( ) ;
for ( V_EVT_HANDLER : : iterator idx = m_vHandlers . begin ( ) ; idx ! = m_vHandlers . end ( ) ; idx + + )
if ( existingInstance ( ) )
{
if ( ( * idx ) = = handler )
// A handler stuck in dispatchEvent(): deleting would wait for that thread forever (the
// basicThread destructor joins it) - leave the object to the process exit instead of hanging
if ( ! _instance - > pause ( ) )
{
m_vHandlers . erase ( idx ) ;
break ;
_instance - > m_pLog - > Log ( " basicEventHeap " , _instance , _LOG_ERROR , " Dispatch thread does not stop - not waiting for it " ) ;
return ;
} ;
basicEventHeap * tHeap ;
{
std : : lock_guard < std : : mutex > tLock ( _instanceLock ) ;
tHeap = _instance ;
_instance = 0 ;
}
delete ( tHeap ) ;
} ;
m_pHandlerMutex - > unlock ( ) ;
}
void basicEventHeap : : moveHandlerBottom ( basicEventHandler * handler ) {
m_pHandlerMutex - > lock ( ) ;
for ( V_EVT_HANDLER : : iterator idx = m_vHandlers . begin ( ) ; idx ! = m_vHandlers . end ( ) ; idx + + )
basicEventHeap : : basicEventHeap ( )
{
m_pLog = basicLogCollection : : getInstance ( ) ;
m_pDispatchingMutex = new basicMutex ( ) ;
m_pCurrentHandler = NULL ;
m_bPaused = false ;
m_bInDispatch = false ;
m_tDispatchThread = pthread_t ( ) ;
basicThread : : init ( ) ;
}
basicEventHeap : : ~ basicEventHeap ( )
{
pause ( ) ;
waitThreadEnd ( true ) ;
clearQueue ( ) ;
{
if ( ( * idx ) = = handler )
{
m_vHandlers . erase ( idx ) ;
break ;
} ;
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_vHandlers . clear ( ) ;
}
delete ( m_pDispatchingMutex ) ;
}
bool basicEventHeap : : isDispatchThread ( void )
{
return ( m_bThreadStatus > = TH_WORKING ) & & pthread_equal ( pthread_self ( ) , m_tDispatchThread ) ;
}
bool basicEventHeap : : isRegistered ( basicEventHandler * handler )
{
return std : : find ( m_vHandlers . begin ( ) , m_vHandlers . end ( ) , handler ) ! = m_vHandlers . end ( ) ;
}
void basicEventHeap : : addHandler ( basicEventHandler * handler , bool atBottom )
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
if ( atBottom )
{
m_vHandlers . push_back ( handler ) ;
}
else
{
m_vHandlers . insert ( m_vHandlers . begin ( ) , handler ) ;
} ;
m_vHandlers . push_back ( handler ) ;
m_pHandlerMutex - > unlock ( ) ;
}
void basicEventHeap : : moveHandlerTop ( basicEventHandler * handler ) {
m_pHandlerMutex - > lock ( ) ;
for ( V_EVT_HANDLER : : iterator idx = m_vHandlers . begin ( ) ; idx ! = m_vHandlers . end ( ) ; idx + + )
/*
Called from ~ basicEventHandler ( any thread ) . Before , the dispatch thread held the handler list
locked while calling dispatchEvent ( ) : a handler deleted from another thread ( shutdown : ~ mojoKernel
deleting the node / module factories ) waited for that lock while the dispatch thread called into
the half - destroyed object - the shutdown hung ; a dispatchEvent ( ) that created or deleted a handler
deadlocked the dispatch thread itself ( non - recursive mutex ) .
Now the list is not locked during dispatchEvent ( ) : this waits until the handler is no longer being
called ( unless called from that very call , which cannot be waited for ) .
*/
void basicEventHeap : : removeHandler ( basicEventHandler * handler )
{
std : : unique_lock < std : : mutex > tLock ( m_mHandlers ) ;
V_EVT_HANDLER : : iterator idx = std : : find ( m_vHandlers . begin ( ) , m_vHandlers . end ( ) , handler ) ;
if ( idx ! = m_vHandlers . end ( ) )
{
m_vHandlers . erase ( idx ) ;
} ;
if ( m_pCurrentHandler = = handler & & ! isDispatchThread ( ) )
{
if ( ( * idx ) = = handler )
if ( ! m_cvDispatch . wait_for ( tLock , std : : chrono : : milliseconds ( EVENT_HEAP_WAIT_DISPATCH_MS ) , [ & ] { return m_pCurrentHandler ! = handler ; } ) )
{
m_vHandlers . erase ( idx ) ;
break ;
LOG_ERROR ( " removeHandler: handler still busy in dispatchEvent() - continuing anyway " ) ;
} ;
} ;
m_vHandlers . insert ( m_vHandlers . begin ( ) , handler ) ;
m_pHandlerMutex - > unlock ( ) ;
}
void basicEventHeap : : moveHandlerBottom ( basicEventHandler * handler )
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
V_EVT_HANDLER : : iterator idx = std : : find ( m_vHandlers . begin ( ) , m_vHandlers . end ( ) , handler ) ;
if ( idx ! = m_vHandlers . end ( ) )
{
m_vHandlers . erase ( idx ) ;
} ;
m_vHandlers . push_back ( handler ) ;
}
void basicEventHeap : : moveHandlerTop ( basicEventHandler * handler )
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
V_EVT_HANDLER : : iterator idx = std : : find ( m_vHandlers . begin ( ) , m_vHandlers . end ( ) , handler ) ;
if ( idx ! = m_vHandlers . end ( ) )
{
m_vHandlers . erase ( idx ) ;
} ;
m_vHandlers . insert ( m_vHandlers . begin ( ) , handler ) ;
}
void basicEventHeap : : m_fThreadProc ( void )
{
basicEventHandler * mpHandler = NULL ;
EVENT_CONTAINER * mpEvent = NULL ;
m_bThreadStatus = TH_WORKING ;
pthread_setname_np ( pthread_self ( ) , " EventHeap " ) ;
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_tDispatchThread = pthread_self ( ) ;
}
m_bThreadStatus = TH_WORKING ;
pthread_setname_np ( pthread_self ( ) , " EventHeap " ) ;
while ( m_bThreadStatus < TH_ENDED )
{
if ( m_bThreadStatus = = TH_WORKING )
// Enter a dispatch round only while not paused (pause() waits for m_bInDispatch == false)
V_EVT_HANDLER tHandlers ;
{
mpEvent = NULL ;
m_pDispatchingMutex - > lock ( ) ;
if ( ! m_vEventQueue . empty ( ) )
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
if ( m_bPaused )
{
mpEvent = m_vEventQueue . front ( ) ;
m_vEventQueue . pop ( ) ;
m_bInDispatch = false ;
}
else
{
m_bInDispatch = true ;
tHandlers = m_vHandlers ; // snapshot: the list may change while handlers are called
} ;
m_pDispatchingMutex - > unlock ( ) ;
if ( mpEvent ! = NULL )
}
if ( ! m_bInDispatch )
{
SLEEP ( 1 ) ;
continue ;
} ;
EVENT_CONTAINER * mpEvent = NULL ;
m_pDispatchingMutex - > lock ( ) ;
if ( ! m_vEventQueue . empty ( ) )
{
mpEvent = m_vEventQueue . front ( ) ;
m_vEventQueue . pop ( ) ;
} ;
m_pDispatchingMutex - > unlock ( ) ;
if ( mpEvent ! = NULL )
{
for ( V_EVT_HANDLER : : iterator idx = tHandlers . begin ( ) ; idx ! = tHandlers . end ( ) ; idx + + )
{
m_pHandlerMutex - > lock ( ) ;
for ( V_EVT_HANDLER : : iterator idx = m_vHandlers . begin ( ) ; idx < m_vHandlers . end ( ) ; idx + + )
basicEventHandler * mpHandler = ( * idx ) ;
{
mpHandler = ( * idx ) ;
if ( mpHandler ! = NULL )
// skip handlers removed since the snapshot (they may be deleted already)
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
if ( mpHandler = = NULL | | ! isRegistered ( mpHandler ) )
{
if ( ! mpHandler - > dispatchEvent ( mpEvent ) )
{
break ; // Stop Propogation if false
}
continue ;
}
} ;
m_pHandlerMutex - > unlock ( ) ;
utilizeBasicEvent ( mpEvent ) ;
}
else
{
SLEEP ( 1 ) ;
}
m_pCurrentHandler = mpHandler ;
}
bool tPropagate = mpHandler - > dispatchEvent ( mpEvent ) ;
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_pCurrentHandler = NULL ;
}
m_cvDispatch . notify_all ( ) ; // a removeHandler() may wait for this handler
if ( ! tPropagate )
{
break ; // Stop Propogation if false
}
} ;
utilizeBasicEvent ( mpEvent ) ;
} ;
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_bInDispatch = false ;
}
m_cvDispatch . notify_all ( ) ; // pause() may wait for the end of the round
if ( mpEvent = = NULL )
{
SLEEP ( 1 ) ;
} ;
} ;
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_bInDispatch = false ;
m_pCurrentHandler = NULL ;
}
m_cvDispatch . notify_all ( ) ;
}
void basicEventHeap : : broadcast ( EVENT_CONTAINER * Evt )
@ -125,5 +237,73 @@ void basicEventHeap::broadcast(EVENT_CONTAINER * Evt)
} ;
}
bool basicEventHeap : : flush ( uint32_t timeoutMs )
{
if ( isDispatchThread ( ) )
{
return false ; // a handler cannot wait for its own dispatch
} ;
uint32_t tWaited = 0 ;
while ( tWaited < timeoutMs )
{
m_pDispatchingMutex - > lock ( ) ;
bool tEmpty = m_vEventQueue . empty ( ) ;
m_pDispatchingMutex - > unlock ( ) ;
if ( tEmpty )
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
if ( ! m_bInDispatch | | m_bPaused | | m_bThreadStatus > = TH_ENDED )
{
return true ;
}
} ;
if ( m_bPaused | | m_bThreadStatus > = TH_ENDED )
{
return false ; // nothing will be delivered
} ;
SLEEP ( 5 ) ;
tWaited + = 5 ;
} ;
LOG_ERROR ( " flush: events still queued after the timeout " ) ;
return false ;
}
bool basicEventHeap : : pause ( void )
{
std : : unique_lock < std : : mutex > tLock ( m_mHandlers ) ;
m_bPaused = true ;
if ( isDispatchThread ( ) | | m_bThreadStatus > = TH_ENDED )
{
return true ; // the current round ends when the caller returns
} ;
if ( ! m_cvDispatch . wait_for ( tLock , std : : chrono : : milliseconds ( EVENT_HEAP_WAIT_DISPATCH_MS ) , [ & ] { return ! m_bInDispatch ; } ) )
{
LOG_ERROR ( " pause: a dispatchEvent() is still running - continuing anyway " ) ;
return false ;
} ;
return true ;
}
void basicEventHeap : : resume ( void )
{
std : : lock_guard < std : : mutex > tLock ( m_mHandlers ) ;
m_bPaused = false ;
}
size_t basicEventHeap : : clearQueue ( void )
{
size_t tCount = 0 ;
m_pDispatchingMutex - > lock ( ) ;
while ( ! m_vEventQueue . empty ( ) )
{
EVENT_CONTAINER * tEvt = m_vEventQueue . front ( ) ;
m_vEventQueue . pop ( ) ;
utilizeBasicEvent ( tEvt ) ;
tCount + + ;
} ;
m_pDispatchingMutex - > unlock ( ) ;
return tCount ;
}
# undef LOG
# undef LOG_ERROR