|
|
|
@ -14,30 +14,24 @@ std::mutex basicEventHeap::_instanceLock; |
|
|
|
/**
|
|
|
|
* Returns the single instance of the object. |
|
|
|
*/ |
|
|
|
basicEventHeap * basicEventHeap::getInstance() |
|
|
|
{ |
|
|
|
basicEventHeap * basicEventHeap::getInstance(){ |
|
|
|
std::lock_guard<std::mutex> tLock(_instanceLock); |
|
|
|
if (0 == _instance) |
|
|
|
{ |
|
|
|
if (0 == _instance){ |
|
|
|
_instance = new basicEventHeap; |
|
|
|
}; |
|
|
|
return _instance; |
|
|
|
} |
|
|
|
|
|
|
|
basicEventHeap * basicEventHeap::existingInstance() |
|
|
|
{ |
|
|
|
basicEventHeap * basicEventHeap::existingInstance(){ |
|
|
|
std::lock_guard<std::mutex> tLock(_instanceLock); |
|
|
|
return _instance; |
|
|
|
} |
|
|
|
|
|
|
|
void basicEventHeap::destroyInstance() |
|
|
|
{ |
|
|
|
if (existingInstance()) |
|
|
|
{ |
|
|
|
void basicEventHeap::destroyInstance(){ |
|
|
|
if (existingInstance()){ |
|
|
|
// 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()) |
|
|
|
{ |
|
|
|
if (! _instance->pause()){ |
|
|
|
_instance->m_pLog->Log("basicEventHeap", _instance, _LOG_ERROR, "Dispatch thread does not stop - not waiting for it"); |
|
|
|
return; |
|
|
|
}; |
|
|
|
@ -51,8 +45,7 @@ void basicEventHeap::destroyInstance() |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
basicEventHeap::basicEventHeap() |
|
|
|
{ |
|
|
|
basicEventHeap::basicEventHeap(){ |
|
|
|
m_pLog = basicLogCollection::getInstance(); |
|
|
|
m_pDispatchingMutex = new basicMutex(); |
|
|
|
m_pCurrentHandler = NULL; |
|
|
|
@ -62,8 +55,7 @@ basicEventHeap::basicEventHeap() |
|
|
|
basicThread::init(); |
|
|
|
} |
|
|
|
|
|
|
|
basicEventHeap::~basicEventHeap() |
|
|
|
{ |
|
|
|
basicEventHeap::~basicEventHeap(){ |
|
|
|
pause(); |
|
|
|
waitThreadEnd(true); |
|
|
|
clearQueue(); |
|
|
|
@ -74,25 +66,19 @@ basicEventHeap::~basicEventHeap() |
|
|
|
delete (m_pDispatchingMutex); |
|
|
|
} |
|
|
|
|
|
|
|
bool basicEventHeap::isDispatchThread(void) |
|
|
|
{ |
|
|
|
bool basicEventHeap::isDispatchThread(void){ |
|
|
|
return (m_bThreadStatus >= TH_WORKING) && pthread_equal(pthread_self(), m_tDispatchThread); |
|
|
|
} |
|
|
|
|
|
|
|
bool basicEventHeap::isRegistered(basicEventHandler * handler) |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
void basicEventHeap::addHandler(basicEventHandler * handler, bool atBottom){ |
|
|
|
std::lock_guard<std::mutex> tLock(m_mHandlers); |
|
|
|
if (atBottom) |
|
|
|
{ |
|
|
|
if (atBottom){ |
|
|
|
m_vHandlers.push_back(handler); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
} else { |
|
|
|
m_vHandlers.insert(m_vHandlers.begin(), handler); |
|
|
|
}; |
|
|
|
} |
|
|
|
@ -106,94 +92,76 @@ void basicEventHeap::addHandler(basicEventHandler * handler, bool atBottom) |
|
|
|
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) |
|
|
|
{ |
|
|
|
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()) |
|
|
|
{ |
|
|
|
if (idx != m_vHandlers.end()){ |
|
|
|
m_vHandlers.erase(idx); |
|
|
|
}; |
|
|
|
if (m_pCurrentHandler == handler && ! isDispatchThread()) |
|
|
|
{ |
|
|
|
if (! m_cvDispatch.wait_for(tLock, std::chrono::milliseconds(EVENT_HEAP_WAIT_DISPATCH_MS), [&] { return m_pCurrentHandler != handler; })) |
|
|
|
{ |
|
|
|
if (m_pCurrentHandler == handler && ! isDispatchThread()){ |
|
|
|
if (! m_cvDispatch.wait_for(tLock, std::chrono::milliseconds(EVENT_HEAP_WAIT_DISPATCH_MS), [&] { return m_pCurrentHandler != handler; })){ |
|
|
|
LOG_ERROR("removeHandler: handler still busy in dispatchEvent() - continuing anyway"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
void basicEventHeap::moveHandlerBottom(basicEventHandler * handler) |
|
|
|
{ |
|
|
|
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()) |
|
|
|
{ |
|
|
|
if (idx == m_vHandlers.end()){ |
|
|
|
return; // not registered (not attached yet / detached): moving must not register it
|
|
|
|
}; |
|
|
|
m_vHandlers.erase(idx); |
|
|
|
m_vHandlers.push_back(handler); |
|
|
|
} |
|
|
|
|
|
|
|
void basicEventHeap::moveHandlerTop(basicEventHandler * 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()) |
|
|
|
{ |
|
|
|
if (idx == m_vHandlers.end()){ |
|
|
|
return; // not registered (not attached yet / detached): moving must not register it
|
|
|
|
}; |
|
|
|
m_vHandlers.erase(idx); |
|
|
|
m_vHandlers.insert(m_vHandlers.begin(), handler); |
|
|
|
} |
|
|
|
|
|
|
|
void basicEventHeap::m_fThreadProc(void) |
|
|
|
{ |
|
|
|
void basicEventHeap::m_fThreadProc(void){ |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
while (m_bThreadStatus < TH_ENDED){ |
|
|
|
// Enter a dispatch round only while not paused (pause() waits for m_bInDispatch == false)
|
|
|
|
V_EVT_HANDLER tHandlers; |
|
|
|
{ |
|
|
|
std::lock_guard<std::mutex> tLock(m_mHandlers); |
|
|
|
if (m_bPaused) |
|
|
|
{ |
|
|
|
if (m_bPaused){ |
|
|
|
m_bInDispatch = false; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
} else { |
|
|
|
m_bInDispatch = true; |
|
|
|
tHandlers = m_vHandlers; // snapshot: the list may change while handlers are called
|
|
|
|
}; |
|
|
|
} |
|
|
|
if (! m_bInDispatch) |
|
|
|
{ |
|
|
|
if (! m_bInDispatch){ |
|
|
|
SLEEP(1); |
|
|
|
continue; |
|
|
|
}; |
|
|
|
EVENT_CONTAINER * mpEvent = NULL; |
|
|
|
m_pDispatchingMutex->lock(); |
|
|
|
if (! m_vEventQueue.empty()) |
|
|
|
{ |
|
|
|
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++) |
|
|
|
{ |
|
|
|
if (mpEvent != NULL){ |
|
|
|
for (V_EVT_HANDLER::iterator idx = tHandlers.begin(); idx != tHandlers.end(); idx++){ |
|
|
|
basicEventHandler * mpHandler = (*idx); |
|
|
|
{ |
|
|
|
// 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 == NULL || ! isRegistered(mpHandler)){ |
|
|
|
continue; |
|
|
|
} |
|
|
|
m_pCurrentHandler = mpHandler; |
|
|
|
@ -204,8 +172,7 @@ void basicEventHeap::m_fThreadProc(void) |
|
|
|
m_pCurrentHandler = NULL; |
|
|
|
} |
|
|
|
m_cvDispatch.notify_all(); // a removeHandler() may wait for this handler
|
|
|
|
if (! tPropagate) |
|
|
|
{ |
|
|
|
if (! tPropagate){ |
|
|
|
break; // Stop Propogation if false
|
|
|
|
} |
|
|
|
}; |
|
|
|
@ -216,8 +183,7 @@ void basicEventHeap::m_fThreadProc(void) |
|
|
|
m_bInDispatch = false; |
|
|
|
} |
|
|
|
m_cvDispatch.notify_all(); // pause() may wait for the end of the round
|
|
|
|
if (mpEvent == NULL) |
|
|
|
{ |
|
|
|
if (mpEvent == NULL){ |
|
|
|
SLEEP(1); |
|
|
|
}; |
|
|
|
}; |
|
|
|
@ -229,38 +195,30 @@ void basicEventHeap::m_fThreadProc(void) |
|
|
|
m_cvDispatch.notify_all(); |
|
|
|
} |
|
|
|
|
|
|
|
void basicEventHeap::broadcast(EVENT_CONTAINER * Evt) |
|
|
|
{ |
|
|
|
if (Evt) |
|
|
|
{ |
|
|
|
void basicEventHeap::broadcast(EVENT_CONTAINER * Evt){ |
|
|
|
if( Evt ){ |
|
|
|
m_pDispatchingMutex->lock(); |
|
|
|
m_vEventQueue.push(Evt); |
|
|
|
m_pDispatchingMutex->unlock(); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
bool basicEventHeap::flush(uint32_t timeoutMs) |
|
|
|
{ |
|
|
|
if (isDispatchThread()) |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
while (tWaited < timeoutMs){ |
|
|
|
m_pDispatchingMutex->lock(); |
|
|
|
bool tEmpty = m_vEventQueue.empty(); |
|
|
|
m_pDispatchingMutex->unlock(); |
|
|
|
if (tEmpty) |
|
|
|
{ |
|
|
|
if (tEmpty){ |
|
|
|
std::lock_guard<std::mutex> tLock(m_mHandlers); |
|
|
|
if (! m_bInDispatch || m_bPaused || m_bThreadStatus >= TH_ENDED) |
|
|
|
{ |
|
|
|
if (! m_bInDispatch || m_bPaused || m_bThreadStatus >= TH_ENDED){ |
|
|
|
return true; |
|
|
|
} |
|
|
|
}; |
|
|
|
if (m_bPaused || m_bThreadStatus >= TH_ENDED) |
|
|
|
{ |
|
|
|
if (m_bPaused || m_bThreadStatus >= TH_ENDED){ |
|
|
|
return false; // nothing will be delivered
|
|
|
|
}; |
|
|
|
SLEEP(5); |
|
|
|
@ -270,34 +228,28 @@ bool basicEventHeap::flush(uint32_t timeoutMs) |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
bool basicEventHeap::pause(void) |
|
|
|
{ |
|
|
|
bool basicEventHeap::pause(void){ |
|
|
|
std::unique_lock<std::mutex> tLock(m_mHandlers); |
|
|
|
m_bPaused = true; |
|
|
|
if (isDispatchThread() || m_bThreadStatus >= TH_ENDED) |
|
|
|
{ |
|
|
|
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; })) |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
void basicEventHeap::resume(void){ |
|
|
|
std::lock_guard<std::mutex> tLock(m_mHandlers); |
|
|
|
m_bPaused = false; |
|
|
|
} |
|
|
|
|
|
|
|
size_t basicEventHeap::clearQueue(void) |
|
|
|
{ |
|
|
|
size_t basicEventHeap::clearQueue(void){ |
|
|
|
size_t tCount = 0; |
|
|
|
m_pDispatchingMutex->lock(); |
|
|
|
while (! m_vEventQueue.empty()) |
|
|
|
{ |
|
|
|
while (! m_vEventQueue.empty()){ |
|
|
|
EVENT_CONTAINER * tEvt = m_vEventQueue.front(); |
|
|
|
m_vEventQueue.pop(); |
|
|
|
utilizeBasicEvent(tEvt); |
|
|
|
|