mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-24 17:15:07 +03:00
cam: event_loop: Add timer events to event loop
Extend the EventLoop class to support periodic timer events. This can be used to run tasks periodically, such as handling the event loop of SDL. Signed-off-by: Eric Curtin <ecurtin@redhat.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Tested-by: Jacopo Mondi <jacopo@jmondi.org> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
parent
48e991476d
commit
a5844adb7b
2 changed files with 30 additions and 1 deletions
|
@ -84,6 +84,30 @@ void EventLoop::addFdEvent(int fd, EventType type,
|
|||
events_.push_back(std::move(event));
|
||||
}
|
||||
|
||||
void EventLoop::addTimerEvent(const std::chrono::microseconds period,
|
||||
const std::function<void()> &callback)
|
||||
{
|
||||
std::unique_ptr<Event> event = std::make_unique<Event>(callback);
|
||||
event->event_ = event_new(base_, -1, EV_PERSIST, &EventLoop::Event::dispatch,
|
||||
event.get());
|
||||
if (!event->event_) {
|
||||
std::cerr << "Failed to create timer event" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = period.count() / 1000000ULL;
|
||||
tv.tv_usec = period.count() % 1000000ULL;
|
||||
|
||||
int ret = event_add(event->event_, &tv);
|
||||
if (ret < 0) {
|
||||
std::cerr << "Failed to add timer event" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
events_.push_back(std::move(event));
|
||||
}
|
||||
|
||||
void EventLoop::dispatchCallback([[maybe_unused]] evutil_socket_t fd,
|
||||
[[maybe_unused]] short flags, void *param)
|
||||
{
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include <event2/util.h>
|
||||
|
@ -37,6 +38,10 @@ public:
|
|||
void addFdEvent(int fd, EventType type,
|
||||
const std::function<void()> &handler);
|
||||
|
||||
using duration = std::chrono::steady_clock::duration;
|
||||
void addTimerEvent(const std::chrono::microseconds period,
|
||||
const std::function<void()> &handler);
|
||||
|
||||
private:
|
||||
struct Event {
|
||||
Event(const std::function<void()> &callback);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue