[libcamera-devel,v1,4/5] cam: event_loop: Add deferred calls support
diff mbox series

Message ID 20201113063815.10288-5-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • cam: Move request processing to main thread
Related show

Commit Message

Laurent Pinchart Nov. 13, 2020, 6:38 a.m. UTC
Add a deferred cals queue to the EventLoop class to support queuing
calls from a different thread and processing them in the event loop's
thread.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/cam/event_loop.cpp | 29 ++++++++++++++++++++++++++++-
 src/cam/event_loop.h   |  9 +++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

Comments

Kieran Bingham Nov. 13, 2020, 10:06 a.m. UTC | #1
On 13/11/2020 06:38, Laurent Pinchart wrote:
> Add a deferred cals queue to the EventLoop class to support queuing
> calls from a different thread and processing them in the event loop's
> thread.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/cam/event_loop.cpp | 29 ++++++++++++++++++++++++++++-
>  src/cam/event_loop.h   |  9 +++++++++
>  2 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp
> index 13f2583da0a1..94c5d1d36245 100644
> --- a/src/cam/event_loop.cpp
> +++ b/src/cam/event_loop.cpp
> @@ -40,8 +40,10 @@ int EventLoop::exec()
>  	exitCode_ = -1;
>  	exit_.store(false, std::memory_order_release);
>  
> -	while (!exit_.load(std::memory_order_acquire))
> +	while (!exit_.load(std::memory_order_acquire)) {
> +		dispatchCalls();
>  		event_base_loop(event_, EVLOOP_NO_EXIT_ON_EMPTY);
> +	}
>  
>  	return exitCode_;
>  }
> @@ -57,3 +59,28 @@ void EventLoop::interrupt()
>  {
>  	event_base_loopbreak(event_);
>  }
> +
> +void EventLoop::callLater(const std::function<void()> &func)
> +{
> +	{
> +		std::unique_lock<std::mutex> locker(lock_);
> +		calls_.push_back(func);
> +	}
> +
> +	interrupt();
> +}
> +
> +void EventLoop::dispatchCalls()
> +{
> +	std::unique_lock<std::mutex> locker(lock_);
> +
> +	for (auto iter = calls_.begin(); iter != calls_.end(); ) {
> +		std::function<void()> call = std::move(*iter);
> +
> +		iter = calls_.erase(iter);
> +
> +		locker.unlock();
> +		call();
> +		locker.lock();
> +	}
> +}
> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h
> index b1c6bd103080..408073c50594 100644
> --- a/src/cam/event_loop.h
> +++ b/src/cam/event_loop.h
> @@ -8,6 +8,9 @@
>  #define __CAM_EVENT_LOOP_H__
>  
>  #include <atomic>
> +#include <functional>
> +#include <list>
> +#include <mutex>
>  
>  struct event_base;
>  
> @@ -22,6 +25,8 @@ public:
>  	int exec();
>  	void exit(int code = 0);
>  
> +	void callLater(const std::function<void()> &func);
> +
>  private:
>  	static EventLoop *instance_;
>  
> @@ -29,7 +34,11 @@ private:
>  	std::atomic<bool> exit_;
>  	int exitCode_;
>  
> +	std::list<std::function<void()>> calls_;
> +	std::mutex lock_;
> +
>  	void interrupt();
> +	void dispatchCalls();
>  };
>  
>  #endif /* __CAM_EVENT_LOOP_H__ */
>
Niklas Söderlund Nov. 13, 2020, 10:36 a.m. UTC | #2
Hi Laurent,

Thanks for your educational material.

On 2020-11-13 08:38:14 +0200, Laurent Pinchart wrote:
> Add a deferred cals queue to the EventLoop class to support queuing
> calls from a different thread and processing them in the event loop's
> thread.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

