Show a patch.

GET /api/patches/17177/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 17177,
    "url": "https://patchwork.libcamera.org/api/patches/17177/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/17177/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20220819111615.39814-3-tomi.valkeinen@ideasonboard.com>",
    "date": "2022-08-19T11:16:11",
    "name": "[libcamera-devel,v4,2/6] py: Create PyCameraManager",
    "commit_ref": "f814b1b6a9f3d091982b42f117630f4dc1300404",
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "1779cd31697b9066e2346854b3d42e90d4aaeda3",
    "submitter": {
        "id": 109,
        "url": "https://patchwork.libcamera.org/api/people/109/?format=api",
        "name": "Tomi Valkeinen",
        "email": "tomi.valkeinen@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/17177/mbox/",
    "series": [
        {
            "id": 3428,
            "url": "https://patchwork.libcamera.org/api/series/3428/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3428",
            "date": "2022-08-19T11:16:09",
            "name": "Python bindings: PyCameraManager and non-blocking eventfd",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/3428/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/17177/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/17177/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 523D9C3275\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 19 Aug 2022 11:16:36 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 12C5861FC3;\n\tFri, 19 Aug 2022 13:16:35 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3544961FA4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Aug 2022 13:16:31 +0200 (CEST)",
            "from deskari.lan (91-158-154-79.elisa-laajakaista.fi\n\t[91.158.154.79])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A6D9C59D;\n\tFri, 19 Aug 2022 13:16:30 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1660907795;\n\tbh=70FpsWphMwFsXjxrhbtR98Jo89gAd0RatrFTkqPgPgg=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=fzgzFsRUrQ8zptmH1J9Bjo6y8NUjPmRMxLTfXRKShKPzIj1uYr75tkphAwpIyMhCm\n\tG7s7IOQE61lKsuEPZOAm2n+Zp5VS51/4dwIOlFfOl89Yxm3IBzBZlt0Q2jqZIWlv4L\n\tmA3Wjv36n3dX7SiUPDHqVW+9qQmNaG5vlZNiXL7pj7sLT+9lVl8dkpjdgZfO1ryVup\n\tj5gDB144hqNBfninfUMJbo4zQiBW62CdrSRDxK8PdgeVpiSuItIBMUCRHNejduXLxt\n\tcaVVMab6dJZEGvgslo1Nx9FnmbU9zcVwFujnKVsInbSnlgqFGrAKaXGpgpyL0h8kVh\n\t+WHSUSkTPdjWA==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1660907791;\n\tbh=70FpsWphMwFsXjxrhbtR98Jo89gAd0RatrFTkqPgPgg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=N5ZGagFwlw3PQuZEkCxuUO/3/gf9n/thowHM39mexARKMNdxef/3lf1ohRRmrd7On\n\tL6QB3qTGhwz+k4U5PmVHk+NPKtHuGok/Dj8t6rPf5jzhw6xFg5bI1+MJn1VHpC5Qwp\n\tawe6asDA94BA0awkNh/yBdhTEG7QVe/pz2mu/6Wg="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"N5ZGagFw\"; dkim-atps=neutral",
        "To": "libcamera-devel@lists.libcamera.org,\n\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tJacopo Mondi <jacopo@jmondi.org>",
        "Date": "Fri, 19 Aug 2022 14:16:11 +0300",
        "Message-Id": "<20220819111615.39814-3-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20220819111615.39814-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20220819111615.39814-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 2/6] py: Create PyCameraManager",
        "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>",
        "From": "Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Wrap the CameraManager with a PyCameraManager class and move the related\ncode inside the new class.\n\nThis helps understanding the life times of the used-to-be global\nvariables, gets rid of static handleRequestCompleted function, and\nallows us to simplify the binding code as the more complex pieces are\ninside the class.\n\nThere should be no user visible functional changes.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/py/libcamera/meson.build           |   1 +\n src/py/libcamera/py_camera_manager.cpp | 127 +++++++++++++++++++++++++\n src/py/libcamera/py_camera_manager.h   |  44 +++++++++\n src/py/libcamera/py_main.cpp           | 120 +++++------------------\n 4 files changed, 198 insertions(+), 94 deletions(-)\n create mode 100644 src/py/libcamera/py_camera_manager.cpp\n create mode 100644 src/py/libcamera/py_camera_manager.h",
    "diff": "diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build\nindex 99c2a8c0..af19ffdd 100644\n--- a/src/py/libcamera/meson.build\n+++ b/src/py/libcamera/meson.build\n@@ -13,6 +13,7 @@ pybind11_proj = subproject('pybind11')\n pybind11_dep = pybind11_proj.get_variable('pybind11_dep')\n \n pycamera_sources = files([\n+    'py_camera_manager.cpp',\n     'py_enums.cpp',\n     'py_geometry.cpp',\n     'py_helpers.cpp',\ndiff --git a/src/py/libcamera/py_camera_manager.cpp b/src/py/libcamera/py_camera_manager.cpp\nnew file mode 100644\nindex 00000000..228fb212\n--- /dev/null\n+++ b/src/py/libcamera/py_camera_manager.cpp\n@@ -0,0 +1,127 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+ */\n+\n+#include \"py_camera_manager.h\"\n+\n+#include <errno.h>\n+#include <memory>\n+#include <sys/eventfd.h>\n+#include <system_error>\n+#include <unistd.h>\n+#include <vector>\n+\n+#include \"py_main.h\"\n+\n+namespace py = pybind11;\n+\n+using namespace libcamera;\n+\n+PyCameraManager::PyCameraManager()\n+{\n+\tLOG(Python, Debug) << \"PyCameraManager()\";\n+\n+\tcameraManager_ = std::make_unique<CameraManager>();\n+\n+\tint fd = eventfd(0, 0);\n+\tif (fd == -1)\n+\t\tthrow std::system_error(errno, std::generic_category(),\n+\t\t\t\t\t\"Failed to create eventfd\");\n+\n+\teventFd_ = fd;\n+\n+\tint ret = cameraManager_->start();\n+\tif (ret) {\n+\t\tclose(fd);\n+\t\teventFd_ = -1;\n+\t\tthrow std::system_error(-ret, std::generic_category(),\n+\t\t\t\t\t\"Failed to start CameraManager\");\n+\t}\n+}\n+\n+PyCameraManager::~PyCameraManager()\n+{\n+\tLOG(Python, Debug) << \"~PyCameraManager()\";\n+\n+\tif (eventFd_ != -1) {\n+\t\tclose(eventFd_);\n+\t\teventFd_ = -1;\n+\t}\n+}\n+\n+py::list PyCameraManager::cameras()\n+{\n+\t/*\n+\t * Create a list of Cameras, where each camera has a keep-alive to\n+\t * CameraManager.\n+\t */\n+\tpy::list l;\n+\n+\tfor (auto &camera : cameraManager_->cameras()) {\n+\t\tpy::object py_cm = py::cast(this);\n+\t\tpy::object py_cam = py::cast(camera);\n+\t\tpy::detail::keep_alive_impl(py_cam, py_cm);\n+\t\tl.append(py_cam);\n+\t}\n+\n+\treturn l;\n+}\n+\n+std::vector<py::object> PyCameraManager::getReadyRequests()\n+{\n+\treadFd();\n+\n+\tstd::vector<py::object> py_reqs;\n+\n+\tfor (Request *request : getCompletedRequests()) {\n+\t\tpy::object o = py::cast(request);\n+\t\t/* Decrease the ref increased in Camera.queue_request() */\n+\t\to.dec_ref();\n+\t\tpy_reqs.push_back(o);\n+\t}\n+\n+\treturn py_reqs;\n+}\n+\n+/* Note: Called from another thread */\n+void PyCameraManager::handleRequestCompleted(Request *req)\n+{\n+\tpushRequest(req);\n+\twriteFd();\n+}\n+\n+void PyCameraManager::writeFd()\n+{\n+\tuint64_t v = 1;\n+\n+\tsize_t s = write(eventFd_, &v, 8);\n+\t/*\n+\t * We should never fail, and have no simple means to manage the error,\n+\t * so let's log a fatal error.\n+\t */\n+\tif (s != 8)\n+\t\tLOG(Python, Fatal) << \"Unable to write to eventfd\";\n+}\n+\n+void PyCameraManager::readFd()\n+{\n+\tuint8_t buf[8];\n+\n+\tif (read(eventFd_, buf, 8) != 8)\n+\t\tthrow std::system_error(errno, std::generic_category());\n+}\n+\n+void PyCameraManager::pushRequest(Request *req)\n+{\n+\tstd::lock_guard guard(completedRequestsMutex_);\n+\tcompletedRequests_.push_back(req);\n+}\n+\n+std::vector<Request *> PyCameraManager::getCompletedRequests()\n+{\n+\tstd::vector<Request *> v;\n+\tstd::lock_guard guard(completedRequestsMutex_);\n+\tswap(v, completedRequests_);\n+\treturn v;\n+}\ndiff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h\nnew file mode 100644\nindex 00000000..9c15f814\n--- /dev/null\n+++ b/src/py/libcamera/py_camera_manager.h\n@@ -0,0 +1,44 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+ */\n+\n+#pragma once\n+\n+#include <mutex>\n+\n+#include <libcamera/libcamera.h>\n+\n+#include <pybind11/smart_holder.h>\n+\n+using namespace libcamera;\n+\n+class PyCameraManager\n+{\n+public:\n+\tPyCameraManager();\n+\t~PyCameraManager();\n+\n+\tpybind11::list cameras();\n+\tstd::shared_ptr<Camera> get(const std::string &name) { return cameraManager_->get(name); }\n+\n+\tstatic const std::string &version() { return CameraManager::version(); }\n+\n+\tint eventFd() const { return eventFd_; }\n+\n+\tstd::vector<pybind11::object> getReadyRequests();\n+\n+\tvoid handleRequestCompleted(Request *req);\n+\n+private:\n+\tstd::unique_ptr<CameraManager> cameraManager_;\n+\n+\tint eventFd_ = -1;\n+\tstd::mutex completedRequestsMutex_;\n+\tstd::vector<Request *> completedRequests_;\n+\n+\tvoid writeFd();\n+\tvoid readFd();\n+\tvoid pushRequest(Request *req);\n+\tstd::vector<Request *> getCompletedRequests();\n+};\ndiff --git a/src/py/libcamera/py_main.cpp b/src/py/libcamera/py_main.cpp\nindex e652837f..7e613a3f 100644\n--- a/src/py/libcamera/py_main.cpp\n+++ b/src/py/libcamera/py_main.cpp\n@@ -7,10 +7,10 @@\n \n #include \"py_main.h\"\n \n-#include <mutex>\n+#include <memory>\n #include <stdexcept>\n-#include <sys/eventfd.h>\n-#include <unistd.h>\n+#include <string>\n+#include <vector>\n \n #include <libcamera/base/log.h>\n \n@@ -21,6 +21,7 @@\n #include <pybind11/stl.h>\n #include <pybind11/stl_bind.h>\n \n+#include \"py_camera_manager.h\"\n #include \"py_helpers.h\"\n \n namespace py = pybind11;\n@@ -33,27 +34,11 @@ LOG_DEFINE_CATEGORY(Python)\n \n }\n \n-static std::weak_ptr<CameraManager> gCameraManager;\n-static int gEventfd;\n-static std::mutex gReqlistMutex;\n-static std::vector<Request *> gReqList;\n-\n-static void handleRequestCompleted(Request *req)\n-{\n-\t{\n-\t\tstd::lock_guard guard(gReqlistMutex);\n-\t\tgReqList.push_back(req);\n-\t}\n-\n-\tuint64_t v = 1;\n-\tsize_t s = write(gEventfd, &v, 8);\n-\t/*\n-\t * We should never fail, and have no simple means to manage the error,\n-\t * so let's log a fatal error.\n-\t */\n-\tif (s != 8)\n-\t\tLOG(Python, Fatal) << \"Unable to write to eventfd\";\n-}\n+/*\n+ * Note: global C++ destructors can be ran on this before the py module is\n+ * destructed.\n+ */\n+static std::weak_ptr<PyCameraManager> gCameraManager;\n \n void init_py_enums(py::module &m);\n void init_py_controls_generated(py::module &m);\n@@ -76,7 +61,7 @@ PYBIND11_MODULE(_libcamera, m)\n \t * https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-c-types-in-docstrings\n \t */\n \n-\tauto pyCameraManager = py::class_<CameraManager>(m, \"CameraManager\");\n+\tauto pyCameraManager = py::class_<PyCameraManager>(m, \"CameraManager\");\n \tauto pyCamera = py::class_<Camera>(m, \"Camera\");\n \tauto pyCameraConfiguration = py::class_<CameraConfiguration>(m, \"CameraConfiguration\");\n \tauto pyCameraConfigurationStatus = py::enum_<CameraConfiguration::Status>(pyCameraConfiguration, \"Status\");\n@@ -110,78 +95,22 @@ PYBIND11_MODULE(_libcamera, m)\n \t/* Classes */\n \tpyCameraManager\n \t\t.def_static(\"singleton\", []() {\n-\t\t\tstd::shared_ptr<CameraManager> cm = gCameraManager.lock();\n-\t\t\tif (cm)\n-\t\t\t\treturn cm;\n-\n-\t\t\tint fd = eventfd(0, 0);\n-\t\t\tif (fd == -1)\n-\t\t\t\tthrow std::system_error(errno, std::generic_category(),\n-\t\t\t\t\t\t\t\"Failed to create eventfd\");\n-\n-\t\t\tcm = std::shared_ptr<CameraManager>(new CameraManager, [](auto p) {\n-\t\t\t\tclose(gEventfd);\n-\t\t\t\tgEventfd = -1;\n-\t\t\t\tdelete p;\n-\t\t\t});\n-\n-\t\t\tgEventfd = fd;\n-\t\t\tgCameraManager = cm;\n-\n-\t\t\tint ret = cm->start();\n-\t\t\tif (ret)\n-\t\t\t\tthrow std::system_error(-ret, std::generic_category(),\n-\t\t\t\t\t\t\t\"Failed to start CameraManager\");\n-\n-\t\t\treturn cm;\n-\t\t})\n-\n-\t\t.def_property_readonly(\"version\", &CameraManager::version)\n+\t\t\tstd::shared_ptr<PyCameraManager> cm = gCameraManager.lock();\n \n-\t\t.def_property_readonly(\"event_fd\", [](CameraManager &) {\n-\t\t\treturn gEventfd;\n-\t\t})\n-\n-\t\t.def(\"get_ready_requests\", [](CameraManager &) {\n-\t\t\tuint8_t buf[8];\n-\n-\t\t\tif (read(gEventfd, buf, 8) != 8)\n-\t\t\t\tthrow std::system_error(errno, std::generic_category());\n-\n-\t\t\tstd::vector<Request *> v;\n-\n-\t\t\t{\n-\t\t\t\tstd::lock_guard guard(gReqlistMutex);\n-\t\t\t\tswap(v, gReqList);\n+\t\t\tif (!cm) {\n+\t\t\t\tcm = std::make_shared<PyCameraManager>();\n+\t\t\t\tgCameraManager = cm;\n \t\t\t}\n \n-\t\t\tstd::vector<py::object> ret;\n-\n-\t\t\tfor (Request *req : v) {\n-\t\t\t\tpy::object o = py::cast(req);\n-\t\t\t\t/* Decrease the ref increased in Camera.queue_request() */\n-\t\t\t\to.dec_ref();\n-\t\t\t\tret.push_back(o);\n-\t\t\t}\n-\n-\t\t\treturn ret;\n+\t\t\treturn cm;\n \t\t})\n \n-\t\t.def(\"get\", py::overload_cast<const std::string &>(&CameraManager::get), py::keep_alive<0, 1>())\n-\n-\t\t/* Create a list of Cameras, where each camera has a keep-alive to CameraManager */\n-\t\t.def_property_readonly(\"cameras\", [](CameraManager &self) {\n-\t\t\tpy::list l;\n-\n-\t\t\tfor (auto &c : self.cameras()) {\n-\t\t\t\tpy::object py_cm = py::cast(self);\n-\t\t\t\tpy::object py_cam = py::cast(c);\n-\t\t\t\tpy::detail::keep_alive_impl(py_cam, py_cm);\n-\t\t\t\tl.append(py_cam);\n-\t\t\t}\n+\t\t.def_property_readonly(\"version\", &PyCameraManager::version)\n+\t\t.def(\"get\", &PyCameraManager::get, py::keep_alive<0, 1>())\n+\t\t.def_property_readonly(\"cameras\", &PyCameraManager::cameras)\n \n-\t\t\treturn l;\n-\t\t});\n+\t\t.def_property_readonly(\"event_fd\", &PyCameraManager::eventFd)\n+\t\t.def(\"get_ready_requests\", &PyCameraManager::getReadyRequests);\n \n \tpyCamera\n \t\t.def_property_readonly(\"id\", &Camera::id)\n@@ -191,7 +120,10 @@ PYBIND11_MODULE(_libcamera, m)\n \t\t                 const std::unordered_map<const ControlId *, py::object> &controls) {\n \t\t\t/* \\todo What happens if someone calls start() multiple times? */\n \n-\t\t\tself.requestCompleted.connect(handleRequestCompleted);\n+\t\t\tauto cm = gCameraManager.lock();\n+\t\t\tASSERT(cm);\n+\n+\t\t\tself.requestCompleted.connect(cm.get(), &PyCameraManager::handleRequestCompleted);\n \n \t\t\tControlList controlList(self.controls());\n \n@@ -202,7 +134,7 @@ PYBIND11_MODULE(_libcamera, m)\n \n \t\t\tint ret = self.start(&controlList);\n \t\t\tif (ret) {\n-\t\t\t\tself.requestCompleted.disconnect(handleRequestCompleted);\n+\t\t\t\tself.requestCompleted.disconnect();\n \t\t\t\treturn ret;\n \t\t\t}\n \n@@ -214,7 +146,7 @@ PYBIND11_MODULE(_libcamera, m)\n \t\t\tif (ret)\n \t\t\t\treturn ret;\n \n-\t\t\tself.requestCompleted.disconnect(handleRequestCompleted);\n+\t\t\tself.requestCompleted.disconnect();\n \n \t\t\treturn 0;\n \t\t})\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "2/6"
    ]
}