{"id":14398,"url":"https://patchwork.libcamera.org/api/1.1/patches/14398/?format=json","web_url":"https://patchwork.libcamera.org/patch/14398/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211028111520.256612-4-jacopo@jmondi.org>","date":"2021-10-28T11:15:13","name":"[libcamera-devel,03/10] libcamera: Introduce Fence class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"216dbf069c1e369a8b78f0b1d30702877d999831","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14398/mbox/","series":[{"id":2674,"url":"https://patchwork.libcamera.org/api/1.1/series/2674/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2674","date":"2021-10-28T11:15:10","name":"libcamera: Introduce Fence support","version":1,"mbox":"https://patchwork.libcamera.org/series/2674/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14398/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14398/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C3C6EBF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 28 Oct 2021 11:14:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 81BFD60253;\n\tThu, 28 Oct 2021 13:14:46 +0200 (CEST)","from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net\n\t[217.70.183.199])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 27C5A600C2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Oct 2021 13:14:38 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 5499DFF802;\n\tThu, 28 Oct 2021 11:14:38 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 28 Oct 2021 13:15:13 +0200","Message-Id":"<20211028111520.256612-4-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.33.1","In-Reply-To":"<20211028111520.256612-1-jacopo@jmondi.org>","References":"<20211028111520.256612-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 03/10] libcamera: Introduce Fence class","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Fences are a synchronization mechanism that allows to receive event\nnotifications of a file descriptor with an optional expiration timeout.\n\nIntroduce the Fence class to model such a mechanism in libcamera.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n include/libcamera/internal/fence.h     |  64 +++++++++\n include/libcamera/internal/meson.build |   1 +\n src/libcamera/fence.cpp                | 185 +++++++++++++++++++++++++\n src/libcamera/meson.build              |   1 +\n 4 files changed, 251 insertions(+)\n create mode 100644 include/libcamera/internal/fence.h\n create mode 100644 src/libcamera/fence.cpp","diff":"diff --git a/include/libcamera/internal/fence.h b/include/libcamera/internal/fence.h\nnew file mode 100644\nindex 000000000000..5a78e0f864c7\n--- /dev/null\n+++ b/include/libcamera/internal/fence.h\n@@ -0,0 +1,64 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * internal/fence.h - Synchronization fence\n+ */\n+#ifndef __LIBCAMERA_INTERNAL_FENCE_H__\n+#define __LIBCAMERA_INTERNAL_FENCE_H__\n+\n+#include <mutex>\n+\n+#include <libcamera/base/class.h>\n+#include <libcamera/base/event_notifier.h>\n+#include <libcamera/base/timer.h>\n+#include <libcamera/file_descriptor.h>\n+\n+namespace libcamera {\n+\n+class Fence\n+{\n+public:\n+\texplicit Fence(int fenceFd, unsigned int timeoutMs = kFenceTimeout);\n+\tFence(Fence &&other);\n+\n+\tFence &operator=(Fence &&other);\n+\n+\tint fd() const { return fd_.fd(); }\n+\n+\tunsigned int timeout() const { return timeout_; }\n+\tbool completed() const { return completed_; }\n+\tbool expired() const { return expired_; }\n+\n+\tvoid enable();\n+\tvoid disable();\n+\n+\tSignal<> complete;\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY(Fence)\n+\n+\t/* 300 milliseconds default timeout. */\n+\tstatic constexpr unsigned int kFenceTimeout = 300;\n+\n+\tvoid moveFence(Fence &other);\n+\n+\tvoid activated();\n+\tvoid timedout();\n+\n+\tFileDescriptor fd_;\n+\tEventNotifier notifier_;\n+\n+\tTimer timer_;\n+\tunsigned int timeout_;\n+\n+\tbool completed_ = false;\n+\tbool expired_ = false;\n+\n+\tstd::mutex mutex_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_INTERNAL_FENCE_H__ */\n+\ndiff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\nindex cae65b0604ff..32d5c3387de3 100644\n--- a/include/libcamera/internal/meson.build\n+++ b/include/libcamera/internal/meson.build\n@@ -22,6 +22,7 @@ libcamera_internal_headers = files([\n     'device_enumerator.h',\n     'device_enumerator_sysfs.h',\n     'device_enumerator_udev.h',\n+    'fence.h',\n     'formats.h',\n     'framebuffer.h',\n     'ipa_manager.h',\ndiff --git a/src/libcamera/fence.cpp b/src/libcamera/fence.cpp\nnew file mode 100644\nindex 000000000000..8fadb2c21f03\n--- /dev/null\n+++ b/src/libcamera/fence.cpp\n@@ -0,0 +1,185 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * fence.cpp - Synchronization fence\n+ */\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/thread.h>\n+#include <libcamera/internal/fence.h>\n+\n+namespace libcamera {\n+\n+/**\n+ * \\file internal/fence.h\n+ * \\brief Synchronization fence\n+ */\n+\n+/**\n+ * \\class Fence\n+ * \\brief Synchronization fence class\n+ *\n+ * A Fence is a synchronization mechanism that allows to wait for a read event\n+ * to happen on a file descriptor before an optional timeout expires.\n+ *\n+ * A Fence is created with a default timeout of 300 milliseconds, which can\n+ * be overridden through the 'timeout' constructor parameter. Passing a 0\n+ * timeout to the constructor disables timeouts completely.\n+ *\n+ * A Fence wraps a FileDescriptor and implements a move-only semantic, as Fence\n+ * instances cannot be copied but only moved, causing the moved Fence to be\n+ * reset by invalidating the wrapped FileDescriptor by disabling its\n+ * notification and timeout mechanisms.\n+ *\n+ * When a read event is notified, the Fence is said to be 'completed',\n+ * alternatively if the timeout expires before any event is notified the Fence\n+ * is said to be 'expired'.\n+ *\n+ * In both cases, when an event notification happens or a timeout expires, the\n+ * class emits the 'complete' signal, to which users of the class can connect\n+ * to and check if the Fence has completed or has expired by calling the\n+ * completed() and expired() functions.\n+ *\n+ * Fence instances are non-active by default (ie no events or timeout are\n+ * generated) and need to be enabled by calling the enable() function. Likewise\n+ * a Fence can be disabled by calling the disable() function.\n+ *\n+ * After a Fence has expired no events are generated and the Fence need to be\n+ * manually re-enabled. Likewise, if the Fence is signalled the expiration\n+ * timeout is cancelled.\n+ */\n+\n+/**\n+ * \\brief Create a synchronization fence\n+ * \\param[in] fenceFd The synchronization fence file descriptor\n+ * \\param[in] timeoutMs The optional fence timeout. Set to 0 to disable timeout\n+ */\n+Fence::Fence(int fenceFd, unsigned int timeoutMs)\n+\t: fd_(std::move(fenceFd)),\n+\t  notifier_(fd_.fd(), EventNotifier::Read, nullptr, false),\n+\t  timeout_(timeoutMs)\n+{\n+\tnotifier_.activated.connect(this, &Fence::activated);\n+\n+\ttimer_.timeout.connect(this, &Fence::timedout);\n+}\n+\n+void Fence::moveFence(Fence &other)\n+{\n+\tfd_ = std::move(other.fd_);\n+\n+\tother.disable();\n+\tif (other.timer_.isRunning())\n+\t\tother.timer_.stop();\n+\tother.timeout_ = 0;\n+}\n+\n+/**\n+ * \\brief Move-construct a Fence\n+ * \\param[in] other The other fence to move\n+ */\n+Fence::Fence(Fence &&other)\n+\t: notifier_(other.fd(), EventNotifier::Read, nullptr, false)\n+{\n+\tmoveFence(other);\n+\n+\tnotifier_.activated.connect(this, &Fence::activated);\n+\ttimer_.timeout.connect(this, &Fence::timedout);\n+}\n+\n+/**\n+ * \\brief Move-assign the value of the \\a other fence to this\n+ * \\param[in] other The other fence to move\n+ * \\return This fence\n+ */\n+Fence &Fence::operator=(Fence &&other)\n+{\n+\tmoveFence(other);\n+\n+\tnotifier_ = EventNotifier(fd_.fd(), EventNotifier::Read, nullptr, false);\n+\n+\treturn *this;\n+}\n+\n+/**\n+ * \\fn Fence::fd() const\n+ * \\brief Return the synchronization fence file descriptor\n+ * \\return The synchronization fence file descriptor\n+ */\n+\n+/**\n+ * \\fn Fence::timeout()\n+ * \\brief Retrieve the fence timeout\n+ * \\return The fence timeout in milliseconds\n+ */\n+\n+/**\n+ * \\fn Fence::completed()\n+ * \\brief Check if the fence has completed before timing out\n+ * \\return True if the fence has completed\n+ */\n+\n+/**\n+ * \\fn Fence::expired()\n+ * \\brief Check if the fence has expired before completing\n+ * \\return True if the fence has expired\n+ */\n+\n+/**\n+ * \\brief Enable a fence by enabling events notifications\n+ */\n+void Fence::enable()\n+{\n+\tMutexLocker lock(mutex_);\n+\n+\texpired_ = false;\n+\tcompleted_ = false;\n+\n+\tnotifier_.setEnabled(true);\n+\tif (timeout_)\n+\t\ttimer_.start(timeout_);\n+}\n+\n+/**\n+ * \\brief Disable a fence by disabling events notifications\n+ */\n+void Fence::disable()\n+{\n+\tnotifier_.setEnabled(false);\n+}\n+\n+/**\n+ * \\var Fence::complete\n+ * \\brief The fence completion signal\n+ *\n+ * A Fence is completed if signalled or timeout.\n+ */\n+\n+void Fence::activated()\n+{\n+\tMutexLocker lock(mutex_);\n+\n+\tif (timer_.isRunning())\n+\t\ttimer_.stop();\n+\n+\tcompleted_ = true;\n+\texpired_ = false;\n+\n+\tcomplete.emit();\n+}\n+\n+void Fence::timedout()\n+{\n+\tMutexLocker lock(mutex_);\n+\n+\texpired_ = true;\n+\tcompleted_ = false;\n+\n+\t/* Disable events notification after timeout. */\n+\tdisable();\n+\n+\tcomplete.emit();\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 6727a777d804..6fb0d5f49b63 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -14,6 +14,7 @@ libcamera_sources = files([\n     'delayed_controls.cpp',\n     'device_enumerator.cpp',\n     'device_enumerator_sysfs.cpp',\n+    'fence.cpp',\n     'file_descriptor.cpp',\n     'formats.cpp',\n     'framebuffer.cpp',\n","prefixes":["libcamera-devel","03/10"]}