The concept of std::function<> and std::bind() was new to me but works 
real neat!

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  src/cam/event_loop.cpp | 29 ++++++++++++++++++++++++++++-
>  src/cam/event_loop.h   |  9 +++++++++
>  2 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp
> index 13f2583da0a1..94c5d1d36245 100644
> --- a/src/cam/event_loop.cpp
> +++ b/src/cam/event_loop.cpp
> @@ -40,8 +40,10 @@ int EventLoop::exec()
>  	exitCode_ = -1;
>  	exit_.store(false, std::memory_order_release);
>  
> -	while (!exit_.load(std::memory_order_acquire))
> +	while (!exit_.load(std::memory_order_acquire)) {
> +		dispatchCalls();
>  		event_base_loop(event_, EVLOOP_NO_EXIT_ON_EMPTY);
> +	}
>  
>  	return exitCode_;
>  }
> @@ -57,3 +59,28 @@ void EventLoop::interrupt()
>  {
>  	event_base_loopbreak(event_);
>  }
> +
> +void EventLoop::callLater(const std::function<void()> &func)
> +{
> +	{
> +		std::unique_lock<std::mutex> locker(lock_);
> +		calls_.push_back(func);
> +	}
> +
> +	interrupt();
> +}
> +
> +void EventLoop::dispatchCalls()
> +{
> +	std::unique_lock<std::mutex> locker(lock_);
> +
> +	for (auto iter = calls_.begin(); iter != calls_.end(); ) {
> +		std::function<void()> call = std::move(*iter);
> +
> +		iter = calls_.erase(iter);
> +
> +		locker.unlock();
> +		call();
> +		locker.lock();
> +	}
> +}
> diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h
> index b1c6bd103080..408073c50594 100644
> --- a/src/cam/event_loop.h
> +++ b/src/cam/event_loop.h
> @@ -8,6 +8,9 @@
>  #define __CAM_EVENT_LOOP_H__
>  
>  #include <atomic>
> +#include <functional>
> +#include <list>
> +#include <mutex>
>  
>  struct event_base;
>  
> @@ -22,6 +25,8 @@ public:
>  	int exec();
>  	void exit(int code = 0);
>  
> +	void callLater(const std::function<void()> &func);
> +
>  private:
>  	static EventLoop *instance_;
>  
> @@ -29,7 +34,11 @@ private:
>  	std::atomic<bool> exit_;
>  	int exitCode_;
>  
> +	std::list<std::function<void()>> calls_;
> +	std::mutex lock_;
> +
>  	void interrupt();
> +	void dispatchCalls();
>  };
>  
>  #endif /* __CAM_EVENT_LOOP_H__ */
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch
diff mbox series

diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp
index 13f2583da0a1..94c5d1d36245 100644
--- a/src/cam/event_loop.cpp
+++ b/src/cam/event_loop.cpp
@@ -40,8 +40,10 @@  int EventLoop::exec()
 	exitCode_ = -1;
 	exit_.store(false, std::memory_order_release);
 
-	while (!exit_.load(std::memory_order_acquire))
+	while (!exit_.load(std::memory_order_acquire)) {
+		dispatchCalls();
 		event_base_loop(event_, EVLOOP_NO_EXIT_ON_EMPTY);
+	}
 
 	return exitCode_;
 }
@@ -57,3 +59,28 @@  void EventLoop::interrupt()
 {
 	event_base_loopbreak(event_);
 }
+
+void EventLoop::callLater(const std::function<void()> &func)
+{
+	{
+		std::unique_lock<std::mutex> locker(lock_);
+		calls_.push_back(func);
+	}
+
+	interrupt();
+}
+
+void EventLoop::dispatchCalls()
+{
+	std::unique_lock<std::mutex> locker(lock_);
+
+	for (auto iter = calls_.begin(); iter != calls_.end(); ) {
+		std::function<void()> call = std::move(*iter);
+
+		iter = calls_.erase(iter);
+
+		locker.unlock();
+		call();
+		locker.lock();
+	}
+}
diff --git a/src/cam/event_loop.h b/src/cam/event_loop.h
index b1c6bd103080..408073c50594 100644
--- a/src/cam/event_loop.h
+++ b/src/cam/event_loop.h
@@ -8,6 +8,9 @@ 
 #define __CAM_EVENT_LOOP_H__
 
 #include <atomic>
+#include <functional>
+#include <list>
+#include <mutex>
 
 struct event_base;
 
@@ -22,6 +25,8 @@  public:
 	int exec();
 	void exit(int code = 0);
 
+	void callLater(const std::function<void()> &func);
+
 private:
 	static EventLoop *instance_;
 
@@ -29,7 +34,11 @@  private:
 	std::atomic<bool> exit_;
 	int exitCode_;
 
+	std::list<std::function<void()>> calls_;
+	std::mutex lock_;
+
 	void interrupt();
+	void dispatchCalls();
 };
 
 #endif /* __CAM_EVENT_LOOP_H__ */