Resource Tuner
Loading...
Searching...
No Matches
Timer.h
1// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2// SPDX-License-Identifier: BSD-3-Clause-Clear
3
4#ifndef RESOURCE_TUNER_TIMER_H
5#define RESOURCE_TUNER_TIMER_H
6
7#include <chrono>
8#include <thread>
9#include <atomic>
10#include <functional>
11#include <mutex>
12#include <condition_variable>
13
14#include "Logger.h"
15#include "ThreadPool.h"
16
20class Timer {
21private:
22 int64_t mDuration;
23 int8_t mIsRecurring;
24 std::atomic<int8_t> mTimerStop;
25 std::condition_variable mTimerCond;
26 std::mutex mTimerMutex; // !<Mutex to protect the condition variable.
27 std::function<void(void*)> mCallback;
28
29 void implementTimer();
30
31public:
32 // Create the ThreadPool as a static member so that all the Timer objects can share it.
33 static ThreadPool* mTimerThreadPool;
34
44 Timer(std::function<void(void*)> callBack, int8_t isRecurring=false);
45 ~Timer();
46
59 int8_t startTimer(int64_t duration);
60
64 void killTimer();
65};
66
67#endif
ThreadPool.
Definition ThreadPool.h:87
Timer.
Definition Timer.h:20
int64_t mDuration
Duration of the timer.
Definition Timer.h:22
std::condition_variable mTimerCond
Condition variable to stop the thread for the timer duration and wake up either after the duration ha...
Definition Timer.h:25
std::function< void(void *)> mCallback
Callback function to be called after timer is over.
Definition Timer.h:27
void killTimer()
Invalidates current timer.
int8_t startTimer(int64_t duration)
Starts the timer for the given duration in milliseconds.
std::atomic< int8_t > mTimerStop
Flag to let the timer thread know it has been killed.
Definition Timer.h:24
int8_t mIsRecurring
Flag to set a recurring timer. It is never modified. False by default.
Definition Timer.h:23
Timer(std::function< void(void *)> callBack, int8_t isRecurring=false)
Initialize the Timer.