[libcamera-devel] [PATCH 3/6] libcamera: object: Add ObjectConditionVariable class
Jacopo Mondi
jacopo at jmondi.org
Sun Oct 27 21:33:32 CET 2019
Add ObjectConditionVariable class that implements an Object
synchronization primitive used to synchronize message delivery between
Object running in different execution contexts.
Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
---
include/libcamera/object.h | 19 ++++++++++++++++
src/libcamera/object.cpp | 46 ++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+)
diff --git a/include/libcamera/object.h b/include/libcamera/object.h
index be07611c25c6..c6477f18417d 100644
--- a/include/libcamera/object.h
+++ b/include/libcamera/object.h
@@ -7,8 +7,10 @@
#ifndef __LIBCAMERA_OBJECT_H__
#define __LIBCAMERA_OBJECT_H__
+#include <condition_variable>
#include <list>
#include <memory>
+#include <mutex>
#include <vector>
#include <libcamera/bound_method.h>
@@ -28,6 +30,23 @@ enum InvocationType {
InvocationTypeBlocking,
};
+class ObjectConditionVariable
+{
+public:
+ ObjectConditionVariable()
+ : delivered_(false)
+ {
+ }
+
+ void notifyReception();
+ void waitDelivery();
+
+private:
+ std::condition_variable cv_;
+ std::mutex mutex_;
+ bool delivered_;
+};
+
class Object
{
public:
diff --git a/src/libcamera/object.cpp b/src/libcamera/object.cpp
index d7c9ac970d1c..88054aba3454 100644
--- a/src/libcamera/object.cpp
+++ b/src/libcamera/object.cpp
@@ -52,6 +52,52 @@ LOG_DEFINE_CATEGORY(Object)
* the called method is not executed
*/
+/**
+ * \class ObjectConditionVariable
+ * \brief Object synchronization primitive that wraps a std::condition_variable
+ *
+ * The ObjectConditionVariable class provides an object synchronization
+ * primitive to synchronize Object running in different execution context.
+ *
+ * It wraps an std::condition_variable that protects a shared variable.
+ *
+ * The class is used to implement blocking message delivery between Object
+ * instances running in different execution context.
+ */
+
+/**
+ * \fn ObjectConditionVariable::ObjectConditionVariable()
+ * \brief Create an ObjectConditionVariable with the shared variable initialized
+ * to false
+ */
+
+/**
+ * \brief Notify the ObjectConditionVariable to signal message reception
+ */
+void ObjectConditionVariable::notifyReception()
+{
+ {
+ MutexLocker locker(mutex_);
+ delivered_ = true;
+ }
+ cv_.notify_one();
+}
+
+/**
+ * \brief Wait for the ObjectConditionVariable to be signaled
+ *
+ * Wait for the ObjectConditionVariable to be signaled by suspending the Object
+ * on the wrapped std::condition_variable. If the shared condition
+ * variable has already been set to true by the Object that was meant to
+ * signal the ObjectConditionVariable before waitDelivery() is invoked,
+ * the operation returns immediately.
+ */
+void ObjectConditionVariable::waitDelivery()
+{
+ MutexLocker locker(mutex_);
+ cv_.wait(locker, [&] { return delivered_; });
+}
+
/**
* \class Object
* \brief Base object to support automatic signal disconnection
--
2.23.0
More information about the libcamera-devel
mailing list