diff options
author | Monika Forstner <Monika_Forstner@mentor.com> | 2020-09-16 10:20:03 +0200 |
---|---|---|
committer | Monika Forstner <Monika_Forstner@mentor.com> | 2020-09-16 10:50:27 +0200 |
commit | 2fb502b4bc23aa13fee1db3395b3cae5e34cc871 (patch) | |
tree | d9cd2c64522f190500dc53291b4fbcf5100b9720 /NodeStateAccess/SimpleTimer.cpp | |
parent | 253d10aada6bf5cdfe307aaff55363f08194c85e (diff) | |
download | node-state-manager-release_NSM_CommonAPI.tar.gz |
NSM Release 3.0release_NSM_CommonAPI
Diffstat (limited to 'NodeStateAccess/SimpleTimer.cpp')
-rw-r--r-- | NodeStateAccess/SimpleTimer.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/NodeStateAccess/SimpleTimer.cpp b/NodeStateAccess/SimpleTimer.cpp new file mode 100644 index 0000000..5aea0b5 --- /dev/null +++ b/NodeStateAccess/SimpleTimer.cpp @@ -0,0 +1,56 @@ +/********************************************************************************************************************** + * + * Copyright (C) 2017 BMW AG + * + * Implements a simple timer which calls a method after a specified timeout in ms. + * This timer can be canceled using "cancelTimer()" + * + * This source file is a part of the NodeStateAccess library (NSMA). + * The architecture requires that the NodeStateManager (NSM) is independent from the D-Bus binding and code generated by + * "CommonAPI". Therefore, the D-Bus communication and generated CommonAPI objects are handled inside of this library. + * The library offers the NSM an interface to use objects generated via CommonAPI. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + **********************************************************************************************************************/ + +#include "SimpleTimer.h" +#include <unistd.h> + +void SimpleTimer::cancelTimer() +{ + std::unique_lock<std::mutex> lock(mMutex); + timerLock--; + if(timerLock <= 0) + { + mCondVar.notify_all(); + } + lock.unlock(); +} + +void SimpleTimer::stopTimer() +{ + std::unique_lock<std::mutex> lock(mMutex); + timerLock = 0; + mCondVar.notify_all(); + lock.unlock(); +} + +void SimpleTimer::joinTimer() +{ + std::unique_lock<std::mutex> lock(mMutex); + while(joined == false) + { + NSMTriggerWatchdog(NsmWatchdogState_Sleep); + mCondVarJoin.wait(lock); + NSMTriggerWatchdog(NsmWatchdogState_Active); + } + lock.unlock(); +} + +SimpleTimer::~SimpleTimer() +{ + joinTimer(); +} |