From patchwork Sun Oct 27 20:33:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 2234 X-Patchwork-Delegate: jacopo@jmondi.org Return-Path: Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8A17A61509 for ; Sun, 27 Oct 2019 21:31:49 +0100 (CET) X-Originating-IP: 93.2.121.143 Received: from uno.localdomain (143.121.2.93.rev.sfr.net [93.2.121.143]) (Authenticated sender: jacopo@jmondi.org) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 3F71DC0006; Sun, 27 Oct 2019 20:31:49 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Sun, 27 Oct 2019 21:33:32 +0100 Message-Id: <20191027203335.26888-4-jacopo@jmondi.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191027203335.26888-1-jacopo@jmondi.org> References: <20191027203335.26888-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/6] libcamera: object: Add ObjectConditionVariable class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 27 Oct 2019 20:31:50 -0000 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 --- 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 #include #include +#include #include #include @@ -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