summaryrefslogtreecommitdiff
path: root/src/cam/event_loop.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-11-08 01:26:33 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-11-15 22:21:30 +0200
commitf49e93338b6309a66b558dea40d114925f01e993 (patch)
treea60dfc235805559cb53fcc27115ebe7ee18955a6 /src/cam/event_loop.cpp
parentd767c84022559e55708f24a3a264853c0142135e (diff)
cam: event_loop: Add deferred calls support
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> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/cam/event_loop.cpp')
-rw-r--r--src/cam/event_loop.cpp29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/cam/event_loop.cpp b/src/cam/event_loop.cpp
index 13f2583d..94c5d1d3 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();
+ }
+}