summaryrefslogtreecommitdiff
path: root/NodeStateAccess/SimpleTimer.cpp
blob: 5aea0b51483139d091afc8b838349382bc3df71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
}