From 3d75cc1bd209cf1de909ce5c8c9cec16789708f6 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 28 Oct 2019 05:31:12 +0200 Subject: libcamera: bound_method: Support connection types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support all connection types in the BoundMethodBase::activePack() method. To support this, add a semaphore to the InvokeMessage to signal delivery. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/libcamera/bound_method.cpp | 30 ++++++++++++++++++++++++++++-- src/libcamera/include/message.h | 5 +++++ src/libcamera/message.cpp | 11 +++++++++-- src/libcamera/object.cpp | 8 +++++++- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/libcamera/bound_method.cpp b/src/libcamera/bound_method.cpp index ab6ecd94..60071736 100644 --- a/src/libcamera/bound_method.cpp +++ b/src/libcamera/bound_method.cpp @@ -8,6 +8,7 @@ #include #include "message.h" +#include "semaphore.h" #include "thread.h" #include "utils.h" @@ -49,12 +50,37 @@ namespace libcamera { void BoundMethodBase::activatePack(void *pack) { - if (Thread::current() == object_->thread()) { + ConnectionType type = connectionType_; + if (type == ConnectionTypeAuto) { + if (Thread::current() == object_->thread()) + type = ConnectionTypeDirect; + else + type = ConnectionTypeQueued; + } + + switch (type) { + case ConnectionTypeDirect: + default: invokePack(pack); - } else { + break; + + case ConnectionTypeQueued: { std::unique_ptr msg = utils::make_unique(this, pack); object_->postMessage(std::move(msg)); + break; + } + + case ConnectionTypeBlocking: { + Semaphore semaphore; + + std::unique_ptr msg = + utils::make_unique(this, pack, &semaphore); + object_->postMessage(std::move(msg)); + + semaphore.acquire(); + break; + } } } diff --git a/src/libcamera/include/message.h b/src/libcamera/include/message.h index 1cfde566..311755cc 100644 --- a/src/libcamera/include/message.h +++ b/src/libcamera/include/message.h @@ -15,6 +15,7 @@ namespace libcamera { class BoundMethodBase; class Object; +class Semaphore; class Thread; class Message @@ -48,14 +49,18 @@ class InvokeMessage : public Message { public: InvokeMessage(BoundMethodBase *method, void *pack, + Semaphore *semaphore = nullptr, bool deleteMethod = false); ~InvokeMessage(); + Semaphore *semaphore() const { return semaphore_; } + void invoke(); private: BoundMethodBase *method_; void *pack_; + Semaphore *semaphore_; bool deleteMethod_; }; diff --git a/src/libcamera/message.cpp b/src/libcamera/message.cpp index efafb655..daf65322 100644 --- a/src/libcamera/message.cpp +++ b/src/libcamera/message.cpp @@ -119,13 +119,14 @@ Message::Type Message::registerMessageType() * \brief Construct an InvokeMessage for method invocation on an Object * \param[in] method The bound method * \param[in] pack The packed method arguments + * \param[in] semaphore The semaphore used to signal message delivery * \param[in] deleteMethod True to delete the \a method when the message is * destroyed */ InvokeMessage::InvokeMessage(BoundMethodBase *method, void *pack, - bool deleteMethod) + Semaphore *semaphore, bool deleteMethod) : Message(Message::InvokeMessage), method_(method), pack_(pack), - deleteMethod_(deleteMethod) + semaphore_(semaphore), deleteMethod_(deleteMethod) { } @@ -135,6 +136,12 @@ InvokeMessage::~InvokeMessage() delete method_; } +/** + * \fn InvokeMessage::semaphore() + * \brief Retrieve the message semaphore passed to the constructor + * \return The message semaphore + */ + /** * \brief Invoke the method bound to InvokeMessage::method_ with arguments * InvokeMessage::pack_ diff --git a/src/libcamera/object.cpp b/src/libcamera/object.cpp index 98aa0af2..b0818f9a 100644 --- a/src/libcamera/object.cpp +++ b/src/libcamera/object.cpp @@ -13,6 +13,7 @@ #include "log.h" #include "message.h" +#include "semaphore.h" #include "thread.h" #include "utils.h" @@ -123,7 +124,12 @@ void Object::message(Message *msg) switch (msg->type()) { case Message::InvokeMessage: { InvokeMessage *iMsg = static_cast(msg); + Semaphore *semaphore = iMsg->semaphore(); iMsg->invoke(); + + if (semaphore) + semaphore->release(); + break; } @@ -150,7 +156,7 @@ void Object::message(Message *msg) void Object::invokeMethod(BoundMethodBase *method, void *args) { std::unique_ptr msg = - utils::make_unique(method, args, true); + utils::make_unique(method, args, nullptr, true); postMessage(std::move(msg)); } -- cgit v1.2.1