#include "basicEventHeap.h" #include "basicEventHandler.h" #include #include #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 tLock(_instanceLock); if (0 == _instance) { _instance = new basicEventHeap; }; return _instance; } basicEventHeap * basicEventHeap::existingInstance() { std::lock_guard tLock(_instanceLock); return _instance; } 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()) { _instance->m_pLog->Log("basicEventHeap", _instance, _LOG_ERROR, "Dispatch thread does not stop - not waiting for it"); return; }; basicEventHeap * tHeap; { std::lock_guard tLock(_instanceLock); tHeap = _instance; _instance = 0; } delete (tHeap); }; } 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(); { std::lock_guard 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 tLock(m_mHandlers); if (atBottom) { m_vHandlers.push_back(handler); } else { m_vHandlers.insert(m_vHandlers.begin(), handler); }; } /* 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 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 (! 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) { std::lock_guard tLock(m_mHandlers); V_EVT_HANDLER::iterator idx = std::find(m_vHandlers.begin(), m_vHandlers.end(), handler); 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) { std::lock_guard tLock(m_mHandlers); V_EVT_HANDLER::iterator idx = std::find(m_vHandlers.begin(), m_vHandlers.end(), handler); 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) { { std::lock_guard tLock(m_mHandlers); m_tDispatchThread = pthread_self(); } m_bThreadStatus = TH_WORKING; pthread_setname_np(pthread_self(), "EventHeap"); 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 tLock(m_mHandlers); if (m_bPaused) { m_bInDispatch = false; } else { m_bInDispatch = true; tHandlers = m_vHandlers; // snapshot: the list may change while handlers are called }; } 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++) { basicEventHandler * mpHandler = (*idx); { // skip handlers removed since the snapshot (they may be deleted already) std::lock_guard tLock(m_mHandlers); if (mpHandler == NULL || ! isRegistered(mpHandler)) { continue; } m_pCurrentHandler = mpHandler; } bool tPropagate = mpHandler->dispatchEvent(mpEvent); { std::lock_guard 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 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 tLock(m_mHandlers); m_bInDispatch = false; m_pCurrentHandler = NULL; } m_cvDispatch.notify_all(); } 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()) { 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 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 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 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