From cfe0e77aaf87a0590ceea42f6afa62b0c7d95e80 Mon Sep 17 00:00:00 2001 From: Aleksandar Donchev Date: Mon, 25 Sep 2017 17:01:18 +0200 Subject: Timer fd is closed at the beginning of the next iteration + some unit tests. Signed-off-by: Christian Linke Change-Id: I8c5d3c436ac9fad62c76a26145c731b538abb1e7 --- AudioManagerUtilities/src/CAmSocketHandler.cpp | 103 +++++++++++++++++-------- 1 file changed, 70 insertions(+), 33 deletions(-) (limited to 'AudioManagerUtilities/src') diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index fad60e5..943a80c 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -44,20 +44,24 @@ namespace am CAmSocketHandler::CAmSocketHandler() : mPipe(), // - mDispatchDone(true), // - mSetPollKeys(MAX_POLLHANDLE), // - mListPoll(), // - mSetTimerKeys(MAX_TIMERHANDLE), - mListTimer(), // - mListActiveTimer(), // - mSetSignalhandlerKeys(MAX_POLLHANDLE), // - mSignalHandlers(), // - mRecreatePollfds(true), - mInternalCodes(internal_codes_e::NO_ERROR), - mSignalFdHandle(0) -#ifndef WITH_TIMERFD -,mStartTime() // -#endif + mDispatchDone(true), // + mSetPollKeys(MAX_POLLHANDLE), // + mListPoll(), // + mSetTimerKeys(MAX_TIMERHANDLE), + mListTimer(), // + #ifndef WITH_TIMERFD + mListActiveTimer(), // + #else + mListRemovedTimers(), + #endif + mSetSignalhandlerKeys(MAX_POLLHANDLE), // + mSignalHandlers(), // + mRecreatePollfds(true), + mInternalCodes(internal_codes_e::NO_ERROR), + mSignalFdHandle(0) + #ifndef WITH_TIMERFD + ,mStartTime() // + #endif { if (pipe(mPipe) == -1) { @@ -80,6 +84,9 @@ CAmSocketHandler::CAmSocketHandler() : CAmSocketHandler::~CAmSocketHandler() { +#ifdef WITH_TIMERFD + closeRemovedTimers(); +#endif for (auto it : mListPoll) { close(it.pollfdValue.fd); @@ -118,6 +125,9 @@ void CAmSocketHandler::start_listenting() { if (mRecreatePollfds) { + #ifdef WITH_TIMERFD + closeRemovedTimers(); + #endif fdPollingArray.clear(); //freeze mListPoll by copying it - otherwise we get problems when we want to manipulate it during the next lines cloneListPoll = mListPoll; @@ -350,9 +360,14 @@ am_Error_e CAmSocketHandler::listenToSignals(const std::vector & listSi * @return E_OK if the descriptor was added, E_NON_EXISTENT if the fd is not valid */ -am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, std::function prepare, - std::function fired, std::function check, - std::function dispatch, void* userData, sh_pollHandle_t& handle) +am_Error_e CAmSocketHandler::addFDPoll(const int fd, + const short event, + std::function prepare, + std::function fired, + std::function check, + std::function dispatch, + void* userData, + sh_pollHandle_t& handle) { if (!fdIsValid(fd)) return (E_NON_EXISTENT); @@ -533,7 +548,6 @@ am_Error_e CAmSocketHandler::addTimer(const timespec & timeouts, std::functionbool - { - callback(handle, userData); - return false; - }, - NULL, userData, handle); + err = addFDPoll(timerItem.fd, + POLLIN, + NULL, + actionPoll, + [callback](const sh_pollHandle_t handle, void* userData)->bool{ + callback(handle, userData); + return false; + }, + NULL, + userData, + handle); if (E_OK == err) { timerItem.handle = handle; @@ -608,20 +627,22 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) if (it == mListTimer.end()) return (E_NON_EXISTENT); - close(it->fd); + mListRemovedTimers.push_back(*it); mListTimer.erase(it); return removeFDPoll(handle); #else stopTimer(handle); std::list::iterator it(mListTimer.begin()); - for (; it != mListTimer.end(); ++it) + while (it != mListTimer.end()) { if (it->handle == handle) { - it = mListTimer.erase(it); + it = mListTimer.erase(it); mSetTimerKeys.pollHandles.erase(handle); return (E_OK); } + else + ++it; } return (E_UNKNOWN); #endif @@ -657,7 +678,7 @@ am_Error_e CAmSocketHandler::updateTimer(const sh_timerHandle_t handle, const ti } else { - if (timerfd_settime(it->fd, 0, &it->countdown, NULL)) + if (timerfd_settime(it->fd, 0, &it->countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -737,7 +758,7 @@ am_Error_e CAmSocketHandler::restartTimer(const sh_timerHandle_t handle) } else { - if (timerfd_settime(it->fd, 0, &it->countdown, NULL)) + if (timerfd_settime(it->fd, 0, &it->countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -811,7 +832,7 @@ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) countdown.it_value.tv_nsec = 0; countdown.it_value.tv_sec = 0; - if (timerfd_settime(it->fd, 0, &countdown, NULL)) + if (timerfd_settime(it->fd, 0, &countdown, NULL)<0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; @@ -820,13 +841,16 @@ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) #else //go through the list and remove the timer with the handle std::list::iterator it(mListActiveTimer.begin()); - for (; it != mListActiveTimer.end(); ++it) + + while (it != mListActiveTimer.end()) { if (it->handle == handle) { it = mListActiveTimer.erase(it); return (E_OK); } + else + it++; } return (E_NON_EXISTENT); #endif @@ -1031,19 +1055,32 @@ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) am_Error_e CAmSocketHandler::createTimeFD(const itimerspec & timeouts, int & fd) { fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); - if (fd <= 0) + if (fd < 0) { logError("Failed to create timer"); return E_NOT_POSSIBLE; } - if (timerfd_settime(fd, 0, &timeouts, NULL)) + if (timerfd_settime(fd, 0, &timeouts, NULL) < 0) { logError("Failed to set timer duration"); return E_NOT_POSSIBLE; } return E_OK; } + +void CAmSocketHandler::closeRemovedTimers() +{ + std::list::iterator it(mListRemovedTimers.begin()); + while (it != mListRemovedTimers.end()) + { + if( it->fd > -1 ) + close( it->fd ); + ++it; + } + mListRemovedTimers.clear(); +} + #endif void CAmSocketHandler::callTimer(sh_timer_s& a) -- cgit v1.2.1