Files
external_libcamera/src/libcamera/timer.cpp
Laurent Pinchart 93e72b695e libcamera: Move internal headers to include/libcamera/internal/
The libcamera internal headers are located in src/libcamera/include/.
The directory is added to the compiler headers search path with a meson
include_directories() directive, and internal headers are included with
(e.g. for the internal semaphore.h header)

  #include "semaphore.h"

All was well, until libcxx decided to implement the C++20
synchronization library. The __threading_support header gained a

  #include <semaphore.h>

to include the pthread's semaphore support. As include_directories()
adds src/libcamera/include/ to the compiler search path with -I, the
internal semaphore.h is included instead of the pthread version.
Needless to say, the compiler isn't happy.

Three options have been considered to fix this issue:

- Use -iquote instead of -I. The -iquote option instructs gcc to only
  consider the header search path for headers included with the ""
  version. Meson unfortunately doesn't support this option.

- Rename the internal semaphore.h header. This was deemed to be the
  beginning of a long whack-a-mole game, where namespace clashes with
  system libraries would appear over time (possibly dependent on
  particular system configurations) and would need to be constantly
  fixed.

- Move the internal headers to another directory to create a unique
  namespace through path components. This causes lots of churn in all
  the existing source files through the all project.

The first option would be best, but isn't available to us due to missing
support in meson. Even if -iquote support was added, we would need to
fix the problem before a new version of meson containing the required
support would be released.

The third option is thus the only practical solution available. Bite the
bullet, and do it, moving headers to include/libcamera/internal/.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Jacopo Mondi <jacopo@jmondi.org>
2020-05-16 03:38:11 +03:00

186 lines
4.0 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* timer.cpp - Generic timer
*/
#include <libcamera/timer.h>
#include <chrono>
#include <libcamera/camera_manager.h>
#include <libcamera/event_dispatcher.h>
#include "libcamera/internal/log.h"
#include "libcamera/internal/message.h"
#include "libcamera/internal/thread.h"
#include "libcamera/internal/utils.h"
/**
* \file timer.h
* \brief Generic timer
*/
namespace libcamera {
LOG_DEFINE_CATEGORY(Timer)
/**
* \class Timer
* \brief Single-shot timer interface
*
* The Timer class models a single-shot timer that is started with start() and
* emits the \ref timeout signal when it times out.
*
* Once started the timer will run until it times out. It can be stopped with
* stop(), and once it times out or is stopped, can be started again with
* start().
*
* The timer deadline is specified as either a duration in milliseconds or an
* absolute time point. If the deadline is set to the current time or to the
* past, the timer will time out immediately when execution returns to the
* event loop of the timer's thread.
*
* Timers run in the thread they belong to, and thus emit the \a ref timeout
* signal from that thread. To avoid race conditions they must not be started
* or stopped from a different thread, attempts to do so will be rejected and
* logged, and may cause undefined behaviour.
*/
/**
* \brief Construct a timer
* \param[in] parent The parent Object
*/
Timer::Timer(Object *parent)
: Object(parent), running_(false)
{
}
Timer::~Timer()
{
stop();
}
/**
* \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.
*
* \context This function is \threadbound.
*/
/**
* \brief Start or restart the timer with a timeout of \a duration
* \param[in] duration The timer duration in milliseconds
*
* If the timer is already running it will be stopped and restarted.
*
* \context This function is \threadbound.
*/
void Timer::start(std::chrono::milliseconds duration)
{
start(utils::clock::now() + duration);
}
/**
* \brief Start or restart the timer with a \a deadline
* \param[in] deadline The timer deadline
*
* If the timer is already running it will be stopped and restarted.
*
* \context This function is \threadbound.
*/
void Timer::start(std::chrono::steady_clock::time_point deadline)
{
if (Thread::current() != thread()) {
LOG(Timer, Error) << "Timer can't be started from another thread";
return;
}
deadline_ = deadline;
LOG(Timer, Debug)
<< "Starting timer " << this << ": deadline "
<< utils::time_point_to_string(deadline_);
if (isRunning())
unregisterTimer();
registerTimer();
}
/**
* \brief Stop the timer
*
* After this function returns the timer is guaranteed not to emit the
* \ref timeout signal.
*
* If the timer is not running this function performs no operation.
*
* \context This function is \threadbound.
*/
void Timer::stop()
{
if (!isRunning())
return;
if (Thread::current() != thread()) {
LOG(Timer, Error) << "Timer can't be stopped from another thread";
return;
}
unregisterTimer();
}
void Timer::registerTimer()
{
thread()->eventDispatcher()->registerTimer(this);
running_ = true;
}
void Timer::unregisterTimer()
{
running_ = false;
thread()->eventDispatcher()->unregisterTimer(this);
}
/**
* \brief Check if the timer is running
* \return True if the timer is running, false otherwise
*/
bool Timer::isRunning() const
{
return running_;
}
/**
* \fn Timer::deadline()
* \brief Retrieve the timer deadline
* \return The timer deadline
*/
/**
* \var Timer::timeout
* \brief Signal emitted when the timer times out
*
* The timer pointer is passed as a parameter.
*/
void Timer::message(Message *msg)
{
if (msg->type() == Message::ThreadMoveMessage) {
if (isRunning()) {
unregisterTimer();
invokeMethod(&Timer::registerTimer,
ConnectionTypeQueued);
}
}
Object::message(msg);
}
} /* namespace libcamera */