From 0408f613c6b2a0653cc32ca5ba4776279cd896ff Mon Sep 17 00:00:00 2001 From: Jens Lorenz Date: Tue, 27 Mar 2018 14:44:21 +0200 Subject: AMUtil: ONLY timers will be closed in worker thread The removeFDPoll API is extended to allow applications to close the fd by the central worker thread. Signed-off-by: Jens Lorenz --- AudioManagerUtilities/include/CAmSocketHandler.h | 6 ++++-- AudioManagerUtilities/src/CAmSocketHandler.cpp | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/AudioManagerUtilities/include/CAmSocketHandler.h b/AudioManagerUtilities/include/CAmSocketHandler.h index 6564d7f..cfa2e89 100644 --- a/AudioManagerUtilities/include/CAmSocketHandler.h +++ b/AudioManagerUtilities/include/CAmSocketHandler.h @@ -50,6 +50,7 @@ namespace am typedef uint16_t sh_pollHandle_t; //! check, std::function dispatch, void* userData, sh_pollHandle_t& handle); am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle); - am_Error_e removeFDPoll(const sh_pollHandle_t handle); + am_Error_e removeFDPoll(const sh_pollHandle_t handle, const sh_rmv_e rmv = RMV_ONLY); am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events); am_Error_e addSignalHandler(std::function callback, sh_pollHandle_t& handle, void * userData); am_Error_e removeSignalHandler(const sh_pollHandle_t handle); diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index 7e12fc0..ac58f48 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -40,6 +40,9 @@ #include #endif +#define SHPOLL_IS_ACTIVE(state) (!(state == poll_states_e::REMOVE || state == poll_states_e::CLOSE)) + + namespace am { @@ -73,7 +76,7 @@ CAmSocketHandler::CAmSocketHandler() : { for (auto & elem : mMapShPoll) { - if (elem.second.state != poll_states_e::REMOVE) + if (SHPOLL_IS_ACTIVE(elem.second.state)) elem.second.state = poll_states_e::UNINIT; } mDispatchDone = true; @@ -137,8 +140,11 @@ void CAmSocketHandler::start_listenting() auto& elem = it->second; switch (elem.state) { - case poll_states_e::REMOVE: + case poll_states_e::CLOSE: close(elem.pollfdValue.fd); + // fallthrough + + case poll_states_e::REMOVE: fdPollIt = fdPollingArray.erase(fdPollIt); it = mMapShPoll.erase(it); continue; @@ -402,7 +408,7 @@ am_Error_e CAmSocketHandler::addFDPoll(const int fd, return E_NON_EXISTENT; const auto elem = mMapShPoll.find(fd); - if (elem != mMapShPoll.end() && elem->second.state != poll_states_e::REMOVE) + if (elem != mMapShPoll.end() && SHPOLL_IS_ACTIVE(elem->second.state)) { logError("CAmSocketHandler::addFDPoll fd", fd, "already registered!"); return E_ALREADY_EXISTS; @@ -472,15 +478,16 @@ am::am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, IAmS /** * removes a filedescriptor from the poll loop * @param handle + * @param [rmv] default RMV_ONLY_FDPOLL * @return */ -am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle) +am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle, const sh_rmv_e rmv) { for (auto& it : mMapShPoll) { if (it.second.handle == handle) { - it.second.state = poll_states_e::REMOVE; + it.second.state = (rmv == sh_rmv_e::RMV_N_CLS ? poll_states_e::CLOSE : poll_states_e::REMOVE); static uint64_t events(1); if (write(mEventFd, &(++events), sizeof(events)) < 0) @@ -673,7 +680,7 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) if (it->handle == handle) { mListTimer.erase(it); - return removeFDPoll(handle); + return removeFDPoll(handle, sh_rmv_e::RMV_N_CLS); } ++it; } @@ -912,7 +919,7 @@ am_Error_e CAmSocketHandler::updateEventFlags(const sh_pollHandle_t handle, cons for (auto& it : mMapShPoll) { auto& elem = it.second; - if (elem.handle == handle && elem.state != poll_states_e::REMOVE) + if (elem.handle == handle && SHPOLL_IS_ACTIVE(elem.state)) { elem.pollfdValue.events = events; elem.pollfdValue.revents = 0; -- cgit v1.2.1