[libcamera-devel] [PATCH v8 1/4] cam: event_loop: Add timer events to event loop

Jacopo Mondi jacopo at jmondi.org
Wed May 18 18:27:57 CEST 2022


Hi Eric,

On Thu, May 05, 2022 at 04:18:48PM +0100, Eric Curtin via libcamera-devel wrote:
> 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.
>
> Also delete all events in the list, before we event_base_loopbreak.
>
> Signed-off-by: Eric Curtin <ecurtin at redhat.com>

Kieran pointed me to your series, and I've just tested it and I can
say it works fine for me

Tested-by: Jacopo Mondi <jacopo at jmondi.org>

Hope it can be merged soon even if I haven't really looked at the patches
though, but since you're at v8 I assume they're in pretty good shape!

Thanks
   j

> ---
>  src/cam/event_loop.cpp | 33 +++++++++++++++++++++++++++++++++
>  src/cam/event_loop.h   |  6 ++++++
>  2 files changed, 39 insertions(+)
>
> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp
> index e25784c0..181c971c 100644
> --- a/src/cam/event_loop.cpp
> +++ b/src/cam/event_loop.cpp
> @@ -47,6 +47,10 @@ int EventLoop::exec()
>  void EventLoop::exit(int code)
>  {
>  	exitCode_ = code;
> +	for (auto const &e : events_) {
> +		event_del(e->event_);
> +	}
> +
>  	event_base_loopbreak(base_);
>  }
>
> @@ -84,6 +88,35 @@ void EventLoop::addEvent(int fd, EventType type,
>  	events_.push_back(std::move(event));
>  }
>
> +void EventLoop::toTimeval(const std::chrono::milliseconds d, struct timeval &tv)
> +{
> +	tv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
> +	tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d).count();
> +}
> +
> +void EventLoop::addTimerEvent(const std::chrono::milliseconds d,
> +			      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;
> +	toTimeval(d, tv);
> +
> +	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)
>  {
> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h
> index a4613eb2..89215dce 100644
> --- a/src/cam/event_loop.h
> +++ b/src/cam/event_loop.h
> @@ -14,6 +14,8 @@
>
>  #include <event2/util.h>
>
> +using namespace std::chrono_literals;
> +
>  struct event_base;
>
>  class EventLoop
> @@ -37,6 +39,9 @@ public:
>  	void addEvent(int fd, EventType type,
>  		      const std::function<void()> &handler);
>
> +	void addTimerEvent(const std::chrono::milliseconds d,
> +			   const std::function<void()> &handler);
> +
>  private:
>  	struct Event {
>  		Event(const std::function<void()> &callback);
> @@ -57,6 +62,7 @@ private:
>  	std::list<std::unique_ptr<Event>> events_;
>  	std::mutex lock_;
>
> +	void toTimeval(const std::chrono::milliseconds d, struct timeval &tv);
>  	static void dispatchCallback(evutil_socket_t fd, short flags,
>  				     void *param);
>  	void dispatchCall();
> --
> 2.35.1
>


More information about the libcamera-devel mailing list