summaryrefslogtreecommitdiff
path: root/src/libcamera/timer.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-09-14 03:40:47 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-09-14 15:05:45 +0300
commitcecfeed61e8bcb4b53c2ed8e1b26d8c8af38b8e3 (patch)
tree783fca9b32651efdca054ad152528b40f2566a74 /src/libcamera/timer.cpp
parent98dff063f2f497434978a46f9a676307365fd878 (diff)
libcamera: Switch to the std::chrono API
Replace the clock_gettime()-based API with durations expressed as integers with the std::chrono API. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/libcamera/timer.cpp')
-rw-r--r--src/libcamera/timer.cpp33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/libcamera/timer.cpp b/src/libcamera/timer.cpp
index c61d77e5..b3cea3da 100644
--- a/src/libcamera/timer.cpp
+++ b/src/libcamera/timer.cpp
@@ -7,7 +7,7 @@
#include <libcamera/timer.h>
-#include <time.h>
+#include <chrono>
#include <libcamera/camera_manager.h>
#include <libcamera/event_dispatcher.h>
@@ -15,6 +15,7 @@
#include "log.h"
#include "message.h"
#include "thread.h"
+#include "utils.h"
/**
* \file timer.h
@@ -42,7 +43,7 @@ LOG_DEFINE_CATEGORY(Timer)
* \param[in] parent The parent Object
*/
Timer::Timer(Object *parent)
- : Object(parent), interval_(0), deadline_(0)
+ : Object(parent)
{
}
@@ -52,22 +53,28 @@ Timer::~Timer()
}
/**
+ * \fn Timer::start(unsigned int msec)
* \brief Start or restart the timer with a timeout of \a msec
* \param[in] msec The timer duration in milliseconds
*
* If the timer is already running it will be stopped and restarted.
*/
-void Timer::start(unsigned int msec)
-{
- struct timespec tp;
- clock_gettime(CLOCK_MONOTONIC, &tp);
- interval_ = msec;
- deadline_ = tp.tv_sec * 1000000000ULL + tp.tv_nsec + msec * 1000000ULL;
+/**
+ * \brief Start or restart the timer with a timeout of \a interval
+ * \param[in] interval The timer duration in milliseconds
+ *
+ * If the timer is already running it will be stopped and restarted.
+ */
+void Timer::start(std::chrono::milliseconds interval)
+{
+ interval_ = interval;
+ deadline_ = utils::clock::now() + interval;
LOG(Timer, Debug)
<< "Starting timer " << this << " with interval "
- << msec << ": deadline " << deadline_;
+ << interval.count() << ": deadline "
+ << utils::time_point_to_string(deadline_);
registerTimer();
}
@@ -84,7 +91,7 @@ void Timer::stop()
{
unregisterTimer();
- deadline_ = 0;
+ deadline_ = utils::time_point();
}
void Timer::registerTimer()
@@ -103,7 +110,7 @@ void Timer::unregisterTimer()
*/
bool Timer::isRunning() const
{
- return deadline_ != 0;
+ return deadline_ != utils::time_point();
}
/**
@@ -115,7 +122,7 @@ bool Timer::isRunning() const
/**
* \fn Timer::deadline()
* \brief Retrieve the timer deadline
- * \return The timer deadline in nanoseconds
+ * \return The timer deadline
*/
/**
@@ -128,7 +135,7 @@ bool Timer::isRunning() const
void Timer::message(Message *msg)
{
if (msg->type() == Message::ThreadMoveMessage) {
- if (deadline_) {
+ if (isRunning()) {
unregisterTimer();
invokeMethod(&Timer::registerTimer);
}