@@ -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:
@@ -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
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@jmondi.org> --- include/libcamera/object.h | 19 ++++++++++++++++ src/libcamera/object.cpp | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)