diff options
author | Jens Lorenz <jlorenz@de.adit-jv.com> | 2018-03-27 14:44:21 +0200 |
---|---|---|
committer | Jens Lorenz <jlorenz@de.adit-jv.com> | 2018-04-09 17:00:47 +0200 |
commit | 0408f613c6b2a0653cc32ca5ba4776279cd896ff (patch) | |
tree | 3f4a93a7b25b997f749211e94a9a7d3120390362 | |
parent | 29b816429d141584af128256545ca0dc96ce0be3 (diff) | |
download | audiomanager-0408f613c6b2a0653cc32ca5ba4776279cd896ff.tar.gz |
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 <jlorenz@de.adit-jv.com>
-rw-r--r-- | AudioManagerUtilities/include/CAmSocketHandler.h | 6 | ||||
-rw-r--r-- | 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; //!<this is a handle for a filedescriptor to be used with the SocketHandler typedef sh_pollHandle_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler +typedef enum:uint8_t { RMV_ONLY, RMV_N_CLS } sh_rmv_e; /** * prototype for poll prepared callback @@ -220,7 +221,8 @@ class CAmSocketHandler UNINIT = 0u, // new, uninitialized element which needs to be inserted to ppoll array VALID = 1u, // it is a valid element in ppoll array UPDATE = 2u, // update of event information therefore update ppoll array - REMOVE = 3u // remove from ppoll array and internal map + REMOVE = 3u, // remove from ppoll array and internal map + CLOSE = 4u // close and remove from ppoll array and internal map } poll_states_e; struct sh_poll_s //!<struct that holds information about polls @@ -462,7 +464,7 @@ public: std::function<bool(const sh_pollHandle_t handle, void* userData)> check, std::function<bool(const sh_pollHandle_t handle, void* userData)> 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<void(const sh_pollHandle_t handle, const signalfd_siginfo & info, void* userData)> 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 <sys/timerfd.h> #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; |