From 9c0acd5dc485e422a4a0e329e77ab31af721efeb Mon Sep 17 00:00:00 2001 From: christian mueller Date: Thu, 29 Mar 2012 17:40:07 +0200 Subject: * [GAM-49] worked in comments to last patches * changed dlt threadsafeness * changes on timers in CAmSockethandler * adopted test of Sockethandler to changes * add versioning scheme to documentation * added forgotten #include on CamDbusWrapper and CAmRoutingSenderAsync * adopted RoutingReceiverAsyncTests to new timers Signed-off-by: christian mueller --- AudioManagerDaemon/docx/images/versioning.png | Bin 0 -> 23647 bytes AudioManagerDaemon/src/CAmDbusWrapper.cpp | 12 +- AudioManagerDaemon/src/CAmDltWrapper.cpp | 4 +- AudioManagerDaemon/src/CAmSocketHandler.cpp | 194 +++++++---- .../AmSocketHandlerTest/CAmSocketHandlerTest.cpp | 21 +- .../include/CAmRoutingSenderAsync.h | 1 + .../test/CAmRoutingReceiverAsync.cpp | 12 +- include/shared/CAmDbusWrapper.h | 1 + include/shared/CAmDltWrapper.h | 125 ++++--- include/shared/CAmSocketHandler.h | 358 +++++++++++++-------- 10 files changed, 445 insertions(+), 283 deletions(-) create mode 100644 AudioManagerDaemon/docx/images/versioning.png diff --git a/AudioManagerDaemon/docx/images/versioning.png b/AudioManagerDaemon/docx/images/versioning.png new file mode 100644 index 0000000..9b587ba Binary files /dev/null and b/AudioManagerDaemon/docx/images/versioning.png differ diff --git a/AudioManagerDaemon/src/CAmDbusWrapper.cpp b/AudioManagerDaemon/src/CAmDbusWrapper.cpp index 115d33d..97b4ccd 100644 --- a/AudioManagerDaemon/src/CAmDbusWrapper.cpp +++ b/AudioManagerDaemon/src/CAmDbusWrapper.cpp @@ -132,7 +132,7 @@ CAmDbusWrapper::~CAmDbusWrapper() */ void CAmDbusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata) { - logInfo("DBusWrapper::~registerCallback register callback:", path); + logInfo("DBusWrapper::registerCallback register callback:", path); std::string completePath = std::string(DBUS_SERVICE_OBJECT_PATH) + "/" + path; dbus_error_init(&mDBusError); @@ -314,10 +314,9 @@ dbus_bool_t CAmDbusWrapper::addTimeoutDelegate(DBusTimeout *timeout, void* userD //prepare handle and callback. new is eval, but there is no other choice because we need the pointer! sh_timerHandle_t* handle = new sh_timerHandle_t; mpListTimerhandles.push_back(handle); - IAmShTimerCallBack* buffer = &pDbusTimerCallback; //add the timer to the pollLoop - mpSocketHandler->addTimer(pollTimeout, buffer, *handle, timeout); + mpSocketHandler->addTimer(pollTimeout, &pDbusTimerCallback, *handle, timeout); //save the handle with dbus context dbus_timeout_set_data(timeout, handle, NULL); @@ -427,7 +426,7 @@ void CAmDbusWrapper::toggleTimeoutDelegate(DBusTimeout *timeout, void* userData) int localTimeout = dbus_timeout_get_interval(timeout); pollTimeout.tv_sec = localTimeout / 1000; pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000; - mpSocketHandler->restartTimer(*handle, pollTimeout); + mpSocketHandler->updateTimer(*handle, pollTimeout); } else { @@ -441,10 +440,7 @@ void CAmDbusWrapper::dbusTimerCallback(sh_timerHandle_t handle, void *userData) assert(userData!=NULL); if (dbus_timeout_get_enabled((DBusTimeout*) userData)) { - timespec ts; - ts.tv_nsec = -1; - ts.tv_sec = -1; - mpSocketHandler->restartTimer(handle, ts); + mpSocketHandler->restartTimer(handle); } dbus_timeout_handle((DBusTimeout*) userData); logInfo("DBusWrapper::dbusTimerCallback was called"); diff --git a/AudioManagerDaemon/src/CAmDltWrapper.cpp b/AudioManagerDaemon/src/CAmDltWrapper.cpp index 5449d7d..61d79c2 100644 --- a/AudioManagerDaemon/src/CAmDltWrapper.cpp +++ b/AudioManagerDaemon/src/CAmDltWrapper.cpp @@ -30,7 +30,7 @@ namespace am { CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL; -pthread_mutex_t CAmDltWrapper::logMutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t CAmDltWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER; CAmDltWrapper *CAmDltWrapper::instance(const bool enableNoDLTDebug) { @@ -96,6 +96,7 @@ void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, c void CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context) { (void) loglevel; + pthread_mutex_lock(&mMutex); if (!context) context = &mDltContext; #ifdef WITH_DLT @@ -115,6 +116,7 @@ void CAmDltWrapper::send() mDltContextData.buffer.str(""); mDltContextData.buffer.clear(); #endif + pthread_mutex_unlock(&mMutex); } void CAmDltWrapper::append(const int8_t value) diff --git a/AudioManagerDaemon/src/CAmSocketHandler.cpp b/AudioManagerDaemon/src/CAmSocketHandler.cpp index a6c4be9..27cb4bb 100644 --- a/AudioManagerDaemon/src/CAmSocketHandler.cpp +++ b/AudioManagerDaemon/src/CAmSocketHandler.cpp @@ -43,7 +43,7 @@ CAmSocketHandler::CAmSocketHandler() : mRecreatePollfds(true), // mStartTime() { - gDispatchDone = 0; + gDispatchDone = 1; } CAmSocketHandler::~CAmSocketHandler() @@ -82,8 +82,7 @@ void CAmSocketHandler::start_listenting() mRecreatePollfds = false; } - if (!mListActiveTimer.empty()) - timerCorrection(); + timerCorrection(); //block until something is on a filedescriptor @@ -102,7 +101,6 @@ void CAmSocketHandler::start_listenting() } } - clock_gettime(CLOCK_MONOTONIC, &mStartTime); if (pollStatus != 0) //only check filedescriptors if there was a change { //todo: here could be a timer that makes sure naughty plugins return! @@ -153,6 +151,16 @@ void CAmSocketHandler::start_listenting() void CAmSocketHandler::stop_listening() { gDispatchDone = 1; + + //this is for all running timers only - we need to handle the additional offset here + if (!mListActiveTimer.empty()) + { + timespec currentTime, correctionTime; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + correctionTime = timespecSub(currentTime, mStartTime); + std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(correctionTime)); + } + } /** @@ -224,7 +232,7 @@ am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle) * @param userData pointer always passed with the call * @return E_OK in case of success */ -am_Error_e CAmSocketHandler::addTimer(const timespec timeouts, IAmShTimerCallBack*& callback, sh_timerHandle_t& handle, void * userData) +am_Error_e CAmSocketHandler::addTimer(const timespec timeouts, IAmShTimerCallBack* callback, sh_timerHandle_t& handle, void * userData) { assert(!((timeouts.tv_sec==0) && (timeouts.tv_nsec==0))); assert(callback!=NULL); @@ -235,11 +243,17 @@ am_Error_e CAmSocketHandler::addTimer(const timespec timeouts, IAmShTimerCallBac handle = ++mLastInsertedHandle; //todo: overflow ruling !o timerItem.handle = handle; timerItem.countdown = timeouts; - timerItem.timeout = timeouts; timerItem.callback = callback; timerItem.userData = userData; mListTimer.push_back(timerItem); + + //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection + timespec currentTime; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + if (!gDispatchDone) //the mainloop is started + timerItem.countdown = timespecAdd(timeouts, timespecSub(currentTime, mStartTime)); + mListActiveTimer.push_back(timerItem); mListActiveTimer.sort(compareCountdown); return (E_OK); @@ -257,7 +271,7 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) //stop the current timer stopTimer(handle); - std::list::iterator it = mListTimer.begin(); + std::list::iterator it(mListTimer.begin()); for (; it != mListTimer.end(); ++it) { if (it->handle == handle) @@ -270,36 +284,117 @@ am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle) } /** - * restarts a timer and updates with a new interval + * restarts a timer and updates with a new interva * @param handle handle to the timer * @param timeouts new timout time * @return E_OK on success, E_NON_EXISTENT if the handle was not found */ -am_Error_e CAmSocketHandler::restartTimer(const sh_timerHandle_t handle, const timespec timeouts) +am_Error_e CAmSocketHandler::updateTimer(const sh_timerHandle_t handle, const timespec timeouts) { + //update the mList .... sh_timer_s timerItem; - std::list::iterator it = mListTimer.begin(); + std::list::iterator it(mListTimer.begin()), activeIt(mListActiveTimer.begin()); + bool found(false); for (; it != mListTimer.end(); ++it) { if (it->handle == handle) { + it->countdown = timeouts; timerItem = *it; + found = true; break; } } + if (!found) + return (E_NON_EXISTENT); + + found = false; - timerItem.timeout=timeouts; - timerItem.countdown=timeouts; + //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection + timespec currentTime, timeoutsCorrected; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + if (!gDispatchDone) //the mainloop is started + timeoutsCorrected = timespecAdd(timeouts, timespecSub(currentTime, mStartTime)); + + for (; activeIt != mListActiveTimer.end(); ++activeIt) + { + if (activeIt->handle == handle) + { + activeIt->countdown = timeoutsCorrected; + found = true; + break; + } + } + if (!found) + timerItem.countdown = timeoutsCorrected; mListActiveTimer.push_back(timerItem); + mListActiveTimer.sort(compareCountdown); return (E_OK); } +/** + * restarts a timer with the original value + * @param handle + * @return E_OK on success, E_NON_EXISTENT if the handle was not found + */ +am_Error_e CAmSocketHandler::restartTimer(const sh_timerHandle_t handle) +{ + sh_timer_s timerItem; //!::iterator it(mListTimer.begin()), activeIt(mListActiveTimer.begin()); + bool found(false); + for (; it != mListTimer.end(); ++it) + { + if (it->handle == handle) + { + timerItem = *it; + found = true; + break; + } + } + if (!found) + return (E_NON_EXISTENT); + + found = false; + + //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection + timespec currentTime, timeoutsCorrected; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + if (!gDispatchDone) //the mainloop is started + { + timeoutsCorrected = timespecAdd(timerItem.countdown, timespecSub(currentTime, mStartTime)); + timerItem.countdown = timeoutsCorrected; + } + + for (; activeIt != mListActiveTimer.end(); ++activeIt) + { + if (activeIt->handle == handle) + { + activeIt->countdown = timerItem.countdown; + found = true; + break; + } + } + + if (!found) + mListActiveTimer.push_back(timerItem); + + mListActiveTimer.sort(compareCountdown); + + return (E_OK); +} + +/** + * stops a timer + * @param handle + * @return E_OK on success, E_NON_EXISTENT if the handle was not found + */ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) { //go through the list and remove the timer with the handle - std::list::iterator it = mListActiveTimer.begin(); + std::list::iterator it(mListActiveTimer.begin()); for (; it != mListActiveTimer.end(); ++it) { if (it->handle == handle) @@ -313,9 +408,7 @@ am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle) /** * updates the eventFlags of a poll - * @param handle buffertime.tv_nsec = mTimeout.tv_nsec; - buffertime.tv_sec = mTimeout.tv_sec; - return ((mTimeout.tv_nsec == -1 && mTimeout.tv_sec == -1) ? NULL : &buffertime); + * @param handle * @param events * @return @return E_OK on succsess, E_NON_EXISTENT if fd was not found */ @@ -350,11 +443,13 @@ bool CAmSocketHandler::fdIsValid(const int fd) const */ void CAmSocketHandler::timerUp() { - //first we need to correct all the countdown values by the one who fired - std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(mListActiveTimer.begin()->countdown)); + //find out the timedifference to starttime + timespec currentTime, diffTime; + clock_gettime(CLOCK_MONOTONIC, ¤tTime); + diffTime = timespecSub(currentTime, mStartTime); - //then find the last value that is zero. Since the first value is subtracted, we will always have the first one - std::list::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownUp()); + //now we need to substract the diffTime from all timers and see if they are up + std::list::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownUp(diffTime)); //copy all fired timers into a list std::vector tempList(overflowIter, mListActiveTimer.rend()); @@ -368,17 +463,7 @@ void CAmSocketHandler::timerUp() } /** - * convert timespec to milliseconds - * @param time time in timespec - * @return time in milliseconds - */ -inline int CAmSocketHandler::timespec2ms(const timespec & time) -{ - return ((time.tv_nsec == -1 && time.tv_sec == -1) ? -1 : time.tv_sec * 1000 + time.tv_nsec / 1000000); -} - -/** - * correct timers + * correct timers and fire the ones who are up */ void CAmSocketHandler::timerCorrection() { @@ -386,28 +471,38 @@ void CAmSocketHandler::timerCorrection() timespec currentTime, correctionTime; clock_gettime(CLOCK_MONOTONIC, ¤tTime); correctionTime = timespecSub(currentTime, mStartTime); + mStartTime = currentTime; - //subtract the correction value from all items in the list - std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(correctionTime)); + if (!mListActiveTimer.empty()) + { - //find the last occurrence of zero -> timer overflowed - std::list::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownUp()); + //subtract the correction value from all items in the list + std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(correctionTime)); - //only if a timer overflowed - if (overflowIter != mListActiveTimer.rend()) - { - //copy all timer that need to be called to a new list - std::vector tempList(overflowIter, mListActiveTimer.rend()); + //find the last occurrence of zero -> timer overflowed + std::list::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownZero()); + + //only if a timer overflowed + if (overflowIter != mListActiveTimer.rend()) + { + //copy all timers that need to be called to a new list + std::vector tempList(overflowIter, mListActiveTimer.rend()); - //erase all fired timers - std::list::iterator it(overflowIter.base()); - mListActiveTimer.erase(mListActiveTimer.begin(), it); + //erase all fired timers + std::list::iterator it(overflowIter.base()); + mListActiveTimer.erase(mListActiveTimer.begin(), it); - //call the callbacks for the timers - std::for_each(tempList.begin(), tempList.end(), CAmShCallTimer()); + //call the callbacks for the timers + std::for_each(tempList.begin(), tempList.end(), CAmShCallTimer()); + } } } +/** + * is used to set the pointer for the ppoll command + * @param buffertime + * @return + */ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) { if (!mListActiveTimer.empty()) @@ -421,14 +516,5 @@ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) } } -/** - * functor to easy substract from each countdown - * @param t value to substract from - */ -void CAmSocketHandler::CAmShSubstractTime::operator ()(sh_timer_s & t) const -{ - t.countdown = timespecSub(t.countdown, param); -} - } diff --git a/AudioManagerDaemon/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp b/AudioManagerDaemon/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp index 19e4772..d6aec07 100644 --- a/AudioManagerDaemon/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp +++ b/AudioManagerDaemon/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp @@ -68,18 +68,18 @@ void am::CAmTimerCb::timer1Callback(sh_timerHandle_t handle, void* userData) timespec timeout; timeout.tv_nsec = 0; timeout.tv_sec = 1; - mSocketHandler->restartTimer(handle,timeout); + mSocketHandler->updateTimer(handle,timeout); } void am::CAmTimerCb::timer2Callback(sh_timerHandle_t handle, void* userData) { (void) handle; (void) userData; - // std::cout << "callback2 called" << std::endl; + std::cout << "callback2 called" << std::endl; timespec timeout; timeout.tv_nsec = 011110000; - timeout.tv_sec = 0; - mSocketHandler->restartTimer(handle,timeout); + timeout.tv_sec = 1; + mSocketHandler->updateTimer(handle,timeout); } void am::CAmTimerCb::timer3Callback(sh_timerHandle_t, void* userData) @@ -117,7 +117,6 @@ void* playWithUnixSocketServer(void* data) TEST(CAmSocketHandlerTest,playWithTimers) { - gDispatchDone = 0; CAmSocketHandler myHandler; CAmTimerCb testCallback(&myHandler); timespec timeoutTime, timeout2, timeout3, timeout4; @@ -129,15 +128,11 @@ TEST(CAmSocketHandlerTest,playWithTimers) timeout3.tv_sec = 3; timeout4.tv_nsec = 0; timeout4.tv_sec = 20; - IAmShTimerCallBack* buf = &testCallback.pTimer1Callback; - IAmShTimerCallBack* buf2 = &testCallback.pTimer2Callback; - IAmShTimerCallBack* buf3 = &testCallback.pTimer3Callback; - IAmShTimerCallBack* buf4 = &testCallback.pTimer4Callback; sh_timerHandle_t handle; - myHandler.addTimer(timeoutTime, buf, handle, NULL); - myHandler.addTimer(timeout2, buf2, handle, NULL); - myHandler.addTimer(timeout3, buf3, handle, NULL); - myHandler.addTimer(timeout4, buf4, handle, NULL); + myHandler.addTimer(timeoutTime, &testCallback.pTimer1Callback, handle, NULL); + myHandler.addTimer(timeout2, &testCallback.pTimer2Callback, handle, NULL); + myHandler.addTimer(timeout3, &testCallback.pTimer3Callback, handle, NULL); + myHandler.addTimer(timeout4, &testCallback.pTimer4Callback, handle, NULL); myHandler.start_listenting(); } diff --git a/PluginRoutingInterfaceAsync/include/CAmRoutingSenderAsync.h b/PluginRoutingInterfaceAsync/include/CAmRoutingSenderAsync.h index ac78eba..c0da207 100644 --- a/PluginRoutingInterfaceAsync/include/CAmRoutingSenderAsync.h +++ b/PluginRoutingInterfaceAsync/include/CAmRoutingSenderAsync.h @@ -22,6 +22,7 @@ #include "IAmRoutingReceiverShadow.h" #include #include +#include namespace am { diff --git a/PluginRoutingInterfaceAsync/test/CAmRoutingReceiverAsync.cpp b/PluginRoutingInterfaceAsync/test/CAmRoutingReceiverAsync.cpp index 55e706a..13c9806 100644 --- a/PluginRoutingInterfaceAsync/test/CAmRoutingReceiverAsync.cpp +++ b/PluginRoutingInterfaceAsync/test/CAmRoutingReceiverAsync.cpp @@ -79,13 +79,12 @@ void CAmEnvironment::SetUp() timespec t; t.tv_nsec = 0; - t.tv_sec = 2; + t.tv_sec = 1; sh_timerHandle_t handle; - IAmShTimerCallBack *buf = &ptimerCallback; //lets use a timeout so the test will finish - pSocketHandler.addTimer(t, buf, handle, (void*) NULL); + pSocketHandler.addTimer(t, &ptimerCallback, handle, (void*) NULL); pSocketHandler.start_listenting(); } @@ -155,11 +154,8 @@ void CAmEnvironment::timerCallback(sh_timerHandle_t handle, void *userData) { (void) handle; (void) userData; + pSocketHandler.restartTimer(handle); pSocketHandler.stop_listening(); - timespec t; - t.tv_nsec = 0; - t.tv_sec = 2; - pSocketHandler.restartTimer(handle, t); } void CAmRoutingReceiverAsync::TearDown() @@ -410,7 +406,7 @@ TEST_F(CAmRoutingReceiverAsync,connectAbortTooLate) am_sinkID_t sinkID = 1; am_ConnectionFormat_e format = CF_GENIVI_ANALOG; - EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(1); + EXPECT_CALL(pReceiveInterface,ackConnect(_,connectionID,E_OK)).Times(2); ASSERT_EQ(E_OK, pRouter->asyncConnect(handle,connectionID,sourceID,sinkID,format)); sleep(3); ASSERT_EQ(E_NON_EXISTENT, pRouter->asyncAbort(handle)); diff --git a/include/shared/CAmDbusWrapper.h b/include/shared/CAmDbusWrapper.h index 0a83bb4..020cf89 100644 --- a/include/shared/CAmDbusWrapper.h +++ b/include/shared/CAmDbusWrapper.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "config.h" #include "CAmSocketHandler.h" diff --git a/include/shared/CAmDltWrapper.h b/include/shared/CAmDltWrapper.h index 7a55626..c9dd4c2 100644 --- a/include/shared/CAmDltWrapper.h +++ b/include/shared/CAmDltWrapper.h @@ -21,6 +21,7 @@ #include "config.h" #include +#include #ifdef WITH_DLT #include @@ -88,7 +89,6 @@ namespace am class CAmDltWrapper { public: - static pthread_mutex_t logMutex; static CAmDltWrapper* instance(const bool enableNoDLTDebug = false); void registerApp(const char *appid, const char * description); void registerContext(DltContext& handle, const char *contextid, const char * description); @@ -118,6 +118,7 @@ private: DltContext mDltContext; //!< the default context DltContextData mDltContextData; //!< contextdata static CAmDltWrapper* mpDLTWrapper; //!< pointer to the wrapper instance + static pthread_mutex_t mMutex; }; @@ -137,11 +138,9 @@ inline CAmDltWrapper* getWrapper() template void logInfo(T value) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); inst->init(DLT_LOG_INFO); inst->append(value); inst->send(); - pthread_mutex_unlock(&inst->logMutex); } /** @@ -152,12 +151,12 @@ template void logInfo(T value) template void logInfo(T value, T1 value1) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -169,13 +168,13 @@ template void logInfo(T value, T1 value1) template void logInfo(T value, T1 value1, T2 value2) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); inst->append(value2); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -188,14 +187,14 @@ template void logInfo(T value, T1 value1, template void logInfo(T value, T1 value1, T2 value2, T3 value3) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); inst->append(value2); inst->append(value3); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -209,7 +208,7 @@ template void logInfo(T value template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -217,7 +216,7 @@ template void lo inst->append(value3); inst->append(value4); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -232,7 +231,7 @@ template void lo template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -241,7 +240,7 @@ templateappend(value4); inst->append(value5); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -257,7 +256,7 @@ template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -267,7 +266,7 @@ templateappend(value5); inst->append(value6); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -284,7 +283,7 @@ template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -295,7 +294,7 @@ templateappend(value6); inst->append(value7); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -313,7 +312,7 @@ template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -325,7 +324,7 @@ templateappend(value7); inst->append(value8); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -344,7 +343,7 @@ template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -357,7 +356,7 @@ templateappend(value8); inst->append(value9); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -377,7 +376,7 @@ template void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_INFO); inst->append(value); inst->append(value1); @@ -391,7 +390,7 @@ templateappend(value9); inst->append(value10); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -401,11 +400,11 @@ template void logError(T value) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -416,12 +415,12 @@ template void logError(T value) template void logError(T value, T1 value1) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -433,13 +432,13 @@ template void logError(T value, T1 value1) template void logError(T value, T1 value1, T2 value2) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); inst->append(value2); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -452,14 +451,14 @@ template void logError(T value, T1 value1, template void logError(T value, T1 value1, T2 value2, T3 value3) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); inst->append(value2); inst->append(value3); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -473,7 +472,7 @@ template void logError(T valu template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -481,7 +480,7 @@ template void lo inst->append(value3); inst->append(value4); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -496,7 +495,7 @@ template void lo template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -505,7 +504,7 @@ templateappend(value4); inst->append(value5); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -521,7 +520,7 @@ template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -531,7 +530,7 @@ templateappend(value5); inst->append(value6); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -548,7 +547,7 @@ template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -559,7 +558,7 @@ templateappend(value6); inst->append(value7); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -577,7 +576,7 @@ template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -589,7 +588,7 @@ templateappend(value7); inst->append(value8); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -608,7 +607,7 @@ template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -621,7 +620,7 @@ templateappend(value8); inst->append(value9); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -641,7 +640,7 @@ template void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(DLT_LOG_ERROR); inst->append(value); inst->append(value1); @@ -655,7 +654,7 @@ templateappend(value9); inst->append(value10); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -667,11 +666,11 @@ template void log(DltContext* const context, DltLogLevelType loglevel, T value) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -684,12 +683,12 @@ template void log(DltContext* const context, DltLogLevelType logleve template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -703,13 +702,13 @@ template void log(DltContext* const context, DltLogLeve template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); inst->append(value2); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -724,14 +723,14 @@ template void log(DltContext* const contex template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); inst->append(value2); inst->append(value3); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -747,7 +746,7 @@ template void log(DltContext* template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); @@ -755,7 +754,7 @@ template void lo inst->append(value3); inst->append(value4); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -772,7 +771,7 @@ template void lo template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); @@ -781,7 +780,7 @@ templateappend(value4); inst->append(value5); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -799,7 +798,7 @@ template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); @@ -809,7 +808,7 @@ templateappend(value5); inst->append(value6); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -828,7 +827,7 @@ template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); @@ -839,7 +838,7 @@ templateappend(value6); inst->append(value7); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } /** @@ -859,7 +858,7 @@ template void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8) { CAmDltWrapper* inst(getWrapper()); - pthread_mutex_lock(&inst->logMutex); + inst->init(loglevel, context); inst->append(value); inst->append(value1); @@ -871,7 +870,7 @@ templateappend(value7); inst->append(value8); inst->send(); - pthread_mutex_unlock(&inst->logMutex); + } } diff --git a/include/shared/CAmSocketHandler.h b/include/shared/CAmSocketHandler.h index 83d2352..990ef0f 100644 --- a/include/shared/CAmSocketHandler.h +++ b/include/shared/CAmSocketHandler.h @@ -27,10 +27,13 @@ #include #include +#include //todo: remove me namespace am { -static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop +#define MAX_NS 1000000000L + +static volatile sig_atomic_t gDispatchDone = 1; //this global is used to stop the mainloop typedef uint16_t sh_timerHandle_t; //! rListTimerIter; - - class CAmShSubstractTime //! rListTimerIter; //! mListPollfd_t; //! mListPoll_t; //!Call(row.pollfdValue, row.handle, row.userData); - } - }; - - class CAmShCallPrep - { - public: - CAmShCallPrep(){}; - void operator()(sh_poll_s& row) - { - if (row.prepareCB) - row.prepareCB->Call(row.handle, row.userData); - } - }; - - class CAmShCallTimer - { - public: - CAmShCallTimer(){}; - void operator()(sh_timer_s& row) - { - row.callback->Call(row.handle, row.userData); - } - }; - - class CAmShCountdownUp - { - public: - CAmShCountdownUp(){}; - bool operator()(sh_timer_s& row) - { - if (row.countdown.tv_nsec == 0 && row.countdown.tv_sec == 0) - return (true); - return (false); - } - }; - - class CAmShCountdownSame - { - private: - sh_timer_s mCompareValue; - public: - CAmShCountdownSame(sh_timer_s& a) : - mCompareValue(a) - {} - bool operator()(const sh_timer_s& b) - { - return ((mCompareValue.countdown.tv_sec == b.countdown.tv_sec) && (mCompareValue.countdown.tv_nsec == b.countdown.tv_nsec) ? true : false); - } - }; - bool fdIsValid(const int fd) const; - void initTimer(); void timerUp(); void timerCorrection(); - int timespec2ms(const timespec& time); timespec* insertTime(timespec& buffertime); - static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b) + /** + * compares countdown values + * @param a + * @param b + * @return true if b greater a + */ + inline static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b) { return ((a.countdown.tv_sec == b.countdown.tv_sec) ? (a.countdown.tv_nsec < b.countdown.tv_nsec) : (a.countdown.tv_sec < b.countdown.tv_sec)); } @@ -246,7 +171,7 @@ private: * @param b * @return subtracted value */ - static timespec timespecSub(const timespec& a, const timespec& b) + inline static timespec timespecSub(const timespec& a, const timespec& b) { timespec result; @@ -259,7 +184,7 @@ private: result.tv_sec = a.tv_sec - b.tv_sec; if (a.tv_nsec < b.tv_nsec) { - result.tv_nsec = a.tv_nsec + 1000000000L - b.tv_nsec; + result.tv_nsec = a.tv_nsec + MAX_NS - b.tv_nsec; result.tv_sec--; /* Borrow a second. */ } else @@ -270,20 +195,32 @@ private: return (result); } - static timespec timespecAdd(const timespec& a, const timespec b) + /** + * adds timespec values + * @param a + * @param b + * @return the added values + */ + inline timespec timespecAdd(const timespec& a, const timespec& b) { timespec result; result.tv_sec = a.tv_sec + b.tv_sec; result.tv_nsec = a.tv_nsec + b.tv_nsec; - if (result.tv_nsec >= 1000000000L) + if (result.tv_nsec >= MAX_NS) { result.tv_sec++; - result.tv_nsec = result.tv_nsec - 1000000000L; + result.tv_nsec = result.tv_nsec - MAX_NS; } return (result); } - static int timespecCompare(const timespec& a, const timespec& b) + /** + * comapares timespec values + * @param a + * @param b + * @return + */ + inline int timespecCompare(const timespec& a, const timespec& b) { //less if (a.tv_sec < b.tv_sec) @@ -302,11 +239,21 @@ private: return (0); } + /** + * functor to return all fired events + * @param a + * @return + */ inline static bool eventFired(const pollfd& a) { return (a.revents == 0 ? false : true); } + /** + * functor to help find the items that do not need dispatching + * @param a + * @return + */ inline static bool noDispatching(const sh_poll_s& a) { //remove from list of there is no checkCB @@ -315,6 +262,11 @@ private: return (!a.checkCB->Call(a.handle, a.userData)); } + /** + * checks if dispatching is already finished + * @param a + * @return + */ inline static bool dispatchingFinished(const sh_poll_s& a) { //remove from list of there is no dispatchCB @@ -323,14 +275,129 @@ private: return (!a.dispatchCB->Call(a.handle, a.userData)); } - mListPollfd_t mfdPollingArray; - mListPoll_t mListPoll; + class CAmShCopyPollfd //!< functor to copy filedescriptors into the poll array + { + private: + mListPollfd_t& mArray; + public: + CAmShCopyPollfd(mListPollfd_t& dest) : + mArray(dest) + { + } + void operator()(const sh_poll_s& row) + { + pollfd temp = row.pollfdValue; + temp.revents = 0; + mArray.push_back(temp); + } + }; + + class CAmShCallFire //!< functor to call the firecallbacks + { + public: + CAmShCallFire() + { + } + ; + void operator()(sh_poll_s& row) + { + row.firedCB->Call(row.pollfdValue, row.handle, row.userData); + } + }; + + class CAmShCallPrep //!< functor to call the preparation callbacks + { + public: + CAmShCallPrep() + { + } + ; + void operator()(sh_poll_s& row) + { + if (row.prepareCB) + row.prepareCB->Call(row.handle, row.userData); + } + }; + + class CAmShCallTimer //!Call(row.handle, row.userData); + } + }; + + class CAmShCountdownUp //! mListTimer; //! mListActiveTimer; //!::iterator it(mListActiveTimer.begin()); +// for(;it!=mListActiveTimer.end();++it) +// { +// std::cout<< "Handle " << it->handle << "sec " << it->countdown.tv_sec << "nsec " << it->countdown.tv_nsec< class TAmShPollFired: public IAmShPollFired @@ -386,12 +460,16 @@ private: public: TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // - mFunction(function){}; + mFunction(function) + { + } + ; virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData) { (*mInstance.*mFunction)(pollfd, handle, userData); - }; + } + ; }; /** @@ -406,12 +484,16 @@ private: public: TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // - mFunction(function){}; + mFunction(function) + { + } + ; virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); - }; + } + ; }; /** @@ -426,12 +508,16 @@ private: public: TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) : mInstance(instance), // - mFunction(function){}; + mFunction(function) + { + } + ; virtual bool Call(const sh_pollHandle_t handle, void* userData) { return ((*mInstance.*mFunction)(handle, userData)); - }; + } + ; }; } /* namespace am */ #endif /* SOCKETHANDLER_H_ */ -- cgit v1.2.1